Migrate to markdown-code-runner's built-in include_section() (#1417)

This commit is contained in:
Bas Nijholt 2026-01-26 09:01:50 +01:00 committed by GitHub
commit f9ccc946ee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 24 additions and 78 deletions

View file

@ -1,61 +1,12 @@
"""Documentation generation utilities for Adaptive Lighting.
Provides functions to extract sections from README.md and transform
content for the documentation site. Used by markdown-code-runner
to generate documentation pages from README content.
Provides functions to transform content for the documentation site.
Used by markdown-code-runner to generate documentation pages from README content.
"""
from __future__ import annotations
import re
from pathlib import Path
# Path to README relative to this module
_MODULE_DIR = Path(__file__).parent
README_PATH = _MODULE_DIR.parent.parent / "README.md"
def readme_section(section_name: str, *, strip_heading: bool = True) -> str:
"""Extract a marked section from README.md.
Sections are marked with HTML comments:
<!-- SECTION:section_name:START -->
content
<!-- SECTION:section_name:END -->
Args:
section_name: The name of the section to extract
strip_heading: If True, remove the first heading from the section
Returns:
The content between the section markers
Raises:
ValueError: If the section is not found in README.md
"""
content = README_PATH.read_text()
start_marker = f"<!-- SECTION:{section_name}:START -->"
end_marker = f"<!-- SECTION:{section_name}:END -->"
start_idx = content.find(start_marker)
if start_idx == -1:
msg = f"Section '{section_name}' not found in README.md"
raise ValueError(msg)
end_idx = content.find(end_marker, start_idx)
if end_idx == -1:
msg = f"End marker for section '{section_name}' not found"
raise ValueError(msg)
section = content[start_idx + len(start_marker) : end_idx].strip()
if strip_heading:
# Remove first heading (# or ## or ###)
section = re.sub(r"^#{1,3}\s+[^\n]+\n+", "", section, count=1)
return _transform_readme_links(section)
def _transform_readme_links(content: str) -> str:

View file

@ -19,8 +19,7 @@ Adaptive Lighting supports three brightness modes:
## Detailed Explanation
<!-- CODE:START -->
<!-- from adaptive_lighting.docs_gen import readme_section -->
<!-- print(readme_section("brightness-modes")) -->
<!-- print(include_section("../../README.md", "brightness-modes", strip_heading=True)) -->
<!-- CODE:END -->
<!-- OUTPUT:START -->
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
@ -90,8 +89,7 @@ adaptive_lighting:
These graphs show how brightness changes throughout the day based on calculated values:
<!-- CODE:START -->
<!-- from adaptive_lighting.docs_gen import readme_section -->
<!-- print(readme_section("graphs")) -->
<!-- print(include_section("../../README.md", "graphs", strip_heading=True)) -->
<!-- CODE:END -->
<!-- OUTPUT:START -->
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->

View file

@ -9,8 +9,7 @@ Adaptive Lighting is designed to work seamlessly with manual adjustments, detect
## How It Works
<!-- CODE:START -->
<!-- from adaptive_lighting.docs_gen import readme_section -->
<!-- print(readme_section("manual-control")) -->
<!-- print(include_section("../../README.md", "manual-control", strip_heading=True)) -->
<!-- CODE:END -->
<!-- OUTPUT:START -->
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->

View file

@ -7,8 +7,8 @@ icon: lucide/bot
Real-world automation examples showing how to integrate Adaptive Lighting with your Home Assistant setup.
<!-- CODE:START -->
<!-- from adaptive_lighting.docs_gen import readme_section -->
<!-- print(readme_section("automation-examples")) -->
<!-- from adaptive_lighting.docs_gen import _transform_readme_links -->
<!-- print(_transform_readme_links(include_section("../README.md", "automation-examples", strip_heading=True))) -->
<!-- CODE:END -->
<!-- OUTPUT:START -->
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->

View file

@ -85,8 +85,8 @@ All configuration options are listed below with their default values. These opti
## Full Configuration Example
<!-- CODE:START -->
<!-- from adaptive_lighting.docs_gen import readme_section -->
<!-- print(readme_section("config-example-full", strip_heading=False)) -->
<!-- from adaptive_lighting.docs_gen import _transform_readme_links -->
<!-- print(_transform_readme_links(include_section("../README.md", "config-example-full"))) -->
<!-- CODE:END -->
<!-- OUTPUT:START -->
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->

View file

@ -26,8 +26,8 @@ By automatically adapting the settings of your lights throughout the day, Adapti
## Features
<!-- CODE:START -->
<!-- from adaptive_lighting.docs_gen import readme_section -->
<!-- print(readme_section("features")) -->
<!-- from adaptive_lighting.docs_gen import _transform_readme_links -->
<!-- print(_transform_readme_links(include_section("../README.md", "features", strip_heading=True))) -->
<!-- CODE:END -->
<!-- OUTPUT:START -->
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->

View file

@ -9,8 +9,8 @@ Resources, tutorials, and related projects for Adaptive Lighting.
## Tutorials & Articles
<!-- CODE:START -->
<!-- from adaptive_lighting.docs_gen import readme_section -->
<!-- print(readme_section("see-also")) -->
<!-- from adaptive_lighting.docs_gen import _transform_readme_links -->
<!-- print(_transform_readme_links(include_section("../README.md", "see-also", strip_heading=True))) -->
<!-- CODE:END -->
<!-- OUTPUT:START -->
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->

View file

@ -112,13 +112,11 @@ data:
## adaptive_lighting.change_switch_settings
<!-- CODE:START -->
<!-- from adaptive_lighting.docs_gen import readme_section -->
<!-- print(readme_section("change-switch-settings")) -->
<!-- from adaptive_lighting.docs_gen import _transform_readme_links -->
<!-- print(_transform_readme_links(include_section("../README.md", "change-switch-settings", strip_heading=True))) -->
<!-- CODE:END -->
<!-- OUTPUT:START -->
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
#### `adaptive_lighting.change_switch_settings`
`adaptive_lighting.change_switch_settings` (new in 1.7.0) Change any of the above configuration options of Adaptive Lighting (such as `sunrise_time` or `prefer_rgb_color`) with a service call directly from your script/automation.
> [!WARNING]

View file

@ -9,8 +9,8 @@ This guide covers common issues and their solutions when using Adaptive Lighting
## Enable Debug Logging
<!-- CODE:START -->
<!-- from adaptive_lighting.docs_gen import readme_section -->
<!-- print(readme_section("troubleshooting-intro")) -->
<!-- from adaptive_lighting.docs_gen import _transform_readme_links -->
<!-- print(_transform_readme_links(include_section("../README.md", "troubleshooting-intro", strip_heading=True))) -->
<!-- CODE:END -->
<!-- OUTPUT:START -->
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->
@ -30,8 +30,8 @@ After the issue occurs, create a new issue report with the log (`/config/home-as
## Common Problems & Solutions
<!-- CODE:START -->
<!-- from adaptive_lighting.docs_gen import readme_section -->
<!-- print(readme_section("common-problems")) -->
<!-- from adaptive_lighting.docs_gen import _transform_readme_links -->
<!-- print(_transform_readme_links(include_section("../README.md", "common-problems", strip_heading=True))) -->
<!-- CODE:END -->
<!-- OUTPUT:START -->
<!-- ⚠️ This content is auto-generated by `markdown-code-runner`. -->

View file

@ -14,7 +14,7 @@ requires-python = ">=3.12"
docs = [
"astral",
"homeassistant",
"markdown-code-runner",
"markdown-code-runner>=2.7.0",
"markdown-gfm-admonition",
"pandas",
"shinylive",

8
uv.lock generated
View file

@ -109,7 +109,7 @@ dev = [
docs = [
{ name = "astral" },
{ name = "homeassistant" },
{ name = "markdown-code-runner" },
{ name = "markdown-code-runner", specifier = ">=2.7.0" },
{ name = "markdown-gfm-admonition" },
{ name = "pandas" },
{ name = "shinylive" },
@ -2790,11 +2790,11 @@ wheels = [
[[package]]
name = "markdown-code-runner"
version = "2.4.0"
version = "2.7.0"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/34/91/6b7030f873b0c49f38131c7a5ac9432238cf1e1cd47104c05b07cf5c32ed/markdown_code_runner-2.4.0.tar.gz", hash = "sha256:7d6229c437f0c71e5c0442585663af4bfe4beacddf884a64ea8c09fd5dbc31dd", size = 19072, upload-time = "2025-08-23T19:07:41.78Z" }
sdist = { url = "https://files.pythonhosted.org/packages/28/d1/2a7753e05dcc711552721048a34341b63a6a2b09e70ac738c7a4c81ca414/markdown_code_runner-2.7.0.tar.gz", hash = "sha256:b36f9314839c0db3ee5e5d644e1b4471454af8f624eb15bd5a5d68dbc6430afe", size = 99832, upload-time = "2026-01-24T21:20:04.772Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/98/3c/8857763070aa0f39591b012f92ab40bd5fe906914085e925eb117e28f306/markdown_code_runner-2.4.0-py3-none-any.whl", hash = "sha256:5be62e75ab35188b37f2d440c19b6683df68615ea24bed4955d95a745e56b483", size = 12292, upload-time = "2025-08-23T19:07:40.972Z" },
{ url = "https://files.pythonhosted.org/packages/cf/92/9d7fa0567cf3d38b551bb1a30c177285e0e42ae2b10c16946af7e8a38e73/markdown_code_runner-2.7.0-py3-none-any.whl", hash = "sha256:fad5d5fbad9c49687141ab9c39ed7e054f857b97aa79d11f031de157afd21c20", size = 14692, upload-time = "2026-01-24T21:20:03.382Z" },
]
[[package]]