Jacek Artymiak wrote to tell me about a new project of his, pyxhtml, a tool for automatically generating XHTML pages. Artymiak is a coauthor of the StarOffice for Linux Bible and Install, Configure, and Customize Slackware Linux, though he is probably better known in the Python community for his articles on beginning Python. He is working on a Python CGI programming book that will cover XML and XHTML as well.
In case you haven't been hanging around in the XML world, XHTML is an implementation of HTML in XML instead of XML's much looser parent language, SGML. For the HTML coder moving to XHTML, that means you must follow XML's stricter rules in creating your documents. Something to help automate the construction of well-formed documents sounds like a good idea to me. I decided to take a look.
Sometimes there is no better learning opportunity than getting in on a project at its humble beginnings. Release .01 of pyxhtml is only 227 lines, and most of those are comments. It defines one class, webdoc, that handles page generation. The program is extremely straightforward. Some defaults are set for what kind of encoding the page will have and what XHTML version to use, the stuff you need to build a generic header for an XHTML file, and a simple bit of body text to use if none is specified. Tossing
pyxhtml.py into my Lib directory, I fired up IDLE and slammed out a quick document like this.
That wasn't too difficult! The main function of webdoc is
webdoc.make(). It pieces together a header with
information you provide for
webdoc.title and then tacks
on a body you define in
webdoc.body. Other utility
functions let you turn off the HTTP header that provides the
content-type, and you change most of the attributes of the
Playing around, I found some nice .01 warts. Anytime you use a
utility function, it runs
webdoc.make() and recreates the
document from your new settings. Well, almost. It doesn't replace
the existing document, it just appends the new document. So your
document just keeps getting longer and longer. To generate it
correctly, I had to set
mydoc.document='' and run
mydoc.make() again after setting the features I wanted.
In my short bit of testing, I also found that the method to change the XHTML
version did not work. I wrote Artymiak back with a couple of suggested
changes. So, there you go, open source in action.
There is plenty of work to be done. There are no functions to assist in the construction of the body of your text, only the document headers. Often the headers are the least of my worries. If you have a need for production XHTML code today, consider the more robust and complete package HyperText. HyperText generates HTML, XML, and XHTML documents. If, however, you have been trying to ramp up your Python programming so you can use it for your CGI projects, consider lending a hand to the pyxhtml project. You may learn a lot and you will be helping yourself by helping to build a useful tool.
Stephen Figgins administrates Linux servers for Sunflower Broadband, a cable company.
Read more Python News columns.
Discuss this article in the O'Reilly Network Python Forum.
Return to the Python DevCenter.
Copyright © 2009 O'Reilly Media, Inc.