From 258299b932bb50d013f543be0f70cc6cbfb4ce97 Mon Sep 17 00:00:00 2001 From: Karl-Hermann Wieners <karl-hermann.wieners@mpimet.mpg.de> Date: Tue, 15 Dec 2015 22:54:19 +0000 Subject: [PATCH] Replaced workaround for wordwrap filter by private routine that allows to suppress breaks on hyphens --- mkexp | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/mkexp b/mkexp index aef02fa..6d32fa8 100755 --- a/mkexp +++ b/mkexp @@ -11,6 +11,7 @@ import re import stat import StringIO import sys +import textwrap from time import strftime from configobj import ConfigObj @@ -295,22 +296,6 @@ template_env = Environment( 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 @jinja2.contextfunction def get_context(ctx, key=None): @@ -318,6 +303,17 @@ def get_context(ctx, key=None): template_env.globals['context'] = get_context # 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... template_env.filters['split'] = lambda x, s, m=-1: x.split(s, m) # - Add list operations filter and map -- GitLab