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