├── .gitignore ├── .travis.yml ├── Gruntfile.js ├── LICENSE ├── README.md ├── bin └── qrep ├── grunt ├── config.js ├── jshint.js ├── nodeunit.js └── watch.js ├── lib ├── cli.js ├── main.js └── qrep │ ├── file.js │ ├── stream.js │ ├── url.js │ └── usage.js ├── package.json └── spec ├── expected └── simple.txt ├── fixtures └── simple.html └── qrep.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | script: 2 | - "npm test" 3 | 4 | language: node_js 5 | 6 | node_js: 7 | - 0.11 8 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | 'use strict'; 3 | var configObject = require('./grunt/config'); 4 | var packageJson = grunt.file.readJSON("package.json"); 5 | grunt.config.init(configObject); 6 | 7 | // Load Grunt Plugins 8 | Object.keys(packageJson.devDependencies) 9 | .slice(1).forEach(grunt.loadNpmTasks); 10 | grunt.registerTask('default', ['watch']); 11 | grunt.registerTask('test', ['jshint']); 12 | }; 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Daijiro Wachi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # qrep 2 | 3 | **grep** meets **querySelectorAll** 4 | 5 | ## Badges 6 | + [![NPM Version](http://img.shields.io/npm/v/qrep.svg)](https://www.npmjs.org/package/qrep) 7 | + [![MIT LICENSE](http://img.shields.io/badge/license-MIT-brightgreen.svg)](https://github.com/watilde/qrep/blob/master/LICENSE) 8 | + [![Build Status](https://api.travis-ci.org/watilde/qrep.svg)](https://travis-ci.org/watilde/qrep) 9 | + [![Dependency Status](https://gemnasium.com/watilde/qrep.svg)](https://gemnasium.com/watilde/qrep) 10 | 11 | ## Install 12 | 13 | Using npm. 14 | 15 | $ npm install -g qrep 16 | 17 | Confirm. 18 | 19 | $ qrep usage 20 | 21 | ## Uninstall 22 | 23 | $ npm uninstall -g qrep 24 | 25 | ## Update 26 | 27 | $ npm update -g qrep 28 | 29 | ## Usage 30 | 31 | qrep selector filename 32 | qrep selector url 33 | 34 | ## Option 35 | qrep selector filename -e encoding(default = utf8) 36 | qrep selector url -e encoding(default = utf8) 37 | 38 | ## Example 39 | 40 | Find selector in file. 41 | 42 | $ qrep #logo ./tpl/index.html 43 |
44 | 45 | Find selector using URL. 46 | 47 | $ qrep span.octicon-logo-github https://github.com 48 | 49 | 50 | Find selector with stdin. 51 | 52 | $ curl -s -L http://google.com | qrep title 53 | Google 54 | -------------------------------------------------------------------------------- /bin/qrep: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | require(__dirname + '/../lib/cli') 3 | -------------------------------------------------------------------------------- /grunt/config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | watch: require('./watch'), 3 | jshint: require('./jshint'), 4 | nodeunit: require('./nodeunit') 5 | }; 6 | -------------------------------------------------------------------------------- /grunt/jshint.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | options: { 3 | strict: true, 4 | indent: 2, 5 | maxlen: 80, 6 | unused: true, 7 | node: true, 8 | devel: true, 9 | }, 10 | app: ['lib/**/*.js', 'lib/*.js'] 11 | }; 12 | -------------------------------------------------------------------------------- /grunt/nodeunit.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | }; 3 | -------------------------------------------------------------------------------- /grunt/watch.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | all: { 3 | files: ['lib/**/*.js', 'lib/*.js'], 4 | tasks: ['jshint'] 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /lib/cli.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | 'use strict'; 3 | var argv = process.argv; 4 | var qrep = require('./main'); 5 | var encoding = 'utf8'; 6 | 7 | if (argv.length === 2 || argv[2] === 'usage') { 8 | qrep.usage(); 9 | return; 10 | } 11 | 12 | if (argv[4] === '-e' && argv[5]) { 13 | encoding = argv[5]; 14 | } 15 | 16 | if (argv[3]) { 17 | if (argv[3].indexOf('http') === 0) { 18 | qrep.url(argv[2], argv[3], encoding); 19 | return; 20 | } else { 21 | qrep.file(argv[2], argv[3], encoding); 22 | return; 23 | } 24 | } else { 25 | qrep.stream(argv[2], process.stdin, encoding); 26 | return; 27 | } 28 | }()); 29 | -------------------------------------------------------------------------------- /lib/main.js: -------------------------------------------------------------------------------- 1 | module.exports.file = require('./qrep/file'); 2 | module.exports.url = require('./qrep/url'); 3 | module.exports.stream = require('./qrep/stream'); 4 | module.exports.usage = require('./qrep/usage'); 5 | 6 | -------------------------------------------------------------------------------- /lib/qrep/file.js: -------------------------------------------------------------------------------- 1 | module.exports = function (selectors, filename, encoding) { 2 | 'use strict'; 3 | var jsdom = require('jsdom'); 4 | var fs = require('fs'); 5 | var path = require('path'); 6 | var html = ''; 7 | 8 | var isDir = fs.lstatSync(filename).isDirectory(); 9 | if (!isDir) { 10 | html = fs.readFileSync(filename, encoding); 11 | jsdom.env(html, function (err, window) { 12 | var parent = window.document; 13 | var node = parent.querySelectorAll(selectors); 14 | for (var i = 0; node.length > i; i++) { 15 | console.log(node[i].outerHTML); 16 | } 17 | }); 18 | } else { 19 | filename = path.resolve(filename); 20 | console.error('Error: ' + filename + ' is directory.'); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /lib/qrep/stream.js: -------------------------------------------------------------------------------- 1 | module.exports = function (selectors, stream, encoding) { 2 | 'use strict'; 3 | var jsdom = require('jsdom'); 4 | 5 | stream.resume(); 6 | stream.setEncoding(encoding); 7 | 8 | var html = ''; 9 | stream.on('data', function (chunk) { 10 | html += chunk; 11 | }); 12 | 13 | stream.on('end', function () { 14 | jsdom.env(html, function (err, window) { 15 | var parent = window.document; 16 | var node = parent.querySelectorAll(selectors); 17 | for (var i = 0; node.length > i; i++) { 18 | console.log(node[i].outerHTML); 19 | } 20 | }); 21 | }); 22 | }; 23 | -------------------------------------------------------------------------------- /lib/qrep/url.js: -------------------------------------------------------------------------------- 1 | module.exports = function (selectors, url, encoding) { 2 | 'use strict'; 3 | var jsdom = require('jsdom'); 4 | 5 | jsdom.env({ 6 | url: url, 7 | encoding: encoding, 8 | done: function (errors, window) { 9 | var parent = window.document; 10 | var node = parent.querySelectorAll(selectors); 11 | for (var i = 0; node.length > i; i++) { 12 | if (i === 0) { 13 | console.log(node[i].outerHTML); 14 | } 15 | } 16 | } 17 | }); 18 | }; 19 | -------------------------------------------------------------------------------- /lib/qrep/usage.js: -------------------------------------------------------------------------------- 1 | module.exports = function () { 2 | 'use strict'; 3 | var message = ''; 4 | message += 'Usage:\n'; 5 | message += ' qrep selector filename\n'; 6 | message += ' qrep selector url\n'; 7 | message += ' standard output | qrep selector\n'; 8 | message += '\nOption:\n'; 9 | message += ' qrep selector filename -e encoding(default = utf8)\n'; 10 | message += ' qrep selector url -e encoding(default = utf8)\n'; 11 | message += ' curl -s -L url | qrep selector -e encoding(default = utf8)\n'; 12 | message += '\ne.g.\n'; 13 | message += ' qrep #logo ./tpl/index.html\n'; 14 | message += ' qrep h1 http://example.com\n'; 15 | message += ' curl -s -L http://google.com | qrep title\n'; 16 | 17 | console.log(message); 18 | }; 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "qrep", 3 | "version": "0.4.5", 4 | "description": "grep meets querySelectorAll", 5 | "bin": { 6 | "qrep": "./bin/qrep" 7 | }, 8 | "main": "./lib/main", 9 | "repository": { 10 | "type": "git", 11 | "url": "git@github.com:watilde/qrep.git" 12 | }, 13 | "homepage": "http://watilde.github.io/qrep/", 14 | "keywords": [ 15 | "qrep", 16 | "grep" 17 | ], 18 | "scripts": { 19 | "test": "grunt test" 20 | }, 21 | "author": { 22 | "name": "Daijiro Wachi", 23 | "email": "daijiro.wachi@gmail.com", 24 | "url": "https://github.com/watilde/" 25 | }, 26 | "license": "MIT", 27 | "dependencies": { 28 | "jsdom": "^6.1.0" 29 | }, 30 | "bugs": { 31 | "url": "https://github.com/watilde/qrep/issues" 32 | }, 33 | "devDependencies": { 34 | "grunt": "^0.4.5", 35 | "grunt-cli": "^0.1.13", 36 | "grunt-contrib-jshint": "^0.11.2", 37 | "grunt-contrib-nodeunit": "^0.4.1", 38 | "grunt-contrib-watch": "^0.6.1" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spec/expected/simple.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /spec/fixtures/simple.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Simple 7 | 8 | 9 | 11 | 15 | 16 | 17 |
18 | 19 |
20 |
21 |

h1

22 |
23 | 24 | 25 | -------------------------------------------------------------------------------- /spec/qrep.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/watilde/qrep/3350b485881253172bd7a769185d8dac9956a821/spec/qrep.js --------------------------------------------------------------------------------