# -*- coding: utf-8 -*-
+try:
+ from collections.abc import Sequence, Iterator, Callable
+except ImportError: # python 2
+ from collections import Sequence, Iterator, Callable
try:
from .tokenizer import tokenize
except (ValueError, SystemError): # python 2
import sys
if sys.version_info[0] == 3:
python3 = True
+ unicode_type = str
+ string_type = str
def unicode(x, y):
return x
else: # python 2
python3 = False
+ unicode_type = unicode
+ string_type = basestring
#
A string containing the rendered template.
"""
- function = type(render)
-
- # If the template is a list
- if type(template) is list:
+ # If the template is a seqeuence but not derived from a string
+ if isinstance(template, Sequence) and \
+ not isinstance(template, string_type):
# Then we don't need to tokenize it
# But it does need to be a generator
tokens = (token for token in template)
# If we're a literal tag
elif tag == 'literal':
# Add padding to the key and add it to the output
- if type(key) != unicode:
+ if not isinstance(key, unicode_type):
key = unicode(key, 'utf-8')
output += key.replace('\n', '\n' + (' ' * padding))
# (inverted tags do this)
# then get the un-coerced object (next in the stack)
thing = scopes[1]
- if type(thing) != unicode:
+ if not isinstance(thing, unicode_type):
thing = unicode(str(thing), 'utf-8')
output += _html_escape(thing)
elif tag == 'no escape':
# Just lookup the key and add it
thing = _get_key(key, scopes)
- if type(thing) != unicode:
+ if not isinstance(thing, unicode_type):
thing = unicode(str(thing), 'utf-8')
output += thing
# If the scope is a callable (as described in
# https://mustache.github.io/mustache.5.html)
- if type(scope) is function:
+ if isinstance(scope, Callable):
# Generate template text from tags
text = unicode('', 'utf-8')
else: # python 2
output += rend.decode('utf-8')
- # If the scope is a list
- elif type(scope) is list:
+ # If the scope is a sequence, an iterator or generator but not
+ # derived from a string
+ elif isinstance(scope, (Sequence, Iterator)) and \
+ not isinstance(scope, string_type):
# Then we need to do some looping
# Gather up all the tags inside the section