| 227 | | return token |
|---|
| 228 | | |
|---|
| 229 | | class DebugLexer(Lexer): |
|---|
| 230 | | def __init__(self, template_string, origin): |
|---|
| 231 | | super(DebugLexer, self).__init__(template_string, origin) |
|---|
| 232 | | |
|---|
| 233 | | def tokenize(self): |
|---|
| 234 | | "Return a list of tokens from a given template_string" |
|---|
| 235 | | result, upto = [], 0 |
|---|
| 236 | | for match in tag_re.finditer(self.template_string): |
|---|
| 237 | | start, end = match.span() |
|---|
| 238 | | if start > upto: |
|---|
| 239 | | result.append(self.create_token(self.template_string[upto:start], (upto, start), False)) |
|---|
| 240 | | upto = start |
|---|
| 241 | | result.append(self.create_token(self.template_string[start:end], (start, end), True)) |
|---|
| 242 | | upto = end |
|---|
| 243 | | last_bit = self.template_string[upto:] |
|---|
| 244 | | if last_bit: |
|---|
| 245 | | result.append(self.create_token(last_bit, (upto, upto + len(last_bit)), False)) |
|---|
| 246 | | return result |
|---|
| 247 | | |
|---|
| 248 | | def create_token(self, token_string, source, in_tag): |
|---|
| 249 | | token = super(DebugLexer, self).create_token(token_string, in_tag) |
|---|
| 250 | | token.source = self.origin, source |
|---|
| 363 | | class DebugParser(Parser): |
|---|
| 364 | | def __init__(self, lexer): |
|---|
| 365 | | super(DebugParser, self).__init__(lexer) |
|---|
| 366 | | self.command_stack = [] |
|---|
| 367 | | |
|---|
| 368 | | def enter_command(self, command, token): |
|---|
| 369 | | self.command_stack.append( (command, token.source) ) |
|---|
| 370 | | |
|---|
| 371 | | def exit_command(self): |
|---|
| 372 | | self.command_stack.pop() |
|---|
| 373 | | |
|---|
| 374 | | def error(self, token, msg): |
|---|
| 375 | | return self.source_error(token.source, msg) |
|---|
| 376 | | |
|---|
| 377 | | def source_error(self, source,msg): |
|---|
| 378 | | e = TemplateSyntaxError(msg) |
|---|
| 379 | | e.source = source |
|---|
| 380 | | return e |
|---|
| 381 | | |
|---|
| 382 | | def create_nodelist(self): |
|---|
| 383 | | return DebugNodeList() |
|---|
| 384 | | |
|---|
| 385 | | def create_variable_node(self, contents): |
|---|
| 386 | | return DebugVariableNode(contents) |
|---|
| 387 | | |
|---|
| 388 | | def extend_nodelist(self, nodelist, node, token): |
|---|
| 389 | | node.source = token.source |
|---|
| 390 | | super(DebugParser, self).extend_nodelist(nodelist, node, token) |
|---|
| 391 | | |
|---|
| 392 | | def unclosed_block_tag(self, parse_until): |
|---|
| 393 | | command, source = self.command_stack.pop() |
|---|
| 394 | | msg = "Unclosed tag '%s'. Looking for one of: %s " % (command, ', '.join(parse_until)) |
|---|
| 395 | | raise self.source_error( source, msg) |
|---|
| 396 | | |
|---|
| 397 | | def compile_function_error(self, token, e): |
|---|
| 398 | | if not hasattr(e, 'source'): |
|---|
| 399 | | e.source = token.source |
|---|
| 400 | | |
|---|
| 819 | | class DebugNodeList(NodeList): |
|---|
| 820 | | def render_node(self, node, context): |
|---|
| 821 | | try: |
|---|
| 822 | | result = node.render(context) |
|---|
| 823 | | except TemplateSyntaxError, e: |
|---|
| 824 | | if not hasattr(e, 'source'): |
|---|
| 825 | | e.source = node.source |
|---|
| 826 | | raise |
|---|
| 827 | | except Exception, e: |
|---|
| 828 | | from sys import exc_info |
|---|
| 829 | | wrapped = TemplateSyntaxError('Caught an exception while rendering: %s' % e) |
|---|
| 830 | | wrapped.source = node.source |
|---|
| 831 | | wrapped.exc_info = exc_info() |
|---|
| 832 | | raise wrapped |
|---|
| 833 | | return result |
|---|
| 834 | | |
|---|
| 863 | | |
|---|
| 864 | | class DebugVariableNode(VariableNode): |
|---|
| 865 | | def render(self, context): |
|---|
| 866 | | try: |
|---|
| 867 | | output = force_unicode(self.filter_expression.resolve(context)) |
|---|
| 868 | | except TemplateSyntaxError, e: |
|---|
| 869 | | if not hasattr(e, 'source'): |
|---|
| 870 | | e.source = self.source |
|---|
| 871 | | raise |
|---|
| 872 | | except UnicodeDecodeError: |
|---|
| 873 | | return '' |
|---|
| 874 | | if (context.autoescape and not isinstance(output, SafeData)) or isinstance(output, EscapeData): |
|---|
| 875 | | return escape(output) |
|---|
| 876 | | else: |
|---|
| 877 | | return output |
|---|