From 6eea95175349e1e2383e74a1157ffbdee5a258d4 Mon Sep 17 00:00:00 2001 From: Mike Bayer <mike_mp@zzzcomputing.com> Date: Fri, 12 Nov 2010 19:18:12 -0500 Subject: [PATCH] - Patch to lexer to not generate an empty '' write in the case of backslash-ended lines. [ticket:155] --- CHANGES | 6 +++++- mako/lexer.py | 3 ++- test/test_lexer.py | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index 8867314..ff20159 100644 --- a/CHANGES +++ b/CHANGES @@ -12,7 +12,11 @@ from startup, particularly for "single execution" environments such as shell scripts. [ticket:153] - + +- Patch to lexer to not generate an empty + '' write in the case of backslash-ended + lines. [ticket:155] + - Fixed missing **extra collection in setup.py which prevented setup.py from running 2to3 on install. diff --git a/mako/lexer.py b/mako/lexer.py index 34fe80b..241853c 100644 --- a/mako/lexer.py +++ b/mako/lexer.py @@ -331,7 +331,8 @@ class Lexer(object): if match: text = match.group(1) - self.append_node(parsetree.Text, text) + if text: + self.append_node(parsetree.Text, text) return True else: return False diff --git a/test/test_lexer.py b/test/test_lexer.py index a3cc11e..a49fc0c 100644 --- a/test/test_lexer.py +++ b/test/test_lexer.py @@ -125,7 +125,7 @@ class LexerTest(TemplateTest): node = Lexer(template).parse() self._compare( node, - TemplateNode({}, [Text(u'\n \n', (1, 1)), Text(u'', (3, 1)), Text(u'% some whatever.\n\n', (3, 2)), Text(u'', (5, 1)), Text(u' %% more some whatever\n', (5, 2)), ControlLine(u'if', u'if foo:', False, (6, 1)), ControlLine(u'if', u'endif', True, (7, 1)), Text(u' ', (8, 1))]) + TemplateNode({}, [Text(u'\n \n', (1, 1)), Text(u'% some whatever.\n\n', (3, 2)), Text(u' %% more some whatever\n', (5, 2)), ControlLine(u'if', u'if foo:', False, (6, 1)), ControlLine(u'if', u'endif', True, (7, 1)), Text(u' ', (8, 1))]) ) def test_text_tag(self): @@ -589,7 +589,7 @@ text text la la nodes = Lexer(template).parse() self._compare( nodes, - TemplateNode({}, [Text(u'<html>\r\n\r\n', (1, 1)), PageTag(u'page', {u'args': u"a=['foo',\n 'bar']"}, (3, 1), []), Text(u'\r\n\r\nlike the name says.\r\n\r\n', (4, 26)), ControlLine(u'for', u'for x in [1,2,3]:', False, (8, 1)), Text(u' ', (9, 1)), Expression(u'x', [], (9, 9)), Text(u'', (9, 13)), ControlLine(u'for', u'endfor', True, (10, 1)), Text(u'\r\n', (11, 1)), Expression(u"trumpeter == 'Miles' and trumpeter or \\\n 'Dizzy'", [], (12, 1)), Text(u'\r\n\r\n', (13, 15)), DefTag(u'def', {u'name': u'hi()'}, (15, 1), [Text(u'\r\n hi!\r\n', (15, 19))]), Text(u'\r\n\r\n</html>\r\n', (17, 8))]) + TemplateNode({}, [Text(u'<html>\r\n\r\n', (1, 1)), PageTag(u'page', {u'args': u"a=['foo',\n 'bar']"}, (3, 1), []), Text(u'\r\n\r\nlike the name says.\r\n\r\n', (4, 26)), ControlLine(u'for', u'for x in [1,2,3]:', False, (8, 1)), Text(u' ', (9, 1)), Expression(u'x', [], (9, 9)), ControlLine(u'for', u'endfor', True, (10, 1)), Text(u'\r\n', (11, 1)), Expression(u"trumpeter == 'Miles' and trumpeter or \\\n 'Dizzy'", [], (12, 1)), Text(u'\r\n\r\n', (13, 15)), DefTag(u'def', {u'name': u'hi()'}, (15, 1), [Text(u'\r\n hi!\r\n', (15, 19))]), Text(u'\r\n\r\n</html>\r\n', (17, 8))]) ) assert flatten_result(Template(template).render()) == """<html> like the name says. 1 2 3 Dizzy </html>""" -- GitLab