-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.py
executable file
·83 lines (63 loc) · 2.38 KB
/
gen.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#! /usr/bin/python
import os, sys, traceback
from jinja2 import Template
import markdown
from markdown.inlinepatterns import Pattern
from markdown.util import etree, AtomicString
import fencedcode
DIR=os.path.dirname(os.path.abspath(__file__))
#---------------------------
#---------------------------
def create_markdown():
md = markdown.Markdown(extensions = [
'markdown.extensions.extra',
'markdown.extensions.meta',
'markdown.extensions.admonition',
'markdown.extensions.codehilite',
'markdown.extensions.toc',
fencedcode.makeExtension()],
extension_configs= {
'markdown.extensions.codehilite' : {
'linenums' : True
}
})
return md
#------------------------
class Page(object):
pass
def get_markdown_page(page_path):
page = Page()
with open(page_path, 'r') as f:
md = create_markdown()
page.html = md.convert(f.read().decode('utf-8'))
page.meta = md.Meta # flask-pages naming convention
for key, value in page.meta.iteritems():
page.meta[key] = ''.join(value) # meta is a list
return page
def gen():
with open('template.html') as f:
template = Template(f.read().decode('utf-8'))
with open('redirect.html') as f:
t_redirect = Template(f.read().decode('utf-8'))
for root, dirs, files in os.walk(DIR):
for f in files:
if f.endswith('.md'):
mdpath = os.path.join(root, f)
page = get_markdown_page(mdpath)
with open(mdpath[:-2] + 'html', 'w') as htmlpath:
htmlpath.write(template.render(page = page).encode('utf-8'))
if f != 'index.md':
# compatible with moon
folder = mdpath[:-3]
if os.path.exists(folder) and os.path.isdir(folder):
pass
else:
os.makedirs(folder)
target = '/' + os.path.relpath(root, DIR).replace('\\', '/') + '/' + f[:-2] + 'html'
with open(os.path.join(folder, 'index.html'), 'w') as rd:
rd.write(t_redirect.render(target = target).encode('utf-8'))
if __name__ == '__main__':
try:
gen()
except:
traceback.print_exc()