Class: Marker::ListBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, item = nil) ⇒ ListBuilder

Returns a new instance of ListBuilder.



14
15
16
17
# File 'lib/marker/lists.rb', line 14

def initialize( type, item=nil )
  @type = type
  @items = (item ? [item] : [])
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



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

def items
  @items
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#add(item) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/marker/lists.rb', line 27

def add( item )
  if items.last and items.last.can_merge? item
    items.last.merge! item
  else
    items << item
  end
end

#can_merge?(other) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/marker/lists.rb', line 19

def can_merge?( other )
  type == other.type
end

#empty?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/marker/lists.rb', line 35

def empty?
  items.empty?
end

#merge!(list) ⇒ Object



23
24
25
# File 'lib/marker/lists.rb', line 23

def merge!( list )
  list.items.each { |item| add( item ) }
end

#to_html(options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/marker/lists.rb', line 39

def to_html( options={} )
  inner_html = items.map { |item| item.to_html(options) }.join
  case type
  when :bulleted
    "<ul>#{inner_html}</ul>"
  when :numbered
    "<ol>#{inner_html}</ol>"
  when :indented
    "<div class='indent'>#{inner_html}</div>"
  when :definition
    "<dl>#{inner_html}</dl>"
  else
    inner_html
  end
end

#to_s(options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/marker/lists.rb', line 55

def to_s( options={} )
  indent = (options[:indent] ? options[:indent] + 1 : -1)
  # sublists are already indented because they are part of an item.
  indent -= 1 if type == :sublist

  case type
  when :numbered
    items.each_with_index.map { |item, num|
      item.to_s(options.merge(:indent => indent, :num => num))
    }.join("\n")
  else
    items.map { |item|
      item.to_s(options.merge(:indent => indent))
    }.join("\n")
  end
end