Skip to content

Commit ff8ce2e

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

3 files changed

Lines changed: 84 additions & 14 deletions

File tree

‎mako/lexer.py‎

Lines changed: 13 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,25 @@ def match_end(self):
352354
else:
353355
return True
354356

357+
def match_percent(self):
358+
match = self.match(r"(?<=^)(\s*)%%(%*)", re.M)
359+
if match:
360+
self.append_node(
361+
parsetree.Text, match.group(1) + "%" + match.group(2)
362+
)
363+
return True
364+
else:
365+
return False
366+
355367
def match_text(self):
356368
match = self.match(
357369
r"""
358370
(.*?) # anything, followed by:
359371
(
360-
(?<=\n)(?=[ \t]*(?=%(?!%)|\#\#)) # an eval or line-based
372+
(?<=\n)(?=[ \t]*(?=%|\#\#)) # an eval or line-based
361373
# comment, preceded by a
362374
# consumed newline and whitespace
363375
|
364-
(?<!%)(?=%%+) # consume the first percent sign
365-
# out of a group of percent signs
366-
|
367376
(?=\${) # an expression
368377
|
369378
(?=</?[%&]) # a substitution or block or call start or end

‎test/test_lexer.py‎

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,10 @@ 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(" some whatever.\n\n", (3, 3)),
205+
Text(" %", (5, 1)),
206+
Text(" more some whatever\n", (5, 7)),
206207
ControlLine("if", "if foo:", False, (6, 1)),
207208
ControlLine("if", "endif", True, (7, 1)),
208209
Text(" ", (8, 1)),
@@ -222,11 +223,12 @@ def test_percent_escape2(self):
222223
TemplateNode(
223224
{},
224225
[
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)),
226+
Text("%", (1, 1)),
227+
Text(" do something\n", (1, 3)),
228+
Text("%%", (2, 1)),
229+
Text(" do something\nif <some condition>:\n", (2, 4)),
230+
Text(" %%%", (4, 1)),
231+
Text(" do something\n ", (4, 9)),
230232
],
231233
),
232234
)
@@ -245,15 +247,56 @@ def test_percent_escape_with_control_block(self):
245247
[
246248
Text("\n", (1, 1)),
247249
ControlLine("for", "for i in [1, 2, 3]:", False, (2, 1)),
248-
Text(" ", (3, 1)),
249-
Text("% do something ", (3, 6)),
250+
Text(" %", (3, 1)),
251+
Text(" do something ", (3, 7)),
250252
Expression("i", [], (3, 21)),
251253
Text("\n", (3, 25)),
252254
ControlLine("for", "endfor", True, (4, 1)),
253255
],
254256
),
255257
)
256258

259+
def test_inline_percent(self):
260+
template = """
261+
%% foo
262+
bar %% baz
263+
"""
264+
node = Lexer(template).parse()
265+
self._compare(
266+
node,
267+
TemplateNode(
268+
{},
269+
[Text("\n%", (1, 1)), Text(" foo\nbar %% baz\n", (2, 3))],
270+
),
271+
)
272+
273+
def test_inline_percent_with_control_block(self):
274+
template = """
275+
% for i in [1, 2, 3]:
276+
%% foo
277+
bar %% baz
278+
% endfor
279+
"""
280+
node = Lexer(template).parse()
281+
self._compare(
282+
node,
283+
TemplateNode(
284+
{},
285+
[
286+
Text("\n", (1, 1)),
287+
ControlLine(
288+
"for", "for i in [1, 2, 3]:", False, (2, 1)
289+
),
290+
Text("%", (3, 1)),
291+
Text(" foo\nbar ", (3, 3)),
292+
Text("%", (3, 3)),
293+
Text("%", (3, 3)),
294+
Text(" baz\n", (4, 7)),
295+
ControlLine("for", "endfor", True, (5, 1)),
296+
],
297+
),
298+
)
299+
257300
def test_old_multiline_comment(self):
258301
template = """#*"""
259302
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)