Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 12 commits
  • 11 files changed
  • 0 commit comments
  • 1 contributor
View
1  .gitignore
@@ -3,3 +3,4 @@
coverage
rdoc
pkg
+.rbenv*
View
3  Gemfile
@@ -0,0 +1,3 @@
+source 'http://rubygems.org'
+
+# gem 'rspec-rails', '~> 1.3'
View
1  Rakefile
@@ -34,5 +34,4 @@ Spec::Rake::SpecTask.new(:rcov) do |spec|
spec.rcov = true
end
-
task :default => :spec
View
7 VERSION.yml
@@ -1,4 +1,5 @@
----
-:patch: 2
+---
:major: 0
-:minor: 1
+:minor: 2
+:patch: 1
+:build:
View
27 differ.gemspec
@@ -4,17 +4,18 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
- s.name = %q{differ}
- s.version = "0.1.2"
+ s.name = "differ"
+ s.version = "0.2.1"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Pieter van de Bruggen"]
- s.date = %q{2011-02-17}
- s.email = %q{pvande@gmail.com}
+ s.date = "2012-04-16"
+ s.email = "pvande@gmail.com"
s.extra_rdoc_files = [
"README.rdoc"
]
s.files = [
+ "Gemfile",
"README.rdoc",
"Rakefile",
"VERSION.yml",
@@ -25,6 +26,7 @@ Gem::Specification.new do |s|
"lib/differ/format/ascii.rb",
"lib/differ/format/color.rb",
"lib/differ/format/html.rb",
+ "lib/differ/format/passage.rb",
"lib/differ/string.rb",
"spec/differ/change_spec.rb",
"spec/differ/diff_spec.rb",
@@ -35,23 +37,12 @@ Gem::Specification.new do |s|
"spec/differ_spec.rb",
"spec/spec_helper.rb"
]
- s.homepage = %q{http://github.com/pvande/differ}
+ s.homepage = "http://github.com/pvande/differ"
s.require_paths = ["lib"]
- s.rubygems_version = %q{1.3.7}
- s.summary = %q{A simple gem for generating string diffs}
- s.test_files = [
- "spec/differ/change_spec.rb",
- "spec/differ/diff_spec.rb",
- "spec/differ/format/ascii_spec.rb",
- "spec/differ/format/color_spec.rb",
- "spec/differ/format/html_spec.rb",
- "spec/differ/string_spec.rb",
- "spec/differ_spec.rb",
- "spec/spec_helper.rb"
- ]
+ s.rubygems_version = "1.8.11"
+ s.summary = "A simple gem for generating string diffs"
if s.respond_to? :specification_version then
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
View
14 lib/differ.rb
@@ -3,6 +3,7 @@
require 'differ/format/ascii'
require 'differ/format/color'
require 'differ/format/html'
+require 'differ/format/passage'
module Differ
class << self
@@ -45,11 +46,12 @@ def format
def format_for(f)
case f
- when Module then f
- when :ascii then Format::Ascii
- when :color then Format::Color
- when :html then Format::HTML
- when nil then nil
+ when Module then f
+ when :ascii then Format::Ascii
+ when :color then Format::Color
+ when :html then Format::HTML
+ when :passage then Format::Passage
+ when nil then nil
else raise "Unknown format type #{f.inspect}"
end
end
@@ -80,4 +82,4 @@ def change(method, array, index)
@diff.same(array.shift)
end
end
-end
+end
View
2  lib/differ/change.rb
@@ -27,4 +27,4 @@ def ==(other)
self.insert == other.insert && self.delete == other.delete
end
end
-end
+end
View
33 lib/differ/diff.rb
@@ -68,17 +68,36 @@ def ==(other)
end
def to_s
- @raw.join()
+ @raw.join
+ end
+
+ def to_passages
+ format_as :passage
end
def format_as(f)
- f = Differ.format_for(f)
- @raw.inject('') do |sum, part|
- part = case part
- when String then part
- when Change then f.format(part)
+ if f == :passage
+ f = Differ.format_for(f)
+ @raw.inject([[], 0]) do |(changes, length), part|
+ case part
+ when String
+ length += part.length
+ when Change
+ p = f.format(part, length)
+ length += part.delete.length
+ changes << p
+ end
+ [changes, length]
+ end.first
+ else
+ f = Differ.format_for(f)
+ @raw.inject('') do |sum, part|
+ part = case part
+ when String then part
+ when Change then f.format(part)
+ end
+ sum << part
end
- sum << part
end
end
View
26 lib/differ/format/passage.rb
@@ -0,0 +1,26 @@
+module Differ
+ module Format
+ module Passage
+ class << self
+ def format(change, length = 0)
+ (change.change? && as_change(change, length)) ||
+ (change.delete? && as_delete(change, length)) ||
+ (change.insert? && as_insert(change, length))
+ end
+
+ private
+ def as_insert(change, length)
+ { :method => :insert, :content => change.insert, :at => length }
+ end
+
+ def as_delete(change, length)
+ { :method => :delete, :content => change.delete, :from => length, :to => length + change.delete.length - 1 }
+ end
+
+ def as_change(change, length)
+ { :method => :change, :content => change.insert, :from => length, :to => length + change.delete.length - 1 }
+ end
+ end
+ end
+ end
+end
View
2  spec/differ/change_spec.rb
@@ -79,4 +79,4 @@
@format.should_receive(:format).once
Differ::Change.new.to_s
end
-end
+end
View
2  spec/differ/diff_spec.rb
@@ -295,4 +295,4 @@
end
end
end
-end
+end

No commit comments for this range

Something went wrong with that request. Please try again.