annotate .svn/pristine/0c/0ccef3aea9c789ba7ff6975c6015bb677b5a97a9.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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