1 | /*
|
---|
2 | * grunt-htmlhint
|
---|
3 | * https://github.com/yaniswang/grunt-htmlhint
|
---|
4 | *
|
---|
5 | * Copyright (c) 2013 Yanis Wang
|
---|
6 | * Licensed under the MIT license.
|
---|
7 | */
|
---|
8 |
|
---|
9 | 'use strict';
|
---|
10 |
|
---|
11 | module.exports = function(grunt) {
|
---|
12 |
|
---|
13 | grunt.registerMultiTask('htmlhint', 'Validate html files with htmlhint.', function() {
|
---|
14 |
|
---|
15 | var HTMLHint = require("htmlhint").HTMLHint;
|
---|
16 | var options = this.options({
|
---|
17 | force: false
|
---|
18 | }),
|
---|
19 | arrFilesSrc = this.filesSrc,
|
---|
20 | verbose = grunt.verbose;
|
---|
21 |
|
---|
22 | if (options.htmlhintrc) {
|
---|
23 | var rc = grunt.file.readJSON(options.htmlhintrc);
|
---|
24 | grunt.util._.defaults(options, rc);
|
---|
25 | delete options.htmlhintrc;
|
---|
26 | }
|
---|
27 |
|
---|
28 | var force = options.force;
|
---|
29 | delete options.force;
|
---|
30 |
|
---|
31 | var hintCount = 0;
|
---|
32 | arrFilesSrc.forEach(function( filepath ) {
|
---|
33 | var file = grunt.file.read( filepath ),
|
---|
34 | msg = "Linting " + filepath + "...",
|
---|
35 | messages;
|
---|
36 | if (file.length) {
|
---|
37 | messages = HTMLHint.verify(file, options);
|
---|
38 | verbose.write( msg );
|
---|
39 | if (messages.length > 0) {
|
---|
40 | verbose.or.write( msg );
|
---|
41 | grunt.log.error();
|
---|
42 | } else {
|
---|
43 | verbose.ok();
|
---|
44 | }
|
---|
45 | messages.forEach(function( message ) {
|
---|
46 | grunt.log.writeln( "[".red + ( "L" + message.line ).yellow + ":".red + ( "C" + message.col ).yellow + "]".red + ' ' + message.message.yellow );
|
---|
47 | var evidence = message.evidence,
|
---|
48 | col = message.col;
|
---|
49 | if (col === 0) {
|
---|
50 | evidence = '?'.inverse.red + evidence;
|
---|
51 | } else if (col > evidence.length) {
|
---|
52 | evidence = evidence + ' '.inverse.red;
|
---|
53 | } else {
|
---|
54 | evidence = evidence.slice(0, col - 1) + evidence[col - 1].inverse.red + evidence.slice(col);
|
---|
55 | }
|
---|
56 | grunt.log.writeln(evidence);
|
---|
57 | hintCount ++;
|
---|
58 | });
|
---|
59 | }
|
---|
60 | else{
|
---|
61 | grunt.log.writeln( "Skipping empty file " + filepath);
|
---|
62 | }
|
---|
63 | });
|
---|
64 |
|
---|
65 | if ( hintCount > 0 ) {
|
---|
66 | return force;
|
---|
67 | }
|
---|
68 |
|
---|
69 | grunt.log.ok(arrFilesSrc.length + ' file' + (arrFilesSrc.length === 1 ? '' : 's') + ' lint free.');
|
---|
70 |
|
---|
71 | });
|
---|
72 |
|
---|
73 | };
|
---|