Skip to content

Commit 8560332

Browse files
committed
add hint stdin for cli
fix htmlhint#97
1 parent 67c8922 commit 8560332

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

CHANGE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add:
1111
4. add cli parameter:`--nocolor`, disable color in cli
1212
5. space-tab-mixed-disabled plugin: add space length require
1313
6. add empty elements: track,command,source,keygen,wbr
14+
7. add hint stdin for cli
1415

1516
fix:
1617

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Quick start
2626
htmlhint www/test.html
2727
htmlhint www/**/*.xhtml
2828
htmlhint www/**/*.{htm,html}
29+
cat test.html | htmlhint stdin
2930

3031
2. results
3132

@@ -73,7 +74,7 @@ HTMLHint is released under the MIT license:
7374

7475
> The MIT License
7576
>
76-
> Copyright (c) 2014-2015 Yanis Wang \< [email protected] \>
77+
> Copyright (c) 2014-2016 Yanis Wang \< [email protected] \>
7778
>
7879
> Permission is hereby granted, free of charge, to any person obtaining a copy
7980
> of this software and associated documentation files (the "Software"), to deal

bin/htmlhint

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ program.on('--help', function(){
3131
console.log(' htmlhint www/test.html');
3232
console.log(' htmlhint www/**/*.xhtml');
3333
console.log(' htmlhint www/**/*.{htm,html}');
34+
console.log(' cat test.html | htmlhint stdin');
3435
console.log(' htmlhint --list');
3536
console.log(' htmlhint --rules tag-pair,id-class-value=underline test.html');
3637
console.log(' htmlhint --config .htmlhintrc test.html');
@@ -190,36 +191,37 @@ function hintAllFiles(target, options, onFinised){
190191
// hint queue
191192
var hintQueue = async.queue(function (filepath, next) {
192193
var startTime = new Date().getTime();
193-
var messages = hintFile(filepath, ruleset);
194-
var spendTime = new Date().getTime() - startTime;
195-
var hintCount = messages.length;
196-
if(hintCount > 0){
197-
formatter.emit('file', {
198-
'file': filepath,
199-
'messages': messages,
200-
'time': spendTime
201-
});
202-
arrTargetMessages.push({
203-
'file': filepath,
204-
'messages': messages,
205-
'time': spendTime
206-
});
207-
targetHintFileCount ++;
208-
targetHintCount += hintCount;
194+
if(filepath === 'stdin'){
195+
hintStdin(ruleset, hintNext);
196+
}
197+
else{
198+
var messages = hintFile(filepath, ruleset);
199+
hintNext(messages);
200+
}
201+
function hintNext(messages){
202+
var spendTime = new Date().getTime() - startTime;
203+
var hintCount = messages.length;
204+
if(hintCount > 0){
205+
formatter.emit('file', {
206+
'file': filepath,
207+
'messages': messages,
208+
'time': spendTime
209+
});
210+
arrTargetMessages.push({
211+
'file': filepath,
212+
'messages': messages,
213+
'time': spendTime
214+
});
215+
targetHintFileCount ++;
216+
targetHintCount += hintCount;
217+
}
218+
targetFileCount ++;
219+
setImmediate(next);
209220
}
210-
targetFileCount ++;
211-
setImmediate(next);
212221
}, 10);
213222
// start hint
214223
var isWalkDone = false;
215224
var isHintDone = true;
216-
walkPath(globInfo, function(filepath){
217-
isHintDone = false;
218-
hintQueue.push(filepath);
219-
}, function(){
220-
isWalkDone = true;
221-
checkAllHinted();
222-
});
223225
hintQueue.drain = function() {
224226
isHintDone = true;
225227
checkAllHinted();
@@ -234,6 +236,19 @@ function hintAllFiles(target, options, onFinised){
234236
});
235237
}
236238
}
239+
if(target === 'stdin'){
240+
isWalkDone = true;
241+
hintQueue.push(target);
242+
}
243+
else{
244+
walkPath(globInfo, function(filepath){
245+
isHintDone = false;
246+
hintQueue.push(filepath);
247+
}, function(){
248+
isWalkDone = true;
249+
checkAllHinted();
250+
});
251+
}
237252
}
238253

239254
// split target to base & glob
@@ -337,3 +352,18 @@ function hintFile(filepath, ruleset){
337352
catch(e){}
338353
return HTMLHint.verify(content, ruleset);
339354
}
355+
356+
// hint stdin
357+
function hintStdin(ruleset, callback){
358+
process.stdin.setEncoding('utf8');
359+
var buffers = [];
360+
process.stdin.on('data', function(text){
361+
buffers.push(text);
362+
});
363+
364+
process.stdin.on('end', function(){
365+
var content = buffers.join('');
366+
var messages = HTMLHint.verify(content, ruleset);
367+
callback(messages);
368+
});
369+
}

0 commit comments

Comments
 (0)