-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyllabus_processor.py
132 lines (111 loc) · 4.54 KB
/
syllabus_processor.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import jinja2
from jinja2 import Template
from latex_renderer import LatexRenderer
import mistune
import os
from os.path import join
import subprocess
class Item:
def __init__(self, name, level, section, parent_section):
self.name = name
self.level = level
self.items = []
self.section_number = section
self.section_string = parent_section + str(section) + "."
self.indent = max(level - 3, 0)
self.children = False
def __iter__(self):
return iter(self.items)
def add(self, item):
self.items.append(item)
self.children = True
return item
def __getitem__(self, item):
return self.items[item]
def section(self):
# Trim leading chars (since everything is under root&title header) and trailing "."
return self.section_string[4:-1]
class TreeRenderer(mistune.Renderer):
def reset_tree(self):
self.tree = Item('root', 0, 0, "")
self.node_stack = [self.tree]
self.level = 0
def add_node(self, text, level, section):
parent = self.node_stack[-1]
new_node = Item(text, level, section, parent.section_string)
parent.add(new_node)
self.node_stack.append(new_node)
self.level = level
def header(self, text, level, raw=None):
if not text in ["Training", "Evaluation"]:
if level == self.level + 1:
# one level deeper: add new node below current one
self.add_node(text, level, 1)
elif level == self.level:
section = self.node_stack.pop().section_number + 1
self.add_node(text, level, section)
elif level <= self.level:
# move up to appropriate parent and add new node there
section = 0
while level <= self.level:
section = self.node_stack.pop().section_number + 1
self.level = self.node_stack[-1].level
self.add_node(text, level, section)
else:
# throw error that new header skips a level
print('error error error')
return super(TreeRenderer, self).header(text, level, raw)
class Renderer(TreeRenderer, LatexRenderer):
pass
def walk(item, level):
print("\t" * level + item.name)
for i in item:
walk(i, level + 1)
class SyllabusResult:
def __init__(self, success, error='', card='', doc='', version='', commit_date=''):
self.success = success
self.error = error
self.card = card
self.doc = doc
self.version = version
self.commit_date = commit_date
class SyllabusProcessor:
def __init__(self, path):
self.path = path
def generate(self):
try:
with open(join(self.path, 'syllabus.md'), encoding='utf-8') as f:
s = f.read()
except IOError as e:
return SyllabusResult(success=False, error="I/O error({0}): {1}".format(e.errno, e.strerror))
tree = Renderer()
md = mistune.Markdown(renderer=tree)
tree.reset_tree()
md.parse(s)
latex_jinja_env = jinja2.Environment(
block_start_string = '\BLOCK{',
block_end_string = '}',
variable_start_string = '\VAR{',
variable_end_string = '}',
comment_start_string = '\#{',
comment_end_string = '}',
line_statement_prefix = '%%',
line_comment_prefix = '%#',
trim_blocks = True,
autoescape = False,
loader = jinja2.FileSystemLoader(os.path.abspath('.'))
)
training_card_template = latex_jinja_env.get_template('training-card.tmpl')
version = self.get_git_version()
commit_date = self.get_git_date()
card = training_card_template.render(items = tree.tree[0], version = version, sessions=8)
doc_template = latex_jinja_env.get_template('training-doc.tmpl')
doc = doc_template.render(content = md(s), title = tree.title, version=version)
return SyllabusResult(success=True, doc=doc, card=card, version=version, commit_date=commit_date)
def get_git_date(self):
return subprocess.check_output(['git', 'log', '-n1', '--pretty=%ci', os.path.abspath(self.path)], cwd = self.path).strip().decode('ascii')
def get_git_version(self):
return subprocess.check_output(['git', 'log', '-n1', '--pretty=%h', os.path.abspath(self.path)], cwd = self.path).strip().decode('ascii')
if __name__ == "__main__":
sp = SyllabusProcessor('.')
sp.generate()