annotate .svn/pristine/1f/1f0cbd778ac799db40d158f816d90f939c73e314.svn-base @ 1464:261b3d9a4903 redmine-2.4

Update to Redmine 2.4 branch rev 12663
author Chris Cannam
date Tue, 14 Jan 2014 14:37:42 +0000
parents
children
rev   line source
Chris@1464 1 # Redmine - project management software
Chris@1464 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1464 3 #
Chris@1464 4 # This program is free software; you can redistribute it and/or
Chris@1464 5 # modify it under the terms of the GNU General Public License
Chris@1464 6 # as published by the Free Software Foundation; either version 2
Chris@1464 7 # of the License, or (at your option) any later version.
Chris@1464 8 #
Chris@1464 9 # This program is distributed in the hope that it will be useful,
Chris@1464 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1464 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1464 12 # GNU General Public License for more details.
Chris@1464 13 #
Chris@1464 14 # You should have received a copy of the GNU General Public License
Chris@1464 15 # along with this program; if not, write to the Free Software
Chris@1464 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1464 17
Chris@1464 18 module Redmine
Chris@1464 19 module Helpers
Chris@1464 20 class Diff
Chris@1464 21 include ERB::Util
Chris@1464 22 include ActionView::Helpers::TagHelper
Chris@1464 23 include ActionView::Helpers::TextHelper
Chris@1464 24 attr_reader :diff, :words
Chris@1464 25
Chris@1464 26 def initialize(content_to, content_from)
Chris@1464 27 @words = content_to.to_s.split(/(\s+)/)
Chris@1464 28 @words = @words.select {|word| word != ' '}
Chris@1464 29 words_from = content_from.to_s.split(/(\s+)/)
Chris@1464 30 words_from = words_from.select {|word| word != ' '}
Chris@1464 31 @diff = words_from.diff @words
Chris@1464 32 end
Chris@1464 33
Chris@1464 34 def to_html
Chris@1464 35 words = self.words.collect{|word| h(word)}
Chris@1464 36 words_add = 0
Chris@1464 37 words_del = 0
Chris@1464 38 dels = 0
Chris@1464 39 del_off = 0
Chris@1464 40 diff.diffs.each do |diff|
Chris@1464 41 add_at = nil
Chris@1464 42 add_to = nil
Chris@1464 43 del_at = nil
Chris@1464 44 deleted = ""
Chris@1464 45 diff.each do |change|
Chris@1464 46 pos = change[1]
Chris@1464 47 if change[0] == "+"
Chris@1464 48 add_at = pos + dels unless add_at
Chris@1464 49 add_to = pos + dels
Chris@1464 50 words_add += 1
Chris@1464 51 else
Chris@1464 52 del_at = pos unless del_at
Chris@1464 53 deleted << ' ' unless deleted.empty?
Chris@1464 54 deleted << h(change[2])
Chris@1464 55 words_del += 1
Chris@1464 56 end
Chris@1464 57 end
Chris@1464 58 if add_at
Chris@1464 59 words[add_at] = '<span class="diff_in">'.html_safe + words[add_at]
Chris@1464 60 words[add_to] = words[add_to] + '</span>'.html_safe
Chris@1464 61 end
Chris@1464 62 if del_at
Chris@1464 63 words.insert del_at - del_off + dels + words_add, '<span class="diff_out">'.html_safe + deleted + '</span>'.html_safe
Chris@1464 64 dels += 1
Chris@1464 65 del_off += words_del
Chris@1464 66 words_del = 0
Chris@1464 67 end
Chris@1464 68 end
Chris@1464 69 words.join(' ').html_safe
Chris@1464 70 end
Chris@1464 71 end
Chris@1464 72 end
Chris@1464 73 end