forked from tomasvotava/fastapi-sso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_reference.py
64 lines (48 loc) · 2.26 KB
/
generate_reference.py
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
"""Generate reference pages for the documentation."""
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import mkdocs.config.defaults
SKIPPED_MODULES = ("fastapi_sso.sso", "fastapi_sso")
def generate_reference_pages(docs_dir: str, nav: list):
"""Generate reference pages for the documentation."""
reference_path = Path(docs_dir, "reference")
reference_path.mkdir(exist_ok=True)
source_path = Path("./fastapi_sso")
reference_nav = []
for path in sorted(source_path.rglob("*.py")):
module_path = path.relative_to(".").with_suffix("")
doc_path = str(path.relative_to(source_path).with_suffix(".md")).replace("/", ".")
full_doc_path = reference_path / doc_path
nav_path = (reference_path / doc_path).relative_to(docs_dir).as_posix()
parts = module_path.parts
if parts[-1] == "__init__":
if len(parts) == 1:
parts = ["fastapi_sso"]
else:
parts = parts[:-1]
elif parts[-1] == "__main__":
continue
import_path = ".".join(parts)
if import_path in SKIPPED_MODULES:
continue
full_doc_path.parent.mkdir(exist_ok=True, parents=True)
with open(full_doc_path, "w", encoding="utf-8") as file:
file.write(f"::: {import_path}\n")
reference_nav.append({import_path: Path(nav_path).as_posix()})
nav.append({"Reference": reference_nav})
def generate_example_pages(docs_dir: str, nav: list):
"""Generate example pages for the documentation."""
examples_path = Path(docs_dir, "examples.md")
source_path = Path("./examples")
examples_path.unlink(missing_ok=True)
with examples_path.open("w", encoding="utf-8") as file:
file.write("# Examples\n\n")
for path in sorted(source_path.rglob("*.py")):
page_title = path.stem.replace("_", " ").title()
file.write(f"## {page_title}\n\n```python\n{path.read_text(encoding='utf-8')}\n```\n\n")
nav.append({"Examples": "examples.md"})
def on_config(config: "mkdocs.config.defaults.MkDocsConfig"):
"""Generate reference pages for the documentation."""
generate_example_pages(config.docs_dir, config.nav)
generate_reference_pages(config.docs_dir, config.nav)