From 41f188a5384e2113766bac750cdefa7210927402 Mon Sep 17 00:00:00 2001 From: Philipp Strube Date: Sat, 6 Apr 2019 10:57:09 +0200 Subject: [PATCH] Add support for template variables to markdown filter --- BER/mitte.py | 6 ++++-- tests/site/data/test_markdown_tpl.yaml | 1 + tests/site/site.yaml | 7 +++++++ tests/site/templates/test-markdown-ctx.html | 1 + tests/test.py | 6 ++++++ 5 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/site/data/test_markdown_tpl.yaml create mode 100644 tests/site/templates/test-markdown-ctx.html diff --git a/BER/mitte.py b/BER/mitte.py index a93c7ea..63af78f 100644 --- a/BER/mitte.py +++ b/BER/mitte.py @@ -79,11 +79,13 @@ def theme_image_url(self, name): def strftime(self, time_struct, format): return strftime(format, time_struct) - def markdown(self, text): + def markdown(self, text, ctx=dict()): md = misaka.Markdown( misaka.HtmlRenderer(), extensions=('fenced-code',)) - return Markup(md(text)) + tpl = JinjaEnvironment().from_string(text) + print(ctx) + return Markup(md(tpl.render(**ctx))) def get_globals(self): globals = { diff --git a/tests/site/data/test_markdown_tpl.yaml b/tests/site/data/test_markdown_tpl.yaml new file mode 100644 index 0000000..d314178 --- /dev/null +++ b/tests/site/data/test_markdown_tpl.yaml @@ -0,0 +1 @@ +text: "# Markdown {{ test_variable }}" diff --git a/tests/site/site.yaml b/tests/site/site.yaml index 5029dcc..3151ce1 100644 --- a/tests/site/site.yaml +++ b/tests/site/site.yaml @@ -130,3 +130,10 @@ pages: format: "yaml" src: "test_markdown.yaml" tpl_name: "/test-markdown.html" + /markdown-with-ctx: + test_variable: TEST-CTX + data_sources: + entry: + format: "yaml" + src: "test_markdown_tpl.yaml" + tpl_name: "/test-markdown-ctx.html" diff --git a/tests/site/templates/test-markdown-ctx.html b/tests/site/templates/test-markdown-ctx.html new file mode 100644 index 0000000..9bc2b20 --- /dev/null +++ b/tests/site/templates/test-markdown-ctx.html @@ -0,0 +1 @@ +{{ entry.text | markdown(ctx=page) }} diff --git a/tests/test.py b/tests/test.py index 885bab7..2f80150 100644 --- a/tests/test.py +++ b/tests/test.py @@ -118,6 +118,12 @@ def test_markdown_filter(self): self.assertEqual(200, response.code) self.assertEqual(b'

Markdown Test

\n', response.body) + def test_markdown_filter_with_context(self): + response = self.fetch('/markdown-with-ctx', method='GET') + + self.assertEqual(200, response.code) + self.assertEqual(b'

Markdown TEST-CTX

\n', response.body) + def test_unpublished_page(self): """ test that pages marked as unpublished return 404 """