@@ -93,19 +93,33 @@ def _underline_token_in_template(
93
93
"""
94
94
95
95
template_before_token = token .template [: token .start_position ]
96
- if skipped_lines := template_before_token .count ("\n " ) - lines_around :
97
- template_before_token = (
98
- f"{ cls ._skipped_lines_message (skipped_lines )} \n "
99
- + "\n " .join (template_before_token .split ("\n " )[- (lines_around + 1 ) :])
96
+ if top_skipped_lines := template_before_token .count ("\n " ) - lines_around :
97
+ template_before_token = "\n " .join (
98
+ template_before_token .split ("\n " )[- (lines_around + 1 ) :]
100
99
)
101
100
101
+ if 0 < top_skipped_lines :
102
+ top_skipped_lines_message = cls ._skipped_lines_message (
103
+ top_skipped_lines
104
+ )
105
+ template_before_token = (
106
+ f"{ top_skipped_lines_message } \n { template_before_token } "
107
+ )
108
+
102
109
template_after_token = token .template [token .end_position :]
103
- if skipped_lines := template_after_token .count ("\n " ) - lines_around :
104
- template_after_token = (
105
- "\n " .join (template_after_token .split ("\n " )[: (lines_around + 1 )])
106
- + f"\n { cls ._skipped_lines_message (skipped_lines )} "
110
+ if bottom_skipped_lines := template_after_token .count ("\n " ) - lines_around :
111
+ template_after_token = "\n " .join (
112
+ template_after_token .split ("\n " )[: (lines_around + 1 )]
107
113
)
108
114
115
+ if 0 < bottom_skipped_lines :
116
+ bottom_skipped_lines_message = cls ._skipped_lines_message (
117
+ bottom_skipped_lines
118
+ )
119
+ template_after_token = (
120
+ f"{ template_after_token } \n { bottom_skipped_lines_message } "
121
+ )
122
+
109
123
lines_before_line_with_token = template_before_token .rsplit ("\n " , 1 )[0 ]
110
124
111
125
line_with_token = (
@@ -122,7 +136,7 @@ def _underline_token_in_template(
122
136
123
137
lines_after_line_with_token = template_after_token .split ("\n " , 1 )[- 1 ]
124
138
125
- return "\n " .join (
139
+ return "\n " + " \n " .join (
126
140
[
127
141
lines_before_line_with_token ,
128
142
line_with_token ,
@@ -771,7 +785,7 @@ def __init__(self, template_path: str) -> None:
771
785
super ().__init__ (template_string )
772
786
773
787
774
- _CACHE : "dict[int, Template| FileTemplate]" = {}
788
+ CACHED_TEMPLATES : "dict[int, Template| FileTemplate]" = {}
775
789
776
790
777
791
def render_string_iter (
@@ -803,19 +817,15 @@ def render_string_iter(
803
817
"""
804
818
key = hash (template_string )
805
819
806
- if cache and key in _CACHE :
807
- return _yield_as_sized_chunks (
808
- _CACHE [key ].render_iter (context or {}, chunk_size ), chunk_size
809
- )
820
+ if cache and key in CACHED_TEMPLATES :
821
+ return CACHED_TEMPLATES [key ].render_iter (context or {}, chunk_size = chunk_size )
810
822
811
823
template = Template (template_string )
812
824
813
825
if cache :
814
- _CACHE [key ] = template
826
+ CACHED_TEMPLATES [key ] = template
815
827
816
- return _yield_as_sized_chunks (
817
- template .render_iter (context or {}), chunk_size = chunk_size
818
- )
828
+ return template .render_iter (context or {}, chunk_size = chunk_size )
819
829
820
830
821
831
def render_string (
@@ -841,13 +851,13 @@ def render_string(
841
851
"""
842
852
key = hash (template_string )
843
853
844
- if cache and key in _CACHE :
845
- return _CACHE [key ].render (context or {})
854
+ if cache and key in CACHED_TEMPLATES :
855
+ return CACHED_TEMPLATES [key ].render (context or {})
846
856
847
857
template = Template (template_string )
848
858
849
859
if cache :
850
- _CACHE [key ] = template
860
+ CACHED_TEMPLATES [key ] = template
851
861
852
862
return template .render (context or {})
853
863
@@ -881,19 +891,15 @@ def render_template_iter(
881
891
"""
882
892
key = hash (template_path )
883
893
884
- if cache and key in _CACHE :
885
- return _yield_as_sized_chunks (
886
- _CACHE [key ].render_iter (context or {}, chunk_size ), chunk_size
887
- )
894
+ if cache and key in CACHED_TEMPLATES :
895
+ return CACHED_TEMPLATES [key ].render_iter (context or {}, chunk_size = chunk_size )
888
896
889
897
template = FileTemplate (template_path )
890
898
891
899
if cache :
892
- _CACHE [key ] = template
900
+ CACHED_TEMPLATES [key ] = template
893
901
894
- return _yield_as_sized_chunks (
895
- template .render_iter (context or {}, chunk_size = chunk_size ), chunk_size
896
- )
902
+ return template .render_iter (context or {}, chunk_size = chunk_size )
897
903
898
904
899
905
def render_template (
@@ -920,12 +926,12 @@ def render_template(
920
926
921
927
key = hash (template_path )
922
928
923
- if cache and key in _CACHE :
924
- return _CACHE [key ].render (context or {})
929
+ if cache and key in CACHED_TEMPLATES :
930
+ return CACHED_TEMPLATES [key ].render (context or {})
925
931
926
932
template = FileTemplate (template_path )
927
933
928
934
if cache :
929
- _CACHE [key ] = template
935
+ CACHED_TEMPLATES [key ] = template
930
936
931
937
return template .render (context or {})
0 commit comments