Skip to content

Commit

Permalink
Merge pull request #206 from johnmcdonnell/wordwrap
Browse files Browse the repository at this point in the history
Allow choice of wrap string in wordwrap filter.
  • Loading branch information
mitsuhiko committed May 18, 2013
2 parents 2b0231c + 012d017 commit 9aa4bdd
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions jinja2/filters.py
Expand Up @@ -470,15 +470,20 @@ def do_truncate(s, length=255, killwords=False, end='...'):
return u' '.join(result)

@environmentfilter
def do_wordwrap(environment, s, width=79, break_long_words=True):
def do_wordwrap(environment, s, width=79, break_long_words=True,
wrapstring=None):
"""
Return a copy of the string passed to the filter wrapped after
``79`` characters. You can override this default using the first
parameter. If you set the second parameter to `false` Jinja will not
split words apart if they are longer than `width`.
split words apart if they are longer than `width`. By default, the newlines
will be the default newlines for the environment, but this can be changed
using the wrapstring keyword argument.
"""
if not wrapstring:
wrapstring = environment.newline_sequence
import textwrap
return environment.newline_sequence.join(textwrap.wrap(s, width=width, expand_tabs=False,
return wrapstring.join(textwrap.wrap(s, width=width, expand_tabs=False,
replace_whitespace=False,
break_long_words=break_long_words))

Expand Down

0 comments on commit 9aa4bdd

Please sign in to comment.