From: Georg Brandl Date: Sat, 14 Apr 2007 22:47:37 +0000 (+0200) Subject: [svn] Add striptags filter.^ X-Git-Tag: 2.0rc1~358 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=af31e4db58cc5c8dce24333e0f7cb583a6203df5;p=jinja2.git [svn] Add striptags filter.^ --HG-- branch : trunk --- diff --git a/CHANGES b/CHANGES index 5f7a71c..18b727a 100644 --- a/CHANGES +++ b/CHANGES @@ -41,6 +41,8 @@ Version 1.1 - added `sum`, `abs` and `round` filters. This fixes #238 +- added `striptags` filter. + Version 1.0 ----------- diff --git a/jinja/filters.py b/jinja/filters.py index e1d7d65..c83ed6a 100644 --- a/jinja/filters.py +++ b/jinja/filters.py @@ -8,6 +8,7 @@ :copyright: 2007 by Armin Ronacher. :license: BSD, see LICENSE for more details. """ +import re from random import choice from urllib import urlencode, quote from jinja.utils import urlize, escape @@ -618,6 +619,16 @@ def do_capture(name='captured', clean=False): return wrapped +def do_striptags(value, rex=re.compile(r'<[^>]+>')): + """ + Strip SGML/XML tags and replace adjacent whitespace by one space. + + *new in Jinja 1.1* + """ + return ' '.join(rex.sub('', value).split()) +do_striptags = stringfilter(do_striptags) + + def do_slice(slices, fill_with=None): """ Slice an iterator and return a list of lists containing @@ -787,6 +798,7 @@ FILTERS = { 'format': do_format, 'capture': do_capture, 'trim': do_trim, + 'striptags': do_striptags, 'slice': do_slice, 'batch': do_batch, 'sum': do_sum,