Archive

Posts Tagged ‘bleach’

remove tags from HTML

July 13, 2016 Leave a comment

Problem
You have an HTML string and you want to remove all the tags from it.

Solution
Install the package “bleach” via pip. Then:

>>> import bleach
>>> html = "Her <h1>name</h1> was <i>Jane</i>."
>>> cleaned = bleach.clean(html, tags=[], attributes={}, styles=[], strip=True)
>>> html
'Her <h1>name</h1> was <i>Jane</i>.'
>>> cleaned
'Her name was Jane.'

Tip from here.

Advertisements
Categories: python Tags: , ,

Flask: linkify a text

July 12, 2015 Leave a comment

Problem
I have a text that I present in a Flask application. The text can contain URLs, and I would like to linkify them, i.e. make them clickable links. Example:

before:

visit http://google.com for...

after:

visit <a href="http://google.com">http://google.com</a> for...

Solution
Before rolling out an own solution, it’s a good idea to check if there is a package for this problem. There is :), and it’s called bleach. Its usage couldn’t be simpler:

>>> import bleach
>>> bleach.linkify('an http://example.com url')
u'an <a href="http://example.com" rel="nofollow">http://example.com</a> url

Flask integration
In your main file (that calls app.run()) add the following filter:

import bleach

@app.template_filter('linkify')
def linkify(s):
    return bleach.linkify(s)

Then use it in your jinja2 templates:

Description: {{ entry.description|linkify|safe }}

Warning! Apply the “safe” filter only if you trust the origin of the text you want to present in a linkified format.