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