-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.py
43 lines (32 loc) · 1.11 KB
/
convert.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
39
40
41
42
43
#!/usr/bin/env python
import argparse
import sys
import jinja2
import markdown
from os import listdir, makedirs
from os.path import isfile, join, exists
reload(sys)
sys.setdefaultencoding('utf-8')
def main(args=None):
src_path = 'src/pages'
dist_path = 'dist'
with open('src/layouts/template.html', 'r') as f:
template = f.read()
if not exists(dist_path):
makedirs(dist_path)
onlyfiles = [f for f in listdir(src_path) if isfile(join(src_path, f))]
for file in onlyfiles:
name, ext = file.split('.')
if ext == 'md':
infile = join(src_path, file)
outfile = '{}/{}.{}'.format(dist_path, name, 'html')
title = "Markdown to HTML from {}".format(name)
with open(infile, 'r') as f:
md = f.read()
html = markdown.markdown(md, output_format='html5')
doc = jinja2.Template(template).render(body=html, subject=title)
out = open(outfile, 'w')
out.write(doc)
out.close()
if __name__ == '__main__':
sys.exit(main())