mailmate 2.1.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +18 -8
- data/docs/Composing and threading.md +2 -0
- data/docs/MCP results.md +68 -0
- data/lib/mailmate/cli/draft.rb +5 -1
- data/lib/mailmate/cli/message.rb +40 -41
- data/lib/mailmate/cli/search.rb +101 -6
- data/lib/mailmate/cli/send.rb +23 -1
- data/lib/mailmate/config.rb +8 -1
- data/lib/mailmate/eml_lookup.rb +8 -0
- data/lib/mailmate/html_markdown.rb +126 -0
- data/lib/mailmate/mcp.rb +312 -70
- data/lib/mailmate/reply_prefill.rb +37 -6
- data/lib/mailmate/search_syntax.rb +1 -1
- data/lib/mailmate/version.rb +1 -1
- metadata +4 -2
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mailmate
|
|
4
|
+
# @api public
|
|
5
|
+
#
|
|
6
|
+
# HTML → clean markdown, shared by `mmmessage --markdown` (terminal reading)
|
|
7
|
+
# and ReplyPrefill (quoting an HTML parent in a forward or reply).
|
|
8
|
+
#
|
|
9
|
+
# Depends on reverse_markdown (which pulls nokogiri), an OPTIONAL dependency
|
|
10
|
+
# so the base install stays free of native extensions. `available?` says
|
|
11
|
+
# whether the converter can run; `convert` returns nil when it can't, and
|
|
12
|
+
# each caller decides what an honest fallback looks like for its surface —
|
|
13
|
+
# mmmessage prints the raw HTML with a hint, a prefill uses a placeholder.
|
|
14
|
+
#
|
|
15
|
+
# reverse_markdown alone is fine for hand-written HTML and hopeless for
|
|
16
|
+
# transactional/marketing mail, which is built from nested LAYOUT tables:
|
|
17
|
+
# every wrapper becomes a pipe-table fragment (`| |`, `| |`) and the
|
|
18
|
+
# message reads as noise (RubyGems' push notice, 2026-09-04). So the DOM is
|
|
19
|
+
# cleaned before conversion:
|
|
20
|
+
# 1. Drop <head>, <style>, <script> and HTML comments (MSO conditionals,
|
|
21
|
+
# template markers like `<!-- tdMobZ1BottomNew-->`).
|
|
22
|
+
# 2. Drop elements hidden inline (`display:none` — preheaders, mobile-only
|
|
23
|
+
# duplicates) and tracking/spacer images (declared ≤ 2px).
|
|
24
|
+
# 3. Unwrap LAYOUT tables into flowing blocks (rows → div, cells → span),
|
|
25
|
+
# keeping DATA tables (a regular grid of short inline cells, ≥ 2 rows ×
|
|
26
|
+
# ≥ 2 columns, no nested tables) as markdown tables. Innermost first.
|
|
27
|
+
# 4. Normalize the text itself: zero-width spacers used for inbox preview
|
|
28
|
+
# padding (U+034F, U+200B/C/D, U+FEFF) removed, non-breaking spaces
|
|
29
|
+
# made ordinary — in the DOM, so reverse_markdown never sees a U+00A0
|
|
30
|
+
# to spell as ` `.
|
|
31
|
+
# 5. Convert with unknown tags BYPASSED (content kept, tag dropped) —
|
|
32
|
+
# reverse_markdown would otherwise print <footer>/<section>/<center>
|
|
33
|
+
# verbatim — then trim trailing whitespace per line and collapse 3+
|
|
34
|
+
# blank lines to one.
|
|
35
|
+
module HtmlMarkdown
|
|
36
|
+
extend self
|
|
37
|
+
|
|
38
|
+
ZERO_WIDTH = /[͏]/
|
|
39
|
+
NBSP_LIKE = /[ - ]/
|
|
40
|
+
BLOCKISH = "img, div, p, table, ul, ol, h1, h2, h3, h4, h5, h6, br, blockquote, pre"
|
|
41
|
+
|
|
42
|
+
def available?
|
|
43
|
+
require "nokogiri"
|
|
44
|
+
require "reverse_markdown"
|
|
45
|
+
true
|
|
46
|
+
rescue LoadError
|
|
47
|
+
false
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Markdown String, or nil when the optional gems are missing.
|
|
51
|
+
def convert(html)
|
|
52
|
+
return nil unless available?
|
|
53
|
+
|
|
54
|
+
doc = Nokogiri::HTML(html.to_s)
|
|
55
|
+
clean!(doc)
|
|
56
|
+
# unknown_tags: :bypass — reverse_markdown's default PASSES THROUGH tags it
|
|
57
|
+
# doesn't know, and it doesn't know HTML5 sectioning (<footer>, <header>,
|
|
58
|
+
# <section>, <center>…), so a bank alert's footer arrived as raw markup.
|
|
59
|
+
# Bypass keeps the content and drops the tag.
|
|
60
|
+
md = ReverseMarkdown.convert(doc.to_html, unknown_tags: :bypass)
|
|
61
|
+
md = md.lines.map(&:rstrip).join("\n")
|
|
62
|
+
md.gsub!(/\n{3,}/, "\n\n")
|
|
63
|
+
md.strip
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# The DOM passes, in order. Public so a caller can inspect the cleaned
|
|
67
|
+
# HTML; `convert` is what everyone actually wants.
|
|
68
|
+
def clean!(doc)
|
|
69
|
+
doc.css("head, style, script").remove
|
|
70
|
+
doc.xpath("//comment()").remove
|
|
71
|
+
doc.css("[style]").each { |n| n.remove if n["style"].to_s.match?(/display\s*:\s*none/i) }
|
|
72
|
+
doc.css("img").each { |img| img.remove if spacer_image?(img) }
|
|
73
|
+
unwrap_layout_tables!(doc)
|
|
74
|
+
doc.xpath("//text()").each do |t|
|
|
75
|
+
s = t.content
|
|
76
|
+
cleaned = s.gsub(ZERO_WIDTH, "").gsub(NBSP_LIKE, " ")
|
|
77
|
+
t.content = cleaned if cleaned != s
|
|
78
|
+
end
|
|
79
|
+
doc
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
private
|
|
83
|
+
|
|
84
|
+
def spacer_image?(img)
|
|
85
|
+
%w[width height].any? do |attr|
|
|
86
|
+
v = img[attr].to_s.strip
|
|
87
|
+
v.match?(/\A\d+(px)?\z/) && v.to_i <= 2
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Innermost first, so a data table nested in a layout wrapper is judged
|
|
92
|
+
# on its own shape before the wrapper around it is dissolved.
|
|
93
|
+
def unwrap_layout_tables!(doc)
|
|
94
|
+
tables = doc.css("table").to_a
|
|
95
|
+
tables.sort_by { |t| -t.ancestors("table").size }.each do |t|
|
|
96
|
+
next if data_table?(t)
|
|
97
|
+
|
|
98
|
+
t.css("td, th").each do |c|
|
|
99
|
+
c.name = "span"
|
|
100
|
+
c.add_next_sibling(Nokogiri::XML::Text.new(" ", doc))
|
|
101
|
+
end
|
|
102
|
+
t.css("tr").each { |r| r.name = "div" }
|
|
103
|
+
t.css("thead, tbody, tfoot, caption, colgroup").each { |n| n.name = "div" }
|
|
104
|
+
t.name = "div"
|
|
105
|
+
t.attributes.each_key { |k| t.remove_attribute(k) }
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# A table we keep as a table: at least two rows with the same cell count
|
|
110
|
+
# (≥ 2), no table inside it, and only short inline content in its cells.
|
|
111
|
+
# Everything else — single-column stacks, image frames, spacer rows,
|
|
112
|
+
# anything nesting another table — is layout.
|
|
113
|
+
def data_table?(table)
|
|
114
|
+
return false if table.at_css("table")
|
|
115
|
+
|
|
116
|
+
rows = table.xpath("./tr|./thead/tr|./tbody/tr|./tfoot/tr")
|
|
117
|
+
return false if rows.size < 2
|
|
118
|
+
|
|
119
|
+
counts = rows.map { |r| r.xpath("./td|./th").size }
|
|
120
|
+
return false unless counts.uniq.size == 1 && counts.first >= 2
|
|
121
|
+
|
|
122
|
+
cells = rows.flat_map { |r| r.xpath("./td|./th").to_a }
|
|
123
|
+
cells.none? { |c| c.at_css(BLOCKISH) || c.text.strip.length > 200 }
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|