Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

pypm install haufe.stripml

How to install haufe.stripml

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install haufe.stripml
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
1.2.2Never BuiltWhy not?
1.2.1 Failed View build log
Windows (64-bit)
1.2.2Never BuiltWhy not?
1.2.1 Failed View build log
Mac OS X (10.5+)
1.2.2Never BuiltWhy not?
1.2.1 Available View build log
Linux (32-bit)
1.2.2Never BuiltWhy not?
1.2.1 Available View build log
Linux (64-bit)
1.2.2 Available View build log
1.2.1 Available View build log
Lastest release
version 1.2.2 on Jan 9th, 2014

Package haufe.stripml

Python extension for stripping HTML markup from text.

This package simply removes HTML-like markup from text in a very quick and brutal manner. It could quite easily be used to strip XML or SGML from text as well. It does not do any syntax checking.

The core functionalities are implemented with the C++ programming language, and thus much quicker than using SGMLParser or regular expressions for the same task.


This package is released under the LGPL 3 See LICENSE.txt.


Use easy_install:

easy_install haufe.stripml

haufe.stripml can be tested by typing:

python setup.py test -m haufe.stripml.tests

Thanks to Gottfried Ganssauge for translating to the C++ programming language.

First we want to test the stripml method.

>>> from haufe.stripml import stripml
>>> stripml.__doc__
'stripml(s) -> string'

The one and only argument is a string.

>>> stripml('foo')
>>> type(stripml('foo')) == type('')

The stripml method supports unicode, too.

>>> stripml(u'bar')
>>> type(stripml(u'foo')) == type(u'')

Trying an integer as first argument. A TypeError should be raised.

>>> try:
...     stripml(10)
... except TypeError, strerror:
...     print strerror
String or unicode string required.

Empty script

>>> stripml ('<script>')
>>> stripml (u'<script>')
>>> stripml ('<script></script>')
>>> stripml (u'<script></script>')

Try some huge element name

>>> stripml ('<some-very-long-element-name-longer-than-foreseeable>')
>>> stripml (u'<some-very-long-element-name-longer-than-foreseeable>')

Now we try some dumb HTML ...

>>> stripml('<b>foo</b>')
>>> stripml('foo <i>bar</i>.')
'foo bar.'
>>> stripml('''<font size = 12><b>Really <i>big</i> string
... </b></font>''')
'Really big string\n'

... and now as unicode.

>>> stripml(u'<b>foo</b>')
>>> stripml(u'foo <i>bar</i>.')
u'foo bar.'
>>> stripml(u'''<font size = 12><b>Really <i>big</i> string
... </b></font>''')
u'Really big string\n'

Sometimes we have script tags, which contents nobody needs.

>>> stripml('''We have a script in here <script language="JavaScript"
... type="text/javascript">alert('Hello, World!');</script>, dude.''')
'We have a script in here , dude.'


>>> stripml(u'''We have a script in here <script language="JavaScript"
... type="text/javascript">alert('Hello, World!');</script>, dude.''')
u'We have a script in here , dude.'

But on the other hand the contents of scrip-Tags (without the trailing 't') should not be stripped

>>> stripml('<scrip>KEEP THIS</scrip>')
>>> stripml(u'<scrip>KEEP THIS</scrip>')

And neither should scripting-Tags

>>> stripml('<scripting>KEEP THIS</scripting>')
>>> stripml(u'<scripting>KEEP THIS</scripting>')
How about forgotten </script>-Tags
>>> stripml('KEEP <script>DO NOT KEEP THIS</script></script>THIS')
>>> stripml(u'KEEP <script>DO NOT KEEP THIS</script></script>THIS')

A much longer string.

>>> result = stripml(u'''
... <?xml version="1.0" encoding="utf-8"?>
... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
... <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
... <head>
... <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
... <meta name="generator" content="" />
... <meta name="keywords" content="" />
... <meta name="description" content="" />
... <title>Test document</title>
... <script language="JavaScript" type="text/javascript">
... var foo=1;
... function getFoo() {
...     return foo;
... }
... </script>
... </head>
... <body onLoad="alert('Hello, World!');">
...   <h1>Test document</h1>
...   <p>This document is<br /> <i>only for testing</i>!</p>
...   <script>getFoo();</script>
... </body>
... </html>
... ''')
>>> result.strip()
u'Test document\n\n\n\n  Test document\n  This document is only for testing!'
>>> type(result)
<type 'unicode'>

A single 'less then' or 'greater then' will be passed through.

>>> stripml(u'<strong>hundred < thousand < million.</strong>')
u'hundred < thousand < million.'
>>> stripml(u'<strong>thousand > hundred.</strong>')
u'thousand > hundred.'
>>> stripml('<strong>hundred < thousand < million.</strong>')
'hundred < thousand < million.'
>>> stripml('<strong>thousand > hundred.</strong>')
'thousand > hundred.'

Let's see if a really long string can be handled well.

>>> s = 5000 * u'<p>This is <span>a span within a paragraph.</span><!-- And this is a comment --></p>\n'
>>> stripml(s) == 5000 * u'This is a span within a paragraph.\n'

And we should have a look at entities and encodings.

>>> stripml(u'In Stra&szlig;e und &Uuml;berf&uuml;hrung haben wir Umlaute.')
u'In Stra&szlig;e und &Uuml;berf&uuml;hrung haben wir Umlaute.'
>>> stripml('In Stra&szlig;e und &Uuml;berf&uuml;hrung haben wir Umlaute.')
'In Stra&szlig;e und &Uuml;berf&uuml;hrung haben wir Umlaute.'
>>> print stripml(u'In Straße und Überführung haben wir Umlaute.').encode('ISO-8859-1') == u'In Straße und Überführung haben wir Umlaute.'.encode('ISO-8859-1')


1.2.2 (2012-11-07)
  • support for visual studio 2010 by M.Honeck
1.2.1 (2008-03-20)
  • added nose testrunner support
1.2.0 (2007-10-23)
  • First public release.
  • License added.

Subscribe to package updates

Last updated Jan 9th, 2014

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.