-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathgenerate_api_readme.py
More file actions
44 lines (33 loc) · 1.3 KB
/
generate_api_readme.py
File metadata and controls
44 lines (33 loc) · 1.3 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
"""
Generate API_README.md files for TypeDoc by stripping the customer reference section
from each package's README.md and the root README.md.
Registered as an mkdocs on_pre_build hook so it runs automatically during `mkdocs build`.
Can also be run standalone: python3 docs/generate_api_readme.py
"""
import re
from pathlib import Path
# Removes the full section body (heading through the last sub-section before ## License)
SECTION_PATTERN = re.compile(
r"\n## How to support Powertools for AWS Lambda \(TypeScript\)\?.*?(?=\n## )",
re.DOTALL,
)
# Removes the TOC entry and its indented children
TOC_ENTRY_PATTERN = re.compile(
r"\n- \[How to support Powertools for AWS Lambda \(TypeScript\)\?[^\n]*"
r"(?:\n - \[[^\n]*)*",
)
def generate(root: Path = Path(".")) -> None:
targets = [root] + sorted((root / "packages").iterdir())
for pkg in targets:
readme = pkg / "README.md"
if not readme.exists():
continue
content = readme.read_text(encoding="utf-8")
clean = SECTION_PATTERN.sub("", content)
clean = TOC_ENTRY_PATTERN.sub("", clean)
(pkg / "API_README.md").write_text(clean, encoding="utf-8")
# mkdocs hook entry point
def on_pre_build(config) -> None: # noqa: ANN001
generate()
if __name__ == "__main__":
generate()