Skip to content

Commit

Permalink
fix(blocktrans): allowed closing transblocks to be indented if they h…
Browse files Browse the repository at this point in the history
…ave a leading space
  • Loading branch information
christopherpickering committed Apr 4, 2023
1 parent 2a6865e commit d667273
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/djlint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ def __init__(
| {%[ ]+?endcomment[ ]+?%}
| {{!--\s*djlint\:on\s*--}}
| {{-?\s*/\*\s*djlint\:on\s*\*/\s*-?}}
# only if has a leading whitespace
| (?:\s|^){%[ ]+?endblocktrans(?:late)?[ ]+?%}
"""

# all html tags possible
Expand Down Expand Up @@ -649,8 +651,12 @@ def __init__(
| {{-?\s*/\*\s*djlint\:off\s*\*/\s*-?}}.*?(?={{-?\s*/\*\s*djlint\:on\s*\*/\s*-?}})
| <!--.*?-->
| <\?php.*?\?>
| {%[ ]*?blocktranslate\b[^(?:%})]*?%}.*?{%[ ]*?endblocktranslate[ ]*?%}
| {%[ ]*?blocktrans\b[^(?:%})]*?%}.*?{%[ ]*?endblocktrans[ ]*?%}
# either with a space before the close block > then we can format the endblock
| {%[ ]*?blocktranslate\b[^(?:%})]*?%}.*?(?=\s{%[ ]*?endblocktranslate[ ]*?%})
| {%[ ]*?blocktrans\b[^(?:%})]*?%}.*?(?=\s{%[ ]*?endblocktrans[ ]*?%})
# or with no space before it, and then we cannot format it.
| {%[ ]*?blocktranslate\b[^(?:%})]*?%}.*?[^\s]{%[ ]*?endblocktranslate[ ]*?%}
| {%[ ]*?blocktrans\b[^(?:%})]*?%}.*?[^\s]{%[ ]*?endblocktrans[ ]*?%}
| {%[ ]*?comment\b[^(?:%})]*?%}(?:(?!djlint:(?:off|on)).)*?(?={%[ ]*?endcomment[ ]*?%})
| ^---[\s\S]+?---
"""
Expand All @@ -675,8 +681,7 @@ def __init__(
| {\#(?!.*djlint:[ ]*?(?:off|on)\b).*\#}
| <\?php.*?\?>
| {%[ ]*?comment\b[^(?:%})]*?%}(?:(?!djlint:(?:off|on)).)*?{%[ ]*?endcomment[ ]*?%}
| {%[ ]*?blocktranslate\b[^(?:%})]*?%}.*?{%[ ]*?endblocktranslate[ ]*?%}
| {%[ ]*?blocktrans\b[^(?:%})]*?%}.*?{%[ ]*?endblocktrans[ ]*?%}
| {%[ ]*?blocktrans(?:late)?\b[^(?:%})]*?%}.*?{%[ ]*?endblocktrans(?:late)?[ ]*?%}
"""

self.optional_single_line_html_tags: str = r"""
Expand Down
41 changes: 41 additions & 0 deletions tests/test_django/test_blocktrans.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,47 @@ def test_blocktranslate(runner: CliRunner, tmp_file: TextIO) -> None:
assert output.exit_code == 0


def test_blocktrans_indent(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""<div>
{% translate "View all" %}
<br />
({% blocktranslate count counter=images|length trimmed %}
{{ counter }} photo
{% plural %}
{{ counter }} photos
{% endblocktranslate %})
({% blocktrans count counter=images|length trimmed %}
{{ counter }} photo
{% plural %}
{{ counter }} photos
{% endblocktrans %})
</div>
""",
)

assert (
output.text
== """<div>
{% translate "View all" %}
<br />
({% blocktranslate count counter=images|length trimmed %}
{{ counter }} photo
{% plural %}
{{ counter }} photos
{% endblocktranslate %})
({% blocktrans count counter=images|length trimmed %}
{{ counter }} photo
{% plural %}
{{ counter }} photos
{% endblocktrans %})
</div>
"""
)


# def test_trans(runner: CliRunner, tmp_file: TextIO) -> None:
# output = reformat(
# tmp_file, runner, b"""<p>{% trans 'Please do <b>Blah</b>.' %}</p>"""
Expand Down

0 comments on commit d667273

Please sign in to comment.