-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjinja2_fmt_day.py
81 lines (70 loc) · 2.32 KB
/
jinja2_fmt_day.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import sys
import re
def format_jinja2_html(file_path):
with open(file_path, "r") as file:
lines = file.readlines()
indent_level = 0
indent_size = 4 # Adjust this to your preferred indentation size
open_tags = []
self_closing_tags = [
"br",
"hr",
"img",
"input",
"link",
"meta",
"area",
"base",
"col",
"command",
"embed",
"keygen",
"param",
"source",
"track",
"wbr",
]
formatted_lines = []
for line in lines:
stripped_line = line.strip()
# Check for Jinja2 blocks and HTML tags
jinja_blocks = re.findall(r"{%.*?%}", stripped_line)
html_tags = re.findall(r"<[^>]+>", stripped_line)
# Process each Jinja2 block and HTML tag
for tag in jinja_blocks + html_tags:
if tag.startswith("{%") and "end" in tag:
indent_level = max(indent_level - 1, 0)
elif (
tag.startswith("<")
and tag[1:3] != "br"
and not tag[1:].startswith(tuple(self_closing_tags))
and not tag.endswith("/>")
):
if tag.startswith("</"):
indent_level = max(indent_level - 1, 0)
else:
open_tags.append(tag)
# Apply indentation
formatted_line = " " * (indent_size * indent_level) + stripped_line
formatted_lines.append(formatted_line)
# Adjust indentation for next line
for tag in jinja_blocks + html_tags:
if tag.startswith("{%") and "end" not in tag:
indent_level += 1
elif (
tag.startswith("<")
and tag[1:3] != "br"
and not tag[1:].startswith(tuple(self_closing_tags))
and not tag.endswith("/>")
and not tag.startswith("</")
):
indent_level += 1
elif tag.startswith("</") and open_tags and tag == f"</{open_tags[-1][1:]}":
open_tags.pop()
print(formatted_lines)
# Write the formatted content to a new file
# with open('formatted_' + file_path, 'w') as file:
# for line in formatted_lines:
# file.write(line + '\n')
# Usage
format_jinja2_html(sys.argv[1])