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