]> git.madduck.net Git - etc/mutt.git/blob - .config/mutt/markdown2html

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

add p&m alternates
[etc/mutt.git] / .config / mutt / markdown2html
1 #!/usr/bin/python3
2 #
3 # markdown2html.py — simple Markdown-to-HTML converter for use with Mutt
4 #
5 # Mutt recently learnt [how to compose `multipart/alternative`
6 # emails][1]. This script assumes a message has been composed using Markdown
7 # (with a lot of pandoc extensions enabled), and translates it to `text/html`
8 # for Mutt to tie into such a `multipart/alternative` message.
9 #
10 # [1]: https://gitlab.com/muttmua/mutt/commit/0e566a03725b4ad789aa6ac1d17cdf7bf4e7e354)
11 #
12 # Configuration:
13 #   muttrc:
14 #     set send_multipart_alternative=yes
15 #     set send_multipart_alternative_filter=/path/to/markdown2html.py
16 #
17 # Optionally, Custom CSS styles will be read from `~/.config/mutt/markdown2html.css`,
18 # if present.
19 #
20 # Requirements:
21 #   - python3
22 #   - PyPandoc (and pandoc installed, or downloaded)
23 #   - Pynliner
24 #
25 # Optional:
26 #   - Pygments, if installed, then syntax highlighting is enabled
27 #
28 # Latest version:
29 #   https://git.madduck.net/etc/mutt.git/blob_plain/HEAD:/.mutt/markdown2html
30 #
31 # Copyright © 2019 martin f. krafft <madduck@madduck.net>
32 # Released under the GPL-2+ licence, just like Mutt itself.
33 #
34
35 import pypandoc
36 import pynliner
37 import re
38 import os
39 import sys
40
41 try:
42     from pygments.formatters import get_formatter_by_name
43     formatter = get_formatter_by_name('html', style='default')
44     DEFAULT_CSS = formatter.get_style_defs('.sourceCode')
45
46 except ImportError:
47     DEFAULT_CSS = ""
48
49
50 DEFAULT_CSS += '''
51 .block {
52     padding: 0 0.5em;
53     margin: 0;
54     font-style: italic;
55     border-left: 2px solid #eee;
56 }
57 .quote, blockquote {
58     padding: 0 0.5em;
59     margin: 0;
60     font-style: italic;
61     border-left: 2px solid #666;
62     color: #666;
63     font-size: 80%;
64 }
65 .quotelead {
66     margin-bottom: -1em;
67     font-size: 80%;
68 }
69 .quotechar { display: none; }
70 .footnote-ref, .footnote-back { text-decoration: none;}
71 .signature {
72     color: #999;
73     font-family: monospace;
74     white-space: pre;
75     margin: 1em 0 0 0;
76     font-size: 80%;
77 }
78 table, th, td {
79     border-collapse: collapse;
80     border: 1px solid #999;
81 }
82 th, td { padding: 0.5em; }
83 .header {
84     background: #eee;
85 }
86 .even { background: #eee; }
87 h1, h2, h3, h4, h5, h6 {
88     color: #666;
89     background-color: #eee;
90     padding-left: 0.5em
91 }
92 h1 { font-size: 130%; }
93 h2 { font-size: 120%; }
94 h3 { font-size: 110%; }
95 h4 { font-size: 107%; }
96 h5 { font-size: 103%; }
97 h6 { font-size: 100%; }
98 p { padding: 0 0.5em; }
99 pre { padding: 0 1em; }
100 '''
101
102 STYLESHEET = os.path.join(os.path.expanduser('~/.config/mutt'),
103                           'markdown2html.css')
104 if os.path.exists(STYLESHEET):
105     DEFAULT_CSS += open(STYLESHEET).read()
106
107 SIGNATURE_HTML = \
108 """
109 <div class="signature"><span class="leader">-- </span><br/>
110 {sig}</div>
111 """
112
113
114 def _preprocess_signature(sig):
115     '''
116     Preprocess the signature before markdown processing.
117     '''
118     return f"```\n{sig}\n```"
119
120 def _preprocess_markdown(mdwn):
121     '''
122     Preprocess Markdown for handling by the converter.
123     '''
124     # convert hard line breaks within paragraphs to 2 trailing spaces, which
125     # is the markdown way of representing hard line breaks. Note how the
126     # regexp will not match between paragraphs.
127     ret = re.sub(r'(\S)\n(\s*\S)', r'\g<1>  \n\g<2>', mdwn, flags=re.MULTILINE)
128
129     # Clients like Thunderbird need the leading '>' to be able to properly
130     # create nested quotes, so we duplicate the symbol, the first instance
131     # will tell pandoc to create a blockquote, while the second instance will
132     # be a <span> containing the character, along with a class that causes CSS
133     # to actually hide it from display. However, this does not work with the
134     # text-mode HTML2text converters, and so it's left commented for now.
135     #ret = re.sub(r'\n>', r'  \n>[>]{.quotechar}', ret, flags=re.MULTILINE)
136
137     # With the autolink_bare_uris extension, we do not need to put links into
138     # angle brackets to have them converted, so let's conserve the brackets
139     # when used around email addresses. Note that this needs a postprocessing
140     # hack because the pandoc autolink converted includes the ambersand
141     # (https://github.com/jgm/pandoc/issues/7398).
142     ret = re.sub(r'<([^@]+@\S+)>', r'&lt;\g<1> PANDOC_BUG_7398 &gt;', ret)
143     return ret
144
145
146 def _identify_quotes_for_later(mdwn):
147     '''
148     Email quoting such as:
149
150     ```
151     On 1970-01-01, you said:
152     > The Flat Earth Society has members all around the globe.
153     ```
154
155     isn't really properly handled by Markdown, so let's do our best to
156     identify the individual elements, and mark them, using a syntax similar to
157     what pandoc uses already in some cases. As pandoc won't actually use these
158     data (yet?), we call `self._reformat_quotes` later to use these markers
159     to slap the appropriate classes on the HTML tags.
160     '''
161
162     def generate_lines_with_context(mdwn):
163         '''
164         Iterates the input string line-wise, returning a triplet of
165         previous, current, and next line, the first and last of which
166         will be None on the first and last line of the input data
167         respectively.
168         '''
169         prev = cur = nxt = None
170         lines = iter(mdwn.splitlines())
171         cur = next(lines)
172         for nxt in lines:
173             yield prev, cur, nxt
174             prev = cur
175             cur = nxt
176         yield prev, cur, None
177
178     ret = []
179     for prev, cur, nxt in generate_lines_with_context(mdwn):
180
181         # The lead-in to a quote is a single line immediately preceding the
182         # quote, and ending with ':'. Note that there could be multiple of
183         # these:
184         if re.match(r'^[^>]+.*:\s*$', cur) and nxt.startswith('>'):
185             ret.append(f'{{.quotelead}}{cur.strip()}')
186             # pandoc needs an empty line before the blockquote, so
187             # we enter one for the purpose of HTML rendition:
188             ret.append('')
189             continue
190
191         # The first blockquote after such a lead-in gets marked as the
192         # "initial" quote:
193         elif prev and re.match(r'^[^>]+.*:\s*$', prev) and cur.startswith('>'):
194             ret.append(re.sub(r'^(\s*>\s*)+(.+)',
195                               r'\g<1>{.quoteinitial}\g<2>',
196                               cur, flags=re.MULTILINE))
197
198         # All other occurrences of blockquotes get the "subsequent" marker:
199         elif cur.startswith('>') and prev is not None and not prev.startswith('>'):
200             ret.append(re.sub(r'^((?:\s*>\s*)+)(.+)',
201                               r'\g<1>{.quotesubsequent}\g<2>',
202                               cur, flags=re.MULTILINE))
203
204         else: # pass through everything else.
205             ret.append(cur)
206
207     return '\n'.join(ret)
208
209
210 def _reformat_quotes(html):
211     '''
212     Earlier in the pipeline, we marked email quoting, using markers, which we
213     now need to turn into HTML classes, so that we can use CSS to style them.
214     '''
215     ret = html.replace('<p>{.quotelead}', '<p class="quotelead">')
216     ret = re.sub(r'<blockquote>\n((?:<blockquote>\n)*)<p>(?:\{\.quote(\w+)\})',
217                  r'<blockquote class="quote \g<2>">\n\g<1><p>', ret, flags=re.MULTILINE)
218     return ret
219
220
221
222 def _convert_with_pandoc(mdwn, inputfmt='markdown', outputfmt='html5',
223                          ext_enabled=None, ext_disabled=None,
224                          standalone=True, selfcontained=True, title='Untitled'):
225     '''
226     Invoke pandoc to do the actual conversion of Markdown to HTML5.
227     '''
228     if not ext_enabled:
229         ext_enabled = [ 'backtick_code_blocks',
230                         'line_blocks',
231                         'fancy_lists',
232                         'startnum',
233                         'definition_lists',
234                         'example_lists',
235                         'table_captions',
236                         'simple_tables',
237                         'multiline_tables',
238                         'grid_tables',
239                         'pipe_tables',
240                         'all_symbols_escapable',
241                         'intraword_underscores',
242                         'strikeout',
243                         'superscript',
244                         'subscript',
245                         'fenced_divs',
246                         'bracketed_spans',
247                         'footnotes',
248                         'inline_notes',
249                         'emoji',
250                         'tex_math_double_backslash',
251                         'autolink_bare_uris'
252                         ]
253     if not ext_disabled:
254         ext_disabled = [ 'tex_math_single_backslash',
255                          'tex_math_dollars',
256                          'smart',
257                          'raw_html',
258                          'yaml_metadata_block'
259                        ]
260
261     enabled = '+'.join(ext_enabled)
262     disabled = '-'.join(ext_disabled)
263     inputfmt = f'{inputfmt}+{enabled}-{disabled}'
264
265     args = ['--metadata=document-css:false']
266     if standalone:
267         args.append('--standalone')
268     if selfcontained:
269         args.append('--self-contained')
270     if title:
271         args.append(f'--metadata=title:{title}')
272
273     return pypandoc.convert_text(mdwn, format=inputfmt, to=outputfmt,
274                                  extra_args=args)
275
276
277 def _apply_styling(html):
278     '''
279     Inline all styles defined and used into the individual HTML tags.
280     '''
281     return pynliner.Pynliner().from_string(html).with_cssString(DEFAULT_CSS).run()
282
283
284 def _postprocess_html(html):
285     '''
286     Postprocess the generated and styled HTML.
287     '''
288
289     # Preprocessing leaves a sentinel to work around
290     # https://github.com/jgm/pandoc/issues/7398, and so we need to remove it:
291     html = html.replace(' PANDOC_BUG_7398 ', '')
292     return html
293
294
295 def convert_markdown_to_html(mdwn):
296     '''
297     Converts the input Markdown to HTML, handling separately the body, as well
298     as an optional signature.
299     '''
300     parts = re.split(r'^-- \n', mdwn, 1, flags=re.MULTILINE)
301     body = parts[0]
302     if len(parts) == 2:
303         sig = parts[1]
304     else:
305         sig = None
306
307     html=''
308     if body:
309         body = _preprocess_markdown(body)
310         body = _identify_quotes_for_later(body)
311         html = _convert_with_pandoc(body, standalone=True, selfcontained=True,
312                                     title=None)
313         html = _reformat_quotes(html)
314
315     if sig:
316         sig = _preprocess_signature(sig)
317         sig = _preprocess_markdown(sig)
318         sig = _convert_with_pandoc(sig, standalone=False, selfcontained=False,
319                                    title=None)
320         sig = SIGNATURE_HTML.format(sig=sig)
321         eob = html.find('</body>')
322         html = f'{html[:eob]}{sig}\n{html[eob:]}'
323
324     html = _apply_styling(html)
325     html = _postprocess_html(html)
326
327     return html
328
329
330 def main():
331     '''
332     Convert text on stdin to HTML, and print it to stdout, like mutt would
333     expect.
334     '''
335     with open(sys.argv[1], 'r') if len(sys.argv) > 1 else sys.stdin as f:
336         html = convert_markdown_to_html(f.read())
337         if html:
338             # mutt expects the content type in the first line, so:
339             print(f'text/html\n\n{html}')
340
341
342 if __name__ == '__main__':
343     main()