Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions .scripts/gen_markdown_chapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
import re
from pathlib import Path

# Load user-defined acronyms once when the module is imported
ACRONYMS_PATH = Path(__file__).resolve().parent / "acronyms.json"
with ACRONYMS_PATH.open("r", encoding="utf-8") as json_data:
USER_ACRONYMS = json.load(json_data)
Comment on lines +23 to +24
Copy link

Copilot AI Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding error handling (e.g., try/except) around the file read to gracefully handle cases when acronyms.json is missing or invalid.

Suggested change
with ACRONYMS_PATH.open("r", encoding="utf-8") as json_data:
USER_ACRONYMS = json.load(json_data)
try:
with ACRONYMS_PATH.open("r", encoding="utf-8") as json_data:
USER_ACRONYMS = json.load(json_data)
except FileNotFoundError:
print(f"Warning: {ACRONYMS_PATH} not found. Using an empty acronym list.")
USER_ACRONYMS = {}
except json.JSONDecodeError as e:
print(f"Error: Failed to parse {ACRONYMS_PATH}: {e}. Using an empty acronym list.")
USER_ACRONYMS = {}

Copilot uses AI. Check for mistakes.

def to_title(name: str) -> str:
"""Return *name* in title format with spaces and preserved acronyms."""

Expand All @@ -27,10 +32,6 @@ def to_title(name: str) -> str:
# Split on underscores, dashes, or spaces
parts = re.split(r"[_\-\s]+", name)

# Import JSON file containing user-defined acronyms
with open('acronyms.json') as json_data:
user_acronyms = json.load(json_data)

# Capitalize each part, preserving all-uppercase acronyms
words = []
for part in parts:
Expand All @@ -41,8 +42,8 @@ def to_title(name: str) -> str:
if not part:
continue

# Check user-defined acronyms from `docs/acronyms.txt`
if part.upper() in user_acronyms:
# Check user-defined acronyms loaded from ``acronyms.json``
if part.upper() in USER_ACRONYMS:
words.append(part.upper())

# If the part is all uppercase and contains only alphanumeric characters,
Expand Down