From 8b51589f8e3bc2e98bfdf5d0038a30698a331f2e Mon Sep 17 00:00:00 2001 From: Austin Ziegler Date: Sun, 31 Jul 2011 11:28:19 -0400 Subject: Fixing https://github.com/halostatue/diff-lcs/issues/4 --- bin/htmldiff | 120 ++++++++++------------------------------------------------- bin/ldiff | 41 +------------------- 2 files changed, 21 insertions(+), 140 deletions(-) (limited to 'bin') diff --git a/bin/htmldiff b/bin/htmldiff index 509860c..056564c 100644 --- a/bin/htmldiff +++ b/bin/htmldiff @@ -1,112 +1,32 @@ #!/usr/bin/env ruby -#-- -# Copyright 2004 Austin Ziegler -# adapted from: -# Algorithm::Diff (Perl) by Ned Konz -# Smalltalk by Mario I. Wolczko -# implements McIlroy-Hunt diff algorithm -# -# This program is free software. It may be redistributed and/or modified under -# the terms of the GPL version 2 (or later), the Perl Artistic licence, or the -# Ruby licence. -# -# $Id$ -#++ + +require 'diff/lcs' +require 'diff/lcs/htmldiff' begin - require 'rubygems' - require_gem 'diff-lcs', "1.1.1" - require 'diff/lcs/string' + require 'text/format' rescue LoadError - require 'diff/lcs' - require 'diff/lcs/string' + Diff::LCS::HTMLDiff.can_expand_tabs = false end -require 'text/format' +if ARGV.size < 2 or ARGV.size > 3 + $stderr.puts "usage: #{File.basename($0)} old new [output.html]" + $stderr.puts " #{File.basename($0)} old new > output.html" + exit 127 +end -class HTMLDiff #:nodoc: - attr_accessor :output +left = IO.read(ARGV[0]).split($/) +right = IO.read(ARGV[1]).split($/) - def initialize(output) - @output = output - end +options = { :title => "diff #{ARGV[0]} #{ARGV[1]}" } - # This will be called with both lines are the same - def match(event) - @output << %Q|
#{event.old_element}
\n| - end +htmldiff = Diff::LCS::HTMLDiff.new(left, right, options) - # This will be called when there is a line in A that isn't in B - def discard_a(event) - @output << %Q|
#{event.old_element}
\n| +if ARGV[2] + File.open(ARGV[2], "w") do |f| + htmldiff.options[:output] = f + htmldiff.run end - - # This will be called when there is a line in B that isn't in A - def discard_b(event) - @output << %Q|
#{event.new_element}
\n| - end -end - -if ARGV.size != 2 - puts "usage: #{File.basename($0)} old new > output.html" - exit 255 +else + htmldiff.run end - -hd = HTMLDiff.new($stdout) -tf = Text::Format.new -tf.tabstop = 4 - -preprocess = lambda { |line| tf.expand(line.chomp) } - -a = IO.readlines(ARGV[0]).map(&preprocess) -b = IO.readlines(ARGV[1]).map(&preprocess) - -$stdout.write <<-START - - - diff #{ARGV[0]} #{ARGV[1]} - - - -

diff  - #{ARGV[0]}  - #{ARGV[1]} -

-
-START - -Diff::LCS.traverse_sequences(a, b, hd) - -$stdout.write <<-END -
- - -END diff --git a/bin/ldiff b/bin/ldiff index ae2b245..21b4ed8 100644 --- a/bin/ldiff +++ b/bin/ldiff @@ -1,45 +1,6 @@ #!/usr/bin/env ruby -#-- -# Copyright 2004 Austin Ziegler -# adapted from: -# Algorithm::Diff (Perl) by Ned Konz -# Smalltalk by Mario I. Wolczko -# implements McIlroy-Hunt diff algorithm -# -# This program is free software. It may be redistributed and/or modified under -# the terms of the GPL version 2 (or later), the Perl Artistic licence, or the -# Ruby licence. -# -# $Id$ -#++ - - # 1) Try to load Ruwiki from the gem. - # 2) Try to load Ruwiki from $LOAD_PATH. - # 3) Modify $LOAD_PATH and try to load it from the modified $LOAD_PATH. - # 4) Fail hard. -load_state = 1 - begin - if 1 == load_state - require 'rubygems' - require_gem 'diff-lcs', '= 1.1.1' - else - require 'diff/lcs' - end - rescue LoadError - load_state += 1 - - case load_state - when 3 - $LOAD_PATH.unshift "#{File.dirname($0)}/../lib" - when 4 - $LOAD_PATH.shift - $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" - when 5 - raise - end - retry - end +require 'diff/lcs' require 'diff/lcs/ldiff' exit Diff::LCS::Ldiff.run(ARGV) -- cgit v1.2.1