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

handle, 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), :singular => (options[:singular] || false))
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, singularity) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bbcoder/tag.rb', line 59

def content_valid?(content, singularity)
  return true if content.nil? && (options[:singular] && singularity)
  return false if content.nil?

  unless options[:match].nil?
    return false if content.match(options[:match]).nil?
  end

  unless options[:match_content].nil?
    return false if content.match(options[:match_content]).nil?
  end

  return true
end

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bbcoder/tag.rb', line 32

def link_valid?(meta, content)
  # only run if we have a :match_link
  return true if options[:match_link].nil?

  if meta.nil? || meta.empty?
    return false if content.match(options[:match_link]).nil?
  else
    return false if meta.match(options[:match_link]).nil?
  end

  return true
end

#meta_valid?(meta, singularity) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bbcoder/tag.rb', line 45

def meta_valid?(meta, singularity)
  return true if meta.nil?

  unless options[:match].nil?
    return false if meta.match(options[:match]).nil?
  end

  unless options[:match_meta].nil?
    return false if meta.match(options[:match_meta]).nil?
  end

  return true
end

#parentsObject



74
75
76
# File 'lib/bbcoder/tag.rb', line 74

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

#singular?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/bbcoder/tag.rb', line 78

def singular?
  options[:singular]
end

#to_html(depth, meta, content, singularity = false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bbcoder/tag.rb', line 10

def to_html(depth, meta, content, singularity = false)
  unless content_valid?(content, singularity) && meta_valid?(meta, singularity) && link_valid?(meta, content)
    return self.class.reform(name, meta, content, singularity, true)
  end

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