Skip to content

Commit

Permalink
- Add the ability to detect what ever the outer most quotes was for e…
Browse files Browse the repository at this point in the history
…ach section and correctly modify the inner quotes

- This will always prioritize what ever the user had set on the outer quotes and will only activate for jinja templates
  • Loading branch information
jessielw committed Jul 16, 2023
1 parent f8fc285 commit e9dc2aa
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions src/djlint/formatter/indent.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def fix_handlebars_template_tags(html: str, match: re.Match) -> str:
in_set_tag = False
is_raw_first_line = False
is_block_raw = False
jinja_replace_list = []

slt_html = config.indent_html_tags

Expand Down Expand Up @@ -320,9 +321,18 @@ def fix_handlebars_template_tags(html: str, match: re.Match) -> str:
if ignored_level == 0:
is_block_raw = False

# set a rule for tmp content to have outer double quotes only for jinja
# detect the outer quotes for jinja
if config.profile == "jinja":
tmp = re.sub(r"=(')(.*?)(')", r'="\2"', tmp)
matches = re.findall(
r"=([\"'])(\{\{[\s\S]*?\}\})\1", tmp, flags=re.MULTILINE
)

for match in matches:
outer_quotes = match[0]
inner_content = match[1]
jinja_replace_list.append(

Check warning on line 333 in src/djlint/formatter/indent.py

View check run for this annotation

Codecov / codecov/patch

src/djlint/formatter/indent.py#L331-L333

Added lines #L331 - L333 were not covered by tests
{"outer_quote": outer_quotes, "content": inner_content}
)

beautified_code = beautified_code + tmp

Expand Down Expand Up @@ -400,22 +410,39 @@ def format_function(config: Config, html: str, match: re.Match) -> str:
leading_space,
)

# # define cleaned match with both quote styles
cleaned_match = (
f"{leading_space}{open_bracket} {tag}({contents}){index} {close_bracket}"
)

if config.profile == "jinja":
# remove double quotes from contents
contents = contents.replace('"', "'")

# check for trailing or leading spaces inside the single quotes and remove them
contents = re.sub(r"(?<=')\s+|\s+(?=')", "", contents)

# update cleaned match
cleaned_match = f"{leading_space}{open_bracket} {tag}({contents}){index} {close_bracket}"
outer_quotes = None
inner_quotes = None

# Determine user quote type
for jinja_content in jinja_replace_list:
content = cleaned_match.replace('"', "'") # Replace double quotes

Check warning on line 424 in src/djlint/formatter/indent.py

View check run for this annotation

Codecov / codecov/patch

src/djlint/formatter/indent.py#L424

Added line #L424 was not covered by tests
if content == jinja_content.get("content"):
outer_quotes = jinja_content.get("outer_quote")
inner_quotes = "'" if outer_quotes == '"' else '"'
break
content = cleaned_match.replace("'", '"') # Replace single quotes

Check warning on line 429 in src/djlint/formatter/indent.py

View check run for this annotation

Codecov / codecov/patch

src/djlint/formatter/indent.py#L426-L429

Added lines #L426 - L429 were not covered by tests
if content == jinja_content.get("content"):
outer_quotes = jinja_content.get("outer_quote")
inner_quotes = '"' if outer_quotes == "'" else "'"
break

Check warning on line 433 in src/djlint/formatter/indent.py

View check run for this annotation

Codecov / codecov/patch

src/djlint/formatter/indent.py#L431-L433

Added lines #L431 - L433 were not covered by tests

if outer_quotes is not None and inner_quotes is not None:
# Replace all content inner quotes and remove trailing/leading spaces
cleaned_contents = re.sub(

Check warning on line 437 in src/djlint/formatter/indent.py

View check run for this annotation

Codecov / codecov/patch

src/djlint/formatter/indent.py#L437

Added line #L437 was not covered by tests
rf"(?<=\{re.escape(outer_quotes)})\s+|\s+(?=\{re.escape(outer_quotes)})",
"",
contents.replace(outer_quotes, inner_quotes),
)

# strip any potential white space from the cleaned match
cleaned_match = cleaned_match.strip()
# Update cleaned match
cleaned_match = f"{leading_space}{open_bracket} {tag}({cleaned_contents}){index} {close_bracket}"
cleaned_match = cleaned_match.strip()

Check warning on line 445 in src/djlint/formatter/indent.py

View check run for this annotation

Codecov / codecov/patch

src/djlint/formatter/indent.py#L444-L445

Added lines #L444 - L445 were not covered by tests

return cleaned_match

Expand Down

0 comments on commit e9dc2aa

Please sign in to comment.