-
Notifications
You must be signed in to change notification settings - Fork 33
/
generate.py
executable file
·159 lines (130 loc) · 5.49 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
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
""" generate html files """
from glob import glob
import os
from jinja2 import Environment, FileSystemLoader
from markdown import markdown
import yaml
import i18n
env = Environment(loader=FileSystemLoader("templates/"), extensions=["jinja2.ext.i18n"])
env.install_gettext_translations(i18n)
def get_page_metadata(locale_slug, page):
"""title/order etc for a page
this is how the markdown file is composed:
> ---
> Header: value
> Another key: another value
> ---
and this is how crowdin sends it back as:
> - - -
> Header: value Another key: another value
> - - -
I don't know how to ask crowdin nicely not to do this, so instead I'm supporting
both styles, which is janky.
"""
headers = []
with open(page, "r", encoding="utf-8") as page_markdown:
header_block_open = False
for line in page_markdown.readlines():
if line.replace(" ", "").strip() == "---":
header_block_open = not header_block_open
continue
if not header_block_open:
break
for word in line.split(" "):
# start of a new header
if word[-1] == ":":
headers.append([word])
elif headers:
headers[-1].append(word)
headers = "\n".join(" ".join(line) for line in headers)
try:
header_obj = yaml.safe_load(headers) or {}
except yaml.parser.ParserError:
header_obj = {}
path_dir = page.split("/")[-1].replace(".md", ".html")
header_obj["path"] = f"/{locale_slug}{path_dir}"
return header_obj
def get_site_data(locale_slug, locale_code, page):
"""this should be a file"""
category_dirs = glob("content/*/")
categories = []
for cat_dir in category_dirs:
with open(f"{cat_dir}/_meta.yml", "r", encoding="utf-8") as meta_yaml:
parsed = yaml.safe_load(meta_yaml)
subcategories = []
location = (
f"locale/{locale_code}/{cat_dir}/*.md" if locale_slug else f"{cat_dir}/*.md"
)
for subcat in glob(location):
subcategories.append(get_page_metadata(locale_slug, subcat))
subcategories.sort(key=lambda v: v.get("Order", -1))
categories.append({**parsed, **{"subcategories": subcategories}})
categories.sort(key=lambda v: v["order"])
template_data = {"categories": categories}
template_data["headers"] = get_page_metadata(locale_slug, page)
return template_data
def format_markdown(file_path):
"""go from markdown to html, extracting headers"""
with open(file_path, "r", encoding="utf-8") as page_markdown:
first_line = page_markdown.readline()
dashed_header_format = first_line == "---\n"
with open(file_path, "r", encoding="utf-8") as markdown_content:
if dashed_header_format:
headerless = []
header_block_open = False
for line in markdown_content.readlines():
if line.replace(" ", "") == "---\n":
header_block_open = not header_block_open
elif not header_block_open:
headerless.append(line)
return markdown(
"".join(headerless),
extensions=["tables", "fenced_code", "codehilite"],
extension_configs={"codehilite": {"css_class": "highlight"}},
)
return markdown(
"".join(markdown_content.readlines()[3:]),
extensions=["tables", "fenced_code", "codehilite"],
extension_configs={"codehilite": {"css_class": "highlight"}},
)
if __name__ == "__main__":
# iterate through each locale
for locale in i18n.locales_metadata:
SLUG = locale["slug"]
paths = [
["index.html", "content/index.md"],
["page.html", "content/**/*.md"],
]
i18n.setLocale(locale["code"])
LOCALIZED_SITE_PATH = "site/"
if locale["code"] != "en_US":
paths = [
["index.html", f"locale/{locale['code']}/content/index.md"],
["page.html", f"locale/{locale['code']}/content/**/*.md"],
]
LOCALIZED_SITE_PATH = f"site/{SLUG}"
# iterate through template types
for (path, content_paths) in paths:
with open(f"templates/{path}", "r", encoding="utf-8") as template_file:
template_string = template_file.read()
template = env.from_string(template_string)
localized_dirs = f"{LOCALIZED_SITE_PATH}"
localized_dirs = localized_dirs[: localized_dirs.rfind("/")]
if not os.path.exists(localized_dirs):
os.makedirs(localized_dirs)
for content_path in glob(content_paths):
output_path = content_path.split("/")[-1].replace(".md", ".html")
print(" Generating", f"{LOCALIZED_SITE_PATH}{output_path}")
with open(
f"{LOCALIZED_SITE_PATH}{output_path}", "w+", encoding="utf-8"
) as render_file:
data = get_site_data(SLUG, locale["code"], content_path)
data["content"] = format_markdown(content_path)
data["path"] = f"/{SLUG}{output_path}"
render_file.write(
template.render(
locale=locale,
locales_metadata=i18n.locales_metadata,
**data,
)
)