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] 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