##// END OF EJS Templates
String#each removed in ruby1.9....
Jean-Philippe Lang -
r2800:4450e6f24b30
parent child
Show More
@@ -1,190 +1,191
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2008 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 18 module Redmine
19 19 # Class used to parse unified diffs
20 20 class UnifiedDiff < Array
21 21 def initialize(diff, options={})
22 22 options.assert_valid_keys(:type, :max_lines)
23 diff = diff.split("\n") if diff.is_a?(String)
23 24 diff_type = options[:type] || 'inline'
24 25
25 26 lines = 0
26 27 @truncated = false
27 28 diff_table = DiffTable.new(diff_type)
28 29 diff.each do |line|
29 30 if line =~ /^(---|\+\+\+) (.*)$/
30 31 self << diff_table if diff_table.length > 1
31 32 diff_table = DiffTable.new(diff_type)
32 33 end
33 34 diff_table.add_line line
34 35 lines += 1
35 36 if options[:max_lines] && lines > options[:max_lines]
36 37 @truncated = true
37 38 break
38 39 end
39 40 end
40 41 self << diff_table unless diff_table.empty?
41 42 self
42 43 end
43 44
44 45 def truncated?; @truncated; end
45 46 end
46 47
47 48 # Class that represents a file diff
48 49 class DiffTable < Hash
49 50 attr_reader :file_name, :line_num_l, :line_num_r
50 51
51 52 # Initialize with a Diff file and the type of Diff View
52 53 # The type view must be inline or sbs (side_by_side)
53 54 def initialize(type="inline")
54 55 @parsing = false
55 56 @nb_line = 1
56 57 @start = false
57 58 @before = 'same'
58 59 @second = true
59 60 @type = type
60 61 end
61 62
62 63 # Function for add a line of this Diff
63 64 def add_line(line)
64 65 unless @parsing
65 66 if line =~ /^(---|\+\+\+) (.*)$/
66 67 @file_name = $2
67 68 return false
68 69 elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
69 70 @line_num_l = $2.to_i
70 71 @line_num_r = $5.to_i
71 72 @parsing = true
72 73 end
73 74 else
74 75 if line =~ /^[^\+\-\s@\\]/
75 76 @parsing = false
76 77 return false
77 78 elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
78 79 @line_num_l = $2.to_i
79 80 @line_num_r = $5.to_i
80 81 else
81 82 @nb_line += 1 if parse_line(line, @type)
82 83 end
83 84 end
84 85 return true
85 86 end
86 87
87 88 def inspect
88 89 puts '### DIFF TABLE ###'
89 90 puts "file : #{file_name}"
90 91 self.each do |d|
91 92 d.inspect
92 93 end
93 94 end
94 95
95 96 private
96 97 # Test if is a Side By Side type
97 98 def sbs?(type, func)
98 99 if @start and type == "sbs"
99 100 if @before == func and @second
100 101 tmp_nb_line = @nb_line
101 102 self[tmp_nb_line] = Diff.new
102 103 else
103 104 @second = false
104 105 tmp_nb_line = @start
105 106 @start += 1
106 107 @nb_line -= 1
107 108 end
108 109 else
109 110 tmp_nb_line = @nb_line
110 111 @start = @nb_line
111 112 self[tmp_nb_line] = Diff.new
112 113 @second = true
113 114 end
114 115 unless self[tmp_nb_line]
115 116 @nb_line += 1
116 117 self[tmp_nb_line] = Diff.new
117 118 else
118 119 self[tmp_nb_line]
119 120 end
120 121 end
121 122
122 123 # Escape the HTML for the diff
123 124 def escapeHTML(line)
124 125 CGI.escapeHTML(line)
125 126 end
126 127
127 128 def parse_line(line, type="inline")
128 129 if line[0, 1] == "+"
129 130 diff = sbs? type, 'add'
130 131 @before = 'add'
131 132 diff.line_right = escapeHTML line[1..-1]
132 133 diff.nb_line_right = @line_num_r
133 134 diff.type_diff_right = 'diff_in'
134 135 @line_num_r += 1
135 136 true
136 137 elsif line[0, 1] == "-"
137 138 diff = sbs? type, 'remove'
138 139 @before = 'remove'
139 140 diff.line_left = escapeHTML line[1..-1]
140 141 diff.nb_line_left = @line_num_l
141 142 diff.type_diff_left = 'diff_out'
142 143 @line_num_l += 1
143 144 true
144 145 elsif line[0, 1] =~ /\s/
145 146 @before = 'same'
146 147 @start = false
147 148 diff = Diff.new
148 149 diff.line_right = escapeHTML line[1..-1]
149 150 diff.nb_line_right = @line_num_r
150 151 diff.line_left = escapeHTML line[1..-1]
151 152 diff.nb_line_left = @line_num_l
152 153 self[@nb_line] = diff
153 154 @line_num_l += 1
154 155 @line_num_r += 1
155 156 true
156 157 elsif line[0, 1] = "\\"
157 158 true
158 159 else
159 160 false
160 161 end
161 162 end
162 163 end
163 164
164 165 # A line of diff
165 166 class Diff
166 167 attr_accessor :nb_line_left
167 168 attr_accessor :line_left
168 169 attr_accessor :nb_line_right
169 170 attr_accessor :line_right
170 171 attr_accessor :type_diff_right
171 172 attr_accessor :type_diff_left
172 173
173 174 def initialize()
174 175 self.nb_line_left = ''
175 176 self.nb_line_right = ''
176 177 self.line_left = ''
177 178 self.line_right = ''
178 179 self.type_diff_right = ''
179 180 self.type_diff_left = ''
180 181 end
181 182
182 183 def inspect
183 184 puts '### Start Line Diff ###'
184 185 puts self.nb_line_left
185 186 puts self.line_left
186 187 puts self.nb_line_right
187 188 puts self.line_right
188 189 end
189 190 end
190 191 end
General Comments 0
You need to be logged in to leave comments. Login now