Skip to content

Commit

Permalink
Revert "fix percent escape not working when not at the beginning of t…
Browse files Browse the repository at this point in the history
…he line"

This reverts commit 2031330.

Fixes: #384
  • Loading branch information
zzzeek committed Jan 25, 2024
1 parent 2c647ad commit edf44dc
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 88 deletions.
9 changes: 3 additions & 6 deletions mako/lexer.py
Expand Up @@ -357,12 +357,9 @@ def match_text(self):
r"""
(.*?) # anything, followed by:
(
(?<=\n)(?=[ \t]*(?=%(?!%)|\#\#)) # an eval or line-based
# comment, preceded by a
# consumed newline and whitespace
|
(?<!%)(?=%%+) # consume the first percent sign
# out of a group of percent signs
(?<=\n)(?=[ \t]*(?=%|\#\#)) # an eval or line-based
# comment preceded by a
# consumed newline and whitespace
|
(?=\${) # an expression
|
Expand Down
4 changes: 0 additions & 4 deletions mako/testing/helpers.py
Expand Up @@ -19,10 +19,6 @@ def result_lines(result):
]


def result_raw_lines(result):
return [x for x in re.split(r"\r?\n", result) if x.strip() != ""]


def make_path(
filespec: Union[Path, str],
make_absolute: bool = True,
Expand Down
48 changes: 2 additions & 46 deletions test/test_lexer.py
Expand Up @@ -201,59 +201,15 @@ def test_percent_escape(self):
{},
[
Text("""\n\n""", (1, 1)),
Text("""% some whatever.\n\n """, (3, 2)),
Text("% more some whatever\n", (5, 6)),
Text("""% some whatever.\n\n""", (3, 2)),
Text(" %% more some whatever\n", (5, 2)),
ControlLine("if", "if foo:", False, (6, 1)),
ControlLine("if", "endif", True, (7, 1)),
Text(" ", (8, 1)),
],
),
)

def test_percent_escape2(self):
template = """%% do something
%%% do something
if <some condition>:
%%%% do something
"""
node = Lexer(template).parse()
self._compare(
node,
TemplateNode(
{},
[
Text("% do something\n", (1, 2)),
Text(
"%% do something\nif <some condition>:\n ", (2, 2)
),
Text("%%% do something\n ", (4, 6)),
],
),
)

def test_percent_escape_with_control_block(self):
template = """
% for i in [1, 2, 3]:
%% do something ${i}
% endfor
"""
node = Lexer(template).parse()
self._compare(
node,
TemplateNode(
{},
[
Text("\n", (1, 1)),
ControlLine("for", "for i in [1, 2, 3]:", False, (2, 1)),
Text(" ", (3, 1)),
Text("% do something ", (3, 6)),
Expression("i", [], (3, 21)),
Text("\n", (3, 25)),
ControlLine("for", "endfor", True, (4, 1)),
],
),
)

def test_old_multiline_comment(self):
template = """#*"""
node = Lexer(template).parse()
Expand Down
32 changes: 0 additions & 32 deletions test/test_template.py
Expand Up @@ -15,7 +15,6 @@
from mako.testing.fixtures import TemplateTest
from mako.testing.helpers import flatten_result
from mako.testing.helpers import result_lines
from mako.testing.helpers import result_raw_lines


class ctx:
Expand Down Expand Up @@ -1668,34 +1667,3 @@ class FuturesTest(TemplateTest):
def test_future_import(self):
t = Template("${ x / y }", future_imports=["division"])
assert result_lines(t.render(x=12, y=5)) == ["2.4"]


class EscapeTest(TemplateTest):
def test_percent_escape(self):
t = Template(
"""%% do something
%%% do something
if <some condition>:
%%%% do something
"""
)
assert result_raw_lines(t.render()) == [
"% do something",
"%% do something",
"if <some condition>:",
" %%% do something",
]

def test_percent_escape2(self):
t = Template(
"""
% for i in [1, 2, 3]:
%% do something ${i}
% endfor
"""
)
assert result_raw_lines(t.render()) == [
" % do something 1",
" % do something 2",
" % do something 3",
]

0 comments on commit edf44dc

Please sign in to comment.