Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
Armin Ronacher | 82b3f3d | 2008-03-31 20:01:08 +0200 | [diff] [blame] | 3 | jinja2.environment |
| 4 | ~~~~~~~~~~~~~~~~~~ |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 5 | |
| 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 Ronacher | 82b3f3d | 2008-03-31 20:01:08 +0200 | [diff] [blame] | 11 | from jinja2.lexer import Lexer |
| 12 | from jinja2.parser import Parser |
Armin Ronacher | 4dfc975 | 2008-04-09 15:03:29 +0200 | [diff] [blame] | 13 | from jinja2.runtime import Undefined |
Armin Ronacher | 82b3f3d | 2008-03-31 20:01:08 +0200 | [diff] [blame] | 14 | from jinja2.defaults import DEFAULT_FILTERS, DEFAULT_TESTS, DEFAULT_NAMESPACE |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 15 | |
| 16 | |
| 17 | class 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 Ronacher | 82b3f3d | 2008-03-31 20:01:08 +0200 | [diff] [blame] | 34 | template_charset='utf-8'): |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 35 | """ |
| 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 Ronacher | 82b3f3d | 2008-03-31 20:01:08 +0200 | [diff] [blame] | 52 | `template_charset` the charset of the templates. |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 53 | ========================= ============================================ |
| 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 Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 64 | self.template_charset = template_charset |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 65 | |
| 66 | # defaults |
| 67 | self.filters = DEFAULT_FILTERS.copy() |
| 68 | self.tests = DEFAULT_TESTS.copy() |
| 69 | self.globals = DEFAULT_NAMESPACE.copy() |
| 70 | |
Armin Ronacher | 4dfc975 | 2008-04-09 15:03:29 +0200 | [diff] [blame] | 71 | # the factory that creates the undefined object |
| 72 | self.undefined_factory = Undefined |
| 73 | |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 74 | # create lexer |
| 75 | self.lexer = Lexer(self) |
| 76 | |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 77 | 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) |