urlize HTML

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
@register.filter("urlize_html")
def urlize_html(html):
    """
    Returns urls found in an (X)HTML text node element as urls via Django urlize filter.
    """
    try:
        from BeautifulSoup import BeautifulSoup
        from django.utils.html import urlize
    except ImportError:
        if settings.DEBUG:
            raise template.TemplateSyntaxError, "Error in urlize_html The Python BeautifulSoup libraries aren't installed."
        return html
    else:
        soup = BeautifulSoup(html)
           
        textNodes = soup.findAll(text=True)
        for textNode in textNodes:
            urlizedText = urlize(textNode)
            textNode.replaceWith(urlizedText)
            
        return str(soup)

More like this

  1. Revisiting Pygments and Markdown by djypsy 5 years, 9 months ago
  2. Sanitize text field HTML (here from the Dojo Toolkit Editor2 widget) by akaihola 6 years, 1 month ago
  3. Prettify HTML body contents in HTTP response by n1k0 2 years, 5 months ago
  4. Whore links by nathan 6 years, 2 months ago
  5. typygmentdown by ubernostrum 5 years, 9 months ago

Comments

Samus_ (on June 19, 2010):

congratulations!

#

(Forgotten your password?)