-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
50 lines (40 loc) · 1.57 KB
/
build.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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
from os.path import abspath, dirname, join, splitext
from os import listdir
import codecs
from jinja2 import Template
from markdown import Markdown
ROOT_DIR = abspath(dirname(__file__))
SOURCE_DIR = join(ROOT_DIR, 'src')
class MarkdownReader(object):
"Reader for markdown documents"
file_extensions = ['md', 'markdown', 'mkd']
extensions = ['extra', 'meta', 'tables', 'toc', 'admonition']
def read(self, source_path):
"""Parse content and metadata of markdown files"""
text = codecs.open(source_path, encoding='utf').read()
md = Markdown(extensions=self.extensions)
content = md.convert(text)
return content
class HTMLWriter(object):
"HTML Writer, builds documentation"
def __init__(self, template):
self.template = template
def write(self, data):
"Write content to the destination path"
destination = 'index.html'
with codecs.open(destination, 'w', encoding='utf') as fd:
fd.write(self.template.render(data))
if __name__ == '__main__':
reader = MarkdownReader()
writer = HTMLWriter(Template(codecs.open('templates/base.html', encoding='utf').read()))
data = {}
for filename in listdir(SOURCE_DIR):
key, _ = splitext(filename)
key = key.replace('-', '_')
content = reader.read(join(SOURCE_DIR, filename))
data[key] = content
# Special: the character sheet, full HTML
data['page_1_col_2'] = codecs.open(join(SOURCE_DIR, 'page-1-col-2.html'), encoding='utf').read()
writer.write(data)