annotate .svn/pristine/ab/ab1a5cb004277811c44708d7b2f380ff38057bbc.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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