diff options
-rw-r--r-- | tempita/__init__.py | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/tempita/__init__.py b/tempita/__init__.py index 467bd5b..5928a0d 100644 --- a/tempita/__init__.py +++ b/tempita/__init__.py @@ -659,12 +659,12 @@ def lex(s, name=None, trim_whitespace=True, line_offset=0, delimiters=None): in_expr = False chunks = [] last = 0 - last_pos = (1, 1) + last_pos = (line_offset + 1, 1) token_re = re.compile(r'%s|%s' % (re.escape(delimiters[0]), re.escape(delimiters[1]))) for match in token_re.finditer(s): expr = match.group(0) - pos = find_position(s, match.end(), line_offset) + pos = find_position(s, match.end(), line_offset, last_pos) if expr == delimiters[0] and in_expr: raise TemplateError('%s inside expression' % delimiters[0], position=pos, @@ -758,10 +758,16 @@ def trim_lex(tokens): return tokens -def find_position(string, index, line_offset): - """Given a string and index, return (line, column)""" - leading = string[:index].splitlines() - return (len(leading) + line_offset, len(leading[-1]) + 1) +def find_position(string, index, last_index, last_pos=(1, 1)): + """ + Given a string and index, return (line, column) + """ + lines = string.count('\n', last_index, index) + if lines > 0: + column = index - string.rfind('\n', last_index, index) + else: + column = last_pos[1] + (index - last_index) + return (last_pos[0] + lines, column) def parse(s, name=None, line_offset=0, delimiters=None): |