Chris@245: # Redmine - project management software Chris@245: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@245: # Chris@245: # This program is free software; you can redistribute it and/or Chris@245: # modify it under the terms of the GNU General Public License Chris@245: # as published by the Free Software Foundation; either version 2 Chris@245: # of the License, or (at your option) any later version. Chris@909: # Chris@245: # This program is distributed in the hope that it will be useful, Chris@245: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@245: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@245: # GNU General Public License for more details. Chris@909: # Chris@245: # You should have received a copy of the GNU General Public License Chris@245: # along with this program; if not, write to the Free Software Chris@245: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@245: Chris@245: module Redmine Chris@245: module Helpers Chris@245: class Diff Chris@245: include ERB::Util Chris@245: include ActionView::Helpers::TagHelper Chris@245: include ActionView::Helpers::TextHelper Chris@245: attr_reader :diff, :words Chris@909: Chris@245: def initialize(content_to, content_from) Chris@245: @words = content_to.to_s.split(/(\s+)/) Chris@245: @words = @words.select {|word| word != ' '} Chris@245: words_from = content_from.to_s.split(/(\s+)/) Chris@909: words_from = words_from.select {|word| word != ' '} Chris@245: @diff = words_from.diff @words Chris@245: end Chris@909: Chris@245: def to_html Chris@245: words = self.words.collect{|word| h(word)} Chris@245: words_add = 0 Chris@245: words_del = 0 Chris@245: dels = 0 Chris@245: del_off = 0 Chris@245: diff.diffs.each do |diff| Chris@245: add_at = nil Chris@245: add_to = nil Chris@245: del_at = nil Chris@909: deleted = "" Chris@245: diff.each do |change| Chris@245: pos = change[1] Chris@245: if change[0] == "+" Chris@245: add_at = pos + dels unless add_at Chris@245: add_to = pos + dels Chris@245: words_add += 1 Chris@245: else Chris@245: del_at = pos unless del_at Chris@245: deleted << ' ' + h(change[2]) Chris@245: words_del += 1 Chris@245: end Chris@245: end Chris@245: if add_at Chris@245: words[add_at] = '' + words[add_at] Chris@245: words[add_to] = words[add_to] + '' Chris@245: end Chris@245: if del_at Chris@245: words.insert del_at - del_off + dels + words_add, '' + deleted + '' Chris@245: dels += 1 Chris@245: del_off += words_del Chris@245: words_del = 0 Chris@245: end Chris@245: end Chris@909: words.join(' ').html_safe Chris@245: end Chris@245: end Chris@245: end Chris@245: end