Skip to content

Instantly share code, notes, and snippets.

@xjamundx
Created July 24, 2012 02:24
Show Gist options
  • Select an option

  • Save xjamundx/3167615 to your computer and use it in GitHub Desktop.

Select an option

Save xjamundx/3167615 to your computer and use it in GitHub Desktop.
html5 validator
// HTML5 Validator using http://about.validator.nu/#api
exports.validate = function(data, done) {
var url = data.url
var html = data.html
if (!url && !html) return done(new Error("You forget a URL for the validator"))
if (html) return complete(html)
utils.printTitle("HTML5 Validator")
request(url, function(err, res, body) {
var statusCode = res.statusCode
if (!err && statusCode === 200) {
complete(body)
} else {
console.log("Status Code: " + statusCode)
console.log("Error:" + err)
}
})
function complete(body) {
validate(body, function(err, data) {
var output = ""
if (err) {
console.log("Error: " + err.message)
} else {
output = formatWarnings(data.warnings)
console.log(output)
console.log("\nValidation complete")
}
done(null, {results: output, warnings: data.warnings, html: data.html})
})
}
function validate(html, cb) {
var validatorOpts = {out: "json"}
var validatorURL = "http://html5.validator.nu/?" + qs.stringify(validatorOpts)
console.log("Hitting: "+ validatorURL + "\n")
request.post({url: validatorURL, headers: {"Content-type": "text/html"}, body: html}, function(err, res, body) {
if (err) return cb(err)
var data = JSON.parse(body)
var warnings = data.messages || []
cb(null, {warnings: warnings, html: html})
})
}
function formatWarnings(warnings) {
var len = warnings.length
var text = "Found " + len + " warnings\n-----------------------"
for (var i = 0; i < len; i++) {
text += "Line " + warnings[i].lastLine + ": " + warnings[i].message + "\n"
}
return text
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment