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