|
| 1 | +""" |
| 2 | +Script called by mkdocs-gen-files that generates markdown documents |
| 3 | +with API reference placeholders. |
| 4 | +
|
| 5 | +https://oprypin.github.io/mkdocs-gen-files/api.html |
| 6 | +""" |
| 7 | + |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import mkdocs_gen_files |
| 11 | + |
| 12 | +nav = mkdocs_gen_files.Nav() |
| 13 | + |
| 14 | +root = Path(__file__).parent.parent.parent |
| 15 | +src = root / "libzim" |
| 16 | +api_reference = Path("api_reference") |
| 17 | + |
| 18 | +# Because we are inspecting a compiled module and all the classes can be seen |
| 19 | +# from the `libzim` namespace, their documentation is shown in the home page. |
| 20 | +# We hide them from the home page as their documentation is also shown in the |
| 21 | +# respective modules where they are defined. |
| 22 | +LIBZIM_ROOT_OPTIONS = """ |
| 23 | + options: |
| 24 | + members: false |
| 25 | +""" |
| 26 | + |
| 27 | + |
| 28 | +for path in sorted(src.rglob("*.pyi")): |
| 29 | + module_path = path.relative_to(root).with_suffix("") |
| 30 | + |
| 31 | + # Package docs get the parent module name. |
| 32 | + if module_path.name == "__init__": |
| 33 | + module_path = module_path.parent |
| 34 | + module_options = LIBZIM_ROOT_OPTIONS |
| 35 | + else: |
| 36 | + module_options = "" |
| 37 | + |
| 38 | + if module_path.name.startswith("_"): |
| 39 | + # Skip other hidden items |
| 40 | + continue |
| 41 | + identifier = ".".join(module_path.parts) |
| 42 | + |
| 43 | + doc_path = identifier + ".md" |
| 44 | + full_doc_path = api_reference / doc_path |
| 45 | + |
| 46 | + nav[identifier] = doc_path |
| 47 | + |
| 48 | + # Create a document with mkdocstrings placeholders. |
| 49 | + with mkdocs_gen_files.open(full_doc_path, "w") as fd: |
| 50 | + fd.write( |
| 51 | + f"""--- |
| 52 | +title: {identifier} |
| 53 | +--- |
| 54 | +
|
| 55 | +
|
| 56 | +::: {identifier} |
| 57 | +{module_options} |
| 58 | +""" |
| 59 | + ) |
| 60 | + |
| 61 | + # Make the edit button on the page link to the source file. |
| 62 | + mkdocs_gen_files.set_edit_path(full_doc_path, Path("..") / path.relative_to(root)) |
| 63 | + |
| 64 | +# Write a navigation file that will be interpreted by literate-nav. |
| 65 | +with mkdocs_gen_files.open(api_reference / "NAVIGATION.md", "w") as fd: |
| 66 | + fd.writelines(nav.build_literate_nav()) |
0 commit comments