Class: Marker::ItemBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, content_or_sublist) ⇒ ItemBuilder

Returns a new instance of ItemBuilder.



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/marker/lists.rb', line 78

def initialize( type, content_or_sublist )
  @type = type

  if content_or_sublist.respond_to? :items
    @content = nil
    @sublists = ListBuilder.new(:sublist, content_or_sublist)
  else
    @content = content_or_sublist
    @sublists = ListBuilder.new(:sublist)
  end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



75
76
77
# File 'lib/marker/lists.rb', line 75

def content
  @content
end

#sublistsObject (readonly)

Returns the value of attribute sublists.



76
77
78
# File 'lib/marker/lists.rb', line 76

def sublists
  @sublists
end

#typeObject (readonly)

Returns the value of attribute type.



74
75
76
# File 'lib/marker/lists.rb', line 74

def type
  @type
end

Instance Method Details

#can_merge?(other) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/marker/lists.rb', line 90

def can_merge?( other )
  other.content.nil?
end

#merge!(item) ⇒ Object



94
95
96
# File 'lib/marker/lists.rb', line 94

def merge!( item )
  sublists.merge! item.sublists
end

#to_html(options = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/marker/lists.rb', line 98

def to_html( options={} )
  case type
  when :bulleted, :numbered
    "<li>" +
      ( content ? content.to_html(options) : "" ) +
      sublists.to_html(options) +
    "</li>"
  when :indented
    "<div class='indented_item'>" +
      ( content ? content.to_html(options) : "" ) +
      sublists.to_html(options) +
    "</div>"
  when :definition
    if content
      term, definition = content
      "<dt>#{term.to_html(options)}</dt><dd>#{definition.to_html(options)}#{sublists.to_html(options)}</dd>"
    else
      "<dd>#{sublists.to_html(options)}</dd>"
    end
  end
end

#to_s(options = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/marker/lists.rb', line 120

def to_s( options={} )
  if content
    indent = '    ' * options[:indent]
    item_str = case type
      when :bulleted
        '  * ' + content.to_s(options)
      when :numbered
        "#{'%2d' % (options[:num]+1)}. " + content.to_s(options)
      when :indented
        '    ' + content.to_s(options)
      when :definition
        term, definition = content
        '    ' + term.to_s(options) + ( definition ? ' :: ' + definition.to_s(options) : '' )
      end
    sublist_str = ( sublists.empty? ? '' : "\n#{sublists.to_s(options)}" )
    "#{indent}#{item_str}#{sublist_str}"
  else
    sublists.to_s(options)
  end
end