# frozen_string_literal: true
# A very simple formatter for imported emails
class EmailCook
def self.raw_regexp
@raw_regexp ||=
%r{^\[plaintext\]$\n(.*)\n^\[/plaintext\]$(?:\s^\[attachments\]$\n(.*)\n^\[/attachments\]$)?(?:\s^\[elided\]$\n(.*)\n^\[/elided\]$)?}m
end
def initialize(raw)
@raw = raw
@body, @attachment_html, @elided = @raw.scan(EmailCook.raw_regexp).first
end
def add_quote(result, buffer)
if buffer.present?
return if buffer =~ /\A(
)+\z$/
result << "
#{buffer}" end end def link_string!(line, unescaped_line) unescaped_line = unescaped_line.strip line.gsub!(/\S+/) do |str| if str.match?(%r{^(https?://)[\S]+$}i) begin url = URI.parse(str).to_s if unescaped_line == url # this could be oneboxed str = %|#{url}| else str = %|#{url}| end rescue URI::Error # don't fail if uri does not parse end end str end end def htmlify(text) result = +"" quote_buffer = +"" in_text = false in_quote = false text.each_line do |line| # replace indentation with non-breaking spaces line.sub!(/^\s{2,}/) { |s| "\u00A0" * s.length } if line =~ /^\s*>/ in_quote = true line.sub!(/^[\s>]*/, "") unescaped_line = line line = CGI.escapeHTML(line) link_string!(line, unescaped_line) quote_buffer << line << "