2015-06-05 10:46:21 -05:00
|
|
|
# A very simple formatter for imported emails
|
2015-10-28 12:31:28 -05:00
|
|
|
|
|
|
|
require 'uri'
|
|
|
|
|
2015-06-05 10:46:21 -05:00
|
|
|
class EmailCook
|
|
|
|
|
|
|
|
def initialize(raw)
|
|
|
|
@raw = raw
|
|
|
|
end
|
|
|
|
|
|
|
|
def cook
|
|
|
|
result = ""
|
|
|
|
|
|
|
|
in_quote = false
|
|
|
|
quote_buffer = ""
|
|
|
|
@raw.each_line do |l|
|
|
|
|
|
|
|
|
if l =~ /^\s*>/
|
|
|
|
in_quote = true
|
|
|
|
quote_buffer << l.sub(/^[\s>]*/, '') << "<br>"
|
|
|
|
elsif in_quote
|
|
|
|
result << "<blockquote>#{quote_buffer}</blockquote>"
|
|
|
|
quote_buffer = ""
|
|
|
|
in_quote = false
|
|
|
|
else
|
|
|
|
result << l << "<br>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if in_quote
|
|
|
|
result << "<blockquote>#{quote_buffer}</blockquote>"
|
|
|
|
end
|
|
|
|
|
|
|
|
result.gsub!(/(<br>){3,10}/, '<br><br>')
|
|
|
|
|
2015-10-28 12:31:28 -05:00
|
|
|
URI.extract(result).each do |m|
|
|
|
|
result.gsub!(m, "<a href='#{m}'>#{m}</a>")
|
|
|
|
end
|
|
|
|
|
2015-06-05 10:46:21 -05:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|