Login

plaintext filter

Author:
onelson
Posted:
April 9, 2010
Language:
Python
Version:
1.1
Tags:
template filter striptags
Score:
1 (after 1 ratings)

Inspired by this terse blog post.

This filter was designed to simplify the stripping out of all (x)html in a given template var, while preserving some meta information from anchor, and image tags.

Why is this even useful? If you have pre-assembled portions of templates, or model fields containing html, that you want to use to populate a search index like django-haystack you can safely discard all the markup, while keeping the text that should be still searchable. Alt text, and title attributes are worth keeping!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from django import template
register = template.Library()
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
from BeautifulSoup import BeautifulSoup, Tag, NavigableString

@register.filter(name='plaintext')
@stringfilter
def plaintext(value):
    soup = BeautifulSoup(value)
    anchors = soup.findAll('a')
    for a in anchors:
        substitute = Tag(soup, 'span')
        substitute.insert(0,a.string)
        meta = []
        attrs = [k for k,v in a.attrs]
        if 'title' in attrs: meta.append(a['title'])
        if 'href' in attrs: meta.append(a['href'])
        if meta: substitute.insert(1,NavigableString(' (%s)' % ', '.join(meta)))
        a.replaceWith(substitute)
    
    images = soup.findAll('img')
    for img in images:
        substitute = Tag(soup,'span')
        meta = []
        attrs = [k for k,v in img.attrs]
        if 'src' in attrs: meta.append(img['src'])
        if 'title' in attrs: meta.append(img['title'])
        if 'alt' in attrs: meta.append(img['alt'])
        if meta: substitute.insert(0,NavigableString(' (%s)' % ', '.join(meta)))
        img.replaceWith(substitute)
    return mark_safe(''.join(soup.findAll(text=True)))
plaintext.mark_safe = True

More like this

  1. Tags & filters for rendering search results by exogen 7 years, 4 months ago
  2. Create PDF files using rml and django templates by mporrato 8 years, 1 month ago
  3. keeptags: strip all HTML tags from output except a specified list of elements by chrominance 8 years, 1 month ago
  4. Auto HTML Linebreak filter by punteney 7 years, 3 months ago
  5. Template tag for stripping blank lines by akaihola 7 years, 6 months ago

Comments

Please login first before commenting.