Skip to content

Commit db93097

Browse files
committed
don't replace percent when it is not the first non-whitespace character
1 parent ab8e747 commit db93097

3 files changed

Lines changed: 104 additions & 13 deletions

File tree

‎mako/lexer.py‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ def parse(self):
247247
continue
248248
if self.match_python_block():
249249
continue
250+
if self.match_percent():
251+
continue
250252
if self.match_text():
251253
continue
252254

@@ -352,18 +354,37 @@ def match_end(self):
352354
else:
353355
return True
354356

357+
def match_percent(self):
358+
match = self.match(r"([^%]*?)%(%+)", re.S)
359+
if match:
360+
text = match.group(1)
361+
if text:
362+
self.append_node(parsetree.Text, text)
363+
for char in text[::-1]: # Look back, check wheither '%'
364+
# is the first non-whitespace character in a line
365+
if char == "\n":
366+
break
367+
elif char in ("\t", " "):
368+
continue
369+
else:
370+
self.append_node(parsetree.Text, "%")
371+
break
372+
self.append_node(parsetree.Text, match.group(2))
373+
else:
374+
self.append_node(parsetree.Text, "%")
375+
return True
376+
else:
377+
return False
378+
355379
def match_text(self):
356380
match = self.match(
357381
r"""
358382
(.*?) # anything, followed by:
359383
(
360-
(?<=\n)(?=[ \t]*(?=%(?!%)|\#\#)) # an eval or line-based
384+
(?<=\n)(?=[ \t]*(?=%|\#\#)) # an eval or line-based
361385
# comment, preceded by a
362386
# consumed newline and whitespace
363387
|
364-
(?<!%)(?=%%+) # consume the first percent sign
365-
# out of a group of percent signs
366-
|
367388
(?=\${) # an expression
368389
|
369390
(?=</?[%&]) # a substitution or block or call start or end

‎test/test_lexer.py‎

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,11 @@ def test_percent_escape(self):
200200
TemplateNode(
201201
{},
202202
[
203-
Text("""\n\n""", (1, 1)),
204-
Text("""% some whatever.\n\n """, (3, 2)),
205-
Text("% more some whatever\n", (5, 6)),
203+
Text("\n\n", (1, 1)),
204+
Text("%", (1, 1)),
205+
Text(" some whatever.\n\n ", (3, 3)),
206+
Text("%", (3, 3)),
207+
Text(" more some whatever\n", (5, 7)),
206208
ControlLine("if", "if foo:", False, (6, 1)),
207209
ControlLine("if", "endif", True, (7, 1)),
208210
Text(" ", (8, 1)),
@@ -222,11 +224,12 @@ def test_percent_escape2(self):
222224
TemplateNode(
223225
{},
224226
[
225-
Text("% do something\n", (1, 2)),
226-
Text(
227-
"%% do something\nif <some condition>:\n ", (2, 2)
228-
),
229-
Text("%%% do something\n ", (4, 6)),
227+
Text("%", (1, 1)),
228+
Text(" do something\n", (1, 3)),
229+
Text("%%", (1, 3)),
230+
Text(" do something\nif <some condition>:\n ", (2, 4)),
231+
Text("%%%", (2, 4)),
232+
Text(" do something\n ", (4, 9)),
230233
],
231234
),
232235
)
@@ -246,14 +249,63 @@ def test_percent_escape_with_control_block(self):
246249
Text("\n", (1, 1)),
247250
ControlLine("for", "for i in [1, 2, 3]:", False, (2, 1)),
248251
Text(" ", (3, 1)),
249-
Text("% do something ", (3, 6)),
252+
Text("%", (3, 1)),
253+
Text(" do something ", (3, 7)),
250254
Expression("i", [], (3, 21)),
251255
Text("\n", (3, 25)),
252256
ControlLine("for", "endfor", True, (4, 1)),
253257
],
254258
),
255259
)
256260

261+
def test_inline_percent(self):
262+
template = """
263+
%% foo
264+
bar %% baz
265+
"""
266+
node = Lexer(template).parse()
267+
self._compare(
268+
node,
269+
TemplateNode(
270+
{},
271+
[
272+
Text("\n", (1, 1)),
273+
Text("%", (1, 1)),
274+
Text(" foo\nbar ", (2, 3)),
275+
Text("%", (2, 3)),
276+
Text("%", (2, 3)),
277+
Text(" baz\n", (3, 7)),
278+
],
279+
),
280+
)
281+
282+
def test_inline_percent_with_control_block(self):
283+
template = """
284+
% for i in [1, 2, 3]:
285+
%% foo
286+
bar %% baz
287+
% endfor
288+
"""
289+
node = Lexer(template).parse()
290+
self._compare(
291+
node,
292+
TemplateNode(
293+
{},
294+
[
295+
Text("\n", (1, 1)),
296+
ControlLine(
297+
"for", "for i in [1, 2, 3]:", False, (2, 1)
298+
),
299+
Text("%", (3, 1)),
300+
Text(" foo\nbar ", (3, 3)),
301+
Text("%", (3, 3)),
302+
Text("%", (3, 3)),
303+
Text(" baz\n", (4, 7)),
304+
ControlLine("for", "endfor", True, (5, 1)),
305+
],
306+
),
307+
)
308+
257309
def test_old_multiline_comment(self):
258310
template = """#*"""
259311
node = Lexer(template).parse()

‎test/test_template.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,3 +1699,21 @@ def test_percent_escape2(self):
16991699
" % do something 2",
17001700
" % do something 3",
17011701
]
1702+
1703+
def test_inline_percent(self):
1704+
t = Template(
1705+
"""
1706+
% for i in [1, 2, 3]:
1707+
%% foo
1708+
bar %% baz
1709+
% endfor
1710+
"""
1711+
)
1712+
assert result_raw_lines(t.render()) == [
1713+
"% foo",
1714+
"bar %% baz",
1715+
"% foo",
1716+
"bar %% baz",
1717+
"% foo",
1718+
"bar %% baz",
1719+
]

0 commit comments

Comments
 (0)