diff --git a/mkexp b/mkexp index aef02fa566d54675421848c4fdbb3c1c34fb0d47..6d32fa8496abddfb184b78e726782ccd7bf45a8d 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