Skip to content

Commit f5dffca

Browse files
committed
Remove obsolete files and folders
1 parent deccf31 commit f5dffca

File tree

5 files changed

+98
-224
lines changed

5 files changed

+98
-224
lines changed

.scripts/examples_to_markdown_files.py renamed to .scripts/gen_markdown_chapters.py

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ def to_title(name: str) -> str:
3737
return " ".join(words)
3838

3939

40-
40+
def to_camel_case(name: str) -> str:
41+
"""Return *name* converted to CamelCase without leading digits."""
42+
# Strip leading numeric prefixes like ``01_``
43+
name = re.sub(r"^\d+_?", "", name)
44+
parts = re.split(r"[_\-\s]+", name)
45+
return " ".join(word.capitalize() for word in parts if word)
4146

4247
def generate_aggregate(folder: Path, output_dir: Path) -> None:
4348
"""Create a single Markdown file aggregating all examples in *folder*."""
@@ -66,47 +71,19 @@ def generate_aggregate(folder: Path, output_dir: Path) -> None:
6671
with agg_path.open("w", encoding="utf-8") as f:
6772
f.write("\n".join(lines).rstrip() + "\n")
6873

69-
70-
def generate_aggregate(folder: Path, base_dir: Path, output_dir: Path) -> None:
71-
"""Create a single Markdown file aggregating all examples in *folder*."""
72-
examples = []
73-
for py_file in sorted(folder.glob("*.py")):
74-
if py_file.name == "__init__.py":
75-
continue
76-
with py_file.open("r", encoding="utf-8") as f:
77-
code = f.read().rstrip()
78-
examples.append((to_camel_case(py_file.stem), code))
79-
80-
if not examples:
81-
return
82-
83-
title = to_camel_case(folder.name)
84-
lines = [f"# {title}", ""]
85-
for name, code in examples:
86-
lines.append(f"## {name}")
87-
lines.append("")
88-
lines.append("```python")
89-
lines.append(code)
90-
lines.append("```")
91-
lines.append("")
92-
93-
agg_path = output_dir / f"{folder.name}.md"
94-
with agg_path.open("w", encoding="utf-8") as f:
95-
f.write("\n".join(lines).rstrip() + "\n")
96-
97-
9874
def main() -> None:
75+
9976
parser = argparse.ArgumentParser(
10077
description="Generate aggregated Markdown files from the examples"
10178
)
10279
parser.add_argument(
10380
"--examples-dir",
104-
default="examples",
81+
default="../examples",
10582
help="Directory containing example .py files",
10683
)
10784
parser.add_argument(
10885
"--output-dir",
109-
default="docs",
86+
default="../docs",
11087
help="Directory where Markdown files will be written",
11188
)
11289
args = parser.parse_args()
@@ -118,16 +95,9 @@ def main() -> None:
11895
# previously generated documentation.
11996
output_dir.mkdir(parents=True, exist_ok=True)
12097

121-
122-
12398
for folder in sorted(examples_dir.iterdir()):
12499
if folder.is_dir():
125100
generate_aggregate(folder, output_dir)
126101

127-
for folder in sorted(examples_dir.iterdir()):
128-
if folder.is_dir():
129-
generate_aggregate(folder, examples_dir, output_dir)
130-
131-
132102
if __name__ == "__main__":
133103
main()

.scripts/gen_markdown_examples.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python3
2+
"""Generate a Markdown file for each Python example.
3+
4+
Usage:
5+
python .scripts/examples_to_markdown_files.py \
6+
--examples-dir examples \
7+
--template templates/example_file.mustache \
8+
--output-dir docs
9+
10+
The directory structure under ``examples`` is mirrored under the output
11+
directory. Each ``.py`` file becomes a ``.md`` file with the same name and
12+
relative path. Files named ``__init__.py`` are ignored and skipped.
13+
"""
14+
15+
import argparse
16+
from pathlib import Path
17+
18+
try:
19+
import pystache
20+
except ModuleNotFoundError as exc: # pragma: no cover - import guard
21+
raise SystemExit(
22+
"pystache is required to run this script. Install it via `pip install pystache`."
23+
) from exc
24+
25+
26+
def parse_example(path: Path) -> dict:
27+
"""Return the file name and entire contents of the example."""
28+
with path.open("r", encoding="utf-8") as f:
29+
code = f.read()
30+
return {"name": path.stem, "code": code.rstrip()}
31+
32+
33+
def render_markdown(template: str, context: dict) -> str:
34+
"""Render the Markdown using a Mustache template."""
35+
renderer = pystache.Renderer()
36+
return renderer.render(template, context)
37+
38+
39+
def process_file(py_path: Path, template: str, base_dir: Path, output_dir: Path) -> None:
40+
"""Write a Markdown version of *py_path* under *output_dir*."""
41+
context = parse_example(py_path)
42+
markdown = render_markdown(template, context)
43+
44+
relative = py_path.relative_to(base_dir).with_suffix(".md")
45+
md_path = output_dir / relative
46+
md_path.parent.mkdir(parents=True, exist_ok=True)
47+
with md_path.open("w", encoding="utf-8") as f:
48+
f.write(markdown)
49+
50+
51+
def main() -> None:
52+
parser = argparse.ArgumentParser(
53+
description="Generate Markdown files mirroring the examples directory"
54+
)
55+
parser.add_argument(
56+
"--examples-dir",
57+
default="examples",
58+
help="Directory containing example .py files",
59+
)
60+
parser.add_argument(
61+
"--template",
62+
default="templates/example_file.mustache",
63+
help="Mustache template file",
64+
)
65+
parser.add_argument(
66+
"--output-dir",
67+
default="docs",
68+
help="Directory where Markdown files will be written",
69+
)
70+
args = parser.parse_args()
71+
72+
examples_dir = Path(args.examples_dir)
73+
output_dir = Path(args.output_dir)
74+
# Ensure the output directory exists but do not wipe it if it already
75+
# contains files. ``exist_ok=True`` prevents accidental deletion of
76+
# previously generated documentation.
77+
output_dir.mkdir(parents=True, exist_ok=True)
78+
79+
with Path(args.template).open("r", encoding="utf-8") as f:
80+
template = f.read()
81+
82+
for py_file in sorted(examples_dir.rglob("*.py")):
83+
if py_file.name == "__init__.py":
84+
continue
85+
process_file(py_file, template, examples_dir, output_dir)
86+
87+
88+
if __name__ == "__main__":
89+
main()
File renamed without changes.

.scripts/generate_hugo_menu.py

Lines changed: 0 additions & 88 deletions
This file was deleted.

.scripts/snake_to_kebab.py

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)