forked from asottile/markdown-code-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown_code_blocks.py
38 lines (27 loc) · 1.05 KB
/
markdown_code_blocks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from __future__ import absolute_import
from __future__ import unicode_literals
import argparse
import io
import mistune
import pygments.formatters
import pygments.lexers
import pygments.util
class CodeRenderer(mistune.Renderer):
def block_code(self, code, lang):
try:
lexer = pygments.lexers.get_lexer_by_name(lang, stripnl=False)
except pygments.util.ClassNotFound:
lexer = pygments.lexers.get_lexer_by_name('text', stripnl=False)
formatter = pygments.formatters.HtmlFormatter()
return pygments.highlight(code, lexer=lexer, formatter=formatter)
def highlight(doc, Renderer=CodeRenderer):
return mistune.Markdown(Renderer(escape=True, hard_wrap=False))(doc)
def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument('filename', default='/dev/stdin')
args = parser.parse_args(argv)
with io.open(args.filename) as f:
hl = highlight(f.read())
print('<!doctype html><html><body>{}</body></html>'.format(hl))
if __name__ == '__main__':
exit(main())