:copyright: 2007-2008 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
-from jinja2.filters import FILTERS as DEFAULT_FILTERS
-from jinja2.tests import TESTS as DEFAULT_TESTS
from jinja2.utils import generate_lorem_ipsum
+# defaults for the parser / lexer
BLOCK_START_STRING = '{%'
BLOCK_END_STRING = '%}'
VARIABLE_START_STRING = '{{'
COMMENT_START_STRING = '{#'
COMMENT_END_STRING = '#}'
LINE_STATEMENT_PREFIX = None
+TRIM_BLOCKS = False
+NEWLINE_SEQUENCE = '\n'
+# default filters, tests and namespace
+from jinja2.filters import FILTERS as DEFAULT_FILTERS
+from jinja2.tests import TESTS as DEFAULT_TESTS
DEFAULT_NAMESPACE = {
'range': xrange,
'dict': lambda **kw: kw,
comment_start_string=COMMENT_START_STRING,
comment_end_string=COMMENT_END_STRING,
line_statement_prefix=LINE_STATEMENT_PREFIX,
- trim_blocks=False,
- newline_sequence='\n',
+ trim_blocks=TRIM_BLOCKS,
+ newline_sequence=NEWLINE_SEQUENCE,
extensions=(),
optimized=True,
undefined=Undefined,
"""
def __new__(cls, source,
- block_start_string='{%',
- block_end_string='%}',
- variable_start_string='{{',
- variable_end_string='}}',
- comment_start_string='{#',
- comment_end_string='#}',
- line_statement_prefix=None,
- trim_blocks=False,
- newline_sequence='\n',
+ block_start_string=BLOCK_START_STRING,
+ block_end_string=BLOCK_END_STRING,
+ variable_start_string=VARIABLE_START_STRING,
+ variable_end_string=VARIABLE_END_STRING,
+ comment_start_string=COMMENT_START_STRING,
+ comment_end_string=COMMENT_END_STRING,
+ line_statement_prefix=LINE_STATEMENT_PREFIX,
+ trim_blocks=TRIM_BLOCKS,
+ newline_sequence=NEWLINE_SEQUENCE,
extensions=(),
optimized=True,
undefined=Undefined,
"""
from collections import deque
from jinja2 import nodes
+from jinja2.defaults import *
from jinja2.environment import get_spontaneous_environment
from jinja2.runtime import Undefined, concat
from jinja2.exceptions import TemplateAssertionError, TemplateSyntaxError
:return: an iterator over ``(lineno, funcname, message, comments)`` tuples.
(comments will be empty currently)
"""
- encoding = options.get('encoding', 'utf-8')
-
- have_trans_extension = False
- extensions = []
+ extensions = set()
for extension in options.get('extensions', '').split(','):
extension = extension.strip()
if not extension:
continue
- extension = import_string(extension)
- if extension is InternationalizationExtension:
- have_trans_extension = True
- extensions.append(extension)
- if not have_trans_extension:
- extensions.append(InternationalizationExtension)
+ extensions.add(import_string(extension))
+ if InternationalizationExtension not in extensions:
+ extensions.add(InternationalizationExtension)
environment = get_spontaneous_environment(
- options.get('block_start_string', '{%'),
- options.get('block_end_string', '%}'),
- options.get('variable_start_string', '{{'),
- options.get('variable_end_string', '}}'),
- options.get('comment_start_string', '{#'),
- options.get('comment_end_string', '#}'),
- options.get('line_statement_prefix') or None,
- options.get('trim_blocks', '').lower() in ('1', 'on', 'yes', 'true'),
- '\n', frozenset(extensions),
+ options.get('block_start_string', BLOCK_START_STRING),
+ options.get('block_end_string', BLOCK_END_STRING),
+ options.get('variable_start_string', VARIABLE_START_STRING),
+ options.get('variable_end_string', VARIABLE_END_STRING),
+ options.get('comment_start_string', COMMENT_START_STRING),
+ options.get('comment_end_string', COMMENT_END_STRING),
+ options.get('line_statement_prefix') or LINE_STATEMENT_PREFIX,
+ str(options.get('trim_blocks', TRIM_BLOCKS)).lower() in \
+ ('1', 'on', 'yes', 'true'),
+ NEWLINE_SEQUENCE, frozenset(extensions),
# fill with defaults so that environments are shared
# with other spontaneus environments. The rest of the
# arguments are optimizer, undefined, finalize, autoescape,
True, Undefined, None, False, None, 0, False
)
- node = environment.parse(fileobj.read().decode(encoding))
+ source = fileobj.read().decode(options.get('encoding', 'utf-8'))
+ node = environment.parse(source)
for lineno, func, message in extract_from_ast(node, keywords):
yield lineno, func, message, []