Method: RDoc::Markup::Parser#build_list

Defined in:
lib/rdoc/markup/parser.rb

#build_list(margin) ⇒ Object

Builds a List flush to margin



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rdoc/markup/parser.rb', line 108

def build_list margin
  p :list_start => margin if @debug

  list = RDoc::Markup::List.new
  label = nil

  until @tokens.empty? do
    type, data, column, = get

    case type
    when *LIST_TOKENS then
      if column < margin || (list.type && list.type != type) then
        unget
        break
      end

      list.type = type
      peek_type, _, column, = peek_token

      case type
      when :NOTE, :LABEL then
        label = [] unless label

        if peek_type == :NEWLINE then
          # description not on the same line as LABEL/NOTE
          # skip the trailing newline & any blank lines below
          while peek_type == :NEWLINE
            get
            peek_type, _, column, = peek_token
          end

          # we may be:
          #   - at end of stream
          #   - at a column < margin:
          #         [text]
          #       blah blah blah
          #   - at the same column, but with a different type of list item
          #       [text]
          #       * blah blah
          #   - at the same column, with the same type of list item
          #       [one]
          #       [two]
          # In all cases, we have an empty description.
          # In the last case only, we continue.
          if peek_type.nil? || column < margin then
            empty = true
          elsif column == margin then
            case peek_type
            when type
              empty = :continue
            when *LIST_TOKENS
              empty = true
            else
              empty = false
            end
          else
            empty = false
          end

          if empty then
            label << data
            next if empty == :continue
            break
          end
        end
      else
        data = nil
      end

      if label then
        data = label << data
        label = nil
      end

      list_item = RDoc::Markup::ListItem.new data
      parse list_item, column
      list << list_item

    else
      unget
      break
    end
  end

  p :list_end => margin if @debug

  if list.empty? then
    return nil unless label
    return nil unless [:LABEL, :NOTE].include? list.type

    list_item = RDoc::Markup::ListItem.new label, RDoc::Markup::BlankLine.new
    list << list_item
  end

  list
end