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.
3 # markdown2html.py — simple Markdown-to-HTML converter for use with Mutt
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.
10 # [1]: https://gitlab.com/muttmua/mutt/commit/0e566a03725b4ad789aa6ac1d17cdf7bf4e7e354)
14 # set send_multipart_alternative=yes
15 # set send_multipart_alternative_filter=/path/to/markdown2html.py
17 # Optionally, Custom CSS styles will be read from `~/.mutt/markdown2html.css`,
22 # - PyPandoc (and pandoc installed, or downloaded)
26 # - Pygments, if installed, then syntax highlighting is enabled
29 # https://git.madduck.net/etc/mutt.git/blob_plain/HEAD:/.mutt/markdown2html
31 # Copyright © 2019 martin f. krafft <madduck@madduck.net>
32 # Released under the GPL-2+ licence, just like Mutt itself.
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')
55 border-left: 2px solid #ccc;
65 .quotechar { display: none; }
66 .footnote-ref, .footnote-back { text-decoration: none;}
69 font-family: monospace;
75 STYLESHEET = os.path.join(os.path.expanduser('~/.mutt'),
77 if os.path.exists(STYLESHEET):
78 DEFAULT_CSS += open(STYLESHEET).read()
80 HTML_DOCUMENT = '''<!DOCTYPE html>
82 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
83 <meta charset="utf-8"/>
84 <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"/>
85 <title>HTML E-Mail</title>
86 </head><body class="email">
92 '<div class="signature"><span class="leader">-- </span>{sig}</div>'
95 def _preprocess_markdown(mdwn):
97 Preprocess Markdown for handling by the converter.
99 # convert hard line breaks within paragraphs to 2 trailing spaces, which
100 # is the markdown way of representing hard line breaks. Note how the
101 # regexp will not match between paragraphs.
102 ret = re.sub(r'(\S)\n(\s*\S)', r'\g<1> \n\g<2>', mdwn, flags=re.MULTILINE)
104 # Clients like Thunderbird need the leading '>' to be able to properly
105 # create nested quotes, so we duplicate the symbol, the first instance
106 # will tell pandoc to create a blockquote, while the second instance will
107 # be a <span> containing the character, along with a class that causes CSS
108 # to actually hide it from display. However, this does not work with the
109 # text-mode HTML2text converters, and so it's left commented for now.
110 #ret = re.sub(r'\n>', r' \n>[>]{.quotechar}', ret, flags=re.MULTILINE)
115 def _identify_quotes_for_later(mdwn):
117 Email quoting such as:
120 On 1970-01-01, you said:
121 > The Flat Earth Society has members all around the globe.
124 isn't really properly handled by Markdown, so let's do our best to
125 identify the individual elements, and mark them, using a syntax similar to
126 what pandoc uses already in some cases. As pandoc won't actually use these
127 data (yet?), we call `self._reformat_quotes` later to use these markers
128 to slap the appropriate classes on the HTML tags.
131 def generate_lines_with_context(mdwn):
133 Iterates the input string line-wise, returning a triplet of
134 previous, current, and next line, the first and last of which
135 will be None on the first and last line of the input data
138 prev = cur = nxt = None
139 lines = iter(mdwn.splitlines())
145 yield prev, cur, None
148 for prev, cur, nxt in generate_lines_with_context(mdwn):
150 # The lead-in to a quote is a single line immediately preceding the
151 # quote, and ending with ':'. Note that there could be multiple of
153 if re.match(r'^.+:\s*$', cur) and nxt.startswith('>'):
154 ret.append(f'{{.quotelead}}{cur.strip()}')
155 # pandoc needs an empty line before the blockquote, so
156 # we enter one for the purpose of HTML rendition:
160 # The first blockquote after such a lead-in gets marked as the
162 elif prev and re.match(r'^.+:\s*$', prev) and cur.startswith('>'):
163 ret.append(re.sub(r'^(\s*>\s*)+(.+)',
164 r'\g<1>{.quoteinitial}\g<2>',
165 cur, flags=re.MULTILINE))
167 # All other occurrences of blockquotes get the "subsequent" marker:
168 elif cur.startswith('>') and prev and not prev.startswith('>'):
169 ret.append(re.sub(r'^((?:\s*>\s*)+)(.+)',
170 r'\g<1>{.quotesubsequent}\g<2>',
171 cur, flags=re.MULTILINE))
173 else: # pass through everything else.
176 return '\n'.join(ret)
179 def _reformat_quotes(html):
181 Earlier in the pipeline, we marked email quoting, using markers, which we
182 now need to turn into HTML classes, so that we can use CSS to style them.
184 ret = html.replace('<p>{.quotelead}', '<p class="quotelead">')
185 ret = re.sub(r'<blockquote>\n((?:<blockquote>\n)*)<p>(?:\{\.quote(\w+)\})',
186 r'<blockquote class="quote \g<2>">\n\g<1><p>', ret, flags=re.MULTILINE)
191 def _convert_with_pandoc(mdwn, inputfmt='markdown', outputfmt='html5',
192 ext_enabled=None, ext_disabled=None,
193 standalone=True, title="HTML E-Mail"):
195 Invoke pandoc to do the actual conversion of Markdown to HTML5.
198 ext_enabled = [ 'backtick_code_blocks',
209 'all_symbols_escapable',
210 'intraword_underscores',
219 'tex_math_double_backslash',
222 ext_disabled = [ 'tex_math_single_backslash',
227 enabled = '+'.join(ext_enabled)
228 disabled = '-'.join(ext_disabled)
229 inputfmt = f'{inputfmt}+{enabled}-{disabled}'
233 args.append('--standalone')
235 args.append(f'--metadata=pagetitle:"{title}"')
237 return pypandoc.convert_text(mdwn, format=inputfmt, to=outputfmt,
241 def _apply_styling(html):
243 Inline all styles defined and used into the individual HTML tags.
245 return pynliner.Pynliner().from_string(html).with_cssString(DEFAULT_CSS).run()
248 def _postprocess_html(html):
250 Postprocess the generated and styled HTML.
255 def convert_markdown_to_html(mdwn):
257 Converts the input Markdown to HTML, handling separately the body, as well
258 as an optional signature.
260 parts = re.split(r'^-- $', mdwn, 1, flags=re.MULTILINE)
269 body = _preprocess_markdown(body)
270 body = _identify_quotes_for_later(body)
271 html = _convert_with_pandoc(body, standalone=False)
272 html = _reformat_quotes(html)
275 sig = _preprocess_markdown(sig)
276 html += SIGNATURE_HTML.format(sig='<br/>'.join(sig.splitlines()))
278 html = HTML_DOCUMENT.format(htmlbody=html)
279 html = _apply_styling(html)
280 html = _postprocess_html(html)
287 Convert text on stdin to HTML, and print it to stdout, like mutt would
290 html = convert_markdown_to_html(sys.stdin.read())
292 # mutt expects the content type in the first line, so:
293 print(f'text/html\n\n{html}')
296 if __name__ == '__main__':