From 1f93e2ea2597db593e2797bb0cff3859be2f6505 Mon Sep 17 00:00:00 2001 From: Ole-Jakob Olsen Date: Tue, 11 Jun 2024 23:51:29 +0200 Subject: [PATCH] Bugfix: accept jinja style templates w/period --- htpy/html2htpy.py | 16 +++++++++++++--- tests/test_html2htpy.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/htpy/html2htpy.py b/htpy/html2htpy.py index 79e4034..f8cd0bc 100644 --- a/htpy/html2htpy.py +++ b/htpy/html2htpy.py @@ -186,19 +186,23 @@ def html2htpy(html: str, shorthand_id_class: bool = False, format: bool = False) def _convert_data_to_string(data: str): _data = str(data) + is_multiline = "\n" in _data + + _data = _data.replace("\n", "") + # escape unescaped dblquote: " -> \" _data = re.compile(r'(? { var } + # {{ var.xx }} -> { var.xx } # { -> {{ # } -> }} template_string_replace_pattern = re.compile( - r"(\{\{\s*(\w+)\s*\}\}|(? +

{{ heading }}

+

Welcome to our cooking site, {{ user.name }}!

+ +

Recipe of the Day: {{ recipe.name }}

+

{{ recipe.description }}

+ +

Instructions:

+
    + {% for step in recipe.steps %} +
  1. {{ step }}
  2. + {% endfor %} +
+ + """ + + actual = html2htpy(input, format=True) + expected = textwrap.dedent( + """\ + body[ + h1[f"{ heading }"], + p[f"Welcome to our cooking site, { user.name }!"], + h2[f"Recipe of the Day: { recipe.name }"], + p[f"{ recipe.description }"], + h3["Instructions:"], + ol[ + \"\"\" {% for step in recipe.steps %} \"\"\", + li[f"{ step }"], + \"\"\" {% endfor %} \"\"\", + ], + ] + """ + ) + + assert actual == expected + + def test_convert_script_style_tags(): input = """