Posts Tagged ‘BeautifulSoup


First library for my CMS started!

I finaly started coding for my CMS. I’m not sure if I’m already up to the job, but I’ll find out soon enough. The first library is part of the template engine. I don’t want people to write crap HTML, so I wrote a library that can generate strict XHTML with python code that is as similar to html as possible, see below for an example.

The big question remains, what to do with user content, even valid XHTML theme break if someone pastes his Word HTML in the editor. I plan on using BeautifulSoup to parse the user code, strip off the ‘invalid’ tags like <i>, <u>, <iframe>, etc… and feed it to my XHTMLLib. The even cooler part is that you could use your old HTML theme, feed it to my lib and fill/replace the content. With the latest version this became even easyer because I added some sort of xpath support(absolute paths only, but with attribute selection).

I present to you, my first lib!

Link: Also available in the sidebar widget. You need as well!


page = XHTMLLib().template() # generate a basic XHTML template
    page.xpath('/html/head/title')['child'] = "Hello world" # set the title

    page.function_factory(['div', 'img', 'fieldset', 'input', 'ul', 'li'], '', '') # register functions

    lists = [li(class_='xtra', child="Home"), li(class_="test", child="Links"), li(child="Contact"), li(class_="test", child="About"), li(child="Sitemap")] # create a list
    lists.sort() # sort elements

    page.xpath('/html/body')['child'] = [ # add to the body
        div(id_='header', child='test & more'), # Entities get converted
        fieldset(class_='test', child=[
            input(type_='text', name='name'),
        ul(child=lists) # Comppose pages of different object sets.

    page.xpath('/html/body/img').__setitem__('onclick', 'alert("!@#$^, stop clicking!")', True) # Voodoo magic to get evil js in, alsays use external js!
    page.xpath('/html/body/ul/li[class=test][4]')['style'] = 'color:red' # Some more 'advanced xpath' to get a specific list item

    print page.render();


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
		<meta content="application/xhtml+xml;charset=utf-8" http-equiv="Content-Type"></meta>
		<title>Hello world</title>
		<div id="header">test &amp; more</div>
		<img src="image.jpg" onclick="<![CDATA[alert("!@#$^, stop clicking!")]]>"></img>
		<fieldset class="test">
			<input type="text" name="name"></input>
			<input type="submit"></input>
			<li class="test">About</li>
			<li class="xtra">Home</li>
			<li style="color:red" class="test">Links</li>

My blog has moved!

My blog has permanently moved to a self hosted Wordpress at

This blog will stay around for accidental search engine visitors.


This is me

Blog Stats

  • 22,921 hits