Skip to content

Commit

Permalink
Documents can be piped to the nokogiri command line tool
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Oct 6, 2012
1 parent fe3c5ed commit 218903d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rdoc
Expand Up @@ -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
Expand Down
23 changes: 19 additions & 4 deletions bin/nokogiri
Expand Up @@ -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:"

Expand All @@ -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
Expand All @@ -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.