@@ -79,9 +79,9 @@ def __init__(self, path: str):
79
79
class TemplateSyntaxError (SyntaxError ):
80
80
"""Raised when a syntax error is encountered in a template."""
81
81
82
- def __init__ (self , message : str , token : Token ):
82
+ def __init__ (self , token : Token , reason : str ):
83
83
"""Provided token is not a valid template syntax at the specified position."""
84
- super ().__init__ (f" { message } \n \n " + self . _underline_token_in_template ( token ) )
84
+ super ().__init__ (self . _underline_token_in_template ( token ) + f" \n \n { reason } " )
85
85
86
86
@staticmethod
87
87
def _skipped_lines_message (nr_of_lines : int ) -> str :
@@ -92,8 +92,8 @@ def _underline_token_in_template(
92
92
cls , token : Token , * , lines_around : int = 4 , symbol : str = "^"
93
93
) -> str :
94
94
"""
95
- Return ``number_of_lines`` lines before and after the token, with the token content underlined
96
- with ``symbol`` e.g.:
95
+ Return ``number_of_lines`` lines before and after `` token`` , with the token content
96
+ underlined with ``symbol`` e.g.:
97
97
98
98
```html
99
99
[8 lines skipped]
@@ -337,17 +337,16 @@ def _resolve_includes_blocks_and_extends(template: str):
337
337
# Check for circular extends
338
338
if extended_template_path in extended_templates :
339
339
raise TemplateSyntaxError (
340
- f"Circular extends" ,
341
340
Token (
342
341
template ,
343
342
extends_match .start (),
344
343
extends_match .end (),
345
344
),
345
+ "Circular extends" ,
346
346
)
347
- else :
348
- extended_templates .add (extended_template_path )
349
347
350
348
# Load extended template
349
+ extended_templates .add (extended_template_path )
351
350
with open (
352
351
extended_template_path , "rt" , encoding = "utf-8"
353
352
) as extended_template_file :
@@ -361,12 +360,12 @@ def _resolve_includes_blocks_and_extends(template: str):
361
360
# Check for any stacked extends
362
361
if stacked_extends_match := _find_extends (template [extends_match .end () :]):
363
362
raise TemplateSyntaxError (
364
- "Incorrect use of {% extends ... %}" ,
365
363
Token (
366
364
template ,
367
365
extends_match .end () + stacked_extends_match .start (),
368
366
extends_match .end () + stacked_extends_match .end (),
369
367
),
368
+ "Incorrect use of {% extends ... %}" ,
370
369
)
371
370
372
371
# Save block replacements
@@ -378,22 +377,22 @@ def _resolve_includes_blocks_and_extends(template: str):
378
377
template [offset : offset + block_match .start ()]
379
378
):
380
379
raise TemplateSyntaxError (
381
- "Token between blocks" ,
382
380
Token (
383
381
template ,
384
382
offset + token_between_blocks_match .start (),
385
383
offset + token_between_blocks_match .end (),
386
384
),
385
+ "Token between blocks" ,
387
386
)
388
387
389
388
if not (endblock_match := _find_endblock (template [offset :], block_name )):
390
389
raise TemplateSyntaxError (
391
- "No matching {% endblock %}" ,
392
390
Token (
393
391
template ,
394
392
offset + block_match .start (),
395
393
offset + block_match .end (),
396
394
),
395
+ "No matching {% endblock %}" ,
397
396
)
398
397
399
398
block_content = template [
@@ -403,12 +402,12 @@ def _resolve_includes_blocks_and_extends(template: str):
403
402
# Check for unsupported nested blocks
404
403
if (nested_block_match := _find_block (block_content )) is not None :
405
404
raise TemplateSyntaxError (
406
- "Nested blocks are not supported" ,
407
405
Token (
408
406
template ,
409
407
offset + block_match .end () + nested_block_match .start (),
410
408
offset + block_match .end () + nested_block_match .end (),
411
409
),
410
+ "Nested blocks are not supported" ,
412
411
)
413
412
414
413
if block_name in block_replacements :
@@ -450,12 +449,12 @@ def _replace_blocks_with_replacements(template: str, replacements: "dict[str, st
450
449
# Check for unsupported nested blocks
451
450
if (nested_block_match := _find_block (block_content )) is not None :
452
451
raise TemplateSyntaxError (
453
- "Nested blocks are not supported" ,
454
452
Token (
455
453
template ,
456
454
block_match .end () + nested_block_match .start (),
457
455
block_match .end () + nested_block_match .end (),
458
456
),
457
+ "Nested blocks are not supported" ,
459
458
)
460
459
461
460
# No replacement for this block, use default content
@@ -602,7 +601,7 @@ def _create_template_rendering_function( # pylint: disable=,too-many-locals,too
602
601
nested_if_statements .append (token )
603
602
elif token .content .startswith (r"{% elif " ):
604
603
if not nested_if_statements :
605
- raise TemplateSyntaxError ("No matching {% if ... %}" , token )
604
+ raise TemplateSyntaxError (token , "No matching {% if ... %}" )
606
605
607
606
indentation_level -= 1
608
607
function_string += (
@@ -611,14 +610,14 @@ def _create_template_rendering_function( # pylint: disable=,too-many-locals,too
611
610
indentation_level += 1
612
611
elif token .content == r"{% else %}" :
613
612
if not nested_if_statements :
614
- raise TemplateSyntaxError ("No matching {% if ... %}" , token )
613
+ raise TemplateSyntaxError (token , "No matching {% if ... %}" )
615
614
616
615
indentation_level -= 1
617
616
function_string += indent * indentation_level + "else:\n "
618
617
indentation_level += 1
619
618
elif token .content == r"{% endif %}" :
620
619
if not nested_if_statements :
621
- raise TemplateSyntaxError ("No matching {% if ... %}" , token )
620
+ raise TemplateSyntaxError (token , "No matching {% if ... %}" )
622
621
623
622
indentation_level -= 1
624
623
nested_if_statements .pop ()
@@ -633,7 +632,7 @@ def _create_template_rendering_function( # pylint: disable=,too-many-locals,too
633
632
nested_for_loops .append (token )
634
633
elif token .content == r"{% empty %}" :
635
634
if not nested_for_loops :
636
- raise TemplateSyntaxError ("No matching {% for ... %}" , token )
635
+ raise TemplateSyntaxError (token , "No matching {% for ... %}" )
637
636
638
637
indentation_level -= 1
639
638
last_forloop_iterable = (
@@ -645,7 +644,7 @@ def _create_template_rendering_function( # pylint: disable=,too-many-locals,too
645
644
indentation_level += 1
646
645
elif token .content == r"{% endfor %}" :
647
646
if not nested_for_loops :
648
- raise TemplateSyntaxError ("No matching {% for ... %}" , token )
647
+ raise TemplateSyntaxError (token , "No matching {% for ... %}" )
649
648
650
649
indentation_level -= 1
651
650
nested_for_loops .pop ()
@@ -660,7 +659,7 @@ def _create_template_rendering_function( # pylint: disable=,too-many-locals,too
660
659
nested_while_loops .append (token )
661
660
elif token .content == r"{% endwhile %}" :
662
661
if not nested_while_loops :
663
- raise TemplateSyntaxError ("No matching {% while ... %}" , token )
662
+ raise TemplateSyntaxError (token , "No matching {% while ... %}" )
664
663
665
664
indentation_level -= 1
666
665
nested_while_loops .pop ()
@@ -680,39 +679,39 @@ def _create_template_rendering_function( # pylint: disable=,too-many-locals,too
680
679
681
680
elif token .content == r"{% endautoescape %}" :
682
681
if not nested_autoescape_modes :
683
- raise TemplateSyntaxError ("No matching {% autoescape ... %}" , token )
682
+ raise TemplateSyntaxError (token , "No matching {% autoescape ... %}" )
684
683
685
684
nested_autoescape_modes .pop ()
686
685
687
686
# Token is a endblock in top-level template
688
687
elif token .content .startswith (r"{% endblock " ):
689
- raise TemplateSyntaxError ("No matching {% block ... %}" , token )
688
+ raise TemplateSyntaxError (token , "No matching {% block ... %}" )
690
689
691
690
# Token is a extends in top-level template
692
691
elif token .content .startswith (r"{% extends " ):
693
- raise TemplateSyntaxError ("Incorrect use of {% extends ... %}" , token )
692
+ raise TemplateSyntaxError (token , "Incorrect use of {% extends ... %}" )
694
693
695
694
else :
696
- raise TemplateSyntaxError (f"Unknown token: { token .content } " , token )
695
+ raise TemplateSyntaxError (token , f"Unknown token: { token .content } " )
697
696
698
697
else :
699
- raise TemplateSyntaxError (f"Unknown token: { token .content } " , token )
698
+ raise TemplateSyntaxError (token , f"Unknown token: { token .content } " )
700
699
701
700
# Move offset to the end of the token
702
701
offset += token_match .end ()
703
702
704
703
# Checking for unclosed blocks
705
704
if len (nested_if_statements ) > 0 :
706
705
last_if_statement = nested_if_statements [- 1 ]
707
- raise TemplateSyntaxError ("No matching {% endif %}" , last_if_statement )
706
+ raise TemplateSyntaxError (last_if_statement , "No matching {% endif %}" )
708
707
709
708
if len (nested_for_loops ) > 0 :
710
709
last_for_loop = nested_for_loops [- 1 ]
711
- raise TemplateSyntaxError ("No matching {% endfor %}" , last_for_loop )
710
+ raise TemplateSyntaxError (last_for_loop , "No matching {% endfor %}" )
712
711
713
712
if len (nested_while_loops ) > 0 :
714
713
last_while_loop = nested_while_loops [- 1 ]
715
- raise TemplateSyntaxError ("No matching {% endwhile %}" , last_while_loop )
714
+ raise TemplateSyntaxError (last_while_loop , "No matching {% endwhile %}" )
716
715
717
716
# No check for unclosed autoescape blocks, as they are optional and do not result in errors
718
717
0 commit comments