-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
indent.py
274 lines (230 loc) · 9.47 KB
/
indent.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"""djLint add indentation to html."""
from functools import partial
import regex as re
from ..helpers import (
inside_ignored_block,
is_ignored_block_closing,
is_ignored_block_opening,
is_safe_closing_tag,
)
from ..settings import Config
from .attributes import format_attributes
def indent_html(rawcode: str, config: Config) -> str:
"""Indent raw code."""
rawcode_flat_list = re.split("\n", rawcode)
indent = config.indent
beautified_code = ""
indent_level = 0
is_raw_first_line = False
is_block_raw = False
slt_html = config.indent_html_tags
# here using all tags cause we allow empty tags on one line
always_self_closing_html = config.always_self_closing_html_tags
# here using all tags cause we allow empty tags on one line
slt_template = config.optional_single_line_template_tags
# nested ignored blocks..
ignored_level = 0
for item in rawcode_flat_list:
# if a raw tag first line
if not is_block_raw and is_ignored_block_opening(config, item):
is_raw_first_line = True
# if a raw tag then start ignoring
if is_ignored_block_opening(config, item):
is_block_raw = True
ignored_level += 1
if is_safe_closing_tag(config, item):
ignored_level -= 1
ignored_level = max(ignored_level, 0)
if is_block_raw is True and ignored_level == 0:
is_block_raw = False
if (
re.findall(
rf"^\s*?(?:{config.ignored_inline_blocks})",
item,
flags=re.IGNORECASE | re.VERBOSE | re.MULTILINE,
)
and is_block_raw is False
):
tmp = (indent * indent_level) + item + "\n"
# if a one-line, inline tag, just process it, only if line starts w/ it
# or if it is trailing text
elif (
(
re.findall(
re.compile(
rf"""^(?:[^<\s].*?)? # start of a line, optionally with some text
(?:
(?:<({slt_html})>)(?:.*?)(?:</(?:\1)>) # <span>stuff</span> >>>> match 1
|(?:<({slt_html})\b[^>]+?>)(?:.*?)(?:</(?:\2)>) # <span stuff>stuff</span> >>> match 2
|(?:<(?:{always_self_closing_html})\b[^>]*?/?>) # <img stuff />
|(?:<(?:{slt_html})\b[^>]*?/>) # <img />
|(?:{{%[ ]*?({slt_template})[ ]+?.*?%}})(?:.*?)(?:{{%[ ]+?end(?:\3)[ ]+?.*?%}}) # >>> match 3
|{config.ignored_inline_blocks}
)[ \t]*?
(?:
.*? # anything
(?: # followed by another slt
(?:<({slt_html})>)(?:.*?)(?:</(?:\4)>) # <span>stuff</span> >>>> match 1
|(?:<({slt_html})\b[^>]+?>)(?:.*?)(?:</(?:\5)>) # <span stuff>stuff</span> >>> match 2
|(?:<(?:{always_self_closing_html})\b[^>]*?/?>) # <img stuff />
|(?:<(?:{slt_html})\b[^>]*?/>) # <img />
|(?:{{%[ ]*?({slt_template})[ ]+?.*?%}})(?:.*?)(?:{{%[ ]+?end(?:\6)[ ]+?.*?%}}) # >>> match 3
|{config.ignored_inline_blocks}
)[ \t]*?
)*? # optional of course
[^<]*?$ # with no other tags following until end of line
""",
re.IGNORECASE | re.VERBOSE | re.MULTILINE,
),
item,
)
)
and is_block_raw is False
):
tmp = (indent * indent_level) + item + "\n"
# if unindent, move left
elif (
re.search(
config.tag_unindent,
item,
re.IGNORECASE | re.MULTILINE | re.VERBOSE,
)
and is_block_raw is False
and not is_safe_closing_tag(config, item)
# and not ending in a slt like <span><strong></strong>.
and not re.findall(
rf"(<({slt_html})>)(.*?)(</(\2)>[^<]*?$)",
item,
re.IGNORECASE | re.VERBOSE | re.MULTILINE,
)
and not re.findall(
rf"(<({slt_html})\\b[^>]+?>)(.*?)(</(\2)>[^<]*?$)",
item,
re.IGNORECASE | re.VERBOSE | re.MULTILINE,
)
):
# block to catch inline block followed by a non-break tag
if (
len(
re.findall(
rf"(^<({slt_html})>)(.*?)(</(\2)>)",
item,
re.IGNORECASE | re.VERBOSE | re.MULTILINE,
)
or re.findall(
re.compile(
rf"(^<({slt_html})\b[^>]+?>)(.*?)(</(\2)>)",
re.IGNORECASE | re.VERBOSE | re.MULTILINE,
),
item,
)
)
> 0
):
# unindent after instead of before
tmp = (indent * indent_level) + item + "\n"
indent_level = max(indent_level - 1, 0)
else:
indent_level = max(indent_level - 1, 0)
tmp = (indent * indent_level) + item + "\n"
elif (
re.search(
r"^" + str(config.tag_unindent_line),
item,
re.IGNORECASE | re.MULTILINE | re.VERBOSE,
)
and is_block_raw is False
):
tmp = (indent * (indent_level - 1)) + item + "\n"
# if indent, move right
elif (
re.search(
re.compile(
r"^(?:" + str(config.tag_indent) + r")",
re.IGNORECASE | re.MULTILINE | re.VERBOSE,
),
item,
)
and is_block_raw is False
):
tmp = (indent * indent_level) + item + "\n"
indent_level = indent_level + 1
elif is_raw_first_line is True or (
is_safe_closing_tag(config, item) and is_block_raw is False
):
tmp = (indent * indent_level) + item + "\n"
elif is_block_raw is True or item.strip() == "":
tmp = item + "\n"
# otherwise, just leave same level
else:
if not config.preserve_leading_space:
# if we are not trying to preserve indenting
# on text, the add it now.
tmp = (indent * indent_level) + item + "\n"
else:
tmp = item + "\n"
# if a opening raw tag then start ignoring.. only if there is no closing tag
# on the same line
if is_ignored_block_opening(config, item):
is_block_raw = True
is_raw_first_line = False
# if a normal tag, we can try to expand attributes
elif is_block_raw is False:
# get leading space, and attributes
func = partial(format_attributes, config, item)
tmp = re.sub(
re.compile(
rf"(\s*?)(<(?:{config.indent_html_tags})\b)((?:\"[^\"]*\"|'[^']*'|{{[^}}]*}}|[^'\">{{}}])+?)(/?>)",
re.VERBOSE | re.IGNORECASE,
),
func,
tmp,
)
# turn off raw block if we hit end - for one line raw blocks, but not an inline raw
if is_ignored_block_closing(config, item):
if not is_safe_closing_tag(config, item):
ignored_level -= 1
ignored_level = max(ignored_level, 0)
if ignored_level == 0:
is_block_raw = False
beautified_code = beautified_code + tmp
# we can try to fix template tags. ignore handlebars
if config.profile not in ["handlebars", "golang"]:
def fix_non_handlebars_template_tags(
html: str, out_format: str, match: re.Match
) -> str:
if inside_ignored_block(config, html, match):
return match.group()
return out_format % (
match.group(1),
match.group(2),
match.group(3),
)
func = partial(fix_non_handlebars_template_tags, beautified_code, "%s %s%s")
beautified_code = re.sub(
r"({[{|%]\-?)(\w[^}].+?)([}|%]})", func, beautified_code
)
func = partial(fix_non_handlebars_template_tags, beautified_code, "%s%s %s")
beautified_code = re.sub(
r"({[{|%])([^}].+?[^(?:\ |\-)])([}|%]})", func, beautified_code
)
func = partial(fix_non_handlebars_template_tags, beautified_code, "%s%s %s")
beautified_code = re.sub(
r"({[{|%])([^}].+?[^ -])(\-+?[}|%]})", func, beautified_code
)
elif config.profile == "handlebars":
def fix_handlebars_template_tags(
html: str, out_format: str, match: re.Match
) -> str:
if inside_ignored_block(config, html, match):
return match.group()
return out_format % (
match.group(1),
match.group(2),
)
func = partial(fix_handlebars_template_tags, beautified_code, "%s %s")
# handlebars templates
beautified_code = re.sub(r"({{#(?:each|if).+?[^ ])(}})", func, beautified_code)
if not config.preserve_blank_lines:
beautified_code = beautified_code.lstrip()
return beautified_code.rstrip() + "\n"