-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.py
176 lines (118 loc) · 5.01 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os, subprocess
def ensure_dir(f):
d = os.path.dirname(f)
if len(d) == 0:
d = "./"
if not os.path.exists(d):
os.makedirs(d)
class Generator:
def __init__(self, output_path="_output/",
default_page_before_body=None,
default_page_after_body=None,
default_css_files=[]):
self.output_path = output_path
self.default_page_before_body = default_page_before_body
self.default_page_after_body = default_page_after_body
self.default_css_files = default_css_files
def copy_file(self, in_path, filename, out_path=None):
if out_path is None:
out_path = in_path
in_file = in_path + filename
out_file = self.output_path + out_path + filename
ensure_dir(out_file)
subprocess.call(['cp', in_file, out_file])
# title is actually configured in page_markdown.
def gen_page_string(self, page_markdown, path_rel_to_output,
css_files=None,
is_mathjax_on=False,
page_before_body=None,
page_after_body=None):
# TODO: figure out whether to use path joining here instead
# and change output_path => output_folder
output_file_path = self.output_path + path_rel_to_output
ensure_dir(output_file_path)
pandoc_call = (['pandoc', '--standalone',
'-f', 'markdown_phpextra+yaml_metadata_block+simple_tables',
'-t', 'html5',
'-o', output_file_path,
'--smart'])
if is_mathjax_on:
pandoc_call += ['--mathjax']
if css_files is None:
css_files = self.default_css_files
for file_name in css_files:
pandoc_call += ['--css', file_name]
if page_before_body is None:
page_before_body = self.default_page_before_body
if page_before_body is not None:
pandoc_call += ['--include-before-body', page_before_body]
if page_after_body is None:
page_after_body = self.default_page_after_body
if page_after_body is not None:
pandoc_call += ['--include-after-body', page_after_body]
p = subprocess.Popen(pandoc_call, stdin=subprocess.PIPE)
p.communicate(input=bytes(page_markdown, 'utf-8'))
# input is a file name instead of markdown string
def gen_page_file(self, input_file, path_rel_to_output,
css_files=None,
is_mathjax_on=False,
page_before_body=None,
page_after_body=None):
# TODO: figure out whether to use path joining here instead
# and change output_path => output_folder
output_file_path = self.output_path + path_rel_to_output
ensure_dir(output_file_path)
pandoc_call = (['pandoc', input_file,
'--standalone',
'-t', 'html5',
'-o', output_file_path,
'--smart'])
if is_mathjax_on:
pandoc_call += ['--mathjax']
if css_files is None:
css_files = self.default_css_files
for file_name in css_files:
pandoc_call += ['--css', file_name]
if page_before_body is None:
page_before_body = self.default_page_before_body
if page_before_body is not None:
pandoc_call += ['--include-before-body', page_before_body]
if page_after_body is None:
page_after_body = self.default_page_after_body
if page_after_body is not None:
pandoc_call += ['--include-after-body', page_after_body]
p = subprocess.call(pandoc_call)
# first concern is the exact organization of
# notes. if each note is a unit of review, it seems like they
# should be fairly small.
def gen_notes(gen):
list_md = ""
folder_name = 'notes/'
for i, fname in enumerate( os.listdir(folder_name) ):
if fname.endswith('.md'):
fname_html = fname.replace('.md', '.html')
list_md += " - [{}]({})\n".format(
fname.replace('.md', ''),
fname_html
)
fpath = folder_name + fname
out_fpath = '/' + fname_html
gen.gen_page_file(fpath, out_fpath,
is_mathjax_on=True)
else:
# assume it's something that needs to be copied to output
# folder (like an image)
gen.copy_file(folder_name, fname, '/')
notes_md = """---
title: notes
---
<div id="titlenav"><span id="title">notes</span></div>
{}
""".format(list_md)
# generate notes index
gen.gen_page_string(notes_md, 'index.html')
gen = Generator(default_css_files=['/css/style.css'],
default_page_before_body='includes/before_body.html',
default_page_after_body='includes/after_body.html')
gen_notes(gen)
gen.copy_file('css/', 'style.css')