forked from python-discord-es/web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
95 lines (76 loc) · 2.64 KB
/
generate.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
import re
import sys
import shutil
from pathlib import Path
from textwrap import dedent
import markdown
from jinja2 import Environment, FileSystemLoader, Template
from lib.comunidades import get_comunidades_page
from lib.podcast import get_podcast_page
def get_header_content(_content):
# Parse first lines
_header = {
"@title": "",
"@logo": "",
"@url": "",
"@description": "",
}
header_index = 0
for line in _content.splitlines():
header_index += 1
if line.startswith("@"):
try:
option, value = line.strip().split(":")
_header[option] = value.strip()
except Exception as e:
raise(f"Error with:\n{line}\n{e}")
elif line == "-----":
break
_content = md.convert(
"\n".join([line for line in _content.splitlines()[header_index:]])
)
return _header, _content
def render_pages(template, md):
for page in Path("pages/").glob("*.md"):
with open(page, encoding="utf-8") as f:
_content = f.read()
_header, _content = get_header_content(_content)
# Hack for empty details/summary paragraph
_content = _content.replace('<p><summary markdown="block"></p>', "")
# Special cases for pages
if "podcast" == page.stem:
_content = get_podcast_page(_content)
elif "otrascomunidades" == page.stem:
_content = get_comunidades_page(_content)
# Adding class for toc 'li'
md.toc = md.toc.replace("<li>", '<li class="menu-item">')
conf = {
"toc": md.toc,
"header": _header,
"content": _content,
}
page_rendered = template.render(conf)
with open(_header["@url"], "w", encoding="utf-8") as f:
f.write(page_rendered)
print(f"> Written {page}")
def render_index(template):
# Special case for the index
_header = {
"@title": "index",
"@url": "index.html",
}
conf = {
"header": _header,
}
page_rendered = template.render(conf)
with open(f"index.html", "w", encoding="utf-8") as f:
f.write(page_rendered)
print(f"> Written index")
if __name__ == "__main__":
BASE_PAGE = "template/page.html"
md = markdown.Markdown(extensions=["toc", "tables", "fenced_code", "codehilite", "md_in_html"])
loader = FileSystemLoader("template/")
template = Environment(loader=loader).from_string(open(BASE_PAGE).read())
render_pages(template, md)
template = Environment(loader=loader).from_string(open("template/index.html").read())
render_index(template)