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