From af31e4db58cc5c8dce24333e0f7cb583a6203df5 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sun, 15 Apr 2007 00:47:37 +0200 Subject: [PATCH] [svn] Add striptags filter.^ --HG-- branch : trunk --- CHANGES | 2 ++ jinja/filters.py | 12 ++++++++++++ 2 files changed, 14 insertions(+) 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, -- 2.26.2