2015-06-05 11:46:21 -04:00
|
|
|
# A very simple formatter for imported emails
|
2015-10-28 13:31:28 -04:00
|
|
|
|
2015-06-05 11:46:21 -04:00
|
|
|
class EmailCook
|
|
|
|
|
2015-10-29 16:01:50 -04:00
|
|
|
def self.url_regexp
|
|
|
|
/[^\>]*((?:https?:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.])(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\([^\s()<>]+\)|[^`!()\[\]{};:'".,<>?«»\s]))/
|
|
|
|
end
|
|
|
|
|
2015-06-05 11:46:21 -04:00
|
|
|
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
|
2015-10-29 16:01:50 -04:00
|
|
|
|
2015-10-29 16:07:45 -04:00
|
|
|
l.scan(EmailCook.url_regexp).each do |m|
|
2015-10-29 16:01:50 -04:00
|
|
|
url = m[0]
|
|
|
|
l.gsub!(url, "<a href='#{url}'>#{url}</a>")
|
|
|
|
end
|
2015-06-05 11:46:21 -04:00
|
|
|
result << l << "<br>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if in_quote
|
|
|
|
result << "<blockquote>#{quote_buffer}</blockquote>"
|
|
|
|
end
|
|
|
|
|
|
|
|
result.gsub!(/(<br>){3,10}/, '<br><br>')
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|