Skip to content

Commit a378a79

Browse files
authored
Merge pull request #145 from braboj/codex/refactor-gen_markdown_chapters.py-for-file-handling
Read acronyms once
2 parents eee7e91 + b39ef84 commit a378a79

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

.scripts/gen_markdown_chapters.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
import re
1919
from 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+
2126
def to_title(name: str) -> str:
2227
"""Return *name* in title format with spaces and preserved acronyms."""
2328

@@ -27,19 +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:
3738
# Skip empty parts that may result from leading/trailing separators
3839
if not part:
3940
continue
4041

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

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

0 commit comments

Comments
 (0)