2015-06-05 10:46:21 -05:00
|
|
|
# A very simple formatter for imported emails
|
2015-10-28 12:31:28 -05:00
|
|
|
|
2015-06-05 10:46:21 -05:00
|
|
|
class EmailCook
|
|
|
|
|
2015-10-29 15:01:50 -05:00
|
|
|
def self.url_regexp
|
2016-07-12 12:33:13 -05:00
|
|
|
/^((?:https?:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.])(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\([^\s()<>]+\)|[^`!()\[\]{};:'".,<>?«»“”‘’\s]))/
|
2015-10-29 15:01:50 -05:00
|
|
|
end
|
|
|
|
|
2015-06-05 10:46:21 -05:00
|
|
|
def initialize(raw)
|
|
|
|
@raw = raw
|
|
|
|
end
|
|
|
|
|
|
|
|
def cook
|
|
|
|
result = ""
|
|
|
|
|
2016-07-12 14:37:19 -05:00
|
|
|
in_text = false
|
2015-06-05 10:46:21 -05:00
|
|
|
in_quote = false
|
2016-07-12 14:37:19 -05:00
|
|
|
|
2015-06-05 10:46:21 -05:00
|
|
|
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
|
2015-10-29 15:01:50 -05:00
|
|
|
|
2016-07-12 12:49:03 -05:00
|
|
|
sz = l.size
|
|
|
|
|
2015-10-29 15:07:45 -05:00
|
|
|
l.scan(EmailCook.url_regexp).each do |m|
|
2015-10-29 15:01:50 -05:00
|
|
|
url = m[0]
|
|
|
|
l.gsub!(url, "<a href='#{url}'>#{url}</a>")
|
|
|
|
end
|
2016-07-12 12:49:03 -05:00
|
|
|
|
|
|
|
result << l
|
2016-07-12 14:37:19 -05:00
|
|
|
|
|
|
|
if sz < 60
|
|
|
|
result << "<br>"
|
|
|
|
if in_text
|
|
|
|
result << "<br>"
|
|
|
|
end
|
|
|
|
in_text = false
|
|
|
|
else
|
|
|
|
in_text = true
|
|
|
|
end
|
2015-06-05 10:46:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if in_quote
|
|
|
|
result << "<blockquote>#{quote_buffer}</blockquote>"
|
|
|
|
end
|
|
|
|
|
2016-07-12 14:37:19 -05:00
|
|
|
result.gsub!(/(<br>\n*){3,10}/, '<br><br>')
|
2015-06-05 10:46:21 -05:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|