This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (107 loc) · 4.18 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
var fs = require('fs'),
html = require('html-entities').AllHtmlEntities,
path = require('path'),
_ = require('lodash'),
gutil = require('gulp-util'),
through = require('through2'),
vorlonChecker = require('./lib/vorlon-checker');
var PluginError = gutil.PluginError,
PLUGIN_NAME = 'gulp-webstandards',
symbols = {
'ok': '✓',
'err': '✖',
'space': ' '
};
// With node.js on Windows: use symbols available in terminal default fonts
if (process && process.platform === 'win32') {
symbols.ok = '\u221A';
symbols.err = '\u00D7';
}
function stripVorlonHtml (s) {
var suggestion = s.split(':');
suggestion[0] = suggestion[0].replace(/<\/?strong>/g, '').trim();
suggestion[1] = suggestion[1].trim();
return suggestion;
}
function prettyPrintVorlon (results) {
_.forIn(results.rules, function (item) {
_.forIn(item.rules, function (rule) {
if (rule.failed) {
// print rule title
gutil.log(symbols.space, gutil.colors.red(symbols.err), gutil.colors.red(rule.title));
gutil.log(symbols.space, symbols.space, rule.description);
// print suggestions
_.forIn(rule.items, function (suggestion) {
if ('title' in suggestion && suggestion.title !== null) {
// only one suggestions
gutil.log(new Array(5).join(symbols.space),
html.decode(suggestion.title.replace(/\r?\n|\r/g, ' ')));
} else {
_.forIn(suggestion.items, function (subsug) {
gutil.log(new Array(5).join(symbols.space),
html.decode(subsug.title.replace(/\r?\n|\r/g, ' ')));
_.forIn(_.pluck(subsug.items, 'title'), function (i) {
gutil.log(new Array(7).join(symbols.space),
stripVorlonHtml(i)[0], 'is', gutil.colors.red(stripVorlonHtml(i)[1]));
});
});
}
});
} else {
gutil.log(symbols.space, gutil.colors.green(symbols.ok), gutil.colors.dim(rule.title));
}
});
});
}
// Plugin level function(dealing with files)
function webstandards () {
return through.obj(function (file, enc, cb) {
var contents = null,
language = null;
if (file.isBuffer()) {
if (typeof file.path === 'string') {
// for some reason file.extname is sometimes undefined but file.path is available.
language = file.extname !== undefined ? file.extname : path.extname(file.path);
// should we be ignoring this file?
if (['.html', '.html', '.js', '.css'].indexOf(language) === -1) {
gutil.log(gutil.colors.dim('Ignoring'), gutil.colors.dim(file.relative));
return cb(null, file);
}
try {
gutil.log('Checking', gutil.colors.underline(file.relative));
// Unfortunately vorlon, jsdom & cssjs accept only the whole file and not a buffer
contents = fs.readFileSync(file.path, enc).toString();
} catch (e) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Error reading file'));
}
// pass off to the approriate checker function...
try {
if (['.html', '.htm'].indexOf(language) !== -1) {
prettyPrintVorlon(vorlonChecker.checkHTML(contents));
} else if (language === '.js') {
prettyPrintVorlon(vorlonChecker.checkJavaScript(contents));
} else if (language === '.css') {
prettyPrintVorlon(vorlonChecker.checkCSS(contents));
}
} catch (e) {
// in case of a jsdom error
gutil.log(gutil.colors.red('Internal error:'), e.message);
}
cb(null, file);
} else {
// We need file paths to be able to read the file and load it into jsdom. one day we will do this statically.
this.emit('error', new PluginError(PLUGIN_NAME, "Needs filepaths so you can't have any plugins before it."));
cb('err', file);
}
} else if (file.isNull()) {
// ignore empty files
cb(null, file);
} else {
// we don't do streams because we don't have a file path typically
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
cb('err', file);
}
});
}
module.exports = webstandards;