-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathgenerate_api_nav.py
More file actions
64 lines (47 loc) · 1.75 KB
/
generate_api_nav.py
File metadata and controls
64 lines (47 loc) · 1.75 KB
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
"""
Script called by mkdocs-gen-files that generates markdown documents
with API reference placeholders.
https://oprypin.github.io/mkdocs-gen-files/api.html
"""
from pathlib import Path
import mkdocs_gen_files
nav = mkdocs_gen_files.Nav()
root = Path(__file__).parent.parent.parent
src = root / "libzim"
api_reference = Path("api_reference")
# Because we are inspecting a compiled module and all the classes can be seen
# from the `libzim` namespace, their documentation is shown in the home page.
# We hide them from the home page as their documentation is also shown in the
# respective modules where they are defined.
LIBZIM_ROOT_OPTIONS = """
options:
members: false
"""
for path in sorted(src.rglob("*.pyi")):
module_path = path.relative_to(root).with_suffix("")
# Package docs get the parent module name.
if module_path.name == "__init__":
module_path = module_path.parent
module_options = LIBZIM_ROOT_OPTIONS
else:
module_options = ""
if module_path.name.startswith("_"):
# Skip other hidden items
continue
identifier = ".".join(module_path.parts)
doc_path = identifier + ".md"
full_doc_path = api_reference / doc_path
nav[identifier] = doc_path
# Create a document with mkdocstrings placeholders.
with mkdocs_gen_files.open(full_doc_path, "w") as fd:
fd.write(f"""---
title: {identifier}
---
::: {identifier}
{module_options}
""")
# Make the edit button on the page link to the source file.
mkdocs_gen_files.set_edit_path(full_doc_path, Path("..") / path.relative_to(root))
# Write a navigation file that will be interpreted by literate-nav.
with mkdocs_gen_files.open(api_reference / "NAVIGATION.md", "w") as fd:
fd.writelines(nav.build_literate_nav())