|
1 | 1 | """Documentation generation utilities for Adaptive Lighting. |
2 | 2 |
|
3 | | -Provides functions to extract sections from README.md and transform |
4 | | -content for the documentation site. Used by markdown-code-runner |
5 | | -to generate documentation pages from README content. |
| 3 | +Provides functions to transform content for the documentation site. |
| 4 | +Used by markdown-code-runner to generate documentation pages from README content. |
6 | 5 | """ |
7 | 6 |
|
8 | 7 | from __future__ import annotations |
9 | 8 |
|
10 | 9 | import re |
11 | | -from pathlib import Path |
12 | | - |
13 | | -# Path to README relative to this module |
14 | | -_MODULE_DIR = Path(__file__).parent |
15 | | -README_PATH = _MODULE_DIR.parent.parent / "README.md" |
16 | | - |
17 | | - |
18 | | -def readme_section(section_name: str, *, strip_heading: bool = True) -> str: |
19 | | - """Extract a marked section from README.md. |
20 | | -
|
21 | | - Sections are marked with HTML comments: |
22 | | - <!-- SECTION:section_name:START --> |
23 | | - content |
24 | | - <!-- SECTION:section_name:END --> |
25 | | -
|
26 | | - Args: |
27 | | - section_name: The name of the section to extract |
28 | | - strip_heading: If True, remove the first heading from the section |
29 | | -
|
30 | | - Returns: |
31 | | - The content between the section markers |
32 | | -
|
33 | | - Raises: |
34 | | - ValueError: If the section is not found in README.md |
35 | | -
|
36 | | - """ |
37 | | - content = README_PATH.read_text() |
38 | | - |
39 | | - start_marker = f"<!-- SECTION:{section_name}:START -->" |
40 | | - end_marker = f"<!-- SECTION:{section_name}:END -->" |
41 | | - |
42 | | - start_idx = content.find(start_marker) |
43 | | - if start_idx == -1: |
44 | | - msg = f"Section '{section_name}' not found in README.md" |
45 | | - raise ValueError(msg) |
46 | | - |
47 | | - end_idx = content.find(end_marker, start_idx) |
48 | | - if end_idx == -1: |
49 | | - msg = f"End marker for section '{section_name}' not found" |
50 | | - raise ValueError(msg) |
51 | | - |
52 | | - section = content[start_idx + len(start_marker) : end_idx].strip() |
53 | | - |
54 | | - if strip_heading: |
55 | | - # Remove first heading (# or ## or ###) |
56 | | - section = re.sub(r"^#{1,3}\s+[^\n]+\n+", "", section, count=1) |
57 | | - |
58 | | - return _transform_readme_links(section) |
59 | 10 |
|
60 | 11 |
|
61 | 12 | def _transform_readme_links(content: str) -> str: |
|
0 commit comments