def self.grab_lines_for_list_item(reader, list_type, sibling_trait = nil, has_text = true)
buffer = []
continuation = :inactive
within_nested_list = false
detached_continuation = nil
while reader.has_more_lines?
this_line = reader.get_line
break if is_sibling_list_item?(this_line, list_type, sibling_trait)
prev_line = buffer.empty? ? nil : buffer.last.chomp
if prev_line == LIST_CONTINUATION
if continuation == :inactive
continuation = :active
has_text = true
buffer[-1] = "\n" unless within_nested_list
end
if this_line.chomp == LIST_CONTINUATION
if continuation != :frozen
continuation = :frozen
buffer << this_line
end
this_line = nil
next
end
end
if match = is_delimited_block?(this_line, true)
if continuation == :active
buffer << this_line
buffer.concat reader.grab_lines_until(:terminator => match.terminator, :grab_last_line => true)
continuation = :inactive
else
break
end
elsif list_type == :dlist && continuation != :active && this_line.match(REGEXP[:attr_line])
break
else
if continuation == :active && !this_line.chomp.empty?
if this_line.match(REGEXP[:lit_par])
reader.unshift_line this_line
buffer.concat reader.grab_lines_until(
:preserve_last_line => true,
:break_on_blank_lines => true,
:break_on_list_continuation => true) {|line|
list_type == :dlist && is_sibling_list_item?(line, list_type, sibling_trait)
}
continuation = :inactive
elsif this_line.match(REGEXP[:blk_title]) || this_line.match(REGEXP[:attr_line]) || this_line.match(REGEXP[:attr_entry])
buffer << this_line
else
if nested_list_type = (within_nested_list ? [:dlist] : NESTABLE_LIST_CONTEXTS).detect {|ctx| this_line.match(REGEXP[ctx]) }
within_nested_list = true
if nested_list_type == :dlist && $~[3].to_s.empty?
has_text = false
end
end
buffer << this_line
continuation = :inactive
end
elsif !prev_line.nil? && prev_line.chomp.empty?
if this_line.chomp.empty?
reader.skip_blank_lines
this_line = reader.get_line
break if this_line.nil? || is_sibling_list_item?(this_line, list_type, sibling_trait)
end
if this_line.chomp == LIST_CONTINUATION
detached_continuation = buffer.size
buffer << this_line
else
if has_text
if this_line.match(REGEXP[:lit_par])
reader.unshift_line this_line
buffer.concat reader.grab_lines_until(
:preserve_last_line => true,
:break_on_blank_lines => true,
:break_on_list_continuation => true) {|line|
list_type == :dlist && is_sibling_list_item?(line, list_type, sibling_trait)
}
elsif is_sibling_list_item?(this_line, list_type, sibling_trait)
break
elsif nested_list_type = NESTABLE_LIST_CONTEXTS.detect {|ctx| this_line.match(REGEXP[ctx]) }
buffer << this_line
within_nested_list = true
if nested_list_type == :dlist && $~[3].to_s.empty?
has_text = false
end
else
break
end
else
buffer.pop unless within_nested_list
buffer << this_line
has_text = true
end
end
else
has_text = true if !this_line.chomp.empty?
if nested_list_type = (within_nested_list ? [:dlist] : NESTABLE_LIST_CONTEXTS).detect {|ctx| this_line.match(REGEXP[ctx]) }
within_nested_list = true
if nested_list_type == :dlist && $~[3].to_s.empty?
has_text = false
end
end
buffer << this_line
end
end
this_line = nil
end
reader.unshift_line this_line if !this_line.nil?
if detached_continuation
buffer.delete_at detached_continuation
end
buffer.pop while !buffer.empty? && buffer.last.chomp.empty?
if !buffer.empty? && buffer.last.chomp == LIST_CONTINUATION
buffer.pop
end
buffer
end