- comments can be placed at the end of control lines, i.e. if foo: # a comment, [ticket:53], thanks to Paul Colomiets
diff --git a/CHANGES b/CHANGES index cfc7185..59b6027 100644 --- a/CHANGES +++ b/CHANGES
@@ -1,6 +1,9 @@ 0.1.9 - filters.Decode filter can also accept a non-basestring object and will call str() + unicode() on it [ticket:47] +- comments can be placed at the end of control lines, +i.e. if foo: # a comment, [ticket:53], thanks to +Paul Colomiets 0.1.8 - variable names declared in render methods by internal
diff --git a/lib/mako/ast.py b/lib/mako/ast.py index db2c316..add5293 100644 --- a/lib/mako/ast.py +++ b/lib/mako/ast.py
@@ -139,9 +139,11 @@ etc. """ def __init__(self, code, **exception_kwargs): - m = re.match(r'^(\w+)(?:\s+(.*?))?:$', code.strip(), re.S) + m = re.match(r'^(\w+)(?:\s+(.*?))?:\s*(#|$)', code.strip(), re.S) if not m: raise exceptions.CompileException("Fragment '%s' is not a partial control statement" % code, **exception_kwargs) + if m.group(3): + code = code[:m.start(3)] (keyword, expr) = m.group(1,2) if keyword in ['for','if', 'while']: code = code + "pass"
diff --git a/test/exceptions_.py b/test/exceptions_.py index 0e5b83e..1ed4f33 100644 --- a/test/exceptions_.py +++ b/test/exceptions_.py
@@ -40,7 +40,7 @@ def test_utf8_html_error_template(self): """test the html_error_template with a Template containing utf8 chars""" code = """# -*- coding: utf-8 -*- -% if 2 == 2: # an innocently-looking comment +% if 2 == 2: /an error ${u'привет'} % endif """ @@ -49,8 +49,8 @@ template.render() except exceptions.CompileException, ce: html_error = exceptions.html_error_template().render() - assert ("CompileException: Fragment 'if 2 == 2: # an " - "innocently-looking comment' is not a partial control " + assert ("CompileException: Fragment 'if 2 == 2: /an " + "error' is not a partial control " "statement at line: 2 char: 1") in html_error assert u"3 ${u'привет'}".encode(sys.getdefaultencoding(), 'htmlentityreplace') in html_error
diff --git a/test/lexer.py b/test/lexer.py index e088a50..a8fbb8a 100644 --- a/test/lexer.py +++ b/test/lexer.py
@@ -380,6 +380,17 @@ """ nodes = Lexer(template).parse() assert repr(nodes) == r"""TemplateNode({}, [NamespaceTag(u'namespace', {u'name': u'foo', u'file': u'somefile.html'}, (1, 1), []), Text(u'\n', (1, 46)), Comment(u'inherit from foobar.html', (2, 1)), InheritTag(u'inherit', {u'file': u'foobar.html'}, (3, 1), []), Text(u'\n\n', (3, 31)), DefTag(u'def', {u'name': u'header()'}, (5, 1), ["Text(u'\\n <div>header</div>\\n', (5, 23))"]), Text(u'\n', (7, 8)), DefTag(u'def', {u'name': u'footer()'}, (8, 1), ["Text(u'\\n <div> footer</div>\\n', (8, 23))"]), Text(u'\n\n<table>\n', (10, 8)), ControlLine(u'for', u'for j in data():', False, (13, 1)), Text(u' <tr>\n', (14, 1)), ControlLine(u'for', u'for x in j:', False, (15, 1)), Text(u' <td>Hello ', (16, 1)), Expression(u'x', ['h'], (16, 23)), Text(u'</td>\n', (16, 30)), ControlLine(u'for', u'endfor', True, (17, 1)), Text(u' </tr>\n', (18, 1)), ControlLine(u'for', u'endfor', True, (19, 1)), Text(u'</table>\n', (20, 1))])""" + + def test_comment_after_statement(self): + template = """ + % if x: #comment + hi + % else: #next + hi + % endif #end +""" + nodes = Lexer(template).parse() + assert repr(nodes) == r"""TemplateNode({}, [Text(u'\n', (1, 1)), ControlLine(u'if', u'if x: #comment', False, (2, 1)), Text(u' hi\n', (3, 1)), ControlLine(u'else', u'else: #next', False, (4, 1)), Text(u' hi\n', (5, 1)), ControlLine(u'if', u'endif #end', True, (6, 1))])""" def test_crlf(self): template = file("./test_htdocs/crlf.html").read()