Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wordwrap doesn't allow user override of break_on_hyphens to False #550

Closed
ryanarn opened this issue Feb 23, 2016 · 0 comments · Fixed by #829
Closed

wordwrap doesn't allow user override of break_on_hyphens to False #550

ryanarn opened this issue Feb 23, 2016 · 0 comments · Fixed by #829
Milestone

Comments

@ryanarn
Copy link

@ryanarn ryanarn commented Feb 23, 2016

In Jinja2.7 the following URLs passed through the wordwrap filter with break_long_words=False still end up breaking on hyphens, periods, and slashes. In my case this is not desirable as I want URLs without white-space to not break at all.

Here's an example that wraps at the first "-" character:

{{ "https://docs.python.org/2/library/textwrap.html#textwrap.TextWrapper.wrap-this-lineis-goingtowrap"|wordwrap(73, False) }}

Adding the following custom wordwrap function which sets break_on_hyphens=False and invoking with customwordwrap() corrects the issue:

@environmentfilter
def do_customwordwrap(environment, s, width=79, break_long_words=True,
                wrapstring=None, break_on_hypens=False):
    """
    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`. 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 wrapstring.join(textwrap.wrap(s, width=width, expand_tabs=False,
                                   replace_whitespace=False,
                                   break_long_words=break_long_words,
                                   break_on_hyphens=break_on_hypens))

{{ "https://docs.python.org/2/library/textwrap.html#textwrap.TextWrapper.wrap-this-lineis-goingtowrap"|customwordwrap(73, False) }}

This also corrects wrapping on '.' and on '/' in long unbroken URLs as well.

I suggest exposing break_on_hypens as a parameter to the wordwrap filter as was done with break_long_words as demonstrated by the custom wordwrap filter I provided.

aogier added a commit to aogier/jinja that referenced this issue Mar 23, 2018
aogier added a commit to aogier/jinja that referenced this issue Mar 23, 2018
aogier added a commit to aogier/jinja that referenced this issue Mar 23, 2018
aogier added a commit to aogier/jinja that referenced this issue Mar 29, 2018
@davidism davidism added this to the 2.11.0 milestone Nov 1, 2019
@davidism davidism closed this in #829 Nov 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants
You can’t perform that action at this time.