Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Nov 15, 2010
0 parents commit 37b0d79
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .document
@@ -0,0 +1,3 @@
-
ChangeLog.*
LICENSE.txt
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
doc/
pkg/
1 change: 1 addition & 0 deletions .rspec
@@ -0,0 +1 @@
--colour --format documentation
1 change: 1 addition & 0 deletions .yardopts
@@ -0,0 +1 @@
--markup markdown --title "nokogiri-diff Documentation" --protected
4 changes: 4 additions & 0 deletions ChangeLog.md
@@ -0,0 +1,4 @@
### 0.1.0 / 2010-11-14

* Initial release:

20 changes: 20 additions & 0 deletions LICENSE.txt
@@ -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.
29 changes: 29 additions & 0 deletions README.md
@@ -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.
35 changes: 35 additions & 0 deletions Rakefile
@@ -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
21 changes: 21 additions & 0 deletions gemspec.yml
@@ -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
1 change: 1 addition & 0 deletions lib/nokogiri/diff.rb
@@ -0,0 +1 @@
require 'nokogiri/diff/node'
100 changes: 100 additions & 0 deletions lib/nokogiri/diff/node.rb
@@ -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
10 changes: 10 additions & 0 deletions nokogiri-diff.gemspec
@@ -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
8 changes: 8 additions & 0 deletions spec/diff_spec.rb
@@ -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
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
@@ -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.