diff --git a/CHANGES b/CHANGES index c568595e26e40e5f1d91a944787990ee0a53110d..b19f3cf20841c6af3b2c3a2db4f5d2c992a89028 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 9e0905f69cef82888ea557f47f40432f80a0a195..179e15cc3c1c049a7f238fb5f7fb0f6c6d46222e 100644 --- a/lib/mako/lexer.py +++ b/lib/mako/lexer.py @@ -59,6 +59,9 @@ class Lexer(object): 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 @@ class Lexer(object): 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 e1efa341cf63cfe26f945798e6ebd065f879effa..01ed09283b9324dc42eaa1586c6d74776cff69f3 100644 --- a/lib/mako/pygen.py +++ b/lib/mako/pygen.py @@ -219,6 +219,7 @@ def adjust_whitespace(text): 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 b12a756a74cd97cb3aed1277d34bf651b79be61a..7e13edd54d4b57120e47d2f15b6eb737e92273e4 100644 --- a/test/lexer.py +++ b/test/lexer.py @@ -238,6 +238,27 @@ class LexerTest(unittest.TestCase): %> """ 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 = """