From 4f5008ff1917356e0d71bedc96546cf0c5db834d Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Fri, 23 May 2008 23:36:07 +0200 Subject: [PATCH] moved environment defaults to jinja2.defaults --HG-- branch : trunk --- jinja2/defaults.py | 8 ++++++-- jinja2/environment.py | 22 +++++++++++----------- jinja2/ext.py | 37 +++++++++++++++++-------------------- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/jinja2/defaults.py b/jinja2/defaults.py index e124930..1d3be69 100644 --- a/jinja2/defaults.py +++ b/jinja2/defaults.py @@ -8,11 +8,10 @@ :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 = '{{' @@ -20,8 +19,13 @@ VARIABLE_END_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, diff --git a/jinja2/environment.py b/jinja2/environment.py index 9a00bb7..95dda11 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -178,8 +178,8 @@ class Environment(object): 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, @@ -442,15 +442,15 @@ class Template(object): """ 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, diff --git a/jinja2/ext.py b/jinja2/ext.py index 7445705..93cde83 100644 --- a/jinja2/ext.py +++ b/jinja2/ext.py @@ -12,6 +12,7 @@ """ 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 @@ -343,31 +344,26 @@ def babel_extract(fileobj, keywords, comment_tags, options): :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, @@ -375,7 +371,8 @@ def babel_extract(fileobj, keywords, comment_tags, options): 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, [] -- 2.26.2