PylonsHQ.

Layout: Fixed-width

Automatic HTML validation

Skip to end of metadata
Go to start of metadata

(From http://blog.workaround.org/2009/01/26/automatic-html-validation-of-pylons-or-any-wsgi-applications/)

Paste's paste.debug.wdg_validate module allows automatic validation of the HTML that a Pylons controller outputs. It is pretty simple to use that layer of WSGI middleware to automatically show HTML validation errors at the end of each page.

All there is to do is 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')

If you do not like to hardcode such functionality you can enable it only if a certain variable (e.g. 'html_validator') in the application's INI file is set that points to the 'validate' binary. And only validate 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'])

In the INI file you would then set the html_validator variable to point to the validate tool. (It depends on the sources you can get from the WDG web site. Of if you are a lucky Debian user you can just "aptitude install wdg-html-validator" and the validate binary will be in /usr/bin/validate.)

[app:main]

html_validator = /usr/bin/validate

Christoph Haas

Patch for a couple of minor issues with paste.debug.wdg_validate.py

There a couple of minor problems with the validator middleware code. Here's a patch:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
--- paste/debug/wdg_validate.py 2009-01-29 00:59:18.000000000 +0000
+++ paste/debug/wdg_validate.py.new     2009-01-29 01:03:03.000000000 +0000
@@ -72,9 +72,13 @@
         # @@: Should capture encoding too
         html_errors = self.call_wdg_validate(
             self.wdg_path, ops, page)
-        if not html_errors:
-            return [page]
-        return self.add_error(page, html_errors)
+        if html_errors:
+            page = self.add_error(page, html_errors)[0]
+            headers.remove(
+                     ('Content-Length', 
+                      str(wsgilib.header_value(headers, 'content-length'))))
+            headers.append(('Content-Length', str(len(page))))
+        return [page]
     
     def call_wdg_validate(self, wdg_path, ops, page):
         if subprocess is None:
@@ -99,7 +103,7 @@
         if match:
             return [html_page[:match.start()]
                     + add_text
-                    + html_page[match.end():]]
+                    + html_page[match.start():]]
         else:
             return [html_page + add_text]

The patch i) corrects the use of match to wrap the original page content around the error report and ii) sets the response Content-Length to reflect any included error report.

OS X users

When compiling the lq-sp parser you may encounter a compilation failure:

ld: in ../lib/liblq-sp.a, archive has no table of contents
collect2: ld returned 1 exit status
make[2]: *** [lq-nsgmls] Error 1
make[1]: *** [nsgmls] Error 2
make: *** [all] Error 2

If so, run

1
$ ranlib lib/liblq-sp.a

Ignore this apparently alarming output:

ranlib: file: lib/liblq-sp.a(entmgr_inst.o) has no symbols
ranlib: file: lib/liblq-sp.a(xentmgr_inst.o) has no symbols
ranlib: file: lib/liblq-sp.a(parser_inst.o) has no symbols
ranlib: file: lib/liblq-sp.a(app_inst.o) has no symbols
ranlib: file: lib/liblq-sp.a(arc_inst.o) has no symbols

Try the "make" again:

$ make
make[2]: Nothing to be done for `all'.
c++ -O2 -ansi   -I. -I./../include  -DSP_HAVE_BOOL -DSP_ANSI_CLASS_INST 
  -DSP_MULTI_BYTE -DSP_HAVE_SOCKET -o lq-nsgmls   nsgmls.o SgmlsEventHandler.o 
  RastEventHandler.o StringSet.o nsgmls_inst.o  ../lib/liblq-sp.a -lm 

Cheers,

Graham Higgins

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Powered by Pylons - Contact Administrators