From 5f8e3aadac30f92812458cbe41b6fe9884bbd576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Thu, 26 Sep 2024 22:55:11 +0200 Subject: [PATCH 1/6] Implement and document the raw option --- docs/index.rst | 16 ++++++++++++++++ src/sphinx_jinja2/__init__.py | 18 ++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 437b5db..392696a 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -129,6 +129,22 @@ To see the rendered templates in the built documentation, use the ``debug`` opti Hallo **{{ name }}**! + + +Raw output +********** + +The role can be used to produce raw ouput instead of interpreted RST: + +.. jinja2-example:: + + .. jinja:: + :ctx: {"name": "World"} + :raw: html + + Hello {{ name }} + + Warning messages **************** diff --git a/src/sphinx_jinja2/__init__.py b/src/sphinx_jinja2/__init__.py index 1e5c7fb..f0cc716 100644 --- a/src/sphinx_jinja2/__init__.py +++ b/src/sphinx_jinja2/__init__.py @@ -97,6 +97,7 @@ class JinjaDirective(SphinxDirective): "file": directives.path, "ctx": directives.unchanged, "debug": directives.flag, + "raw": directives.unchanged, } options: JinjaOptions arguments: list[str] @@ -187,12 +188,17 @@ def _warn(msg: str) -> None: _warn(f"Error rendering jinja template: {exc.__class__.__name__}: {exc}") return [] - # insert the new content into the source stream - # setting the source and line number - new_lines = StringList( - new_content.splitlines(), items=[(source, line - 1) for _ in new_content.splitlines()] - ) - self.state_machine.insert_input(new_lines, source) + if not 'raw' in self.options: + # insert the new content into the source stream + # setting the source and line number + new_lines = StringList( + new_content.splitlines(), items=[(source, line - 1) for _ in new_content.splitlines()] + ) + self.state_machine.insert_input(new_lines, source) + else: + raw_node = nodes.raw('', new_content, classes='jinja-rendered', format=self.options['raw']) + (raw_node.source, raw_node.line) = self.state_machine.get_source_and_line(self.lineno) + return [raw_node] if conf.debug or "debug" in self.options: # return the rendered template From 3ed2299a9d3c01ce07190e24a5d4369aea02319e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 26 Sep 2024 20:58:27 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/sphinx_jinja2/__init__.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/sphinx_jinja2/__init__.py b/src/sphinx_jinja2/__init__.py index f0cc716..68bcd1f 100644 --- a/src/sphinx_jinja2/__init__.py +++ b/src/sphinx_jinja2/__init__.py @@ -188,15 +188,18 @@ def _warn(msg: str) -> None: _warn(f"Error rendering jinja template: {exc.__class__.__name__}: {exc}") return [] - if not 'raw' in self.options: + if "raw" not in self.options: # insert the new content into the source stream # setting the source and line number new_lines = StringList( - new_content.splitlines(), items=[(source, line - 1) for _ in new_content.splitlines()] + new_content.splitlines(), + items=[(source, line - 1) for _ in new_content.splitlines()], ) self.state_machine.insert_input(new_lines, source) else: - raw_node = nodes.raw('', new_content, classes='jinja-rendered', format=self.options['raw']) + raw_node = nodes.raw( + "", new_content, classes="jinja-rendered", format=self.options["raw"] + ) (raw_node.source, raw_node.line) = self.state_machine.get_source_and_line(self.lineno) return [raw_node] From cf87e82b6ceaffcc0b271cf578be1a420baade75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Thu, 26 Sep 2024 23:14:36 +0200 Subject: [PATCH 3/6] (fixes) --- src/sphinx_jinja2/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sphinx_jinja2/__init__.py b/src/sphinx_jinja2/__init__.py index 68bcd1f..d50d4cf 100644 --- a/src/sphinx_jinja2/__init__.py +++ b/src/sphinx_jinja2/__init__.py @@ -88,6 +88,8 @@ class JinjaOptions(TypedDict, total=False): relative to current file, or src directory (if starts with ``/``) """ debug: bool """Also output the rendered template""" + raw: str + """The rendered template is raw output in given format, rather than input put back into the source parser.""" class JinjaDirective(SphinxDirective): @@ -197,9 +199,7 @@ def _warn(msg: str) -> None: ) self.state_machine.insert_input(new_lines, source) else: - raw_node = nodes.raw( - "", new_content, classes="jinja-rendered", format=self.options["raw"] - ) + raw_node = nodes.raw('', new_content, classes=['jinja-rendered'], format=self.options['raw']) (raw_node.source, raw_node.line) = self.state_machine.get_source_and_line(self.lineno) return [raw_node] From 6de829c98c628e56c1e73f8459b8076b1053918a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 26 Sep 2024 21:17:49 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/sphinx_jinja2/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/sphinx_jinja2/__init__.py b/src/sphinx_jinja2/__init__.py index d50d4cf..6e84acc 100644 --- a/src/sphinx_jinja2/__init__.py +++ b/src/sphinx_jinja2/__init__.py @@ -199,7 +199,9 @@ def _warn(msg: str) -> None: ) self.state_machine.insert_input(new_lines, source) else: - raw_node = nodes.raw('', new_content, classes=['jinja-rendered'], format=self.options['raw']) + raw_node = nodes.raw( + "", new_content, classes=["jinja-rendered"], format=self.options["raw"] + ) (raw_node.source, raw_node.line) = self.state_machine.get_source_and_line(self.lineno) return [raw_node] From 2dfd8139e51aed7b648dd5facad3a1da1f3f1976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20=C5=A0milauer?= Date: Thu, 26 Sep 2024 23:59:13 +0200 Subject: [PATCH 5/6] omit classes so that additional
is not created --- src/sphinx_jinja2/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sphinx_jinja2/__init__.py b/src/sphinx_jinja2/__init__.py index 6e84acc..d9ad9a5 100644 --- a/src/sphinx_jinja2/__init__.py +++ b/src/sphinx_jinja2/__init__.py @@ -200,7 +200,7 @@ def _warn(msg: str) -> None: self.state_machine.insert_input(new_lines, source) else: raw_node = nodes.raw( - "", new_content, classes=["jinja-rendered"], format=self.options["raw"] + "", new_content, classes=[], format=self.options["raw"] ) (raw_node.source, raw_node.line) = self.state_machine.get_source_and_line(self.lineno) return [raw_node] From ead55cc88eb04720eba978971a351c06bf0893db Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 26 Sep 2024 21:59:28 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/sphinx_jinja2/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/sphinx_jinja2/__init__.py b/src/sphinx_jinja2/__init__.py index d9ad9a5..e70835d 100644 --- a/src/sphinx_jinja2/__init__.py +++ b/src/sphinx_jinja2/__init__.py @@ -199,9 +199,7 @@ def _warn(msg: str) -> None: ) self.state_machine.insert_input(new_lines, source) else: - raw_node = nodes.raw( - "", new_content, classes=[], format=self.options["raw"] - ) + raw_node = nodes.raw("", new_content, classes=[], format=self.options["raw"]) (raw_node.source, raw_node.line) = self.state_machine.get_source_and_line(self.lineno) return [raw_node]