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
Provenance
- Commit
f3acf0b- Tree
3871f4d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent 1
840e7e0- Parent 2
7e912c6
7 files changed+47 added−7 removed
modified:docs/templates.rst+3 added-1 removed
154 unmodified lines155 In the default configuration, a single trailing newline is stripped if156 present, and whitespace is not further modified by the template engine. Each157 whitespace (spaces, tabs, newlines etc.) is returned unchanged. If the158 application configures Jinja to `trim_blocks` the first newline after a a159-template tag is removed automatically (like in PHP).159+template tag is removed automatically (like in PHP). To keep the160+single trailing newline when it is present, configure Jinja to161+`keep_trailing_newline`.162 163 But you can also strip whitespace in templates by hand. If you put an minus164 sign (``-``) to the start or end of an block (for example a for tag), a165 comment or variable expression you can remove the whitespaces after or before1218 unmodified linesmodified:jinja2/testsuite/lexnparse.py+13 added
106 unmodified lines107 tmpl = env.from_string('1\n2\r\n3\n4\n')108 result = tmpl.render()109 assert result.replace(seq, 'X') == '1X2X3X4'110 111+ 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)124 125 class ParserTestCase(JinjaTestCase):126 127 def test_php_syntax(self):300 unmodified linesmodified:jinja2/defaults.py+1 added
21 unmodified lines22 LINE_STATEMENT_PREFIX = None23 LINE_COMMENT_PREFIX = None24 TRIM_BLOCKS = False25 NEWLINE_SEQUENCE = '\n'26+KEEP_TRAILING_NEWLINE = False27 28 29 # default filters, tests and namespace30 from jinja2.filters import FILTERS as DEFAULT_FILTERS12 unmodified linesmodified:jinja2/environment.py+14 added-3 removed
13 unmodified lines14 from 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_NEWLINE20 from jinja2.lexer import get_lexer, TokenStream21 from jinja2.parser import Parser22 from jinja2.optimizer import optimize23 from jinja2.compiler import generate123 unmodified lines147 ``'\n'`` or ``'\r\n'``. The default is ``'\n'`` which is a148 useful default for Linux and OS X systems as well as web149 applications.150 151+ `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.155+156+ .. versionadded:: 2.7157+158 `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 lines239 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 lines271 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_newline276 277 # runtime information278 self.undefined = undefined279 self.optimized = optimized557 unmodified lines837 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 line847 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)854 855 @classmethod856 def from_code(cls, environment, code, globals, uptodate=None):284 unmodified linesmodified:jinja2/ext.py+3 added-1 removed
587 unmodified lines588 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 linesmodified:jinja2/lexer.py+11 added-2 removed
389 unmodified lines390 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] = lexer34 unmodified lines434 # block suffix if trimming is enabled435 block_suffix_re = environment.trim_blocks and '\\n?' or ''436 437 self.newline_sequence = environment.newline_sequence438+ self.keep_trailing_newline = environment.keep_trailing_newline439 440 # global lexing rules441 self.rules = {442 'root': [115 unmodified lines558 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 linesmodified:CHANGES+2 added
11 unmodified lines12 - 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