moved environment defaults to jinja2.defaults
authorArmin Ronacher <armin.ronacher@active-4.com>
Fri, 23 May 2008 21:36:07 +0000 (23:36 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Fri, 23 May 2008 21:36:07 +0000 (23:36 +0200)
--HG--
branch : trunk

jinja2/defaults.py
jinja2/environment.py
jinja2/ext.py

index e12493076df6ea6c213272fd9bfd6d158b4b5c35..1d3be69a9656d4d3c90bd487dc4c59c53028a6ca 100644 (file)
@@ -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,
index 9a00bb701607ed4b00f362ae9e089807db36b5fe..95dda11844fe01004fb493c5afa489efd67790a3 100644 (file)
@@ -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,
index 74457051aecf642534651f429cb647784386874f..93cde8343a8888cf135f195fcaf8a84337667cee 100644 (file)
@@ -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, []