- fixes to code parsing/whitespace adjusting where plain python comments may contain quote characters [ticket:23]
diff --git a/CHANGES b/CHANGES index c568595..b19f3cf 100644 --- a/CHANGES +++ b/CHANGES
@@ -1,4 +1,6 @@ - got defs-within-defs to be cacheable +- fixes to code parsing/whitespace adjusting where plain python +comments may contain quote characters [ticket:23] 0.1.3 - ***Small Syntax Change*** - the single line comment character is now
diff --git a/lib/mako/lexer.py b/lib/mako/lexer.py index 9e0905f..179e15c 100644 --- a/lib/mako/lexer.py +++ b/lib/mako/lexer.py
@@ -59,6 +59,9 @@ def parse_until_text(self, *text): startpos = self.match_position while True: + match = self.match(r'#.*\n') + if match: + continue match = self.match(r'(\"\"\"|\'\'\'|\"|\')') if match: m = self.match(r'.*?%s' % match.group(1), re.S) @@ -69,7 +72,7 @@ if match: return (self.text[startpos:self.match_position-len(match.group(1))], match.group(1)) else: - match = self.match(r".*?(?=\"|\'|%s)" % r'|'.join(text), re.S) + match = self.match(r".*?(?=\"|\'|#|%s)" % r'|'.join(text), re.S) if not match: raise exceptions.SyntaxException("Expected: %s" % ','.join(text), self.matched_lineno, self.matched_charpos, self.filename)
diff --git a/lib/mako/pygen.py b/lib/mako/pygen.py index e1efa34..01ed092 100644 --- a/lib/mako/pygen.py +++ b/lib/mako/pygen.py
@@ -219,6 +219,7 @@ state[backslashed] = True else: state[backslashed] = False + line = re.split(r'#', line)[0] triples = len(re.findall(r"\"\"\"|\'\'\'", line)) if triples == 1 or triples % 2 != 0: state[triplequoted] = not state[triplequoted]
diff --git a/test/lexer.py b/test/lexer.py index b12a756..7e13edd 100644 --- a/test/lexer.py +++ b/test/lexer.py
@@ -238,6 +238,27 @@ %> """ nodes = Lexer(template).parse() + + def test_tricky_code_2(self): + template = """<% + # someone's comment + %> + """ + nodes = Lexer(template).parse() + assert repr(nodes) == r"""TemplateNode({}, [Code(u" \n # someone's comment\n \n", False, (1, 1)), Text(u'\n ', (3, 11))])""" + + template= """<% + print 'hi' + # this is a comment + # another comment + x = 7 # someone's '''comment + print ''' + there + ''' + # someone else's comment + %> '''and now some text '''""" + nodes = Lexer(template).parse() + assert repr(nodes) == r"""TemplateNode({}, [Code(u"\nprint 'hi'\n# this is a comment\n# another comment\nx = 7 # someone's '''comment\nprint '''\n there\n '''\n# someone else's comment\n \n", False, (1, 1)), Text(u" '''and now some text '''", (10, 11))])""" def test_control_lines(self): template = """