Debian Codegraph @

scripts/gen_ref_nav.py

raw
1 """Generate the code reference pages and navigation."""
2
3 import textwrap
4 import yaml
5 from pathlib import Path
6
7 import mkdocs_gen_files
8
9 nav = mkdocs_gen_files.Nav()
10
11 per_module_options = {
12 "markdown": {"summary": {"attributes": True, "functions": True, "classes": True}}
13 }
14
15 base_path = Path(__file__).resolve().parent.parent
16
17 modules = [
18 base_path.joinpath("markdown", "__init__.py"),
19 base_path.joinpath("markdown", "preprocessors.py"),
20 base_path.joinpath("markdown", "blockparser.py"),
21 base_path.joinpath("markdown", "blockprocessors.py"),
22 base_path.joinpath("markdown", "treeprocessors.py"),
23 base_path.joinpath("markdown", "inlinepatterns.py"),
24 base_path.joinpath("markdown", "postprocessors.py"),
25 base_path.joinpath("markdown", "serializers.py"),
26 base_path.joinpath("markdown", "util.py"),
27 base_path.joinpath("markdown", "htmlparser.py"),
28 base_path.joinpath("markdown", "test_tools.py"),
29 *sorted(base_path.joinpath("markdown", "extensions").rglob("*.py")),
30 ]
31
32 for src_path in modules:
33 path = src_path.relative_to(base_path)
34 module_path = path.with_suffix("")
35 doc_path = path.with_suffix(".md")
36 full_doc_path = Path("reference", doc_path)
37
38 parts = tuple(module_path.parts)
39
40 if parts[-1] == "__init__":
41 parts = parts[:-1]
42 doc_path = doc_path.with_name("index.md")
43 full_doc_path = full_doc_path.with_name("index.md")
44 elif parts[-1].startswith("_"):
45 continue
46
47 nav_parts = [f"<code>{part}</code>" for part in parts]
48 nav[nav_parts] = doc_path.as_posix()
49
50 with mkdocs_gen_files.open(full_doc_path, "w") as fd:
51 ident = ".".join(parts)
52 fd.write(f"::: {ident}")
53 if ident in per_module_options:
54 yaml_options = yaml.dump({"options": per_module_options[ident]})
55 fd.write(f"\n{textwrap.indent(yaml_options, prefix=' ')}")
56 elif ident.startswith("markdown.extensions."):
57 yaml_options = yaml.dump({"options": {"inherited_members": False}})
58 fd.write(f"\n{textwrap.indent(yaml_options, prefix=' ')}")
59
60 mkdocs_gen_files.set_edit_path(full_doc_path, ".." / path)
61
62 with mkdocs_gen_files.open("reference/SUMMARY.md", "w") as nav_file:
63 nav_file.writelines(nav.build_literate_nav())