Skip to content

Commit 980bd8e

Browse files
authored
Fix #171: Document ENV config TOML file (#174)
* Fix #171: Document ENV config TOML file * Generate the RST from TOML file through a Python script (`toml_parser.py`). It can be executed as a standalone script or integrated into Sphinx. * Enhance `conf.py` to read the TOML config(s) and create the generated file. The generate file is included in `reference/env-toml/index.rst`. * The generated file has a different extension than .rst (actually `.rst.inc`). This was necessary otherwise you get a warning from Sphinx about duplicate labels. * Enrich the TOML file with comments similar to "docstrings": it contains the syntax "KEYNAME(TYPE): DESCRIPTION" This is a kind of "self-documenting" approach. * Currently, the standard tomllib library doesn't preserve comments. To not rely on a different library, we need to read it line by line. :( * There is a small optimization in the conf.py file: the .rst.inc file is only generated when the TOML file has a newer date. * Use "github-action" group for CI test * Use "uv lock --no-upgrade" to upgrade uv.lock file * Rework regex * Add UTF-8 encoding when writing RST * Use UTF-8 encoding when reading TOML * Fix comment * Fix regexes for doc comments --------- Co-authored-by: Sushant Gaurav <[email protected]>
1 parent 13cc03c commit 980bd8e

9 files changed

Lines changed: 919 additions & 22 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ env:
2929
UV_FROZEN: "1"
3030
# Needed for coverage/multiprocessing reliability
3131
PYTHONSTARTMETHOD: spawn
32-
# The group to use for installing/testing dependencies
33-
PYPROJECT_GROUP: tests
32+
# The group to use for installing/testing dependencies, see pyproject.toml
33+
PYPROJECT_GROUP: github-action
3434

3535
jobs:
3636
test-ubuntu:

docs/source/conf.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@
77
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
88

99
from datetime import datetime
10+
from pathlib import Path
11+
import sys
12+
13+
from sphinx.application import Sphinx
14+
from sphinx.errors import ExtensionError
15+
from sphinx.util import logging
1016

1117
from docbuild.__about__ import __version__
1218

19+
# Add the directory containing toml_parser.py to sys.path
20+
# If it's in the same directory as conf.py:
21+
sys.path.insert(0, str(Path(__file__).parent))
22+
1323
project = "docbuild"
1424
current_year = datetime.now().year
1525
copyright = f"{current_year}, Tom Schraitle | Sushant Gaurav" # noqa: A001
@@ -219,3 +229,63 @@
219229
# Just ignore useless example URLs
220230
r"https://github\.com/org/repo",
221231
]
232+
233+
234+
# Configuration for TOML documentation generation
235+
# Format: (input_toml, output, {additional_config})
236+
# Paths are relative to the conf.py directory
237+
toml_doc_config = [
238+
# 1
239+
(
240+
"../../etc/docbuild/env.example.toml",
241+
"reference/env-toml/env-toml-ref.rst.inc",
242+
{}, # Use default prefix, no need to add that.
243+
),
244+
]
245+
246+
logger = logging.getLogger(__name__)
247+
248+
249+
def run_toml_generator(app: Sphinx) -> None:
250+
"""Run the TOML documentation generator before the build process starts."""
251+
from toml_parser import generate_toml_reference
252+
253+
conf_dir = Path(app.confdir)
254+
for input_rel, output_rel, config_data in toml_doc_config:
255+
toml_input = (conf_dir / input_rel).resolve()
256+
rst_output = (conf_dir / output_rel).resolve()
257+
258+
# Check if the input file exists before proceeding
259+
if not toml_input.exists():
260+
msg = (
261+
"TOML documentation generation failed: "
262+
f"Input file not found at {toml_input}"
263+
)
264+
logger.error(msg)
265+
raise ExtensionError(msg)
266+
267+
# Optimization: Only generate if input is newer than output
268+
if rst_output.exists():
269+
input_mtime = toml_input.stat().st_mtime
270+
output_mtime = rst_output.stat().st_mtime
271+
272+
if input_mtime <= output_mtime:
273+
logger.info(
274+
"Skipping TOML reference generation: %s is up to date.",
275+
rst_output.name,
276+
)
277+
continue
278+
279+
# Ensure the directory exists
280+
rst_output.parent.mkdir(parents=True, exist_ok=True)
281+
282+
logger.info(
283+
"Generating TOML reference: %s -> %s", toml_input.name, rst_output.name
284+
)
285+
generate_toml_reference(toml_input, rst_output, **config_data)
286+
287+
288+
def setup(app: Sphinx) -> None:
289+
"""Sphinx setup function to connect the TOML generator to the build process."""
290+
# This hook ensures the file exists before Sphinx tries to 'include' it
291+
app.connect("builder-inited", run_toml_generator)

0 commit comments

Comments
 (0)