-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
367 lines (283 loc) · 11.5 KB
/
main.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""
Scans Manticore Search manual files and compile into a single big file.
"""
import re
from collections.abc import Iterator
from functools import cache
from pathlib import Path
from shutil import copy, rmtree
import markdown
import msgspec
from jinja2 import Template
from rich import print
MANTICORE_VERSION = "6.2.12 (Aug 23, 2023)"
SRC_ROOT: Path = Path("sources/")
DST_ROOT = Path("docs/")
ASSETS_ROOT = Path("assets/")
# Lines looks like this:
# "* [1️⃣ Installation](Installation/Installation.md)
LINE_PATTERN = r"^(\s*)\* \[(.+)\](.+)?$"
# Double anchor pattern
DOUBLE_ANCHOR_PATTERN = r"\([^\)#]+#([^\)#]+)#([^\)#]+)\)"
MD_BULET = r"^(\s*)\*"
# Store destination paths for each source path, used to fix links.
DESTINATION_FOR_PATH: dict[str, str] = {}
class Entry(msgspec.Struct, omit_defaults=True):
title: str
path: str | None = None
anchor: str | None = None
children: list["Entry"] = []
toc_path: str = ""
breadcrumb: str = ""
nest_level: int = 0
def cssid(self) -> str:
return f"toc-{self.breadcrumb}-{self.title.replace(' ', '_').lower()}"
def clean_breadcrumb(self) -> str:
parts = self.breadcrumb.split(".")
cleaned_parts = [part.lstrip("0") for part in parts]
return ".".join(cleaned_parts)
def filename(self) -> str:
"""Name of a file for this entry, to be placed in a chapter path."""
return f"{self.breadcrumb}-{self.title.replace(" ", "_").lower()}.html"
def print(self, indent: str = ""):
print(f"{indent}{self.breadcrumb or " "} {self.title} [{self.path}]")
for entry in self.children:
entry.print(indent + " ")
def dfs(self) -> Iterator["Entry"]:
yield self
for child in self.children:
yield from child.dfs()
def collect_paths(self) -> set[Path]:
paths: set[Path] = set()
for entry in root.dfs():
if entry.path:
paths.add(SRC_ROOT / entry.path)
return paths
def source_path(self) -> Path | None:
if not self.path:
return None
return SRC_ROOT / self.path
def source_exists(self) -> bool:
p = self.source_path()
return (p or False) and p.exists() and p.is_file()
def __hash__(self):
"""Since we are only using this to cache to_html, we can use the path as a hash."""
return hash(self.path)
@cache
def html_header_and_content(self):
"""Returns a tuple with the header and the content of the HTML file."""
html = self.to_html()
parts = html.split("</h1>", maxsplit=1)
if len(parts) == 1:
return "", html
else:
return (parts[0] + "</h1>"), parts[1]
@cache
def to_html(self) -> str:
if not self.source_exists():
return ""
p: Path = self.source_path() # type: ignore
buffer: list[str] = list()
def open_div(klass: str):
buffer.append(f"\n<div class='{klass}'>\n")
def close_div(klass: str):
buffer.append("\n</div>\n")
# We'll read file line by line to do a lil cleanup.
with p.open("r", encoding="utf-8") as file:
source = file.read()
# Fix links. We'll need to fix double anchors when scannign line by line.
source = source.replace("../../", "")
for source_path, dest_path in DESTINATION_FOR_PATH.items():
source = source.replace(source_path, dest_path)
# PyMarkdown removes the () from the links, so we need to fix them too.
source = source.replace("%28%29", "")
last_line = ""
in_code_block = False
insert_code_block = False
for line in source.splitlines(keepends=True):
# PyMarkdown doesn't like lists that are not separated by empty lines.
md_match = re.match(MD_BULET, line)
if md_match:
# PyMarkdown doesn't like nested items that are not nested in multiples of 4.
spaces = " " * 4 * (max(len(md_match[1]) - 2, 0) // 2)
line = f"{spaces}{line.lstrip()}"
if not re.match(MD_BULET, last_line):
buffer.append("\n")
# Previously when we replaced links we may have produced double anchors, so fix them.
matches = re.findall(DOUBLE_ANCHOR_PATTERN, line, re.UNICODE)
if matches:
for match in matches:
line = line.replace(
f"#{match[0]}#{match[1]}", f"#{match[1].lower()}"
)
if not in_code_block:
buffer.append(line)
elif not line.startswith("#####"):
buffer.append(line)
last_line = line
# Let's add some markup for the code blocks.
if line.startswith("<!-- example"):
in_code_block = True
insert_code_block = True
elif line.startswith("<!-- request") or line.startswith(
"<!-- response"
):
if in_code_block:
match = re.findall(r"<!-- (request|response)(.+)-->", line)[0]
code_type = match[0]
code_id = match[1].strip()
if insert_code_block:
open_div("xcodeblock")
open_div("xcodeblock-item")
insert_code_block = False
elif code_type == "request":
close_div("xcodeblock-item")
open_div("xcodeblock-item")
if code_type == "request":
open_div("item-select")
if code_type == "request":
buffer.append(code_id if code_id else "Req")
else:
buffer.append(f"Resp: {code_id}" if code_id else "Resp")
close_div("item-select")
elif line.startswith("<!-- intro"):
pass
elif line.startswith("<!-- end"):
if in_code_block:
close_div("xcodeblock-item")
close_div("xcodeblock")
in_code_block = False
# if self.path:
# with open(f"fix-{self.path.replace('/', '_')}", "w") as f:
# f.write("".join(buffer))
return markdown.markdown(
"".join(buffer),
extensions=[
"extra",
"toc",
"fenced_code",
"codehilite",
"tables",
"nl2br",
],
)
def parse_entries() -> Entry:
root = Entry(title="Manticore Search Manual")
stack = [root]
with open(SRC_ROOT / "README.md", "r") as file:
lines = file.readlines()
for line in lines:
matches = re.findall(LINE_PATTERN, line, re.UNICODE)
indent, title, href = matches[0]
href = href.strip()[1:-1]
indent = 1 + len(indent) // 4 # Indents will go from 1 to 4.
title = re.sub(r"[^A-Za-z0-9 _\-]+", "", title).strip()
title = re.sub(r"^\d+\s*", r"", title).strip()
entry = Entry(title=title)
if "#" in href:
path, anchor = href.split("#") # type: ignore
entry.path = path
entry.anchor = anchor
else:
entry.path = href
if not entry.path:
entry.path = None
parent: Entry
if indent == len(stack):
parent = stack[-1]
parent.children.append(entry)
elif indent > len(stack):
parent = stack[-1].children[-1]
parent.children.append(entry)
stack.append(parent)
else:
while len(stack) > indent:
stack.pop()
parent = stack[-1]
parent.children.append(entry)
entry.nest_level = indent
if parent.breadcrumb:
entry.breadcrumb = f"{parent.breadcrumb}.{len(parent.children):02}"
else:
entry.breadcrumb = f"{len(parent.children):02}"
return root
def sanity_checks(root: Entry):
errors: list[str] = []
# Sanity check: Paths from ToC should exist.
toc_paths = root.collect_paths()
for path in toc_paths:
if not path.exists():
errors.append(f"{path} is referenced in ToC but does NOT exist!")
existing_sources: set[Path] = set()
for path in SRC_ROOT.rglob("*.md"):
existing_sources.add(Path(path))
# Sanity check: no missing documents.
missing_documents = toc_paths - existing_sources
if len(missing_documents) > 0:
errors.append(f"ToC is missing documents!: ${missing_documents}")
# Sanity check: we are only skipping README.md
skipped = existing_sources - toc_paths
skipped.remove(SRC_ROOT / "README.md")
for skip in skipped:
errors.append(f"ToC skips existing file!: {skip}")
if errors:
print("errors found!")
for error in errors:
print(error)
exit(1)
def write_output(root: Entry):
"""Generate the output file."""
if DST_ROOT.exists():
rmtree(DST_ROOT)
DST_ROOT.mkdir(parents=True, exist_ok=True)
# Copy assets.
copy(ASSETS_ROOT / "common.css", DST_ROOT)
copy(ASSETS_ROOT / "toc.css", DST_ROOT)
copy(ASSETS_ROOT / "chapter.css", DST_ROOT)
copy(ASSETS_ROOT / "pygments.css", DST_ROOT)
img_ext: set[str] = set([".png", ".jpg", ".jpeg", ".gif", ".svg"])
for path in SRC_ROOT.rglob("*"):
if path.suffix in img_ext:
copy(path, DST_ROOT)
# Prepare the ToC.
toc_entries: list[Entry] = []
first_path: Path | None = None
print("* Scanning ToC...")
for top_entry in root.children:
chapter_path = DST_ROOT / top_entry.filename()
rel_path = chapter_path.relative_to(DST_ROOT)
if not first_path:
first_path = rel_path
for chapter_item in top_entry.dfs():
chapter_item.toc_path = f"{rel_path}#{chapter_item.cssid()}"
toc_entries.append(chapter_item)
source_path = chapter_item.source_path()
if source_path:
DESTINATION_FOR_PATH[source_path.relative_to(SRC_ROOT).as_posix()] = (
chapter_item.toc_path
)
toc = DST_ROOT / "index.html"
print(f"* Writing {toc}...")
with toc.open("w") as dst:
template = Template(open(ASSETS_ROOT / "toc.html.j2").read())
result = template.render(
entries=toc_entries,
version=MANTICORE_VERSION,
first_path=first_path,
)
dst.write(result)
chapter_template = Template(open(ASSETS_ROOT / "chapter.html.j2", "r").read())
for top_entry in root.children:
chap = DST_ROOT / top_entry.filename()
print(f"* Writing {chap}...")
with chap.open("w") as dst:
result = chapter_template.render(
entries=[chapter for chapter in top_entry.dfs()],
version=MANTICORE_VERSION,
chapter_title=top_entry.title,
)
dst.write(result)
root = parse_entries()
sanity_checks(root)
write_output(root)
print("\nDone!\n")