#!/usr/bin/env python """This is a toy example not a serious conformance checker. In particular, it only reports parse errors when reading the document; it does not report any of the other (many) possible types of conformance errors that may exist in a HTML5 document""" import sys import urllib2 import cgi import html5lib htmlTemplate = u"""
%s is valid HTML5!
%s"""%(uri, treeStr) writeOutput(htmlTemplate%{"title":"Validation Results", "body":bodyText}) def writeInvalid(uri, treeStr, errors): errList=[] for pos, message in errors: errList.append("Line %i Col %i"%pos + " " + message) errStr = "
%s is not valid HTML5
%s"""%(uri, errStr, treeStr) writeOutput(htmlTemplate%{"title":"Validation Results", "body":bodyText}) def writeErr(uri): bodyText = "
Failed to load URI %s
"%(uri,) writeOutput(htmlTemplate%{"title":"Error", "body":bodyText}) def writeOutput(s): print s.encode('utf-8') print "Content-type: text/html" print "" try: form = cgi.FieldStorage() uri = form.getvalue("uri") document = getDocument(uri) except: writeErr(uri) sys.exit(1) errors, tree = parseDocument(document) if errors: writeInvalid(uri, tree, errors) else: writeValid(uri, tree)