/
htmlhint.js
71 lines (59 loc) · 1.97 KB
/
htmlhint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* grunt-htmlhint
* https://github.com/yaniswang/grunt-htmlhint
*
* Copyright (c) 2013 Yanis Wang
* Licensed under the MIT license.
*/
'use strict';
module.exports = function(grunt) {
grunt.registerMultiTask('htmlhint', 'Validate html files with htmlhint.', function() {
var HTMLHint = require("htmlhint").HTMLHint;
var options = this.options({
force: false
}),
arrFilesSrc = this.filesSrc,
verbose = grunt.verbose;
if (options.htmlhintrc) {
var rc = grunt.file.readJSON(options.htmlhintrc);
grunt.util._.defaults(options, rc);
delete options.htmlhintrc;
}
var force = options.force;
delete options.force;
var hintCount = 0;
var fileCount = 0;
arrFilesSrc.forEach(function(filepath) {
var file = grunt.file.read(filepath),
msg = " " + filepath,
messages;
if (file.length) {
messages = HTMLHint.verify(file, options);
if (messages.length > 0) {
grunt.log.writeln(msg);
var arrLogs = HTMLHint.format(messages, {
colors: true,
indent: 6
});
arrLogs.forEach(function(log){
grunt.log.writeln(log);
});
grunt.log.writeln('');
hintCount += messages.length;
fileCount ++;
}
}
});
if (hintCount > 0) {
grunt.log.error('%d errors in %d files'.red, hintCount, fileCount);
return force;
}
else{
verbose.ok();
}
grunt.log.ok(arrFilesSrc.length + ' file' + (arrFilesSrc.length === 1 ? '' : 's') + ' lint free.');
});
function repeatStr(n, str){
return new Array(n + 1).join(str || ' ');
}
};