2006-01-22 22:40:20 +01:00
|
|
|
module HTMLDiff
|
2005-01-24 19:52:04 +01:00
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
Match = Struct.new(:start_in_old, :start_in_new, :size)
|
|
|
|
class Match
|
|
|
|
def end_in_old
|
|
|
|
self.start_in_old + self.size
|
2005-11-14 11:13:18 +01:00
|
|
|
end
|
2006-01-22 22:40:20 +01:00
|
|
|
|
|
|
|
def end_in_new
|
|
|
|
self.start_in_new + self.size
|
2005-11-14 11:13:18 +01:00
|
|
|
end
|
2006-01-22 22:40:20 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
Operation = Struct.new(:action, :start_in_old, :end_in_old, :start_in_new, :end_in_new)
|
2005-11-14 11:13:18 +01:00
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
class DiffBuilder
|
2005-11-14 11:13:18 +01:00
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
def initialize(old_version, new_version)
|
|
|
|
@old_version, @new_version = old_version, new_version
|
|
|
|
@content = []
|
2005-11-14 11:13:18 +01:00
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
def build
|
|
|
|
split_inputs_to_words
|
|
|
|
index_new_words
|
|
|
|
operations.each {|opcode| perform_operation(opcode)}
|
|
|
|
return @content.join
|
2005-11-14 11:13:18 +01:00
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
def split_inputs_to_words
|
|
|
|
@old_words = convert_html_to_list_of_words(explode(@old_version))
|
|
|
|
@new_words = convert_html_to_list_of_words(explode(@new_version))
|
2005-11-14 11:13:18 +01:00
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
def index_new_words
|
|
|
|
@word_indices = {}
|
|
|
|
@new_words.each_with_index { |word, i| (@word_indices[word] ||= []) << i }
|
2005-11-14 11:13:18 +01:00
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
def operations
|
|
|
|
position_in_old = position_in_new = 0
|
|
|
|
operations = []
|
|
|
|
matching_blocks.each do |match|
|
|
|
|
match_starts_at_current_position_in_old = (position_in_old == match.start_in_old)
|
|
|
|
match_starts_at_current_position_in_new = (position_in_new == match.start_in_new)
|
|
|
|
|
|
|
|
action_upto_match_positions =
|
|
|
|
case [match_starts_at_current_position_in_old, match_starts_at_current_position_in_new]
|
|
|
|
when [false, false]
|
|
|
|
:replace
|
|
|
|
when [true, false]
|
|
|
|
:insert
|
|
|
|
when [false, true]
|
|
|
|
:delete
|
2005-11-19 15:46:27 +01:00
|
|
|
else
|
2006-01-22 22:40:20 +01:00
|
|
|
# this happens if the first few words are same in both versions
|
|
|
|
:none
|
2005-11-19 15:46:27 +01:00
|
|
|
end
|
2005-01-24 19:52:04 +01:00
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
if action_upto_match_positions != :none
|
|
|
|
operation_upto_match_positions =
|
|
|
|
Operation.new(action_upto_match_positions,
|
|
|
|
position_in_old, match.start_in_old,
|
|
|
|
position_in_new, match.start_in_new)
|
|
|
|
operations << operation_upto_match_positions
|
|
|
|
end
|
|
|
|
match_operation = Operation.new(:equal,
|
|
|
|
match.start_in_old, match.end_in_old,
|
|
|
|
match.start_in_new, match.end_in_new)
|
|
|
|
operations << match_operation
|
2005-01-24 19:52:04 +01:00
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
position_in_old = match.end_in_old
|
|
|
|
position_in_new = match.end_in_new
|
|
|
|
end
|
|
|
|
operations
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
def matching_blocks
|
|
|
|
matching_blocks = []
|
|
|
|
recursively_find_matching_blocks(0, @old_words.size, 0, @new_words.size, matching_blocks)
|
|
|
|
matching_blocks
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
2006-01-22 22:40:20 +01:00
|
|
|
|
|
|
|
def recursively_find_matching_blocks(start_in_old, end_in_old, start_in_new, end_in_new, matching_blocks)
|
|
|
|
match = find_match(start_in_old, end_in_old, start_in_new, end_in_new)
|
|
|
|
if match
|
|
|
|
if start_in_old < match.start_in_old and start_in_new < match.start_in_new
|
|
|
|
recursively_find_matching_blocks(
|
|
|
|
start_in_old, match.start_in_old, start_in_new, match.start_in_new, matching_blocks)
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
2006-01-22 22:40:20 +01:00
|
|
|
matching_blocks << match
|
|
|
|
if match.end_in_old < end_in_old and match.end_in_new < end_in_new
|
|
|
|
recursively_find_matching_blocks(
|
|
|
|
match.end_in_old, end_in_old, match.end_in_new, end_in_new, matching_blocks)
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2006-01-22 22:40:20 +01:00
|
|
|
|
|
|
|
def find_match(start_in_old, end_in_old, start_in_new, end_in_new)
|
|
|
|
besti, bestj, bestsize = start_in_old, start_in_new, 0
|
2005-01-24 19:52:04 +01:00
|
|
|
|
|
|
|
j2len = {}
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
(start_in_old..end_in_old).step do |i|
|
2005-01-24 19:52:04 +01:00
|
|
|
newj2len = {}
|
2006-01-22 22:40:20 +01:00
|
|
|
(@word_indices[@old_words[i]] || []).each do |j|
|
|
|
|
next if j < start_in_new
|
|
|
|
break if j >= end_in_new
|
2005-01-24 19:52:04 +01:00
|
|
|
|
|
|
|
k = newj2len[j] = (j2len[j - 1] || 0) + 1
|
|
|
|
if k > bestsize
|
|
|
|
besti, bestj, bestsize = i - k + 1, j - k + 1, k
|
|
|
|
end
|
|
|
|
end
|
|
|
|
j2len = newj2len
|
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
while besti > start_in_old and bestj > start_in_new and @old_words[besti - 1] == @new_words[bestj - 1]
|
2005-11-14 11:13:18 +01:00
|
|
|
besti, bestj, bestsize = besti - 1, bestj - 1, bestsize + 1
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
while besti + bestsize < end_in_old and bestj + bestsize < end_in_new and
|
|
|
|
@old_words[besti + bestsize] == @new_words[bestj + bestsize]
|
2005-01-24 19:52:04 +01:00
|
|
|
bestsize += 1
|
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
if bestsize == 0
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
return Match.new(besti, bestj, bestsize)
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
VALID_METHODS = [:replace, :insert, :delete, :equal]
|
|
|
|
def perform_operation(operation)
|
|
|
|
@operation = operation
|
|
|
|
self.send operation.action, operation
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
2006-01-22 22:40:20 +01:00
|
|
|
|
|
|
|
def replace(operation)
|
|
|
|
delete(operation, 'diffmod')
|
|
|
|
insert(operation, 'diffmod')
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
def insert(operation, tagclass = 'diffins')
|
|
|
|
insert_tag('ins', tagclass, @new_words[operation.start_in_new...operation.end_in_new])
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
2006-01-22 22:40:20 +01:00
|
|
|
|
|
|
|
def delete(operation, tagclass = 'diffdel')
|
|
|
|
insert_tag('del', tagclass, @old_words[operation.start_in_old...operation.end_in_old])
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
2006-01-22 22:40:20 +01:00
|
|
|
|
|
|
|
def equal(operation)
|
|
|
|
# no tags to insert, simply copy the matching words from onbe of the versions
|
|
|
|
@content += @new_words[operation.start_in_new...operation.end_in_new]
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
2006-01-22 22:40:20 +01:00
|
|
|
|
|
|
|
def opening_tag?(item)
|
|
|
|
item =~ %r!^\s*<[^>]+>\s*$!
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
def closing_tag?(item)
|
|
|
|
item =~ %r!^\s*</[^>]+>\s*$!
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
def tag?(item)
|
|
|
|
opening_tag?(item) or closing_tag?(item)
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
# Tries to enclose diff tags (<ins> or <del>) within <p> tags
|
|
|
|
# As a result the diff tags should be the "most inside" possible.
|
|
|
|
def insert_tag(tagname, cssclass, words)
|
|
|
|
unless words.any? { |word| tag?(word) }
|
|
|
|
@content << wrap_text(words.join, tagname, cssclass)
|
|
|
|
else
|
|
|
|
loop do
|
|
|
|
break if words.empty?
|
|
|
|
@content << words and break if words.all? { |word| tag?(word) }
|
|
|
|
# We are outside of a diff tag
|
|
|
|
@content << words.shift while not words.empty? and tag?(words.first)
|
|
|
|
@content << %(<#{tagname} class="#{cssclass}">)
|
|
|
|
# We are inside a diff tag
|
|
|
|
@content << words.shift until words.empty? or tag?(words.first)
|
|
|
|
@content << %(</#{tagname}>)
|
2005-11-14 11:13:18 +01:00
|
|
|
end
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
def wrap_text(text, tagname, cssclass)
|
|
|
|
%(<#{tagname} class="#{cssclass}">#{text}</#{tagname}>)
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
def explode(sequence)
|
|
|
|
sequence.is_a?(String) ? sequence.split(//) : sequence
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
2006-01-22 22:40:20 +01:00
|
|
|
|
|
|
|
def end_of_tag?(char)
|
|
|
|
char == '>'
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
2006-01-22 22:40:20 +01:00
|
|
|
|
|
|
|
def start_of_tag?(char)
|
|
|
|
char == '<'
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
def whitespace?(char)
|
|
|
|
char =~ /\s/
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_html_to_list_of_words(x, use_brackets = false)
|
|
|
|
mode = :char
|
|
|
|
current_word = ''
|
|
|
|
words = []
|
2005-11-14 11:13:18 +01:00
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
explode(x).each do |char|
|
|
|
|
case mode
|
|
|
|
when :tag
|
|
|
|
if end_of_tag? char
|
|
|
|
current_word << (use_brackets ? ']' : '>')
|
|
|
|
words << current_word
|
|
|
|
current_word = ''
|
|
|
|
if whitespace?(char)
|
|
|
|
mode = :whitespace
|
|
|
|
else
|
|
|
|
mode = :char
|
|
|
|
end
|
|
|
|
else
|
|
|
|
current_word << char
|
|
|
|
end
|
|
|
|
when :char
|
|
|
|
if start_of_tag? char
|
|
|
|
words << current_word unless current_word.empty?
|
|
|
|
current_word = (use_brackets ? '[' : '<')
|
|
|
|
mode = :tag
|
|
|
|
elsif /\s/.match char
|
|
|
|
words << current_word unless current_word.empty?
|
|
|
|
current_word = char
|
|
|
|
mode = :whitespace
|
|
|
|
else
|
|
|
|
current_word << char
|
|
|
|
end
|
|
|
|
when :whitespace
|
|
|
|
if start_of_tag? char
|
|
|
|
words << current_word unless current_word.empty?
|
|
|
|
current_word = (use_brackets ? '[' : '<')
|
|
|
|
mode = :tag
|
|
|
|
elsif /\s/.match char
|
|
|
|
current_word << char
|
|
|
|
else
|
|
|
|
words << current_word unless current_word.empty?
|
|
|
|
current_word = char
|
|
|
|
mode = :char
|
2005-11-14 11:13:18 +01:00
|
|
|
end
|
2006-01-22 22:40:20 +01:00
|
|
|
else
|
|
|
|
raise "Unknown mode #{mode.inspect}"
|
2005-11-14 11:13:18 +01:00
|
|
|
end
|
|
|
|
end
|
2006-01-22 22:40:20 +01:00
|
|
|
words << current_word unless current_word.empty?
|
|
|
|
words
|
|
|
|
end
|
2005-01-24 19:52:04 +01:00
|
|
|
|
2006-01-22 22:40:20 +01:00
|
|
|
end # of class Diff Builder
|
|
|
|
|
|
|
|
def diff(a, b)
|
|
|
|
DiffBuilder.new(a, b).build
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|
2005-11-14 11:13:18 +01:00
|
|
|
|
2005-01-24 19:52:04 +01:00
|
|
|
end
|