annotate lib/redmine/helpers/.svn/text-base/diff.rb.svn-base @ 1180:14058c37047a feature_14

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