Class: Marker::ListBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/marker/lists.rb

Overview

used to collect list lines into lists structured hierarchically, like in HTML

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attrsObject (readonly)

Returns the value of attribute attrs.



12
13
14
# File 'lib/marker/lists.rb', line 12

def attrs
  @attrs
end

#contentsObject (readonly)

Returns the value of attribute contents.



12
13
14
# File 'lib/marker/lists.rb', line 12

def contents
  @contents
end

#tagObject (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( options = {} )
  if tag
    "<#{tag}" +
    ( attrs.any?? ' ' : '' ) +
    attrs.map { |k, v|
      "#{k}='#{v}'"
    }.join(' ') +
    ">" +
    contents.map { |i|
      i.to_html(options)
    }.join +
    "</#{tag}>"
  else
    contents.map { |i|
      i.to_html(options)
    }.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( options = {} )
  strs = []
  contents.each_with_index { |i, ind|
    strs << i.to_s( options.merge( 
        :indent => ( options[:indent] || 0 ) + ( tag ? 1 : 0 ),
        :num => ind + 1
      ) )
  }
  strs.join("\n")
end