Permalink
Browse files

Documents can be piped to the nokogiri command line tool

  • Loading branch information...
1 parent fe3c5ed commit 218903d63c9ea2ad5e510188e2d7915729327a7e @tenderlove tenderlove committed Oct 6, 2012
Showing with 21 additions and 4 deletions.
  1. +2 −0 CHANGELOG.rdoc
  2. +19 −4 bin/nokogiri
View
2 CHANGELOG.rdoc
@@ -7,6 +7,8 @@
* Node#native_content= allows setting unescaped node contant. #768
* XPath lookup with namespaces supports symbol keys. #729 (Thanks, Ben Langfeld.)
* XML::Node#[]= stringifies values. #729 (Thanks, Ben Langfeld.)
+ * bin/nokogiri will process a document from $stdin
+ * bin/nokogiri -e will execute a program from the command line
* Bugfixes
View
23 bin/nokogiri
@@ -16,6 +16,7 @@ opts = OptionParser.new do |opts|
opts.separator "Examples:"
opts.separator " nokogiri http://www.ruby-lang.org/"
opts.separator " nokogiri ./public/index.html"
+ opts.separator " curl -s http://nokogiri.org | nokogiri -e'p $_.css(\"h1\").length'"
opts.separator ""
opts.separator "Options:"
@@ -27,6 +28,10 @@ opts = OptionParser.new do |opts|
encoding = v
end
+ opts.on("-e command", "Specifies script from command-line.") do |v|
+ @script = v
+ end
+
opts.on("--rng <uri|path>", "Validate using this rng file.") do |v|
@rng = open(v) {|f| Nokogiri::XML::RelaxNG(f)}
end
@@ -45,19 +50,29 @@ opts.parse!
uri = ARGV.shift
-if uri.to_s.strip.empty?
+if uri.to_s.strip.empty? && $stdin.tty?
puts opts
exit 1
end
-@doc = parse_class.parse(open(uri).read, nil, encoding)
+if $stdin.tty?
+ @doc = parse_class.parse(open(uri).read, nil, encoding)
+else
+ @doc = parse_class.parse($stdin, nil, encoding)
+end
+
+$_ = @doc
if @rng
@rng.validate(@doc).each do |error|
puts error.message
end
else
- puts "Your document is stored in @doc..."
- IRB.start
+ if @script
+ eval @script, binding, '<main>'
+ else
+ puts "Your document is stored in @doc..."
+ IRB.start
+ end
end

0 comments on commit 218903d

Please sign in to comment.