annotate .svn/pristine/1f/1f0cbd778ac799db40d158f816d90f939c73e314.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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