Passed
Pull Request — master (#12)
by Konstantinos
01:11
created

gen_api_refs_pages   A

Complexity

Total Complexity 0

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
eloc 23
dl 0
loc 51
rs 10
c 0
b 0
f 0
1
"""Generate the code reference pages."""
2
3
from pathlib import Path
4
5
import mkdocs_gen_files
6
7
nav = mkdocs_gen_files.Nav()
8
9
src = Path(__file__).parent.parent / "src"
10
11
for path in sorted(src.rglob("*.py")):
12
    module_path = path.relative_to(src).with_suffix("")
13
    doc_path = path.relative_to(src).with_suffix(".md")
14
    full_doc_path = Path("reference", doc_path)
15
16
    parts = list(module_path.parts)
17
18
    if parts[-1] == "__init__":
19
        parts = parts[:-1]
20
        ## https://mkdocstrings.github.io/recipes/#bind-pages-to-sections-themselves
21
        doc_path = doc_path.with_name("index.md")
22
        full_doc_path = full_doc_path.with_name("index.md")
23
        ##
24
    elif parts[-1] == "__main__":
25
        continue
26
27
    # Progressively build the navigation object
28
    nav[parts] = doc_path.as_posix()
29
30
    with mkdocs_gen_files.open(full_doc_path, "w") as fd:
31
        identifier = ".".join(parts)
32
        print("::: " + identifier, file=fd)
33
34
    # ROOT
35
    #  -> docs
36
    #     -> scripts
37
    # -> src/python_package
38
    mkdocs_gen_files.set_edit_path(full_doc_path, Path("../") / path)
39
    # so that it correctly sets the edit path of (for example) nst_math.py to
40
    # <repo_url>/blob/master/src/artificial_artwork/nst_math.py instead of
41
    # <repo_url>/blob/master/docs/src/artificial_artwork/nst_math.py
42
43
    # mkdocs_gen_files.set_edit_path(full_doc_path, path)
44
45
46
# At the end, create a magic, literate navigation file called SUMMARY.md in the
47
# reference folder.
48
with mkdocs_gen_files.open("reference/SUMMARY.md", "w") as nav_file:
49
    # Write the navigation as a Markdown list in the literate navigation file
50
    nav_file.writelines(nav.build_literate_nav())
51
52
# Now we are able to remove our hard-coded navigation in mkdocs.yml,
53
# and replace it with a single line!
54
55
# IE: - Code Reference: reference/
56
# Note the trailing slash! It is needed so that mkdocs-literate-nav knows it has
57
# to look for a SUMMARY.md file in that folder
58