def unquote_value(value): """Unquote a configuration value.""" if not value: return '' if value[0] in '"\'' and value[0] == value[-1]: value = value[1:-1].decode('string-escape') return soft_unicode(value)
def escape_rtf(val): if val == colander.null or val == None: val = u'' val = soft_unicode(val) r = '' prevc = None for c in val: if (c == '\n' and prevc != '\r') or (c == '\r' and prevc != '\n'): r += '\line ' elif (c == '\n' and prevc == '\r') or (c == '\r' and prevc == '\n'): pass elif c in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ': r += c else: r += '\u{}?'.format(ord(c)) prevc = c return r
def do_format(value, *args, **kwargs): """Apply the given values to a `printf-style`_ format string, like ``string % values``. .. sourcecode:: jinja {{ "%s, %s!"|format(greeting, name) }} Hello, World! In most cases it should be more convenient and efficient to use the ``%`` operator or :meth:`str.format`. .. code-block:: text {{ "%s, %s!" % (greeting, name) }} {{ "{}, {}!".format(greeting, name) }} .. _printf-style: https://docs.python.org/library/stdtypes.html #printf-style-string-formatting """ if args and kwargs: raise FilterArgumentError( "can't handle positional and keyword arguments at the same time") return soft_unicode(value) % (kwargs or args)
def do_trim(value, chars=None): """Strip leading and trailing characters, by default whitespace.""" return soft_unicode(value).strip(chars)
def do_wordcount(s): """Count the words in that string.""" return len(_word_re.findall(soft_unicode(s)))
def do_capitalize(s): """Capitalize a value. The first character will be uppercase, all others lowercase. """ return soft_unicode(s).capitalize()
def do_lower(s): """Convert a value to lowercase.""" return soft_unicode(s).lower()
def do_upper(s): """Convert a value to uppercase.""" return soft_unicode(s).upper()
def p(s): if s is colander.null or s is None: return u'' return soft_unicode(s)
def sk(x): return collator.sort_key(soft_unicode(x))
def nl2br(val): result = soft_unicode(Markup.escape(val)).replace("\n", Markup("<br />\n")) return Markup(result)
def converter(self, value=None): """Convert values to stripped unicode values""" return soft_unicode(value).strip()
def do_trim(value): """Strip leading and trailing whitespace.""" return soft_unicode(value).strip()