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