Created
January 29, 2015 05:05
-
-
Save prologic/4f574bda826b9a67533b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(django-upgrade)➜ ~ diff -u django-template-parser-1.2.py django-template-parser-1.4.py | |
--- django-template-parser-1.2.py 2015-01-29 15:04:34.579306331 +1000 | |
+++ django-template-parser-1.4.py 2015-01-29 15:03:43.918306352 +1000 | |
@@ -7,28 +7,32 @@ | |
self.add_library(lib) | |
def parse(self, parse_until=None): | |
- if parse_until is None: parse_until = [] | |
+ if parse_until is None: | |
+ parse_until = [] | |
nodelist = self.create_nodelist() | |
while self.tokens: | |
token = self.next_token() | |
- if token.token_type == TOKEN_TEXT: | |
+ # Use the raw values here for TOKEN_* for a tiny performance boost. | |
+ if token.token_type == 0: # TOKEN_TEXT | |
self.extend_nodelist(nodelist, TextNode(token.contents), token) | |
- elif token.token_type == TOKEN_VAR: | |
+ elif token.token_type == 1: # TOKEN_VAR | |
if not token.contents: | |
self.empty_variable(token) | |
filter_expression = self.compile_filter(token.contents) | |
var_node = self.create_variable_node(filter_expression) | |
- self.extend_nodelist(nodelist, var_node,token) | |
- elif token.token_type == TOKEN_BLOCK: | |
- if token.contents in parse_until: | |
- # put token back on token list so calling code knows why it terminated | |
- self.prepend_token(token) | |
- return nodelist | |
+ self.extend_nodelist(nodelist, var_node, token) | |
+ elif token.token_type == 2: # TOKEN_BLOCK | |
try: | |
command = token.contents.split()[0] | |
except IndexError: | |
self.empty_block_tag(token) | |
- # execute callback function for this tag and append resulting node | |
+ if command in parse_until: | |
+ # put token back on token list so calling | |
+ # code knows why it terminated | |
+ self.prepend_token(token) | |
+ return nodelist | |
+ # execute callback function for this tag and append | |
+ # resulting node | |
self.enter_command(command, token) | |
try: | |
compile_func = self.tags[command] | |
@@ -64,7 +68,8 @@ | |
if nodelist.contains_nontext: | |
raise AttributeError | |
except AttributeError: | |
- raise TemplateSyntaxError("%r must be the first tag in the template." % node) | |
+ raise TemplateSyntaxError("%r must be the first tag " | |
+ "in the template." % node) | |
if isinstance(nodelist, NodeList) and not isinstance(node, TextNode): | |
nodelist.contains_nontext = True | |
nodelist.append(node) | |
@@ -86,11 +91,12 @@ | |
def invalid_block_tag(self, token, command, parse_until=None): | |
if parse_until: | |
- raise self.error(token, "Invalid block tag: '%s', expected %s" % (command, get_text_list(["'%s'" % p for p in parse_until]))) | |
+ raise self.error(token, "Invalid block tag: '%s', expected %s" % | |
+ (command, get_text_list(["'%s'" % p for p in parse_until]))) | |
raise self.error(token, "Invalid block tag: '%s'" % command) | |
def unclosed_block_tag(self, parse_until): | |
- raise self.error(None, "Unclosed tags: %s " % ', '.join(parse_until)) | |
+ raise self.error(None, "Unclosed tags: %s " % ', '.join(parse_until)) | |
def compile_function_error(self, token, e): | |
pass | |
@@ -109,7 +115,9 @@ | |
self.filters.update(lib.filters) | |
def compile_filter(self, token): | |
- "Convenient wrapper for FilterExpression" | |
+ """ | |
+ Convenient wrapper for FilterExpression | |
+ """ | |
return FilterExpression(token, self) | |
def find_filter(self, filter_name): |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment