From 828eff9142856c6d5c3eb70e5dbe7bcb17f9356f Mon Sep 17 00:00:00 2001 From: noah morrison Date: Sat, 1 Nov 2014 16:30:28 -0400 Subject: [PATCH] Fixed Indented Multiline Standalone failure * Handle empty data * Close file instead of EOFError * Yield final literal --- chevron.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/chevron.py b/chevron.py index 71c0b90..996b02b 100755 --- a/chevron.py +++ b/chevron.py @@ -38,7 +38,7 @@ def tokenize(template): def get(amount=1): data = template.read(amount) if len(data) != amount: - raise EOFError() + template.close() return data @@ -54,10 +54,14 @@ def tokenize(template): def grab_literal(until=None): until = until or l_del literal = get() - while literal[-2:] != until: + while not template.closed: + if literal[-2:] == until: + return literal[:-2] + literal += get() - return literal[:-2] + return literal + tag_types = { '!': 'comment', @@ -77,16 +81,16 @@ def tokenize(template): l_del = '{{' r_del = '}}' while not template.closed: - try: - literal = grab_literal() - except EOFError: - return - - if literal: - if len(literal) > 1 and literal.startswith('\n'): - literal = literal[1:] + literal = grab_literal() + + if literal == '': + continue + else: yield ('literal', literal) + if template.closed: + break + tag_type = tag_types.get(peek(0, 1), 'variable') if tag_type != 'variable': template.seek(template.tell() + 1) @@ -144,7 +148,7 @@ def render(template, data, partials_path='.', partials_ext='mustache'): if tag == 'end': scopes = scopes[1:] - elif not scopes[0]: + elif not scopes[0] and len(scopes) != 1: pass elif tag == 'literal': -- 2.47.3