👌 Allow access to the parser during the read phase (#12361)

This commit allows access to the `Parser` instance during the read phase, using the read-only property `BuildEnvironment.parser`.

This aims to allow for parser agnostic logic within roles and directives,
as opposed to hard-coding for implementation details of the default docutils `RSTParser`.
This commit is contained in:
Chris Sewell 2024-05-21 14:46:43 +02:00 committed by GitHub
parent ac3f74a3e0
commit 548f0f928a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 0 deletions

View File

@ -13,6 +13,9 @@ Deprecated
Features added
--------------
* #12361: Allow the parser instance to be accessed within directives
with ``self.env.parser``. Patch by Chris Sewell.
* #11165: Support the `officially recommended`_ ``.jinja`` suffix for template
files.
Patch by James Addison and Adam Turner

View File

@ -485,6 +485,7 @@ class Builder:
filename = self.env.doc2path(docname)
filetype = get_filetype(self.app.config.source_suffix, filename)
publisher = self.app.registry.get_publisher(self.app, filetype)
self.env.temp_data['_parser'] = publisher.parser
# record_dependencies is mutable even though it is in settings,
# explicitly re-initialise for each document
publisher.settings.record_dependencies = DependencyList()

View File

@ -28,6 +28,7 @@ if TYPE_CHECKING:
from docutils import nodes
from docutils.nodes import Node
from docutils.parsers import Parser
from sphinx.application import Sphinx
from sphinx.builders import Builder
@ -549,6 +550,11 @@ class BuildEnvironment:
"""Returns the docname of the document currently being parsed."""
return self.temp_data['docname']
@property
def parser(self) -> Parser:
"""Returns the parser being used for to parse the current document."""
return self.temp_data['_parser']
def new_serialno(self, category: str = '') -> int:
"""Return a serial number, e.g. for index entry targets.