Skip to content
Myr

Merge remote-tracking branch 'wking/keep-trailing-newline'

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parents 840e7e0, 7e912c6 · Browse files · 7 files+47 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+47 added−7 removed

  • other:+47 added−7 removed

7 files+47 added−7 removed

modified:docs/templates.rst+3 added-1 removed
154 unmodified lines
155In the default configuration, a single trailing newline is stripped if156present, and whitespace is not further modified by the template engine. Each157whitespace (spaces, tabs, newlines etc.) is returned unchanged. If the158application configures Jinja to `trim_blocks` the first newline after a a159template tag is removed automatically (like in PHP).159template tag is removed automatically (like in PHP). To keep the160single trailing newline when it is present, configure Jinja to161`keep_trailing_newline`.162163But you can also strip whitespace in templates by hand. If you put an minus164sign (``-``) to the start or end of an block (for example a for tag), a165comment or variable expression you can remove the whitespaces after or before
1218 unmodified lines
modified:jinja2/testsuite/lexnparse.py+13 added
106 unmodified lines
107 tmpl = env.from_string('1\n2\r\n3\n4\n')108 result = tmpl.render()109 assert result.replace(seq, 'X') == '1X2X3X4'110111 def test_trailing_newline(self):112 for keep in [True, False]:113 env = Environment(keep_trailing_newline=keep)114 for template,expected in [115 ('', {}),116 ('no\nnewline', {}),117 ('with\nnewline\n', {False: 'with\nnewline'}),118 ('with\nseveral\n\n\n', {False: 'with\nseveral\n\n'}),119 ]:120 tmpl = env.from_string(template)121 expect = expected.get(keep, template)122 result = tmpl.render()123 assert result == expect, (keep, template, result, expect)124125class ParserTestCase(JinjaTestCase):126127 def test_php_syntax(self):
300 unmodified lines
modified:jinja2/defaults.py+1 added
21 unmodified lines
22LINE_STATEMENT_PREFIX = None23LINE_COMMENT_PREFIX = None24TRIM_BLOCKS = False25NEWLINE_SEQUENCE = '\n'26KEEP_TRAILING_NEWLINE = False272829# default filters, tests and namespace30from jinja2.filters import FILTERS as DEFAULT_FILTERS
12 unmodified lines
modified:jinja2/environment.py+14 added-3 removed
13 unmodified lines
14from jinja2.defaults import BLOCK_START_STRING, \15 BLOCK_END_STRING, VARIABLE_START_STRING, VARIABLE_END_STRING, \16 COMMENT_START_STRING, COMMENT_END_STRING, LINE_STATEMENT_PREFIX, \17 LINE_COMMENT_PREFIX, TRIM_BLOCKS, NEWLINE_SEQUENCE, \18 DEFAULT_FILTERS, DEFAULT_TESTS, DEFAULT_NAMESPACE18 DEFAULT_FILTERS, DEFAULT_TESTS, DEFAULT_NAMESPACE, \19 KEEP_TRAILING_NEWLINE20from jinja2.lexer import get_lexer, TokenStream21from jinja2.parser import Parser22from jinja2.optimizer import optimize23from jinja2.compiler import generate
123 unmodified lines
147 ``'\n'`` or ``'\r\n'``. The default is ``'\n'`` which is a148 useful default for Linux and OS X systems as well as web149 applications.150151 `keep_trailing_newline`152 Preserve the trailing newline when rendering templates.153 The default is ``False``, which causes a single newline,154 if present, to be stripped from the end of the template.155156 .. versionadded:: 2.7157158 `extensions`159 List of Jinja extensions to use. This can either be import paths160 as strings or extension classes. For more information have a161 look at :ref:`the extensions documentation <jinja-extensions>`.
77 unmodified lines
239 line_statement_prefix=LINE_STATEMENT_PREFIX,240 line_comment_prefix=LINE_COMMENT_PREFIX,241 trim_blocks=TRIM_BLOCKS,242 newline_sequence=NEWLINE_SEQUENCE,243 keep_trailing_newline=KEEP_TRAILING_NEWLINE,244 extensions=(),245 optimized=True,246 undefined=Undefined,247 finalize=None,
23 unmodified lines
271 self.line_statement_prefix = line_statement_prefix272 self.line_comment_prefix = line_comment_prefix273 self.trim_blocks = trim_blocks274 self.newline_sequence = newline_sequence275 self.keep_trailing_newline = keep_trailing_newline276277 # runtime information278 self.undefined = undefined279 self.optimized = optimized
557 unmodified lines
837 line_statement_prefix=LINE_STATEMENT_PREFIX,838 line_comment_prefix=LINE_COMMENT_PREFIX,839 trim_blocks=TRIM_BLOCKS,840 newline_sequence=NEWLINE_SEQUENCE,841 keep_trailing_newline=KEEP_TRAILING_NEWLINE,842 extensions=(),843 optimized=True,844 undefined=Undefined,845 finalize=None,
1 unmodified line
847 env = get_spontaneous_environment(848 block_start_string, block_end_string, variable_start_string,849 variable_end_string, comment_start_string, comment_end_string,850 line_statement_prefix, line_comment_prefix, trim_blocks,840 newline_sequence, frozenset(extensions), optimized, undefined,841 finalize, autoescape, None, 0, False, None)851 newline_sequence, keep_trailing_newline, frozenset(extensions),852 optimized, undefined, finalize, autoescape, None, 0, False, None)853 return env.from_string(source, template_class=cls)854855 @classmethod856 def from_code(cls, environment, code, globals, uptodate=None):
284 unmodified lines
modified:jinja2/ext.py+3 added-1 removed
587 unmodified lines
588 options.get('comment_end_string', COMMENT_END_STRING),589 options.get('line_statement_prefix') or LINE_STATEMENT_PREFIX,590 options.get('line_comment_prefix') or LINE_COMMENT_PREFIX,591 getbool(options, 'trim_blocks', TRIM_BLOCKS),592 NEWLINE_SEQUENCE, frozenset(extensions),592 NEWLINE_SEQUENCE,593 getbool(options, 'keep_trailing_newline', KEEP_TRAILING_NEWLINE),594 frozenset(extensions),595 cache_size=0,596 auto_reload=False597 )598
24 unmodified lines
modified:jinja2/lexer.py+11 added-2 removed
389 unmodified lines
390 environment.comment_end_string,391 environment.line_statement_prefix,392 environment.line_comment_prefix,393 environment.trim_blocks,394 environment.newline_sequence)394 environment.newline_sequence,395 environment.keep_trailing_newline)396 lexer = _lexer_cache.get(key)397 if lexer is None:398 lexer = Lexer(environment)399 _lexer_cache[key] = lexer
34 unmodified lines
434 # block suffix if trimming is enabled435 block_suffix_re = environment.trim_blocks and '\\n?' or ''436437 self.newline_sequence = environment.newline_sequence438 self.keep_trailing_newline = environment.keep_trailing_newline439440 # global lexing rules441 self.rules = {442 'root': [
115 unmodified lines
558 def tokeniter(self, source, name, filename=None, state=None):559 """This method tokenizes the text and returns the tokens in a560 generator. Use this method if you just want to tokenize a template.561 """560 source = '\n'.join(six.text_type(source).splitlines())562 source = six.text_type(source)563 lines = source.splitlines()564 if self.keep_trailing_newline and source:565 for newline in ('\r\n', '\r', '\n'):566 if source.endswith(newline):567 lines.append('')568 break569 source = '\n'.join(lines)570 pos = 0571 lineno = 1572 stack = ['root']573 if state is not None and state != 'root':
125 unmodified lines
modified:CHANGES+2 added
11 unmodified lines
12- Added a non-silent option for babel extraction.13- Added `urlencode` filter that automatically quotes values for14 URL safe usage with utf-8 as only supported encoding. If applications15 want to change this encoding they can override the filter.16- Added `keep-trailing-newline` configuration to environments and17 templates to optionally preserve the final trailing newline.18- Accessing `last` on the loop context no longer causes the iterator19 to be consumed into a list.20- Python requirement changed: 2.6, 2.7 or >= 3.3 are required now,21 supported by same source code, using the "six" compatibility library.
271 unmodified lines