|
5 | 5 | import html |
6 | 6 | from typing import TYPE_CHECKING |
7 | 7 |
|
8 | | -from .text_utils import format_html_text |
| 8 | +from .text_utils import format_html_text, slugify |
9 | 9 |
|
10 | 10 | if TYPE_CHECKING: # pragma: no cover - hints only |
11 | 11 | from .compendium import Compendium |
12 | 12 | from .entities import Citation, Section |
13 | 13 |
|
14 | 14 |
|
15 | | -def _slugify(text: str) -> str: |
16 | | - """Convert text to a URL-friendly slug.""" |
17 | | - import re |
18 | | - |
19 | | - slug = re.sub(r"[^a-z0-9]+", "-", text.lower()).strip("-") |
20 | | - return slug or "page" |
21 | | - |
22 | | - |
23 | 15 | def _html_head(title: str, depth: int = 0) -> list[str]: |
24 | 16 | """Generate HTML head section.""" |
25 | 17 | return [ |
@@ -50,7 +42,7 @@ def _nav_links( |
50 | 42 | parts.append(" <li>Sections:") |
51 | 43 | parts.append(" <ul>") |
52 | 44 | for section in sections: |
53 | | - section_slug = _slugify(section.identifier) |
| 45 | + section_slug = slugify(section.identifier) |
54 | 46 | href = f"{prefix}sections/{section_slug}.html" |
55 | 47 | label = html.escape(section.title) |
56 | 48 | parts.append(f' <li><a href="{href}">{label}</a></li>') |
@@ -105,7 +97,7 @@ def _render_index_page(compendium: "Compendium") -> str: |
105 | 97 | parts.append(" <h2>Sections</h2>") |
106 | 98 | parts.append(" <ul>") |
107 | 99 | for section in compendium.sections: |
108 | | - section_slug = _slugify(section.identifier) |
| 100 | + section_slug = slugify(section.identifier) |
109 | 101 | href = f"sections/{section_slug}.html" |
110 | 102 | label = html.escape(section.title) |
111 | 103 | summary = format_html_text(section.summary) |
@@ -320,7 +312,7 @@ def render_html_site(compendium: "Compendium") -> dict[str, str]: |
320 | 312 |
|
321 | 313 | # Section pages |
322 | 314 | for section in compendium.sections: |
323 | | - section_slug = _slugify(section.identifier) |
| 315 | + section_slug = slugify(section.identifier) |
324 | 316 | path = f"sections/{section_slug}.html" |
325 | 317 | files[path] = _render_section_page(section, compendium) |
326 | 318 |
|
|
0 commit comments