Skip to content
Snippets Groups Projects
Commit 258299b9 authored by Karl-Hermann Wieners's avatar Karl-Hermann Wieners
Browse files

Replaced workaround for wordwrap filter by private routine that allows to...

Replaced workaround for wordwrap filter by private routine that allows to suppress breaks on hyphens
parent a5cfe7c6
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,7 @@ import re ...@@ -11,6 +11,7 @@ import re
import stat import stat
import StringIO import StringIO
import sys import sys
import textwrap
from time import strftime from time import strftime
from configobj import ConfigObj from configobj import ConfigObj
...@@ -295,22 +296,6 @@ template_env = Environment( ...@@ -295,22 +296,6 @@ template_env = Environment(
extensions=['jinja2.ext.do'] extensions=['jinja2.ext.do']
) )
# Work-around for pre-2.7 Jinja (missing wrapstring arg for wordwrap filter)
# Helper functions to set and restore the newline sequence
@jinja2.environmentfunction
def push_newline_sequence(env, string):
if not hasattr(env, 'newline_stack'):
env.newline_stack = []
env.newline_stack.append(env.newline_sequence)
env.newline_sequence = string
template_env.globals['push_newline'] = push_newline_sequence
@jinja2.environmentfunction
def pop_newline_sequence(env):
env.newline_sequence = env.newline_stack.pop()
template_env.globals['pop_newline'] = pop_newline_sequence
# Helper function to get context information within templates # Helper function to get context information within templates
@jinja2.contextfunction @jinja2.contextfunction
def get_context(ctx, key=None): def get_context(ctx, key=None):
...@@ -318,6 +303,17 @@ def get_context(ctx, key=None): ...@@ -318,6 +303,17 @@ def get_context(ctx, key=None):
template_env.globals['context'] = get_context template_env.globals['context'] = get_context
# Additional filters # Additional filters
# A better wordwrap filter
@jinja2.environmentfilter
def wordwrap(env, s, width=79, break_long_words=True, break_on_hyphens=True,
wrapstring=None):
if wrapstring is None:
wrapstring = env.newline_sequence
tw = textwrap.TextWrapper(width=width, break_long_words=break_long_words,
break_on_hyphens=break_on_hyphens)
return wrapstring.join(tw.wrap(s))
template_env.filters['wordwrap'] = wordwrap
# - There's a join but no split operation... # - There's a join but no split operation...
template_env.filters['split'] = lambda x, s, m=-1: x.split(s, m) template_env.filters['split'] = lambda x, s, m=-1: x.split(s, m)
# - Add list operations filter and map # - Add list operations filter and map
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment