Class: BBCoder::Tag

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/bbcoder/tag.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

reform, reform_end, reform_open

Constructor Details

#initialize(name, options = {}) ⇒ Tag

Returns a new instance of Tag.



5
6
7
8
# File 'lib/bbcoder/tag.rb', line 5

def initialize(name, options = {})
  @name = name
  @options = options.merge(:as => (options[:as] || name))
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/bbcoder/tag.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/bbcoder/tag.rb', line 3

def options
  @options
end

Instance Method Details

#content_valid?(content) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/bbcoder/tag.rb', line 25

def content_valid?(content)
  return true if options[:match].nil?

  return !content.match(options[:match]).nil?
end

#parentsObject



31
32
33
# File 'lib/bbcoder/tag.rb', line 31

def parents
  options[:parents] || []
end

#to_html(meta, content) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bbcoder/tag.rb', line 10

def to_html(meta, content)
  return self.class.reform(name, meta, content) unless content_valid?(content)


  if options[:block].nil?
    "<#{options[:as]}>#{content}</#{options[:as]}>"
  else
    options[:block].binding.eval <<-EOS
      @meta = %Q{#{Regexp.escape(meta.to_s)}}
      @content = %Q{#{Regexp.escape(content.to_s)}}
    EOS
    options[:block].call
  end
end