Method: RedClothForTex#lists

Defined in:
lib/redcloth_for_tex.rb

#lists(text) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/redcloth_for_tex.rb', line 318

def lists( text ) 
  text.gsub!( /^([#*]+?#{C} .*?)$(?![^#*])/m ) do |match|
    lines = match.split( /\n/ )
    last_line = -1
    depth = []
    lines.each_with_index do |line, line_id|
      if line =~ /^([#*]+)(#{A}#{C}) (.*)$/m
        tl,atts,content = $~[1..3]
        if depth.last
          if depth.last.length > tl.length
           (depth.length - 1).downto(0) do |i|
              break if depth[i].length == tl.length
              lines[line_id - 1] << "\n\t\\end{#{ lT( depth[i] ) }}\n\t"
              depth.pop
            end
          end
          if !depth.last.nil? && !tl.length.nil? && depth.last.length == tl.length
            lines[line_id - 1] << ''
          end
        end
        unless depth.last == tl
          depth << tl
          atts = pba( atts )
          lines[line_id] = "\t\\begin{#{ lT(tl) }}\n\t\\item #{ content }"
        else
          lines[line_id] = "\t\t\\item #{ content }"
        end
        last_line = line_id
        
      elsif line =~ /^\s+\S/
        last_line = line_id
      elsif line_id - last_line < 2 and line =~ /^\S/
        last_line = line_id
      end
      if line_id - last_line > 1 or line_id == lines.length - 1
        depth.delete_if do |v|
          lines[last_line] << "\n\t\\end{#{ lT( v ) }}"
        end
      end
    end
    lines.join( "\n" )
  end
end