Class: EmailReplyTrimmer

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

Constant Summary collapse

VERSION =
"0.3.0"
DELIMITER =
"d"
EMBEDDED =
"b"
EMPTY =
"e"
EMAIL_HEADER =
"h"
QUOTE =
"q"
SIGNATURE =
"s"
TEXT =
"t"

Class Method Summary collapse

Class Method Details

.compute_elided(text, lines) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/email_reply_trimmer.rb', line 223

def self.compute_elided(text, lines)
  elided = []

  t = 0
  l = 0

  while t < text.size
    while l < lines.size && text[t] == lines[l]
      t += 1
      l += 1
    end
    elided << text[t]
    t += 1
  end

  elided.join("\n").strip
end

.extract_embedded_email(text) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/email_reply_trimmer.rb', line 146

def self.extract_embedded_email(text)
  return if text.nil? || text =~ /\A[[:space:]]*\z/m

  # do some cleanup
  preprocess!(text)

  # from now on, we'll work on a line-by-line basis
  lines = text.split("\n")

  # identify content of each lines
  pattern = lines.map { |l| identify_line_content(l) }.join

  if index = pattern =~ /(?:h[eqd]*?){3,}[tq]/
    embedded = lines[index..-1].join("\n").strip
  elsif index = pattern =~ /b(?:[eqd]*){3,}[tq]/
    # Exception for email clients (macOS / iOS) which embed fwd emails in quotes.
    embedded = lines[index + 1..-1].map { |l| l.gsub(/^>\s*/, '') }.join("\n").strip
  end

  if index
    before = lines[0...(pattern[0...index] =~ /e*(b[eqd]*|b*[ed]*)$/)].join("\n").strip
    [embedded, before]
  end
end

.hoist_code_blocks(text) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/email_reply_trimmer.rb', line 173

def self.hoist_code_blocks(text)
  blocks = {}
  pattern = /^```\w*$\n.*?^```$/m

  text.gsub!(pattern) do |block|
    token = SecureRandom.hex
    blocks[token] = block
    token
  end

  [text, blocks]
end

.identify_line_content(line) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/email_reply_trimmer.rb', line 21

def self.identify_line_content(line)
  return EMPTY        if EmptyLineMatcher.match? line
  return DELIMITER    if DelimiterMatcher.match? line
  return SIGNATURE    if SignatureMatcher.match? line
  return EMBEDDED     if EmbeddedEmailMatcher.match? line
  return EMAIL_HEADER if EmailHeaderMatcher.match? line
  return QUOTE        if QuoteMatcher.match? line
  TEXT
end

.is_reply_at_end?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/email_reply_trimmer.rb', line 241

def self.is_reply_at_end?(pattern)
  pattern =~ /^b[^t]+t[et]*$/
end

.preprocess!(text) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/email_reply_trimmer.rb', line 186

def self.preprocess!(text)
  # normalize line endings
  text.gsub!("\r\n", "\n")

  # remove PGP markers
  text.gsub!(/\A-----BEGIN PGP SIGNED MESSAGE-----\n(?:Hash: \w+)?\s+/i, "")
  text.gsub!(/^-----BEGIN PGP SIGNATURE-----$[\s\S]+^-----END PGP SIGNATURE-----/, "")

  # remove unsubscribe links
  text.gsub!(/^Unsubscribe: .+@.+(\n.+http:.+)?\s*\z/i, "")

  # remove alias-style quotes marker
  text.gsub!(/^.*>{5} "[^"\n]+" == .+ writes:/, "")

  # change enclosed-style quotes format
  text.gsub!(/^>>> ?(.+) ?>>>$\n([\s\S]+?)\n^<<< ?\1 ?<<<$/) { $2.gsub(/^/, "> ") }
  text.gsub!(/^>{4,}[[:blank:]]*$\n([\s\S]+?)\n^<{4,}[[:blank:]]*$/) { $1.gsub(/^/, "> ") }

  # fix all quotes formats
  text.gsub!(/^((?:[[:blank:]]*[[:alpha:]]*[>|])+)/) { $1.gsub(/([[:alpha:]]+>|\|)/, ">") }

  # fix embedded email markers that might span over multiple lines
  (
    EmbeddedEmailMatcher::ON_DATE_SOMEONE_WROTE_REGEXES +
    EmbeddedEmailMatcher::SOMEONE_WROTE_ON_DATE_REGEXES +
    EmbeddedEmailMatcher::DATE_SOMEONE_WROTE_REGEXES +
    [EmbeddedEmailMatcher::DATE_SOMEONE_EMAIL_REGEX]
  ).each do |r|
    text.gsub!(r) do |m|
      m.count("\n") > 4 ? m : m.gsub(/\n+[[:space:]]*/, " ")
    end
  end

  # remove leading/trailing whitespaces
  text.strip!
end

.trim(text, split = false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/email_reply_trimmer.rb', line 31

def self.trim(text, split = false)
  return if text.nil? || text =~ /\A[[:space:]]*\z/m

  # do some cleanup
  preprocess!(text)

  # stash the code blocks - replace them with hashes
  text, blocks = hoist_code_blocks(text)

  # from now on, we'll work on a line-by-line basis
  lines = text.split("\n")
  lines_dup = lines.dup

  # create a string of characters, one per line, according to the line content
  pattern = lines.map { |l| identify_line_content(l) }.join

  # remove everything after the first delimiter
  if pattern =~ /d/
    index = pattern =~ /d/
    underscore_separator_before_embedded_email =
      lines[index] =~ /\A_+\z/ && pattern[(index + 1)..-1] =~ /b/

    unless underscore_separator_before_embedded_email
      pattern = pattern[0...index]
      lines = lines[0...index]
    end
  end

  # remove all mobile signatures
  while pattern =~ /s/
    index = pattern =~ /s/
    pattern.slice!(index)
    lines.slice!(index)
  end

  # when the reply is at the end of the email
  if is_reply_at_end?(pattern)
    index = pattern =~ /t[et]*$/
    pattern = ""
    lines = lines[index..-1]
  end

  # if there is an embedded email marker, not followed by a quote
  # then take everything up to that marker
  if pattern =~ /te*b[^q]*$/
    index = pattern =~ /te*b[^q]*$/
    pattern = pattern[0..index]
    lines = lines[0..index]
  end

  # if there is an embedded email marker, followed by a huge quote
  # then take everything up to that marker
  if pattern =~ /te*b[eqbh]*([te]*)$/ && $1.count("t") < 7 && pattern !~ /bq[eqbh]*t/
    index = pattern =~ /te*b[eqbh]*[te]*$/
    pattern = pattern[0..index]
    lines = lines[0..index]
  end

  # if there is some text before a huge quote ending the email,
  # then remove the quote
  if pattern =~ /t?e*[qbe]+$/
    index = pattern =~ /t?e*[qbe]+$/
    pattern = pattern[0..index]
    lines = lines[0..index]
  end

  # if there still are some embedded email markers, just remove them
  while pattern =~ /b/
    index = pattern =~ /b/
    pattern.slice!(index)
    lines.slice!(index)
  end

  # fix email headers when they span over multiple lines
  if pattern =~ /h+[hte]+h+e/
    index = pattern =~ /h+[hte]+h+e/
    size = pattern[/h+[hte]+h+e/].size
    size.times.each { |s| pattern[index + s] = EMAIL_HEADER }
  end

  # if there are at least 3 consecutive email headers,
  # take everything up to these headers
  if pattern =~ /t[eq]*h{3,}/
    index = pattern =~ /t[eq]*h{3,}/
    pattern = pattern[0..index]
    lines = lines[0..index]
  end

  # if there still are some email headers, just remove them
  while pattern =~ /h/
    index = pattern =~ /h/
    pattern.slice!(index)
    lines.slice!(index)
  end

  # remove trailing quotes when there's at least one line of text
  if pattern =~ /t/ && pattern =~ /[eq]+$/
    index = pattern =~ /[eq]+$/
    pattern = pattern[0...index]
    lines = lines[0...index]
  end

  # results
  trimmed = lines.join("\n").strip

  # re-inject code blocks
  blocks.each { |token, block| trimmed.gsub!(token, block) }

  if split
    [trimmed, compute_elided(lines_dup, lines)]
  else
    trimmed
  end
end