Skip to content

Commit 548f0f9

Browse files
authored
👌 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`.
1 parent ac3f74a commit 548f0f9

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

‎CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Deprecated
1313
Features added
1414
--------------
1515

16+
* #12361: Allow the parser instance to be accessed within directives
17+
with ``self.env.parser``. Patch by Chris Sewell.
18+
1619
* #11165: Support the `officially recommended`_ ``.jinja`` suffix for template
1720
files.
1821
Patch by James Addison and Adam Turner

‎sphinx/builders/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ def read_doc(self, docname: str, *, _cache: bool = True) -> None:
485485
filename = self.env.doc2path(docname)
486486
filetype = get_filetype(self.app.config.source_suffix, filename)
487487
publisher = self.app.registry.get_publisher(self.app, filetype)
488+
self.env.temp_data['_parser'] = publisher.parser
488489
# record_dependencies is mutable even though it is in settings,
489490
# explicitly re-initialise for each document
490491
publisher.settings.record_dependencies = DependencyList()

‎sphinx/environment/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
from docutils import nodes
3030
from docutils.nodes import Node
31+
from docutils.parsers import Parser
3132

3233
from sphinx.application import Sphinx
3334
from sphinx.builders import Builder
@@ -549,6 +550,11 @@ def docname(self) -> str:
549550
"""Returns the docname of the document currently being parsed."""
550551
return self.temp_data['docname']
551552

553+
@property
554+
def parser(self) -> Parser:
555+
"""Returns the parser being used for to parse the current document."""
556+
return self.temp_data['_parser']
557+
552558
def new_serialno(self, category: str = '') -> int:
553559
"""Return a serial number, e.g. for index entry targets.
554560

0 commit comments

Comments
 (0)