blob: 77e0d03453699cd66b218458015a1314a1e4d1c8 [file] [log] [blame]
Armin Ronacher07bc6842008-03-31 14:18:49 +02001# -*- coding: utf-8 -*-
2"""
Armin Ronacher82b3f3d2008-03-31 20:01:08 +02003 jinja2.environment
4 ~~~~~~~~~~~~~~~~~~
Armin Ronacher07bc6842008-03-31 14:18:49 +02005
6 Provides a class that holds runtime and parsing time options.
7
8 :copyright: 2007 by Armin Ronacher.
9 :license: BSD, see LICENSE for more details.
10"""
Armin Ronacher82b3f3d2008-03-31 20:01:08 +020011from jinja2.lexer import Lexer
12from jinja2.parser import Parser
Armin Ronacher4dfc9752008-04-09 15:03:29 +020013from jinja2.runtime import Undefined
Armin Ronacher82b3f3d2008-03-31 20:01:08 +020014from jinja2.defaults import DEFAULT_FILTERS, DEFAULT_TESTS, DEFAULT_NAMESPACE
Armin Ronacher07bc6842008-03-31 14:18:49 +020015
16
17class Environment(object):
18 """
19 The Jinja environment.
20
21 The core component of Jinja is the `Environment`. It contains
22 important shared variables like configuration, filters, tests,
23 globals and others.
24 """
25
26 def __init__(self,
27 block_start_string='{%',
28 block_end_string='%}',
29 variable_start_string='{{',
30 variable_end_string='}}',
31 comment_start_string='{#',
32 comment_end_string='#}',
33 trim_blocks=False,
Armin Ronacher82b3f3d2008-03-31 20:01:08 +020034 template_charset='utf-8'):
Armin Ronacher07bc6842008-03-31 14:18:49 +020035 """
36 Here the possible initialization parameters:
37
38 ========================= ============================================
39 `block_start_string` the string marking the begin of a block.
40 this defaults to ``'{%'``.
41 `block_end_string` the string marking the end of a block.
42 defaults to ``'%}'``.
43 `variable_start_string` the string marking the begin of a print
44 statement. defaults to ``'{{'``.
45 `comment_start_string` the string marking the begin of a
46 comment. defaults to ``'{#'``.
47 `comment_end_string` the string marking the end of a comment.
48 defaults to ``'#}'``.
49 `trim_blocks` If this is set to ``True`` the first newline
50 after a block is removed (block, not
51 variable tag!). Defaults to ``False``.
Armin Ronacher82b3f3d2008-03-31 20:01:08 +020052 `template_charset` the charset of the templates.
Armin Ronacher07bc6842008-03-31 14:18:49 +020053 ========================= ============================================
54 """
55
56 # lexer / parser information
57 self.block_start_string = block_start_string
58 self.block_end_string = block_end_string
59 self.variable_start_string = variable_start_string
60 self.variable_end_string = variable_end_string
61 self.comment_start_string = comment_start_string
62 self.comment_end_string = comment_end_string
63 self.trim_blocks = trim_blocks
Armin Ronacher07bc6842008-03-31 14:18:49 +020064 self.template_charset = template_charset
Armin Ronacher07bc6842008-03-31 14:18:49 +020065
66 # defaults
67 self.filters = DEFAULT_FILTERS.copy()
68 self.tests = DEFAULT_TESTS.copy()
69 self.globals = DEFAULT_NAMESPACE.copy()
70
Armin Ronacher4dfc9752008-04-09 15:03:29 +020071 # the factory that creates the undefined object
72 self.undefined_factory = Undefined
73
Armin Ronacher07bc6842008-03-31 14:18:49 +020074 # create lexer
75 self.lexer = Lexer(self)
76
Armin Ronacher07bc6842008-03-31 14:18:49 +020077 def parse(self, source, filename=None):
78 """
79 Parse the sourcecode and return the abstract syntax tree. This tree
80 of nodes is used by the `translators`_ to convert the template into
81 executable source- or bytecode.
82
83 .. _translators: translators.txt
84 """
85 parser = Parser(self, source, filename)
86 return parser.parse()
87
88 def lex(self, source, filename=None):
89 """
90 Lex the given sourcecode and return a generator that yields tokens.
91 The stream returned is not usable for Jinja but can be used if
92 Jinja templates should be processed by other tools (for example
93 syntax highlighting etc)
94
95 The tuples are returned in the form ``(lineno, token, value)``.
96 """
97 return self.lexer.tokeniter(source, filename)