This repository was archived by the owner on Mar 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (76 loc) · 2.85 KB
/
Copy pathmain.py
File metadata and controls
110 lines (76 loc) · 2.85 KB
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
import mistune
import os
import json
import datetime
from jinja2 import Template
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
ARTICLE_DATA = []
TEMPLATES = {}
def load_templates():
"""
Load the templates defined in templates dir in .tpl format
"""
global TEMPLATES
for filename in os.listdir("./templates"):
with open('./templates/' + filename, 'r') as f:
TEMPLATES.update({ filename.rstrip(".tpl"): Template(f.read()) })
def render_template(template, data):
"""
Render the template
"""
return TEMPLATES[template].render(**data)
# list out contents and convert from markdown to html
class BlogContentRenderer(mistune.Renderer):
def block_code(self, code, lang):
global ARTICLE_DATA
if not lang:
return '\n<pre><code id="block_code">%s</code></pre>' % mistune.escape(code)
if lang == "blogcfg":
article_info = json.loads(code)
print(article_info)
ARTICLE_DATA.append(article_info)
return ""
lexer = get_lexer_by_name(lang, stripall=True)
formatter = HtmlFormatter(linenos=False, cssclass="source")
return highlight(code, lexer, formatter)
def render_articles():
"""
Renders the markdown content to raw html by mistune, then applied on a
jinja template and exported to out/
"""
for md_filename in os.listdir('./content'):
# skip draft files from processing
# draft files are named as <hexcode>_draft.md
if "draft" in md_filename:
continue
with open("./content/" + md_filename, 'r') as f:
# articles are named by their hex code GEEKY AF
article_name = md_filename.rstrip('.md')
print('[+] Rendering Article ', article_name)
# render the markdown to raw html
renderer = BlogContentRenderer(escape=True)
markdown = mistune.Markdown(renderer=renderer)
raw_html = markdown(f.read())
# add the hex code
# ARTICLE_DATA[-1]['hex_code'] = article_name
# apply the article jinja template to it
with open("./out/article/" + article_name + '.html', 'w') as html_f:
html_f.write(render_template('article', {'meta': ARTICLE_DATA[-1],
'content': raw_html }))
# update the main page with new contents
def render_main_page():
"""
Render the main page by updating with article list
"""
with open('./out/index.html', 'w') as f:
f.write(render_template('mainpage',
{'articles': ARTICLE_DATA}))
# upload to boot-error.github.com
def main():
load_templates()
render_articles()
render_main_page()
if __name__ == "__main__":
main()