Class: Marker::ListBuilder
- Inherits:
-
Object
- Object
- Marker::ListBuilder
- Defined in:
- lib/marker/lists.rb
Overview
used to collect list lines into lists structured hierarchically, like in HTML
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Instance Method Summary collapse
- #+(l) ⇒ Object
- #<<(l) ⇒ Object
-
#===(l) ⇒ Object
returns true if the l has the same tag.
-
#initialize(contents, tag, attrs = {}) ⇒ ListBuilder
constructor
A new instance of ListBuilder.
- #to_html(options = {}) ⇒ Object
- #to_s(options = {}) ⇒ Object
Constructor Details
#initialize(contents, tag, attrs = {}) ⇒ ListBuilder
Returns a new instance of ListBuilder.
14 15 16 17 18 |
# File 'lib/marker/lists.rb', line 14 def initialize( contents, tag, attrs = {} ) @contents = [contents].flatten @tag = tag @attrs = attrs end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
12 13 14 |
# File 'lib/marker/lists.rb', line 12 def attrs @attrs end |
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
12 13 14 |
# File 'lib/marker/lists.rb', line 12 def contents @contents end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
12 13 14 |
# File 'lib/marker/lists.rb', line 12 def tag @tag end |
Instance Method Details
#+(l) ⇒ Object
37 38 39 40 41 |
# File 'lib/marker/lists.rb', line 37 def +( l ) l.contents.each { |i| self << i } if self === l self end |
#<<(l) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/marker/lists.rb', line 25 def <<( l ) last = contents.last if last and last === l last += l else contents << l end self end |
#===(l) ⇒ Object
returns true if the l has the same tag
21 22 23 |
# File 'lib/marker/lists.rb', line 21 def ===( l ) ( l.is_a? self.class and tag == l.tag ) end |
#to_html(options = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/marker/lists.rb', line 43 def to_html( = {} ) if tag "<#{tag}" + ( attrs.any?? ' ' : '' ) + attrs.map { |k, v| "#{k}='#{v}'" }.join(' ') + ">" + contents.map { |i| i.to_html() }.join + "</#{tag}>" else contents.map { |i| i.to_html() }.join end end |
#to_s(options = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/marker/lists.rb', line 62 def to_s( = {} ) strs = [] contents.each_with_index { |i, ind| strs << i.to_s( .merge( :indent => ( [:indent] || 0 ) + ( tag ? 1 : 0 ), :num => ind + 1 ) ) } strs.join("\n") end |