Automatic HTML validation of Pylons (or any WSGI) applications
While many other "web designers" create hacky websites (that break in many funny way once the browser developers figure out how to do their jobs right making the hacks fail) I almost always try to use W3C's web standards. Just that sometimes I forget to validate my own pages for proper CSS or (X)HTML. Thanks to the great "Web Developer" Firefox plugin I can press Ctrl+Shift+A and validate my local (X)HTML using their validation service. But I felt that a more automatic way would be nice. Besides I use Pylons (a shiny Python web framework) for a few closed-source applications and don't want to send that information to a public web site. So a while back we were discussing automatic validation in Pylons applications. And today Michael van Tellingen told me in #pylons that Ian Bicking's Paste has automatic validation built-in as a WSGI middleware.
(For those who don't know WSGI. It's an interface similar to CGI that is used by Python web frameworks. It's what is spoken between an application creating the HTML output and the web server. WSGI allows you to hook up several layers of functionality which are called "middleware". Middleware can be used for example for authentication or for intercepting errors. It works similar to a UNIX pipe. The output of one layer is the input to the next layer. You want to profile every HTML page? Just add profiling middleware.)
So all I had to do was import the paste.debug.wdg_validate module and use the middleware in the right place. One way to do that is edit the config/middleware.py file of a Pylons application and just insert this line where it reads "CUSTOM MIDDLEWARE HERE":
app = paste.debug.wdg_validate.WDGValidateMiddleware(
app, global_conf, wdg_path='/usr/bin/validate')
But I'm not a big fan of hardcoding such functionality. So I enabled it only if a certain variable ('html_validator') in the application's INI file is set that points to the 'validate' binary and if the application is run in debug mode like during development:
if config['debug'] is True and 'html_validator' in config:
import paste.debug.wdg_validate
app = paste.debug.wdg_validate.WDGValidateMiddleware(app, global_conf,
wdg_path=config['html_validator'])
And in the INI file I put:
[app:main]
html_validator = /usr/bin/validate
The validation software is from the WDG's web site. But in case you are lucky Debian user an "aptitude install wdg-html-validator" is all it takes.
Voila - now every (X)HTML validation error gets displayed on the bottom of a page. The output isn't very nifty but it's sufficient to find typos without having to run every single page through the validator. And it's completely local thus being faster and avoiding to disclose internal HTML data to the W3C web site.
Recent comments
4 days 15 hours ago
4 days 20 hours ago
4 days 22 hours ago
6 days 9 hours ago
6 days 21 hours ago
1 week 4 hours ago
1 week 6 hours ago
1 week 14 hours ago
1 week 16 hours ago
1 week 1 day ago