👌 Allow access to the parser during the read phase #12361
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR allows access to the
Parser
instance during parsing, using the read-only propertyBuildEnvironment.parser
.The primary use case here is for within directives (also to a lesser extent roles).
Context:
To be fair to docutils, it never actually exposes directives as an extension point to users, this is done by sphinx (mainly in
Sphinx.add_directive
andDomain.directives
)The "problem" with this is that
Directives
are currently a pretty leaky abstraction; exposing a lot of the "internals" of the docutils rST parser, particularly when it comes to nested parsing or parsing from other sources (like docstrings).This has started to lead to a proliferation of "hacks" into the rst parser, to try to overcome these shortcomings.
To name a few:
nested_parse_with_titles
https://github.com/sphinx-doc/sphinx/blob/v7.3.0/sphinx/util/nodes.py#L327parse_generated_content
https://github.com/sphinx-doc/sphinx/blob/v7.3.0/sphinx/ext/autodoc/directive.py#L84switch_source_input
https://github.com/sphinx-doc/sphinx/blob/v7.3.0/sphinx/util/docutils.py#L362Include.run
https://github.com/sphinx-doc/sphinx/blob/v7.3.0/sphinx/directives/other.py#L384Only.run
https://github.com/sphinx-doc/sphinx/blob/v7.3.0/sphinx/directives/other.py#L330sphinx.ext.collapse
https://github.com/sphinx-doc/sphinx/pull/10532/files#r1581911730These are exceptionally difficult to reconcile with other parsers like Myst, or even alternative rST parsers
Allowing access to the parser would allow the possibility for this sort of code to be centralised and abstracted better on the parser implementation
Also somewhat related #12238 by @n-peugnet