-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssg.py
329 lines (273 loc) · 9.13 KB
/
ssg.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/bin/python
# hack to get unicode working with jinja2
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import glob
import commands as c
from jinja2 import Template, Environment, FileSystemLoader
import os
from tag_ontology import tag_synonyms, tag_implications
from metadata import *
site_dir = "_site/"
tags_dir = "tags/" # relative to site_dir
class Route(object):
'''
Each Route object is a function Filepath -> Filepath
'''
def __init__(self, route):
self.route = route
class Compiler(object):
'''
Each Compiler object is a function (Item, Rules) -> Item
'''
def __init__(self, compiler):
self.compiler = compiler
def to_dir(dirname):
'''
Site_dir(str) -> Filepath -> Filepath
'''
@Route
def f(filepath):
return Filepath(dirname + filepath.filename())
return f
site_dir_route = to_dir(site_dir)
@Route
def my_route(filepath):
return filepath.route_with(set_extension("")).route_with(drop_one_parent_dir_route).route_with(site_dir_route)
@Route
def standard_tags_route(filepath):
'''
Filepath -> Filepath
Here filepath is just Filepath(tag.name) for a tag.
'''
return filepath.route_with(to_dir(site_dir + tags_dir))
@Compiler
def copy_file_compiler(item, rules):
'''
(Item, Route) -> Item
Basically, ignore the rules and return the item.
'''
return item
class AbsolutePathException(Exception):
pass
class Configuration(object):
pass
def split_path(path):
# See http://stackoverflow.com/a/15050936/3422337
a, b = os.path.split(path)
return (split_path(a) if len(a) and len(b) else []) + [b]
@Route
def drop_one_parent_dir_route(filepath):
return Filepath('/'.join([i for i in split_path(filepath.path)[1:]]))
class Filepath(object):
'''
Specify a single item.
'''
def __init__(self, path):
if path.startswith('/'):
raise AbsolutePathException("path is absolute; must be relative")
self.path = path
def __str__(self):
return self.path
def filename(self):
return os.path.split(self.path)[1]
def directory(self):
return os.path.split(self.path)[0] + "/"
def path_lst(self):
return split_path(self.path)
def route_with(self, route):
return route.route(self)
def go_to_other(self, other):
path1 = os.path.normpath(self.path)
path2 = os.path.normpath(other.path)
depth = len(Filepath(path1).path_lst()) - 1
return Filepath("../" * depth + path2)
def relative_to(self, other):
return other.go_to_other(self)
def to_item(self):
with open(self.path, 'r') as f:
return Item(self, f.read())
class Item(object):
'''
'''
def __init__(self, filepath, body):
self.filepath = filepath
self.body = body
def set_body(self, new_body):
self.body = new_body
def compile_with(self, compiler, rules):
'''
(Item, Compiler, Rules) -> Item
'''
return compiler.compiler(self, rules)
class Rules(object):
'''
Each Rules object contains rules for compiling an Item object.
'''
def __init__(self, route=site_dir_route, compiler=copy_file_compiler, tags_route=standard_tags_route):
self.route = route
self.compiler = compiler
self.tags_route = tags_route
# so do like Item(Filepath('pages/hello.md'), "hello world!")
@Compiler
def self_reference_compiler(item, rules):
'''
(Item, Rules) -> Item
This is a silly example to show why compilers need route;
in short, since Item only contains where it currently is and
the body, it won't know where it *will* be. But sometimes we
want to know where it *will* be, and reference it in the output.
Use like so:
item = ...
route = ...
compiler = Compiler(self_reference_compiler)
rules = Rules(route, compiler)
item.compile_with(compiler, rules)
'''
body = "I will be stored in " + item.filepath.route_with(rules.route).path
return Item(item.filepath, body)
@Compiler
def markdown_to_html_compiler(item, rules):
'''
(Item, Rules) -> Item
'''
filename = item.filepath.filename
command = "pandoc -f markdown -t json -s {path}".format(path=item.filepath.path)
json_lst = json.loads(c.run_command(command))
# This will return a dict {'json': ..., 'tags': ...}
file_dict = organize_tags(json_lst, tag_synonyms, tag_implications)
json_lst = file_dict['json']
tags = [Tag(t) for t in file_dict['tags']]
command = "pandoc -f json -t html --mathjax --base-header-level=2"
html_output = c.run_command(command, pipe_in=json.dumps(json_lst, separators=(',',':'))).encode('utf-8')
env = Environment(loader=FileSystemLoader('.'))
skeleton = env.get_template('templates/skeleton.html')
# Get metadata ready
ctx = Context(
title = get_metadata_field(json_lst, "title"),
math = get_metadata_field(json_lst, "math"),
license = get_metadata_field(json_lst, "license"),
)
final_filepath = item.filepath.route_with(rules.route)
tags_lst = []
for tag in tags:
tag_filepath = Filepath(tag.name).route_with(rules.tags_route)
rel_path = tag_filepath.relative_to(final_filepath).path
tags_lst.append({'name': tag.name, 'path': rel_path})
new_body = skeleton.render(body=html_output, title=ctx.title, tags=tags_lst, license=ctx.license, math=ctx.math).encode('utf-8')
# We keep the original filepath
return Item(item.filepath, new_body)
def match(pattern, rules):
'''
(Pattern(str), Rules) -> IO
'''
paths_lst = glob.glob(pattern)
for path in paths_lst:
output = Filepath(path).to_item().compile_with(rules.compiler, rules).body
write_to = Filepath(path).route_with(rules.route).path
with open(write_to, 'w') as f:
f.write(output)
def create(path, base_item, rules):
'''
(Path(str), Item, Rules) -> IO
'''
output = base_item.compile_with(rules.compiler, rules).body
write_to = Filepath(path).path # Filepath makes sure the path is relative
with open(write_to, 'w') as f:
f.write(output)
@Compiler
def all_tags_page_compiler(item, rules):
global tags_dir
tags_lst_of_dicts = []
for tag in tags_lst:
tag_dict = {'title': tag, 'url': (tagsdir + tag)}
tags_lst_of_dicts.append(tag_dict)
env = Environment(loader=FileSystemLoader('.'))
page_list = env.get_template('templates/page-list.html')
pagesdir_depth = len(split_path(pagesdir[:-1]))
print pagesdir_depth*"../"
output = page_list.render(pages=tags_lst_of_dicts)
for i in tags_lst_of_dicts:
print pagesdir_depth*"../" + i['url']
skeleton = env.get_template('templates/skeleton.html')
final = skeleton.render(body=output, title="List of all tags", license='CC0').encode('utf-8')
return final
@Compiler
def tag_page_compiler(item, rules):
pass
def set_extension(extension):
'''
Extension(str) -> Filepath -> Filepath
'''
@Route
def f(filepath):
'''
Filepath -> Filepath
'''
return Filepath(os.path.splitext(filepath.path)[0] + extension)
return f
@Route
def id_route(filepath):
'''
Filepath -> Filepath
'''
return filepath
class Context(object):
'''
So you can do things like x = Context(title="hello, world!", math="true") then access with x.title, x.math and so on.
FIXME: add some default fields.
'''
def __init__(self, title="", math="True", tags=[], license="CC-BY", authors=[], **kwargs):
self.title = title
if type(math) is bool:
if math:
self.math = "True"
else:
self.math = "False"
else:
self.math = math
self.tags = tags
self.license = license
self.authors = authors
for key in kwargs:
self.__setattr__(key, kwargs[key])
class Tag(object):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
def get_pages_using(self, items):
'''
(Tag, [Item]) -> [Item]
'''
final = []
for item in items:
json_lst = json.loads(c.run_command("pandoc -f markdown -t json", pipe_in=item.body))
if self.name in get_metadata_field(json_lst, "tags"):
final.append(item)
return final
def get_items_from_pattern(pattern):
'''
Pattern(str) -> [Item]
'''
paths = glob.glob(pattern)
final = []
for path in paths:
with open(path, 'r') as f:
body = f.read()
final.append(Item(Filepath(path), body))
return final
if __name__ == "__main__":
# The end-user should be able to use this program like so:
fi = Filepath("pages/hello.md").to_item()
ro = my_route
co = markdown_to_html_compiler
ru = Rules(ro, co)
#print fi.compile_with(co, ru).filepath.path
#print fi.filepath.route_with(ro).path
#print fi.compile_with(co, ru).body
#match("pages/*.md", ru)
x = Tag("chemistry").get_pages_using(get_items_from_pattern("pages/*.md"))
for i in x:
print i.filepath.path