|
| 1 | +/* |
| 2 | + * grunt-htmlhint-inline |
| 3 | + * https://github.com/kazu69/grunt-htmlhint-inline |
| 4 | + * |
| 5 | + * Copyright (c) 2015 kazu69 |
| 6 | + * Licensed under the MIT license. |
| 7 | + */ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +module.exports = function (grunt) { |
| 12 | + |
| 13 | + var lintinline = require('./lib/inlinehint'), |
| 14 | + HTMLHint = require("htmlhint").HTMLHint, |
| 15 | + path = require('path'); |
| 16 | + |
| 17 | + grunt.registerMultiTask('htmlhint_inline', 'Validate inline html with htmlhint', function () { |
| 18 | + var done = this.async(), |
| 19 | + log = grunt.log, |
| 20 | + output = ''; |
| 21 | + |
| 22 | + var options = this.options({ |
| 23 | + force: false, |
| 24 | + reporterOutput: null, |
| 25 | + patterns: [], |
| 26 | + htmlhintrc: null |
| 27 | + }); |
| 28 | + |
| 29 | + var force = options.force; |
| 30 | + delete options.force; |
| 31 | + |
| 32 | + var reporterOutput = options.reporterOutput; |
| 33 | + delete options.reporterOutput; |
| 34 | + |
| 35 | + var patterns = options.patterns; |
| 36 | + delete options.patterns; |
| 37 | + |
| 38 | + var htmlhintrc = options.htmlhintrc |
| 39 | + delete options.htmlhintrc |
| 40 | + |
| 41 | + var ignore = options.ignore; |
| 42 | + delete options.ignore; |
| 43 | + |
| 44 | + if (htmlhintrc) { |
| 45 | + var _htmlhintrc = grunt.file.readJSON(htmlhintrc); |
| 46 | + // set to the default options value |
| 47 | + grunt.util._.defaults(options, _htmlhintrc); |
| 48 | + } |
| 49 | + |
| 50 | + // output pre hook |
| 51 | + if (reporterOutput) { |
| 52 | + grunt.util.hooker.hook(process.stdout, 'write', { |
| 53 | + pre: function(stdout) { |
| 54 | + output += stdout; |
| 55 | + // https://github.com/cowboy/javascript-hooker#hookerpreempt |
| 56 | + return grunt.util.hooker.preempt(); |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + var tempFiles = lintinline.wrapReporter(options, this.filesSrc, ignore, patterns), |
| 62 | + tempFilesLength = Object.keys(tempFiles).length, |
| 63 | + hintCount = 0, eachCount = 0; |
| 64 | + |
| 65 | + Object.keys(tempFiles).forEach(function( file ) { |
| 66 | + var realFilePath = tempFiles[file]['filepath'], |
| 67 | + file = grunt.file.read( file ), |
| 68 | + msg = "Linting " + realFilePath + "...", |
| 69 | + messages; |
| 70 | + |
| 71 | + if (file.length) { |
| 72 | + messages = HTMLHint.verify(file, options); |
| 73 | + |
| 74 | + log.write(msg); |
| 75 | + |
| 76 | + (messages.length > 0)? log.error() : log.ok(); |
| 77 | + |
| 78 | + messages.forEach(function( message ) { |
| 79 | + log.writeln( |
| 80 | + "[".red + ( "L" + message.line ).yellow +":".red + |
| 81 | + ( "C" + message.col ).yellow + "]".red + ' ' + message.message.yellow |
| 82 | + ); |
| 83 | + |
| 84 | + var evidence = message.evidence, |
| 85 | + col = message.col; |
| 86 | + if (col === 0) { |
| 87 | + evidence = '?'.red + evidence; |
| 88 | + } else if(col > evidence.length) { |
| 89 | + evidence = evidence + ' '.red; |
| 90 | + } else { |
| 91 | + evidence = evidence.slice(0, col - 1) + evidence[col - 1].red + evidence.slice(col); |
| 92 | + } |
| 93 | + log.writeln(evidence); |
| 94 | + hintCount ++; |
| 95 | + }); |
| 96 | + } |
| 97 | + else { log.writeln( "Skipping empty file " + file); } |
| 98 | + |
| 99 | + if (reporterOutput) { |
| 100 | + var destDir = path.dirname(reporterOutput); |
| 101 | + if (eachCount >= tempFilesLength) { |
| 102 | + grunt.util.hooker.unhook(process.stdout, 'write'); |
| 103 | + } |
| 104 | + |
| 105 | + if (!grunt.file.exists(destDir)) { |
| 106 | + grunt.file.mkdir(destDir); |
| 107 | + grunt.file.write(options.filePath, ''); |
| 108 | + } |
| 109 | + |
| 110 | + reporterOutput = grunt.template.process(reporterOutput); |
| 111 | + grunt.file.write(reporterOutput, output); |
| 112 | + } |
| 113 | + |
| 114 | + eachCount++; |
| 115 | + |
| 116 | + if ( hintCount > 0 ) return force; |
| 117 | + |
| 118 | + log.ok( |
| 119 | + tempFilesLength + ' file' + |
| 120 | + (tempFilesLength === 1 ? '' : 's') + ' lint free.' |
| 121 | + ); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}; |
0 commit comments