## parse the lines of text into chunk objects. the heuristics here
## need tweaking in some nice manner. TODO: move these heuristics
## into the classes themselves.
-
def text_to_chunks lines
state = :text # one of :text, :quote, or :sig
chunks = []
lines.each_with_index do |line, i|
nextline = lines[(i + 1) ... lines.length].find { |l| l !~ /^\s*$/ } # skip blank lines
+
case state
when :text
newstate = nil
+
if line =~ QUOTE_PATTERN || (line =~ QUOTE_START_PATTERN && (nextline =~ QUOTE_PATTERN || nextline =~ QUOTE_START_PATTERN))
newstate = :quote
elsif line =~ SIG_PATTERN && (lines.length - i) < MAX_SIG_DISTANCE
elsif line =~ BLOCK_QUOTE_PATTERN
newstate = :block_quote
end
+
if newstate
chunks << Text.new(chunk_lines) unless chunk_lines.empty?
chunk_lines = [line]
else
chunk_lines << line
end
+
when :quote
newstate = nil
+
if line =~ QUOTE_PATTERN || line =~ QUOTE_START_PATTERN || line =~ /^\s*$/
chunk_lines << line
elsif line =~ SIG_PATTERN && (lines.length - i) < MAX_SIG_DISTANCE
else
newstate = :text
end
+
if newstate
if chunk_lines.empty?
# nothing
chunk_lines = [line]
state = newstate
end
+
when :block_quote
chunk_lines << line
+
when :sig
chunk_lines << line
end