This repository was archived by the owner on Jan 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathextract_example_code_from_docs.py
More file actions
79 lines (61 loc) · 2.05 KB
/
extract_example_code_from_docs.py
File metadata and controls
79 lines (61 loc) · 2.05 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# pyright: strict
import re
from typing import Optional
from pathlib import Path
EXAMPLE_PATTERN = re.compile(r"## Example\r?\n\r?\n```cpp\r?\n(.*?)\r?\n```", re.DOTALL)
def extract_cpp_code(md_path: Path) -> Optional[str]:
with open(md_path, "r", encoding="utf-8") as f:
content = f.read()
code_blocks: list[str] = re.findall(EXAMPLE_PATTERN, content)
if len(code_blocks) == 0:
return # No match, skip
elif len(code_blocks) > 1:
msg = f"File '{md_path}' contains more than one '## Example' C++ code block."
raise ValueError(msg)
cpp_code = code_blocks[0]
header = f"""
// This file was auto-generated from:
// {md_path}
""".lstrip()
if "pro::skills::format" in cpp_code:
cpp_code = f"""
#include <proxy/proxy.h>
#ifdef PRO4D_HAS_FORMAT
{cpp_code}
#else
int main() {{
// std::format not available
return 77;
}}
#endif
""".strip()
return header + cpp_code
def main() -> None:
import argparse
import os
from dataclasses import dataclass
@dataclass(frozen=True)
class Args:
input_dir: Path
output_dir: Path
parser = argparse.ArgumentParser()
_ = parser.add_argument("input_dir", type=Path, help="Path to Markdown documents")
_ = parser.add_argument("output_dir", type=Path, help="Source code output path")
args = parser.parse_args(namespace=Args)
input_dir = args.input_dir
output_dir = args.output_dir
for root, _, files in os.walk(input_dir):
for file in files:
if file.endswith(".md"):
md_path = Path(root) / file
rel_path = md_path.relative_to(input_dir)
rel_base = "_".join([*rel_path.parent.parts, rel_path.stem])
cpp_path = output_dir / f"example_{rel_base}.cpp"
cpp_code = extract_cpp_code(md_path)
if cpp_code is None:
continue
with open(cpp_path, "w", encoding="utf-8") as f:
_ = f.write(cpp_code)
if __name__ == "__main__":
main()