-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.py
185 lines (106 loc) · 4.25 KB
/
render.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
import pypandoc
import os
import re
from pathlib import Path
from config import RENDERER_CONFIG
from yaml import safe_load
from bs4 import BeautifulSoup
from notes import Note
input_directory = os.fspath(RENDERER_CONFIG["input_directory"])
output_directory = os.fspath(RENDERER_CONFIG["output_directory"])
image_directory = os.fspath(RENDERER_CONFIG["image_directory"])
def get_metadata(file_path: str) -> dict:
with open(file_path) as markdown_file:
metadata = ""
meta_delimiter_count = 0
for line in markdown_file:
if "---" in line:
meta_delimiter_count = meta_delimiter_count + 1
if meta_delimiter_count > 1:
parsed_metadata = safe_load(metadata)
return parsed_metadata
elif meta_delimiter_count > 0:
metadata += line
def get_summary_from_html(output_file):
paragraph = re.compile(r"(?:<p>)(.*)(?:</p>)")
decorators = re.compile(r"<.*?>")
sup = re.compile(r"<sup?>.*?</sup?>")
with open(output_file) as html_file:
for line in html_file:
try:
result = paragraph.search(line)
result = sup.sub("", result[1])
return(decorators.sub("", result))
except TypeError:
continue
return ""
def get_tags(notes):
tag_set = set()
for note in notes:
for tag in note.tags:
tag_set.add(tag)
return sorted(tag_set)
def add_toc(md_file, html_file):
rendered_soup = BeautifulSoup(open(html_file), "html.parser")
toc_tags = [toc_tag for toc_tag in rendered_soup.find_all("p", string="[TOC]")]
if len(toc_tags) > 0:
standalone_html = pypandoc.convert_file(
md_file,
"html5",
extra_args=["-s", "--toc"],
)
standalone_soup = BeautifulSoup(standalone_html, "html.parser")
toc = standalone_soup.find_all("nav", id="TOC")[0]
for tag in toc_tags:
tag.replaceWith(toc)
with open(html_file, "w") as html:
html.write(str(rendered_soup))
def update_img_tags(html_file):
soup = BeautifulSoup(open(html_file), "html.parser")
img_tags = soup.find_all("img")
for tag in img_tags:
if "/" not in tag["src"]:
tag["src"] = f"/static/images/post/{tag['src']}"
with open(html_file, "w") as html:
html.write(str(soup))
def render_all():
notes = set()
static_pages = set()
Path(output_directory).mkdir(parents=True, exist_ok=True)
Path(image_directory).mkdir(parents=True, exist_ok=True)
for file in os.listdir(input_directory):
filename = os.fsdecode(file)
clean_filename = os.path.splitext(filename)[0]
file_path = f"{input_directory}{filename}"
if filename.endswith(RENDERER_CONFIG["input_file_extensions"]):
output_file = f"{output_directory}{clean_filename}.html"
pypandoc.convert_file(
file_path,
"html5",
outputfile=output_file,
extra_args=(RENDERER_CONFIG["pandoc_extra_args"])
)
update_img_tags(output_file)
add_toc(file_path, output_file)
metadata = get_metadata(file_path)
if "summary" not in metadata:
metadata["summary"] = get_summary_from_html(output_file)
if "static_url" in metadata:
static_pages.add(
Note(output_file, clean_filename, **metadata)
)
else:
notes.add(Note(output_file, clean_filename, **metadata))
elif filename.endswith(RENDERER_CONFIG["image_file_extensions"]):
try:
os.symlink(file_path, f"{image_directory}/{filename}")
# link already exists, so we can skip
except FileExistsError:
pass
tag_set = get_tags(notes)
published_notes = (note for note in notes if note.publication_date is not None)
published_notes = sorted(published_notes, reverse=True,
key=lambda n: n.publication_date)
return published_notes, tag_set, static_pages
if __name__ == "__main__":
render_all()