22"""Generate aggregated Markdown files for each examples subfolder.
33
44Usage:
5- python .scripts/examples_to_markdown_files .py \
5+ python .scripts/gen_markdown_chapters .py \
66 --examples-dir examples \
77 --output-dir docs
88
1818import re
1919from pathlib import Path
2020
21+ # Load user-defined acronyms once when the module is imported
22+ ACRONYMS_PATH = Path (__file__ ).resolve ().parent / "acronyms.json"
23+ with ACRONYMS_PATH .open ("r" , encoding = "utf-8" ) as json_data :
24+ USER_ACRONYMS = json .load (json_data )
25+
2126def to_title (name : str ) -> str :
2227 """Return *name* in title format with spaces and preserved acronyms."""
2328
@@ -27,22 +32,15 @@ def to_title(name: str) -> str:
2732 # Split on underscores, dashes, or spaces
2833 parts = re .split (r"[_\-\s]+" , name )
2934
30- # Import JSON file containing user-defined acronyms
31- with open ('acronyms.json' ) as json_data :
32- user_acronyms = json .load (json_data )
33-
3435 # Capitalize each part, preserving all-uppercase acronyms
3536 words = []
3637 for part in parts :
37-
38- print (part )
39-
4038 # Skip empty parts that may result from leading/trailing separators
4139 if not part :
4240 continue
4341
44- # Check user-defined acronyms from `docs/ acronyms.txt `
45- if part .upper () in user_acronyms :
42+ # Check user-defined acronyms loaded from `` acronyms.json` `
43+ if part .upper () in USER_ACRONYMS :
4644 words .append (part .upper ())
4745
4846 # If the part is all uppercase and contains only alphanumeric characters,
@@ -56,13 +54,6 @@ def to_title(name: str) -> str:
5654
5755 return " " .join (words )
5856
59- def to_camel_case (name : str ) -> str :
60- """Return *name* converted to CamelCase without leading digits."""
61- # Strip leading numeric prefixes like ``01_``
62- name = re .sub (r"^\d+_?" , "" , name )
63- parts = re .split (r"[_\-\s]+" , name )
64- return " " .join (word .capitalize () for word in parts if word )
65-
6657def generate_aggregate (folder : Path , output_dir : Path ) -> None :
6758 """Create a single Markdown file aggregating all examples in *folder*."""
6859 examples = []
0 commit comments