-
Notifications
You must be signed in to change notification settings - Fork 140
/
convert.py
86 lines (75 loc) · 2.65 KB
/
convert.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
import re
from typing import Dict, List, Tuple
from utils import (
DocLink,
DocPath,
Settings,
parse_graph,
pp,
raw_dir,
site_dir,
write_settings,
)
if __name__ == "__main__":
Settings.parse_env()
Settings.sub_file(site_dir / "config.toml")
Settings.sub_file(site_dir / "content/_index.md")
Settings.sub_file(site_dir / "templates/macros/footer.html")
Settings.sub_file(site_dir / "static/js/graph.js")
nodes: Dict[str, str] = {}
edges: List[Tuple[str, str]] = []
section_count = 0
all_paths = list(sorted(raw_dir.glob("**/*")))
for path in [raw_dir, *all_paths]:
doc_path = DocPath(path)
if doc_path.is_file:
if doc_path.is_md:
# Page
nodes[doc_path.abs_url] = doc_path.page_title
content = doc_path.content
parsed_lines: List[str] = []
for line in content:
parsed_line, linked = DocLink.parse(line, doc_path)
# Fix LaTEX new lines
parsed_line = re.sub(r"\\\\\s*$", r"\\\\\\\\", parsed_line)
parsed_lines.append(parsed_line)
edges.extend([doc_path.edge(rel_path) for rel_path in linked])
content = [
"---",
f'title: "{doc_path.page_title}"',
f"date: {doc_path.modified}",
f"updated: {doc_path.modified}",
"template: docs/page.html",
"---",
# To add last line-break
"",
]
doc_path.write(["\n".join(content), *parsed_lines])
print(f"Found page: {doc_path.new_rel_path}")
else:
# Resource
doc_path.copy()
print(f"Found resource: {doc_path.new_rel_path}")
else:
"""Section"""
# Frontmatter
# TODO: sort_by depends on settings
content = [
"---",
f'title: "{doc_path.section_title}"',
"template: docs/section.html",
f"sort_by: {Settings.options['SORT_BY']}",
f"weight: {section_count}",
"extra:",
f" sidebar: {doc_path.section_sidebar}",
"---",
# To add last line-break
"",
]
section_count += 1
doc_path.write_to("_index.md", "\n".join(content))
print(f"Found section: {doc_path.new_rel_path}")
pp(nodes)
pp(edges)
parse_graph(nodes, edges)
write_settings()