✨ Render marked-RST blocks in src-trace directive (#43) - #99
Conversation
The `src-trace` directive previously only rendered one-line needs and silently discarded marked-RST blocks even when `get_rst = true` was set. Marked-RST support is now opt-in via the existing `get_rst` toggle: when enabled, each extracted RST block is parsed inline into the current document with `nested_parse_with_titles`, giving authors full control over the emitted nodes (needs, cross-references, admonitions, ...). Source-page anchor mappings are updated for marked-RST blocks too, so the generated highlighted source page links the marker line back to the document that hosts the `src-trace` directive. - Extend `render_needs()` in `sphinx_extension/directives/src_trace.py` with a `_render_marked_rst()` helper. - Drop the "only supports one-line needs" attention banner in `docs/source/components/directive.rst` and document the opt-in. - Add a `doc_test/marked_rst_basic` Sphinx fixture and doctree snapshot covering `.. impl::` rendered from a C++ block comment. Refs useblocks#43
|
@ubmarco @juiwenchen not sure if this helps for the source_to_rst topic? |
|
Thanks @cpolzer. Wonderful work |
|
Thanks for this — the approach is clean, the per-line The parser is the host's, not RST's. Difference vs. plain
Crucially, neither is parser-agnostic — both call Recommendation on the parse call. I'd avoid And the CLI can't decide either. codelinks has a CLI ( My position — this makes it work cleanly: on the Sphinx level, reject marked-RST content when |
- Use nested_parse_to_nodes (sphinx.util.parsing) with allow_section_headings=False instead of the deprecated nested_parse_with_titles; available across sphinx>=7.4 with no version gating needed. - Warn and skip marked-RST blocks when the hosting document is not an RST file, preventing silent misparse in MyST (.md) hosts.
@ubmarco All three points addressed in a2c4839:
|
|
Interesting. I believe we have to give the task to ensure we only add rst content from comments to rst files to the user. Why? Reason: c file: with multi project config and fetch in his rst files the rst section rst file: and in his md files the md section md file: @ubmarco: maybe we should rename the config parameter OR: We could even think, to set the parsing language by configuration: With this, we may could even support to parse rst comments to md files or vice versa, as we could change the parser. Not sure about, may have to ask @chrisjsewell? |
|
@cpolzer thanks for the fixing round |
Summary
Fixes the gap discussed in #43: the
src-tracedirective extracted marked-RST blocks (@rst ... @endrst) whenget_rst = truewas configured, but then silently discarded them. Only one-line needs made it into the doctree.This PR wires marked-RST rendering into the directive. Support remains opt-in via the existing
get_rsttoggle — behaviour for projects that do not enable it is unchanged.Behaviour
When
get_rst = trueis set for a project, each extracted RST block is parsed inline into the current document vianested_parse_with_titles. Authors keep full control over what the block emits — sphinx-needs directives (.. impl::,.. spec::, ...), cross-references, admonitions, plain paragraphs, etc.Example marker in C++:
renders the
implneed into the document that hosts thesrc-tracedirective.Source-page anchor mappings (
file_lineno_href) are updated for marked-RST blocks too, so the generated highlighted source page links the marker line back to the hosting document whenset_local_url = true.Changes
src/sphinx_codelinks/sphinx_extension/directives/src_trace.py— extendrender_needs()with a_render_marked_rst()helper.docs/source/components/directive.rst— drop the "only supports one-line needs" attention banner; document the opt-in and add a C++ example.tests/doc_test/marked_rst_basic/— new Sphinx fixture withget_rst = trueand a C++ source containing a marked-RSTimpl.tests/test_src_trace.py— parametrizetest_build_htmlwith the new fixture.tests/__snapshots__/test_src_trace/…sphinx_project6…doctree.xml— asserts theNeednode is rendered with the correct id and body.Verification
pytest: 347 passed (59 snapshots).pre-commit run --all-files: green.mypyon the touched file: clean.Open questions for reviewers
get_rststays opt-in and remainsFalseby default (also on the Sphinx side). Happy to auto-enable it under the extension if you prefer.docnameonly (notdocname#need-id). Open to a different convention.comment_type; adding Java is out of scope for this PR.Closes #43.