Module: BBTagCloser

Defined in:
lib/bb_tag_closer.rb,
lib/bb_tag_closer/railtie.rb,
lib/bb_tag_closer/version.rb

Defined Under Namespace

Modules: ClassMethods Classes: Configuration, Railtie

Constant Summary collapse

VERSION =
"1.0.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#font_colorObject

Returns the value of attribute font_color.



7
8
9
# File 'lib/bb_tag_closer.rb', line 7

def font_color
  @font_color
end

#font_sizeObject

Returns the value of attribute font_size.



6
7
8
# File 'lib/bb_tag_closer.rb', line 6

def font_size
  @font_size
end

Class Method Details

.configure {|BBTagCloser::Configuration| ... } ⇒ Object

Sets configuration options. Currently supports setting of custom bb_tags through BBTagCloser.configure {|config| config.bb_tags = [“u”, “b”, “s”, “i”, “quote”, “url”, “img”, “email”, “youtube”, “size”, “color”, # additional tags ]}



63
64
65
# File 'lib/bb_tag_closer.rb', line 63

def self.configure(&block)
  yield BBTagCloser::Configuration
end

.included(base) ⇒ Object



10
11
12
13
# File 'lib/bb_tag_closer.rb', line 10

def self.included(base)
  base.attr_accessible :font_size, :font_color
  base.extend ClassMethods
end

Instance Method Details

#close_bb_tagsObject

Called during a callback specified in the :on paramater in close_tags or close_tags_for. Closes forum tags in the opposite order they were added by the user.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bb_tag_closer.rb', line 69

def close_bb_tags
  text_attributes = self.class.taggable_fields
  text_attributes.each do |attribute|
    tags = send(attribute).scan(Regexp.new "\\[(.*?)\\]").flatten.delete_if {|c| c.include? "/"}.map {|i| i.include?("=") ? i.slice(Regexp.new "(.*)\\=").chomp("=") : i}.reverse.map(&:downcase).delete_if { |x| not Configuration.bb_tags.include? x } 
    tags.each do |tag|
      open_tags = send(attribute).downcase.scan(Regexp.new "\\[#{tag}=").length + send(attribute).downcase.scan(Regexp.new "\\[#{tag}\\]").length
      closed_tags = send(attribute).downcase.scan(Regexp.new "\\[\\/#{tag}\\]").length
      difference = open_tags - closed_tags
      if open_tags > closed_tags
        send(attribute) << "[/#{tag}]"
      end
    end
  end
end