Skip to content

Commit

Permalink
format code style
Browse files Browse the repository at this point in the history
  • Loading branch information
cocolato committed Jan 19, 2024
1 parent f282029 commit 51a931d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
.tox/
.cache/
.vscode
.venv
6 changes: 1 addition & 5 deletions mako/testing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ def result_lines(result):


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


def make_path(
Expand Down
56 changes: 56 additions & 0 deletions my/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from mako.template import Template
from mako.lexer import Lexer
from mako.testing.helpers import result_raw_lines


# t = Template("""%% do something
# %%% do something
# if <some condition>:
# %%%% do something
# """)


t = Template(
"""
% for i in [1, 2, 3]:
%% do something ${i}
% endfor
"""
)
t = """
% for i in [1, 2, 3]:
%% do something ${i}
% endfor
"""
# print(result_raw_lines(t.render()))
print(Lexer(t).parse())
# import re

# pat = re.compile(
# 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
# |
# (?=\${) # an expression
# |
# (?=</?[%&]) # a substitution or block or call start or end
# # - don't consume
# |
# (\\\r?\n) # an escaped newline - throw away
# |
# \Z # end of string
# )""", re.S | re.X
# )


# c = """
# % for
# """

# r = pat.match(c)
# print(r)
20 changes: 11 additions & 9 deletions test/test_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,9 @@ def test_percent_escape2(self):
{},
[
Text("% do something\n", (1, 2)),
Text("%% do something\nif <some condition>:\n ",
(2, 2)),
Text(
"%% do something\nif <some condition>:\n ", (2, 2)
),
Text("%%% do something\n ", (4, 6)),
],
),
Expand All @@ -242,13 +243,14 @@ def test_percent_escape_with_control_block(self):
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))]
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)),
],
),
)

Expand Down
15 changes: 10 additions & 5 deletions test/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from mako.testing.config import config
from mako.testing.fixtures import TemplateTest
from mako.testing.helpers import flatten_result
from mako.testing.helpers import result_lines, result_raw_lines
from mako.testing.helpers import result_lines
from mako.testing.helpers import result_raw_lines


class ctx:
Expand Down Expand Up @@ -1671,11 +1672,13 @@ def test_future_import(self):

class EscapeTest(TemplateTest):
def test_percent_escape(self):
t = Template("""%% do something
t = Template(
"""%% do something
%%% do something
if <some condition>:
%%%% do something
""")
"""
)
assert result_raw_lines(t.render()) == [
"% do something",
"%% do something",
Expand All @@ -1684,11 +1687,13 @@ def test_percent_escape(self):
]

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

0 comments on commit 51a931d

Please sign in to comment.