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. ultralize by catellar 2 years, 7 months ago
  2. youtubize template tag by TheJester 6 years, 11 months ago
  3. plaintext filter by onelson 4 years ago
  4. Revisiting Pygments and Markdown by djypsy 6 years, 8 months ago
  5. twitterize filter by thomasw 5 years ago

Comments

Samus_ (on June 19, 2010):

congratulations!

#

(Forgotten your password?)