diff --git a/CHANGE.md b/CHANGE.md index c920f4706..30c616249 100644 --- a/CHANGE.md +++ b/CHANGE.md @@ -11,6 +11,7 @@ add: 4. add cli parameter:`--nocolor`, disable color in cli 5. space-tab-mixed-disabled plugin: add space length require 6. add empty elements: track,command,source,keygen,wbr +7. add hint stdin for cli fix: diff --git a/README.md b/README.md index 6b8c302b8..a81194579 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Quick start htmlhint www/test.html htmlhint www/**/*.xhtml htmlhint www/**/*.{htm,html} + cat test.html | htmlhint stdin 2. results @@ -73,7 +74,7 @@ HTMLHint is released under the MIT license: > The MIT License > -> Copyright (c) 2014-2015 Yanis Wang \< yanis.wang@gmail.com \> +> Copyright (c) 2014-2016 Yanis Wang \< yanis.wang@gmail.com \> > > Permission is hereby granted, free of charge, to any person obtaining a copy > of this software and associated documentation files (the "Software"), to deal diff --git a/bin/htmlhint b/bin/htmlhint index 7a16f2121..b3b75d457 100755 --- a/bin/htmlhint +++ b/bin/htmlhint @@ -31,6 +31,7 @@ program.on('--help', function(){ console.log(' htmlhint www/test.html'); console.log(' htmlhint www/**/*.xhtml'); console.log(' htmlhint www/**/*.{htm,html}'); + console.log(' cat test.html | htmlhint stdin'); console.log(' htmlhint --list'); console.log(' htmlhint --rules tag-pair,id-class-value=underline test.html'); console.log(' htmlhint --config .htmlhintrc test.html'); @@ -190,36 +191,37 @@ function hintAllFiles(target, options, onFinised){ // hint queue var hintQueue = async.queue(function (filepath, next) { var startTime = new Date().getTime(); - var messages = hintFile(filepath, ruleset); - var spendTime = new Date().getTime() - startTime; - var hintCount = messages.length; - if(hintCount > 0){ - formatter.emit('file', { - 'file': filepath, - 'messages': messages, - 'time': spendTime - }); - arrTargetMessages.push({ - 'file': filepath, - 'messages': messages, - 'time': spendTime - }); - targetHintFileCount ++; - targetHintCount += hintCount; + if(filepath === 'stdin'){ + hintStdin(ruleset, hintNext); + } + else{ + var messages = hintFile(filepath, ruleset); + hintNext(messages); + } + function hintNext(messages){ + var spendTime = new Date().getTime() - startTime; + var hintCount = messages.length; + if(hintCount > 0){ + formatter.emit('file', { + 'file': filepath, + 'messages': messages, + 'time': spendTime + }); + arrTargetMessages.push({ + 'file': filepath, + 'messages': messages, + 'time': spendTime + }); + targetHintFileCount ++; + targetHintCount += hintCount; + } + targetFileCount ++; + setImmediate(next); } - targetFileCount ++; - setImmediate(next); }, 10); // start hint var isWalkDone = false; var isHintDone = true; - walkPath(globInfo, function(filepath){ - isHintDone = false; - hintQueue.push(filepath); - }, function(){ - isWalkDone = true; - checkAllHinted(); - }); hintQueue.drain = function() { isHintDone = true; checkAllHinted(); @@ -234,6 +236,19 @@ function hintAllFiles(target, options, onFinised){ }); } } + if(target === 'stdin'){ + isWalkDone = true; + hintQueue.push(target); + } + else{ + walkPath(globInfo, function(filepath){ + isHintDone = false; + hintQueue.push(filepath); + }, function(){ + isWalkDone = true; + checkAllHinted(); + }); + } } // split target to base & glob @@ -337,3 +352,18 @@ function hintFile(filepath, ruleset){ catch(e){} return HTMLHint.verify(content, ruleset); } + +// hint stdin +function hintStdin(ruleset, callback){ + process.stdin.setEncoding('utf8'); + var buffers = []; + process.stdin.on('data', function(text){ + buffers.push(text); + }); + + process.stdin.on('end', function(){ + var content = buffers.join(''); + var messages = HTMLHint.verify(content, ruleset); + callback(messages); + }); +}