1414import re
1515
1616from flowmark .linewrapping .atomic_patterns import (
17- HTML_COMMENT_CLOSE ,
18- HTML_COMMENT_CLOSE_RE ,
19- HTML_COMMENT_OPEN ,
20- HTML_COMMENT_OPEN_RE ,
21- JINJA_COMMENT_CLOSE ,
22- JINJA_COMMENT_CLOSE_RE ,
23- JINJA_COMMENT_OPEN ,
24- JINJA_COMMENT_OPEN_RE ,
25- JINJA_TAG_CLOSE ,
26- JINJA_TAG_CLOSE_RE ,
27- JINJA_TAG_OPEN ,
28- JINJA_TAG_OPEN_RE ,
29- JINJA_VAR_CLOSE ,
30- JINJA_VAR_CLOSE_RE ,
31- JINJA_VAR_OPEN ,
32- JINJA_VAR_OPEN_RE ,
3317 PAIRED_HTML_COMMENT ,
3418 PAIRED_JINJA_COMMENT ,
3519 PAIRED_JINJA_TAG ,
7458# Pattern to detect adjacent tags (closing tag immediately followed by opening tag)
7559# This handles cases like %}{% or --><!-- where there's no space between
7660_adjacent_tags_re : re .Pattern [str ] = re .compile (
77- rf"({ JINJA_TAG_CLOSE_RE } )({ JINJA_TAG_OPEN_RE } )|"
78- rf"({ JINJA_COMMENT_CLOSE_RE } )({ JINJA_COMMENT_OPEN_RE } )|"
79- rf"({ JINJA_VAR_CLOSE_RE } )({ JINJA_VAR_OPEN_RE } )|"
80- rf"({ HTML_COMMENT_CLOSE_RE } )({ HTML_COMMENT_OPEN_RE } )"
61+ rf"({ SINGLE_JINJA_TAG . close_re } )({ SINGLE_JINJA_TAG . open_re } )|"
62+ rf"({ SINGLE_JINJA_COMMENT . close_re } )({ SINGLE_JINJA_COMMENT . open_re } )|"
63+ rf"({ SINGLE_JINJA_VAR . close_re } )({ SINGLE_JINJA_VAR . open_re } )|"
64+ rf"({ SINGLE_HTML_COMMENT . close_re } )({ SINGLE_HTML_COMMENT . open_re } )"
8165)
8266
8367# Pattern to remove spaces between adjacent tags that were added during word splitting
8468_denormalize_tags_re : re .Pattern [str ] = re .compile (
85- rf"({ JINJA_TAG_CLOSE_RE } ) ({ JINJA_TAG_OPEN_RE } )|"
86- rf"({ JINJA_COMMENT_CLOSE_RE } ) ({ JINJA_COMMENT_OPEN_RE } )|"
87- rf"({ JINJA_VAR_CLOSE_RE } ) ({ JINJA_VAR_OPEN_RE } )|"
88- rf"({ HTML_COMMENT_CLOSE_RE } ) ({ HTML_COMMENT_OPEN_RE } )"
69+ rf"({ SINGLE_JINJA_TAG . close_re } ) ({ SINGLE_JINJA_TAG . open_re } )|"
70+ rf"({ SINGLE_JINJA_COMMENT . close_re } ) ({ SINGLE_JINJA_COMMENT . open_re } )|"
71+ rf"({ SINGLE_JINJA_VAR . close_re } ) ({ SINGLE_JINJA_VAR . open_re } )|"
72+ rf"({ SINGLE_HTML_COMMENT . close_re } ) ({ SINGLE_HTML_COMMENT . open_re } )"
8973)
9074
9175
@@ -136,18 +120,18 @@ def _is_tag_only_line(line: str) -> bool:
136120
137121 # Check if it starts with a tag
138122 starts_tag = (
139- stripped .startswith (JINJA_TAG_OPEN )
140- or stripped .startswith (JINJA_COMMENT_OPEN )
141- or stripped .startswith (JINJA_VAR_OPEN )
142- or stripped .startswith (HTML_COMMENT_OPEN )
123+ stripped .startswith (SINGLE_JINJA_TAG . open_delim )
124+ or stripped .startswith (SINGLE_JINJA_COMMENT . open_delim )
125+ or stripped .startswith (SINGLE_JINJA_VAR . open_delim )
126+ or stripped .startswith (SINGLE_HTML_COMMENT . open_delim )
143127 )
144128
145129 # Check if it ends with a tag
146130 ends_tag = (
147- stripped .endswith (JINJA_TAG_CLOSE )
148- or stripped .endswith (JINJA_COMMENT_CLOSE )
149- or stripped .endswith (JINJA_VAR_CLOSE )
150- or stripped .endswith (HTML_COMMENT_CLOSE )
131+ stripped .endswith (SINGLE_JINJA_TAG . close_delim )
132+ or stripped .endswith (SINGLE_JINJA_COMMENT . close_delim )
133+ or stripped .endswith (SINGLE_JINJA_VAR . close_delim )
134+ or stripped .endswith (SINGLE_HTML_COMMENT . close_delim )
151135 )
152136
153137 return starts_tag and ends_tag
@@ -214,13 +198,13 @@ def line_ends_with_tag(line: str) -> bool:
214198 return False
215199 # Check for Jinja-style tags
216200 if (
217- stripped .endswith (JINJA_TAG_CLOSE )
218- or stripped .endswith (JINJA_COMMENT_CLOSE )
219- or stripped .endswith (JINJA_VAR_CLOSE )
201+ stripped .endswith (SINGLE_JINJA_TAG . close_delim )
202+ or stripped .endswith (SINGLE_JINJA_COMMENT . close_delim )
203+ or stripped .endswith (SINGLE_JINJA_VAR . close_delim )
220204 ):
221205 return True
222206 # Check for HTML comments
223- if stripped .endswith (HTML_COMMENT_CLOSE ):
207+ if stripped .endswith (SINGLE_HTML_COMMENT . close_delim ):
224208 return True
225209 return False
226210
@@ -232,13 +216,13 @@ def line_starts_with_tag(line: str) -> bool:
232216 return False
233217 # Check for Jinja-style tags
234218 if (
235- stripped .startswith (JINJA_TAG_OPEN )
236- or stripped .startswith (JINJA_COMMENT_OPEN )
237- or stripped .startswith (JINJA_VAR_OPEN )
219+ stripped .startswith (SINGLE_JINJA_TAG . open_delim )
220+ or stripped .startswith (SINGLE_JINJA_COMMENT . open_delim )
221+ or stripped .startswith (SINGLE_JINJA_VAR . open_delim )
238222 ):
239223 return True
240224 # Check for HTML comments
241- if stripped .startswith (HTML_COMMENT_OPEN ):
225+ if stripped .startswith (SINGLE_HTML_COMMENT . open_delim ):
242226 return True
243227 return False
244228
@@ -444,10 +428,10 @@ def _fix_closing_tag_spacing(text: str) -> str:
444428# where a multi-line opening tag ends and a closing tag follows on the same line.
445429# Uses named group "closing_tag" to capture the start of the closing tag.
446430_multiline_closing_pattern : re .Pattern [str ] = re .compile (
447- rf"{ JINJA_TAG_CLOSE_RE } \s*(?P<closing_tag>{ JINJA_TAG_OPEN_RE } \s*/)|" # %}{% /
448- rf"{ JINJA_COMMENT_CLOSE_RE } \s*(?P<closing_comment>{ JINJA_COMMENT_OPEN_RE } \s*/)|" # #}{# /
449- rf"{ JINJA_VAR_CLOSE_RE } \s*(?P<closing_var>{ JINJA_VAR_OPEN_RE } \s*/)|" # }}{{ /
450- rf"{ HTML_COMMENT_CLOSE_RE } \s*(?P<closing_html>{ HTML_COMMENT_OPEN_RE } \s*/)" # --><!-- /
431+ rf"{ SINGLE_JINJA_TAG . close_re } \s*(?P<closing_tag>{ SINGLE_JINJA_TAG . open_re } \s*/)|"
432+ rf"{ SINGLE_JINJA_COMMENT . close_re } \s*(?P<closing_comment>{ SINGLE_JINJA_COMMENT . open_re } \s*/)|"
433+ rf"{ SINGLE_JINJA_VAR . close_re } \s*(?P<closing_var>{ SINGLE_JINJA_VAR . open_re } \s*/)|"
434+ rf"{ SINGLE_HTML_COMMENT . close_re } \s*(?P<closing_html>{ SINGLE_HTML_COMMENT . open_re } \s*/)"
451435)
452436
453437
@@ -490,10 +474,10 @@ def _fix_multiline_opening_tag_with_closing(text: str) -> str:
490474 # Only process lines that are continuations (don't start with a tag opener).
491475 # If a line starts with a tag opener, the tag began on that line, not a continuation.
492476 is_tag_start = (
493- stripped .startswith (JINJA_TAG_OPEN )
494- or stripped .startswith (JINJA_COMMENT_OPEN )
495- or stripped .startswith (JINJA_VAR_OPEN )
496- or stripped .startswith (HTML_COMMENT_OPEN )
477+ stripped .startswith (SINGLE_JINJA_TAG . open_delim )
478+ or stripped .startswith (SINGLE_JINJA_COMMENT . open_delim )
479+ or stripped .startswith (SINGLE_JINJA_VAR . open_delim )
480+ or stripped .startswith (SINGLE_HTML_COMMENT . open_delim )
497481 )
498482
499483 if not is_tag_start :
0 commit comments