From 21e1b43ec408f2bb41290a55984561def562f37f Mon Sep 17 00:00:00 2001 From: Chance Zibolski Date: Thu, 22 Aug 2024 11:27:36 -0700 Subject: [PATCH] Expose myst_substitions to RST While this doesn't fix the fact that .. replace:: does not work within eval-rst, it does make it possible to perform substitutions within eval-rst using the same substitutions available to Jinja substitutions. Related: #680 Signed-off-by: Chance Zibolski --- myst_parser/mdit_to_docutils/base.py | 11 +++++++++++ tests/test_sphinx/sourcedirs/substitutions/index.md | 6 ++++++ .../test_sphinx_builds/test_substitutions.html | 6 ++++++ .../test_sphinx_builds/test_substitutions.xml | 6 ++++++ 4 files changed, 29 insertions(+) diff --git a/myst_parser/mdit_to_docutils/base.py b/myst_parser/mdit_to_docutils/base.py index bdd15156..e7cd70ff 100644 --- a/myst_parser/mdit_to_docutils/base.py +++ b/myst_parser/mdit_to_docutils/base.py @@ -280,6 +280,17 @@ def _render_finalise(self) -> None: substitution_node, f"wordcount-{key}" ) + for key, value in self.md_config.substitutions.items(): + if value is None: + continue + + substitution_node = nodes.substitution_definition( + str(value), nodes.Text(str(value)) + ) + substitution_node.source = self.document["source"] + substitution_node["names"].append(key) + self.document.note_substitution_def(substitution_node, key) + def nested_render_text( self, text: str, diff --git a/tests/test_sphinx/sourcedirs/substitutions/index.md b/tests/test_sphinx/sourcedirs/substitutions/index.md index 9773a951..a475aeb4 100644 --- a/tests/test_sphinx/sourcedirs/substitutions/index.md +++ b/tests/test_sphinx/sourcedirs/substitutions/index.md @@ -52,6 +52,12 @@ This will not process the substitution {{ text_with_nest }} ``` +Test substitutions are processed within eval-rst + +```{eval-rst} +a |text| b +``` + Using env and filters: {{ env.docname | upper }} diff --git a/tests/test_sphinx/test_sphinx_builds/test_substitutions.html b/tests/test_sphinx/test_sphinx_builds/test_substitutions.html index 38ff9bae..2bf4b38d 100644 --- a/tests/test_sphinx/test_sphinx_builds/test_substitutions.html +++ b/tests/test_sphinx/test_sphinx_builds/test_substitutions.html @@ -62,6 +62,12 @@ +

+ Test substitutions are processed within eval-rst +

+

+ a - text b +

Using env and filters:

diff --git a/tests/test_sphinx/test_sphinx_builds/test_substitutions.xml b/tests/test_sphinx/test_sphinx_builds/test_substitutions.xml index 37bd766a..a9fe9b11 100644 --- a/tests/test_sphinx/test_sphinx_builds/test_substitutions.xml +++ b/tests/test_sphinx/test_sphinx_builds/test_substitutions.xml @@ -47,6 +47,12 @@ This will not process the substitution {{ text_with_nest }} + + Test substitutions are processed within eval-rst + + a + - text + b Using env and filters: