Dearest developers,
I found the following strange behaviour in jinja2:
jinja2.Template('\n').render() => ''
jinja2.Template('\n\n').render() => '\n'
This is because of line 552 in lexer.py:
source = '\n'.join(unicode(source).splitlines()) (*)
I believe that this is a bug, since the API docs at
http://jinja.pocoo.org/docs/templates/#whitespace-control
explicitly state that "whitespace is not further modified".
While this is just a nuisance for bigger templates for HTML, it
matters when formatting short snippets, concatenating them later.
(Also, one feels like a fool when some of the newlines just disappear ;-)
As the newlines (as defined by newline_re) are already replaced by
newline_sequence in Lexer._normalize_newlines(), I would propose to
drop the line (*) altogether. Doing so fixes the behavior (so
jinja2.Template('\n').render() => '\n').
I think that the Whitespace control section would be better with an
explanation what happens to the newlines, such as:
[Every occurrence of sequence "\n", "\r" or "\r\n" is replaced by
`Environment.newline_sequence`.]
Just as an open suggestion -- disabling this newline-replacing
behaviour would permit using binary-like templates. This could be done
i.e. by allowing newline_sequence=None and slightly modifying
_normalize_newlines() to pass in that case. As far as I know, there is
currently no way to preserve both '\r' and '\n' while all the other
characters are preserved.
I would gladly provide patches for all proposed changes.
Keep up the great work on the best templating language!
Cheers,
Tomas
--
You received this message because you are subscribed to the Google Groups
"pocoo-libs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pocoo-libs?hl=en.