-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdrender.py
executable file
·36 lines (31 loc) · 1.14 KB
/
mdrender.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
#!/bin/env python
#import markdown as md
import sys, re, html, subprocess
template = \
"""\
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>%s</title>
<link rel="stylesheet" href="./style.css">
<!-- <link rel="stylesheet" href="./hljsdefault.min.css">
<script src="./hljs.min.js"></script>
<script>hljs.highlightAll();</script> -->
</head>
<body>
"""
filename = sys.argv[1]
outfname = sys.argv[2] if len(sys.argv) > 2 else re.sub('(\\.md)?$', '.html', filename, re.T)
with open(filename) as inf:
output = subprocess.check_output(['cmark-gfm', '-e', 'table', '--unsafe'],
stdin=inf).decode('UTF-8')
with open(outfname, 'w') as outf:
inf.seek(0)
text = '\n'.join(inf.readlines())
tmatches = re.findall('^\\s*#\\s*(.*)\\n', text)
title = tmatches[0] #+ " — lemon's page" if len(tmatches) > 0 else "lemon's page"
outf.write(template % html.escape(title))
outf.write(output)
outf.write("</body></html>")