html-validator
Advanced tools
Comparing version 0.0.2 to 0.0.3
39
index.js
@@ -1,18 +0,35 @@ | ||
var request = require('request') | ||
, reqOpts = {uri : 'http://html5.validator.nu', qs : {}}; | ||
var request = require('request'); | ||
module.exports = function(opts, callback){ | ||
function mkReqOpts(opts){ | ||
var newOpts = { | ||
uri: 'http://html5.validator.nu', | ||
qs: {out:opts.format}, | ||
method: 'GET' | ||
}; | ||
if(!opts.url){ | ||
return callback(new Error('Missing required option: url'), null); | ||
} else { | ||
reqOpts.qs.doc = opts.url; | ||
if(opts.url){ | ||
newOpts.qs.doc = opts.url; | ||
} | ||
if(opts.format){ | ||
reqOpts.qs.out = opts.format; | ||
if(opts.data){ | ||
newOpts.body = opts.data; | ||
newOpts.method = 'POST'; | ||
newOpts.headers = { | ||
'Content-Type': 'text/html; charset=utf-8' | ||
}; | ||
} | ||
request(reqOpts, function(error, response, body){ | ||
return newOpts; | ||
} | ||
module.exports = function(opts, callback){ | ||
if(!opts.format || (!opts.url && !opts.data)){ | ||
return callback(new Error('Missing required params'), null) | ||
} | ||
var reqOpts = mkReqOpts(opts); | ||
request(reqOpts, function(error, response, result){ | ||
if(error){ | ||
@@ -22,3 +39,3 @@ return callback(error, null); | ||
var data = opts.format == 'json' ? JSON.parse(body) : body; | ||
var data = opts.format == 'json' ? JSON.parse(result) : result; | ||
@@ -25,0 +42,0 @@ return callback(null, data); |
{ | ||
"name": "html-validator", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Validates html using validator.nu", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,3 +5,5 @@ #html-validator# | ||
At the moment only the URL as a GET parameter is implemented from the Validator.nu [Web Service Interface](http://wiki.whatwg.org/wiki/Validator.nu_Web_Service_Interface) | ||
Supports the following modes from Validator.nu [Web Service Interface](http://wiki.whatwg.org/wiki/Validator.nu_Web_Service_Interface) | ||
- Document URL as a GET parameter; the service retrieves the document by URL over HTTP or HTTPS. | ||
- Document POSTed as the HTTP entity body; parameters in query string as with GET. | ||
@@ -18,6 +20,7 @@ ##Installation## | ||
**url** is required and is the url to the page you want to validate. | ||
**format** This is the formatting of the returned data and it is required. It supports json, html, xhtml, xml, gnu and text. | ||
**format** This is the formatting of the returned data. It supports json, html, xhtml, xml, gnu and text. | ||
**url** The url to the page you want to validate. | ||
```javascript | ||
@@ -36,2 +39,26 @@ var validator = require('html-validator') | ||
``` | ||
**data** The html you want to validate | ||
```javascript | ||
var validator = require('html-validator') | ||
, fs = require('fs') | ||
, opts = { | ||
format : 'json' | ||
}; | ||
fs.readFile( 'file-to-validate.html', 'utf8', function( err, html ) { | ||
if (err) throw err; | ||
opts.data = html; | ||
validator(opts, function(err, data){ | ||
if(err) throw err; | ||
console.log(data); | ||
}); | ||
}); | ||
``` |
3775
31
61