Created
June 27, 2013 03:54
-
-
Save ahomu/5873817 to your computer and use it in GitHub Desktop.
grunt-html5validator .... 0.4.xで動作するかは不明 X(
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* grunt-html5validator | |
* | |
* @author ahomu | |
*/ | |
module.exports = function(grunt) { | |
'use strict'; | |
var TEMP_HTTP_RESOURCE = '.h5v.http.tmp', | |
TEMP_GZIP_RESPONSE = '.h5v.tmp'; | |
var http = require('http'), | |
fs = require('fs'), | |
zlib = require('zlib'), | |
gzip = zlib.createGzip(), | |
util = grunt.utils || grunt.util, | |
_ = util._, | |
async = util.async, | |
post_options = { | |
host: 'html5.validator.nu', | |
port: 80, | |
path: '/?out=json', | |
method: 'POST', | |
headers: { | |
// 'Host': 'html5.validator.nu', | |
'Accept-Encoding': 'gzip', | |
'Content-Type': 'text/html', | |
'Content-Encoding': 'gzip' | |
} | |
}; | |
grunt.registerMultiTask('h5v', 'Validator your html', function() { | |
var options = this.options ? this.options() : this.data.options, // for 0.4.0 | |
srcFiles = options.http ? this.data.src : grunt.file.expandFiles(this.data.src), | |
done = this.async(); | |
async.forEachSeries(srcFiles, function(src, next) { | |
var req = http.request(post_options); | |
// on response | |
req.on('response', function(res) { | |
grunt.log.subhead('validation....' + src); | |
// write to temporary file | |
var output = fs.createWriteStream(TEMP_GZIP_RESPONSE); | |
res.pipe(zlib.createGunzip()).pipe(output); | |
res.on('end', function() { | |
var json = JSON.parse(grunt.file.read(TEMP_GZIP_RESPONSE)), | |
fail = false; | |
json.messages.forEach(function(mess) { | |
switch (mess.type) { | |
case 'error': | |
grunt.log.writeln(('[WARN]').red + (' L:' + mess.lastLine).green + ' ' + mess.message); | |
fail = true; | |
break; | |
case 'info': | |
grunt.log.writeln(('[INFO]').yellow + (' L:' + mess.lastLine).green + ' ' + mess.message); | |
break; | |
} | |
}); | |
if (fail) { | |
grunt.log.error('There were errors. (Tried in the text/html mode.)'); | |
} else { | |
grunt.log.ok('The document is valid HTML5 + ARIA + SVG 1.1 + MathML 2.0 (subject to the utter previewness of this service).'); | |
} | |
fs.unlinkSync(TEMP_GZIP_RESPONSE); | |
next(); | |
}); | |
}); | |
/** | |
* @param {String} content | |
*/ | |
var writeGzipedContent = function(content) { | |
zlib.gzip(content, function(_, gzipbuff) { | |
req.write(gzipbuff); | |
req.end(); | |
}); | |
}; | |
/** | |
* Write gzipped buffer | |
*/ | |
if (fs.existsSync(src)) { | |
// local file | |
writeGzipedContent(grunt.file.read(src)); | |
} else if (src.indexOf('http') === 0) { | |
// load external resource | |
http.get(src, function(res) { | |
res.pipe(fs.createWriteStream(TEMP_HTTP_RESOURCE)); | |
res.on('end', function() { | |
writeGzipedContent(grunt.file.read(TEMP_HTTP_RESOURCE)); | |
fs.unlinkSync(TEMP_HTTP_RESOURCE); | |
}); | |
}); | |
} | |
}, done); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment