Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 37b0d79444a0ae6ae5dfbb290cbdabd63516fd96 @postmodern committed Nov 15, 2010
Showing with 241 additions and 0 deletions.
  1. +3 −0 .document
  2. +2 −0 .gitignore
  3. +1 −0 .rspec
  4. +1 −0 .yardopts
  5. +4 −0 ChangeLog.md
  6. +20 −0 LICENSE.txt
  7. +29 −0 README.md
  8. +35 −0 Rakefile
  9. +21 −0 gemspec.yml
  10. +1 −0 lib/nokogiri/diff.rb
  11. +100 −0 lib/nokogiri/diff/node.rb
  12. +10 −0 nokogiri-diff.gemspec
  13. +8 −0 spec/diff_spec.rb
  14. +6 −0 spec/spec_helper.rb
@@ -0,0 +1,3 @@
+-
+ChangeLog.*
+LICENSE.txt
@@ -0,0 +1,2 @@
+doc/
+pkg/
1 .rspec
@@ -0,0 +1 @@
+--colour --format documentation
@@ -0,0 +1 @@
+--markup markdown --title "nokogiri-diff Documentation" --protected
@@ -0,0 +1,4 @@
+### 0.1.0 / 2010-11-14
+
+* Initial release:
+
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Hal Brodigan
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
+# nokogiri-diff
+
+* [Source](http://github.com/postmodern/nokogiri-diff)
+* [Issues](http://github.com/postmodern/nokogiri-diff/issues)
+* Postmodern (postmodern.mod3 at gmail.com)
+
+## Description
+
+nokogiri-diff adds the ability to calculate the differences (added or
+removed nodes) between two XML/HTML documents.
+
+## Examples
+
+ require 'nokogiri/diff'
+
+## Requirements
+
+* [tdiff](http://github.com/postmodern/tdiff) ~> 0.3.0
+* [nokogiri](http://nokogiri.rubyforge.org/) ~> 1.4.1
+
+## Install
+
+ $ gem install nokogiri-diff
+
+## Copyright
+
+Copyright (c) 2010 Hal Brodigan
+
+See {file:LICENSE.txt} for details.
@@ -0,0 +1,35 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ gem 'ore-tasks', '~> 0.3.0'
+ require 'ore/tasks'
+
+ Ore::Tasks.new
+rescue LoadError => e
+ STDERR.puts e.message
+ STDERR.puts "Run `gem install ore-tasks` to install 'ore/tasks'."
+end
+
+begin
+ gem 'rspec', '~> 2.0.0'
+ require 'rspec/core/rake_task'
+
+ RSpec::Core::RakeTask.new
+rescue LoadError => e
+ task :spec do
+ abort "Please run `gem install rspec` to install RSpec."
+ end
+end
+task :default => :spec
+
+begin
+ gem 'yard', '~> 0.6.0'
+ require 'yard'
+
+ YARD::Rake::YardocTask.new
+rescue LoadError => e
+ task :yard do
+ abort "Please run `gem install yard` to install YARD."
+ end
+end
@@ -0,0 +1,21 @@
+name: nokogiri-diff
+version: 0.1.0
+summary: Calculate the differences between two XML/HTML documents.
+description:
+ Nokogiri::Diff adds the ability to calculate the differences (added
+ or removed nodes) between two XML/HTML documents.
+
+license: MIT
+authors: Postmodern
+email: postmodern.mod3@gmail.com
+homepage: http://github.com/postmodern/nokogiri-diff
+has_yard: true
+
+dependencies:
+ tdiff: ~> 0.3.0
+ nokogiri: ~> 1.4.1
+
+development_dependencies:
+ ore-tasks: ~> 0.3.0
+ rspec: ~> 2.0.0
+ yard: ~> 0.6.0
@@ -0,0 +1 @@
+require 'nokogiri/diff/node'
@@ -0,0 +1,100 @@
+require 'nokogiri'
+require 'tdiff'
+
+class Nokogiri::XML::Node
+
+ include TDiff
+ include TDiff::Unordered
+
+ #
+ # Compares two XML/HTML nodes.
+ #
+ # @param [Nokogiri::XML::Node] node1
+ #
+ # @param [Nokogiri::XML::Node] node2
+ #
+ # @return [Boolean]
+ # Specifies whether the two nodes are equal.
+ #
+ def tdiff_equal(node)
+ if (self.class == node.class)
+ case node
+ when Nokogiri::XML::Document
+ self.root.tdiff_equal(node.root)
+ when Nokogiri::XML::Attr
+ (self.name == node.name && self.value == node.value)
+ when Nokogiri::XML::Element
+ self.name == node.name
+ when Nokogiri::XML::Text
+ self.text == node.text
+ else
+ false
+ end
+ else
+ false
+ end
+ end
+
+ #
+ # Enumerates over the children of a XML/HTML node.
+ #
+ # @param [Nokogiri::XML::Node] node
+ #
+ # @yield [child]
+ # The given block will be passed every child of the node.
+ #
+ # @yieldparam [Nokogiri::XML::Node] node
+ # A child node.
+ #
+ def tdiff_each_child(node,&block)
+ if node.kind_of?(Nokogiri::XML::Element)
+ node.attributes.each_value(&block)
+ end
+
+ node.children.each(&block)
+ end
+
+ #
+ # Finds the differences between the node and another node.
+ #
+ # @param [Nokogiri::XML::Node] other
+ # The other node to compare against.
+ #
+ # @param [Hash] options
+ # Additional options for filtering changes.
+ #
+ # @option options [Boolean] :added
+ # Yield nodes that were added.
+ #
+ # @option options [Boolean] :removed
+ # Yield nodes that were removed.
+ #
+ # @yield [change, node]
+ # The given block will be passed each changed node.
+ #
+ # @yieldparam [' ', '-', '+'] change
+ # Indicates whether the node stayed the same, was removed or added.
+ #
+ # @yieldparam [Nokogiri::XML::Node] node
+ # The changed node.
+ #
+ # @return [Enumerator]
+ # If no block was given, an Enumerator object will be returned.
+ #
+ def diff(other,options={},&block)
+ return enum_for(:diff,other,options) unless block
+
+ if (options[:added] || options[:removed])
+ tdiff_unordered(other) do |change,node|
+ if (change == '+' && options[:added])
+ yield change, node
+ elsif (change == '-' && options[:removed])
+ yield change, node
+ end
+ end
+ else
+ tdiff(other,&block)
+ end
+ end
+
+end
@@ -0,0 +1,10 @@
+# -*- encoding: utf-8 -*-
+
+begin
+ Ore::Specification.new do |gemspec|
+ # custom logic here
+ end
+rescue NameError
+ STDERR.puts "The 'nokogiri-diff.gemspec' file requires Ore."
+ STDERR.puts "Run `gem install ore-core` to install Ore."
+end
@@ -0,0 +1,8 @@
+require 'spec_helper'
+require 'nokogiri/diff'
+
+describe Nokogiri::Diff do
+ it "should have a VERSION constant" do
+ Nokogiri::Diff.const_get('VERSION').should_not be_empty
+ end
+end
@@ -0,0 +1,6 @@
+gem 'rspec', '~> 2.0.0'
+require 'rspec'
+require 'nokogiri/diff/version'
+
+include Nokogiri::Diff
+

0 comments on commit 37b0d79

Please sign in to comment.