-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.py
More file actions
51 lines (36 loc) · 1.61 KB
/
hooks.py
File metadata and controls
51 lines (36 loc) · 1.61 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
from __future__ import annotations
import re
from pathlib import Path
from openapidocs.mk.v3 import OpenAPIV3DocumentationHandler
from openapidocs.utils.source import read_from_source
OAD_PATTERN = re.compile(r"\[OAD\(([^)]+)\)\]")
def _render_oad(markdown_path: Path, templates_path: Path | None) -> str:
source_dir = markdown_path.parent
markdown = markdown_path.read_text(encoding="utf-8")
def replace(match: re.Match[str]) -> str:
source = match.group(1).strip("'\"")
source_path = (source_dir / source).resolve()
data = read_from_source(str(source_path), source_dir)
handler = OpenAPIV3DocumentationHandler(
data,
style="MKDOCS",
source=str(source_path),
templates_path=str(templates_path) if templates_path else None,
)
return handler.write()
return OAD_PATTERN.sub(replace, markdown)
def generate_openapi(docs_dir: Path) -> None:
docs_dir = docs_dir.resolve()
templates_path = docs_dir / "oad-templates"
templates_dir = templates_path if templates_path.exists() else None
for markdown_path in docs_dir.glob("openapi-*.md"):
markdown = markdown_path.read_text(encoding="utf-8")
if "[OAD(" not in markdown:
continue
output_path = markdown_path.with_suffix(".generated.md")
output = _render_oad(markdown_path, templates_dir)
output_path.write_text(output, encoding="utf-8")
def on_pre_build(config) -> None:
generate_openapi(Path(config["docs_dir"]))
if __name__ == "__main__":
generate_openapi(Path(__file__).resolve().parent / "docs")