skip to navigation
skip to content

haufe.stripml 1.2.2

Python extension for removing markup from strings.

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 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" "">
... <html xmlns="" 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.
File Type Py Version Uploaded on Size
haufe.stripml-1.2.2.tar.gz (md5) Source 2012-11-08 11KB