Class: EmailCook

Inherits:
Object
  • Object
show all
Defined in:
lib/email_cook.rb

Overview

A very simple formatter for imported emails

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ EmailCook

Returns a new instance of EmailCook.



10
11
12
13
# File 'lib/email_cook.rb', line 10

def initialize(raw)
  @raw = raw
  @body, @attachment_html, @elided = @raw.scan(EmailCook.raw_regexp).first
end

Class Method Details

.raw_regexpObject



5
6
7
8
# File 'lib/email_cook.rb', line 5

def self.raw_regexp
  @raw_regexp ||=
    %r{\A\[plaintext\]$\n(.*)\n^\[/plaintext\]$(?:\s^\[attachments\]$\n(.*)\n^\[/attachments\]$)?(?:\s^\[elided\]$\n(.*)\n^\[/elided\]$)?}m
end

Instance Method Details

#add_quote(result, buffer) ⇒ Object



15
16
17
18
19
20
# File 'lib/email_cook.rb', line 15

def add_quote(result, buffer)
  if buffer.present?
    return if buffer =~ /\A(<br>)+\z\z/
    result << "<blockquote>#{buffer}</blockquote>"
  end
end

#cook(opts = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/email_cook.rb', line 92

def cook(opts = {})
  # fallback to PrettyText if we failed to detect a body
  return PrettyText.cook(@raw, opts) if @body.nil?

  result = htmlify(@body)
  result << "\n<br>" << @attachment_html if @attachment_html.present?
  result << "\n<br><br>" << Email::Receiver.elided_html(htmlify(@elided)) if @elided.present?
  result
end

#htmlify(text) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/email_cook.rb', line 42

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!(/\A\s{2,}/) { |s| "\u00A0" * s.length }

    if line =~ /\A\s*>/
      in_quote = true
      line.sub!(/\A[\s>]*/, "")

      unescaped_line = line
      line = CGI.escapeHTML(line)
      link_string!(line, unescaped_line)

      quote_buffer << line << "<br>"
    elsif in_quote
      add_quote(result, quote_buffer)
      quote_buffer = ""
      in_quote = false
    else
      sz = line.size

      unescaped_line = line
      line = CGI.escapeHTML(line)
      link_string!(line, unescaped_line)

      if sz < 60
        result << "<br>" if in_text && line == "\n"
        result << line
        result << "<br>"

        in_text = false
      else
        result << line
        in_text = true
      end
    end
  end

  add_quote(result, quote_buffer) if in_quote && quote_buffer.present?

  result.gsub!(/(<br>\n*){3,10}/, "<br><br>")
  result
end


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/email_cook.rb', line 22

def link_string!(line, unescaped_line)
  unescaped_line = unescaped_line.strip
  line.gsub!(/\S+/) do |str|
    if str.match?(%r{\A(https?://)[\S]+\z}i)
      begin
        url = URI.parse(str).to_s
        if unescaped_line == url
          # this could be oneboxed
          str = %|<a href="#{url}" class="onebox" target="_blank">#{url}</a>|
        else
          str = %|<a href="#{url}">#{url}</a>|
        end
      rescue URI::Error
        # don't fail if uri does not parse
      end
    end
    str
  end
end