Created
December 22, 2015 08:31
-
-
Save mkhl/3b6d482f5d513d4dbd2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -w | |
require "rubygems" | |
require "nokogiri" | |
require "open-uri" | |
PROGRAM_BASE = File.basename $PROGRAM_NAME | |
def usage | |
puts <<-USAGE | |
Usage: #{PROGRAM_BASE} <query> [<file>|<URL>]... | |
Search <query> in each HTML or XML document read from <file>, <URL> or stdin. | |
USAGE | |
exit 64 | |
end | |
def search(io, query) | |
Nokogiri.parse(io.read) do |doc| | |
doc.remove_namespaces! | |
return doc.search(query) | |
end | |
end | |
usage if ARGV.empty? | |
usage if ARGV.first =~ /-h|--?help/ | |
query = ARGV.shift | |
puts search($stdin, query) if ARGV.empty? | |
ARGV.each do |path| | |
open(path) do |io| | |
puts search(io, query) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment