INNER CODE UNIT · Python
synthesize_heading
OWASP/wstg · .github/ebooks/list-chapters.py:62
def synthesize_heading(dirpath: str, root: str) -> tuple[str, str]:
"""Return (display title, anchor id) for a directory's chapter heading.
The id is derived the same way links.lua derives a link target from a
folder name (numbering stripped, then slugified) so it always matches.
The display title preserves chapter numbers (0-6) and appendix letters
(A-F) for clarity in the outline.
"""
name = os.path.basename(dirpath)
num_match = DIR_NUMBER_RE.match(name)
letter_match = re.match(r"^([A-F])-", name)
chapter_num = int(num_match.group(1)) if num_match else None
chapter_letter = letter_match.group(1) if letter_match else None
base = re.sub(r"^[\d.]+-", "", name)
base = re.sub(r"^[A-Za-z]-", "", base)
plain_title = base.replace("_", " ")
anchor_id = slugify(plain_title)