Passed
Pull Request — master (#12)
by Konstantinos
03:52
created

gen_api_refs_pages   A

Complexity

Total Complexity 0

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 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
35
    # ROOT
36
    #  -> docs
37
    #     -> scripts
38
    # -> src/python_package
39
    mkdocs_gen_files.set_edit_path(full_doc_path, Path("../") / path)
40
    # so that it correctly sets the edit path of (for example) nst_math.py to
41
    # <repo_url>/blob/master/src/artificial_artwork/nst_math.py instead of
42
    # <repo_url>/blob/master/docs/src/artificial_artwork/nst_math.py
43
44
45
    # mkdocs_gen_files.set_edit_path(full_doc_path, path)
46
47
48
# At the end, create a magic, literate navigation file called SUMMARY.md in the
49
# reference folder.
50
with mkdocs_gen_files.open("reference/SUMMARY.md", "w") as nav_file:
51
    # Write the navigation as a Markdown list in the literate navigation file
52
    nav_file.writelines(nav.build_literate_nav())
53
54
# Now we are able to remove our hard-coded navigation in mkdocs.yml,
55
# and replace it with a single line!
56
57
# IE: - Code Reference: reference/
58
# Note the trailing slash! It is needed so that mkdocs-literate-nav knows it has
59
# to look for a SUMMARY.md file in that folder
60