| 
									
										
										
										
											2019-03-17 16:36:34 +00:00
										 |  |  | # frozen_string_literal: true | 
					
						
							| 
									
										
										
										
											2019-03-15 01:32:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2021-03-25 06:58:56 +00:00
										 |  |  | # Copyright (C) 2006-2021  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2011-09-01 00:44:26 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2011-09-01 00:44:26 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Redmine | 
					
						
							|  |  |  |   # Class used to parse unified diffs | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |   class UnifiedDiff < Array | 
					
						
							| 
									
										
										
										
											2012-09-18 19:32:58 +00:00
										 |  |  |     attr_reader :diff_type, :diff_style | 
					
						
							| 
									
										
										
										
											2011-09-01 00:44:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-07 14:40:33 +00:00
										 |  |  |     def initialize(diff, options={}) | 
					
						
							| 
									
										
										
										
											2012-09-18 19:32:58 +00:00
										 |  |  |       options.assert_valid_keys(:type, :style, :max_lines) | 
					
						
							| 
									
										
										
										
											2009-10-10 15:19:27 +00:00
										 |  |  |       diff = diff.split("\n") if diff.is_a?(String) | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |       @diff_type = options[:type] || 'inline' | 
					
						
							| 
									
										
										
										
											2012-09-18 19:32:58 +00:00
										 |  |  |       @diff_style = options[:style] | 
					
						
							| 
									
										
										
										
											2020-05-05 11:56:05 +00:00
										 |  |  |       # remove git footer | 
					
						
							|  |  |  |       if diff.length > 1 && | 
					
						
							|  |  |  |            diff[-2] =~ /^--/ && | 
					
						
							|  |  |  |            diff[-1] =~ /^[0-9]/ | 
					
						
							|  |  |  |         diff.pop(2) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2008-12-07 15:21:40 +00:00
										 |  |  |       lines = 0
 | 
					
						
							|  |  |  |       @truncated = false | 
					
						
							| 
									
										
										
										
											2012-09-18 19:32:58 +00:00
										 |  |  |       diff_table = DiffTable.new(diff_type, diff_style) | 
					
						
							| 
									
										
										
										
											2013-03-07 08:47:38 +00:00
										 |  |  |       diff.each do |line_raw| | 
					
						
							|  |  |  |         line = Redmine::CodesetUtil.to_utf8_by_setting(line_raw) | 
					
						
							| 
									
										
										
										
											2013-03-07 08:31:58 +00:00
										 |  |  |         unless diff_table.add_line(line) | 
					
						
							| 
									
										
										
										
											2011-02-23 07:03:45 +00:00
										 |  |  |           self << diff_table if diff_table.length > 0
 | 
					
						
							| 
									
										
										
										
											2012-09-18 19:32:58 +00:00
										 |  |  |           diff_table = DiffTable.new(diff_type, diff_style) | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2008-12-07 15:21:40 +00:00
										 |  |  |         lines += 1
 | 
					
						
							|  |  |  |         if options[:max_lines] && lines > options[:max_lines] | 
					
						
							|  |  |  |           @truncated = true | 
					
						
							|  |  |  |           break | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |       self << diff_table unless diff_table.empty? | 
					
						
							|  |  |  |       self | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-02-21 03:59:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-07 15:21:40 +00:00
										 |  |  |     def truncated?; @truncated; end | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Class that represents a file diff | 
					
						
							| 
									
										
										
										
											2011-09-01 00:44:26 +00:00
										 |  |  |   class DiffTable < Array | 
					
						
							| 
									
										
										
										
											2018-04-27 09:14:36 +00:00
										 |  |  |     attr_reader :file_name, :previous_file_name | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Initialize with a Diff file and the type of Diff View | 
					
						
							|  |  |  |     # The type view must be inline or sbs (side_by_side) | 
					
						
							| 
									
										
										
										
											2012-09-18 19:32:58 +00:00
										 |  |  |     def initialize(type="inline", style=nil) | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |       @parsing = false | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |       @added = 0
 | 
					
						
							|  |  |  |       @removed = 0
 | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |       @type = type | 
					
						
							| 
									
										
										
										
											2012-09-18 19:32:58 +00:00
										 |  |  |       @style = style | 
					
						
							|  |  |  |       @file_name = nil | 
					
						
							| 
									
										
										
										
											2018-04-27 09:14:36 +00:00
										 |  |  |       @previous_file_name = nil | 
					
						
							| 
									
										
										
										
											2012-09-19 02:29:11 +00:00
										 |  |  |       @git_diff = false | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Function for add a line of this Diff | 
					
						
							| 
									
										
										
										
											2009-11-11 13:25:53 +00:00
										 |  |  |     # Returns false when the diff ends | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |     def add_line(line) | 
					
						
							|  |  |  |       unless @parsing | 
					
						
							|  |  |  |         if line =~ /^(---|\+\+\+) (.*)$/ | 
					
						
							| 
									
										
										
										
											2012-09-18 19:32:58 +00:00
										 |  |  |           self.file_name = $2 | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |         elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/ | 
					
						
							| 
									
										
										
										
											2008-09-21 18:45:30 +00:00
										 |  |  |           @line_num_l = $2.to_i | 
					
						
							|  |  |  |           @line_num_r = $5.to_i | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |           @parsing = true | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       else | 
					
						
							| 
									
										
										
										
											2019-03-27 02:15:24 +00:00
										 |  |  |         if %r{^[^\+\-\s@\\]}.match?(line) | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |           @parsing = false | 
					
						
							|  |  |  |           return false | 
					
						
							|  |  |  |         elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/ | 
					
						
							| 
									
										
										
										
											2008-09-21 18:45:30 +00:00
										 |  |  |           @line_num_l = $2.to_i | 
					
						
							|  |  |  |           @line_num_r = $5.to_i | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2011-09-01 00:44:26 +00:00
										 |  |  |           parse_line(line, @type) | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       return true | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-01 00:44:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |     def each_line | 
					
						
							|  |  |  |       prev_line_left, prev_line_right = nil, nil | 
					
						
							|  |  |  |       each do |line| | 
					
						
							|  |  |  |         spacing = prev_line_left && prev_line_right && (line.nb_line_left != prev_line_left+1) && (line.nb_line_right != prev_line_right+1) | 
					
						
							|  |  |  |         yield spacing, line | 
					
						
							|  |  |  |         prev_line_left = line.nb_line_left.to_i if line.nb_line_left.to_i > 0
 | 
					
						
							|  |  |  |         prev_line_right = line.nb_line_right.to_i if line.nb_line_right.to_i > 0
 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def inspect | 
					
						
							|  |  |  |       puts '### DIFF TABLE ###' | 
					
						
							|  |  |  |       puts "file : #{file_name}" | 
					
						
							|  |  |  |       self.each do |d| | 
					
						
							|  |  |  |         d.inspect | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |     private | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-18 19:32:58 +00:00
										 |  |  |     def file_name=(arg) | 
					
						
							| 
									
										
										
										
											2012-09-19 02:29:11 +00:00
										 |  |  |       both_git_diff = false | 
					
						
							|  |  |  |       if file_name.nil? | 
					
						
							| 
									
										
										
										
											2019-03-27 02:15:24 +00:00
										 |  |  |         @git_diff = true if %r{^(a/|/dev/null)}.match?(arg) | 
					
						
							| 
									
										
										
										
											2012-09-19 02:29:11 +00:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2019-03-27 02:15:24 +00:00
										 |  |  |         both_git_diff = (@git_diff && %r{^(b/|/dev/null)}.match?(arg)) | 
					
						
							| 
									
										
										
										
											2012-09-19 02:29:11 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |       if both_git_diff | 
					
						
							| 
									
										
										
										
											2012-09-18 19:32:58 +00:00
										 |  |  |         if file_name && arg == "/dev/null" | 
					
						
							|  |  |  |           # keep the original file name | 
					
						
							| 
									
										
										
										
											2012-09-19 02:29:11 +00:00
										 |  |  |           @file_name = file_name.sub(%r{^a/}, '') | 
					
						
							| 
									
										
										
										
											2012-09-18 19:32:58 +00:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2018-04-27 09:14:36 +00:00
										 |  |  |           # remove leading a/ | 
					
						
							|  |  |  |           @previous_file_name = file_name.sub(%r{^a/}, '') unless file_name == "/dev/null" | 
					
						
							| 
									
										
										
										
											2012-09-19 02:29:11 +00:00
										 |  |  |           # remove leading b/ | 
					
						
							|  |  |  |           @file_name = arg.sub(%r{^b/}, '') | 
					
						
							| 
									
										
										
										
											2018-04-27 09:14:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |           @previous_file_name = nil if @previous_file_name == @file_name | 
					
						
							| 
									
										
										
										
											2012-09-18 19:32:58 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2012-09-19 02:29:11 +00:00
										 |  |  |       elsif @style == "Subversion" | 
					
						
							| 
									
										
										
										
											2012-09-18 19:44:08 +00:00
										 |  |  |         # removing trailing "(revision nn)" | 
					
						
							|  |  |  |         @file_name = arg.sub(%r{\t+\(.*\)$}, '') | 
					
						
							| 
									
										
										
										
											2012-09-18 19:32:58 +00:00
										 |  |  |       else | 
					
						
							|  |  |  |         @file_name = arg | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |     def diff_for_added_line | 
					
						
							|  |  |  |       if @type == 'sbs' && @removed > 0 && @added < @removed | 
					
						
							|  |  |  |         self[-(@removed - @added)] | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         diff = Diff.new | 
					
						
							|  |  |  |         self << diff | 
					
						
							|  |  |  |         diff | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def parse_line(line, type="inline") | 
					
						
							|  |  |  |       if line[0, 1] == "+" | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |         diff = diff_for_added_line | 
					
						
							| 
									
										
										
										
											2012-02-15 17:39:47 +00:00
										 |  |  |         diff.line_right = line[1..-1] | 
					
						
							| 
									
										
										
										
											2008-09-21 18:45:30 +00:00
										 |  |  |         diff.nb_line_right = @line_num_r | 
					
						
							|  |  |  |         diff.type_diff_right = 'diff_in' | 
					
						
							|  |  |  |         @line_num_r += 1
 | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |         @added += 1
 | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |         true | 
					
						
							|  |  |  |       elsif line[0, 1] == "-" | 
					
						
							|  |  |  |         diff = Diff.new | 
					
						
							| 
									
										
										
										
											2012-02-15 17:39:47 +00:00
										 |  |  |         diff.line_left = line[1..-1] | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |         diff.nb_line_left = @line_num_l | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |         diff.type_diff_left = 'diff_out' | 
					
						
							|  |  |  |         self << diff | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |         @line_num_l += 1
 | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |         @removed += 1
 | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |         true | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |       else | 
					
						
							|  |  |  |         write_offsets | 
					
						
							| 
									
										
										
										
											2019-03-27 02:15:24 +00:00
										 |  |  |         if /\s/.match?(line[0, 1]) | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |           diff = Diff.new | 
					
						
							| 
									
										
										
										
											2012-02-15 17:39:47 +00:00
										 |  |  |           diff.line_right = line[1..-1] | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |           diff.nb_line_right = @line_num_r | 
					
						
							| 
									
										
										
										
											2012-02-15 17:39:47 +00:00
										 |  |  |           diff.line_left = line[1..-1] | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |           diff.nb_line_left = @line_num_l | 
					
						
							|  |  |  |           self << diff | 
					
						
							|  |  |  |           @line_num_l += 1
 | 
					
						
							|  |  |  |           @line_num_r += 1
 | 
					
						
							|  |  |  |           true | 
					
						
							|  |  |  |         elsif line[0, 1] = "\\" | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |           true | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           false | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-01 00:44:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |     def write_offsets | 
					
						
							|  |  |  |       if @added > 0 && @added == @removed | 
					
						
							|  |  |  |         @added.times do |i| | 
					
						
							|  |  |  |           line = self[-(1 + i)] | 
					
						
							|  |  |  |           removed = (@type == 'sbs') ? line : self[-(1 + @added + i)] | 
					
						
							|  |  |  |           offsets = offsets(removed.line_left, line.line_right) | 
					
						
							|  |  |  |           removed.offsets = line.offsets = offsets | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       @added = 0
 | 
					
						
							|  |  |  |       @removed = 0
 | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-01 00:44:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |     def offsets(line_left, line_right) | 
					
						
							|  |  |  |       if line_left.present? && line_right.present? && line_left != line_right | 
					
						
							|  |  |  |         max = [line_left.size, line_right.size].min | 
					
						
							|  |  |  |         starting = 0
 | 
					
						
							| 
									
										
										
										
											2019-10-19 11:15:54 +00:00
										 |  |  |         while starting < max && | 
					
						
							|  |  |  |                 line_left[starting] == line_right[starting] | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |           starting += 1
 | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         ending = -1
 | 
					
						
							| 
									
										
										
										
											2019-10-19 11:15:54 +00:00
										 |  |  |         while ending >= -(max - starting) && | 
					
						
							|  |  |  |                (line_left[ending] == line_right[ending]) | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |           ending -= 1
 | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         unless starting == 0 && ending == -1
 | 
					
						
							|  |  |  |           [starting, ending] | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # A line of diff | 
					
						
							| 
									
										
										
										
											2011-09-01 00:44:26 +00:00
										 |  |  |   class Diff | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |     attr_accessor :nb_line_left | 
					
						
							|  |  |  |     attr_accessor :line_left | 
					
						
							|  |  |  |     attr_accessor :nb_line_right | 
					
						
							|  |  |  |     attr_accessor :line_right | 
					
						
							|  |  |  |     attr_accessor :type_diff_right | 
					
						
							|  |  |  |     attr_accessor :type_diff_left | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |     attr_accessor :offsets | 
					
						
							| 
									
										
										
										
											2011-09-01 00:44:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-20 12:15:28 +00:00
										 |  |  |     def initialize | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |       self.nb_line_left = '' | 
					
						
							|  |  |  |       self.nb_line_right = '' | 
					
						
							|  |  |  |       self.line_left = '' | 
					
						
							|  |  |  |       self.line_right = '' | 
					
						
							|  |  |  |       self.type_diff_right = '' | 
					
						
							|  |  |  |       self.type_diff_left = '' | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-01 00:44:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |     def type_diff | 
					
						
							|  |  |  |       type_diff_right == 'diff_in' ? type_diff_right : type_diff_left | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-01 00:44:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |     def line | 
					
						
							|  |  |  |       type_diff_right == 'diff_in' ? line_right : line_left | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-01 00:44:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |     def html_line_left | 
					
						
							| 
									
										
										
										
											2012-02-15 17:39:47 +00:00
										 |  |  |       line_to_html(line_left, offsets) | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-01 00:44:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |     def html_line_right | 
					
						
							| 
									
										
										
										
											2012-02-15 17:39:47 +00:00
										 |  |  |       line_to_html(line_right, offsets) | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2011-09-01 00:44:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |     def html_line | 
					
						
							| 
									
										
										
										
											2012-02-15 17:39:47 +00:00
										 |  |  |       line_to_html(line, offsets) | 
					
						
							| 
									
										
										
										
											2011-03-11 20:23:29 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def inspect | 
					
						
							|  |  |  |       puts '### Start Line Diff ###' | 
					
						
							|  |  |  |       puts self.nb_line_left | 
					
						
							|  |  |  |       puts self.line_left | 
					
						
							|  |  |  |       puts self.nb_line_right | 
					
						
							|  |  |  |       puts self.line_right | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2012-02-15 17:39:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def line_to_html(line, offsets) | 
					
						
							| 
									
										
										
										
											2013-03-07 10:17:45 +00:00
										 |  |  |       html = line_to_html_raw(line, offsets) | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |       html.force_encoding('UTF-8') | 
					
						
							| 
									
										
										
										
											2013-03-07 10:17:45 +00:00
										 |  |  |       html | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def line_to_html_raw(line, offsets) | 
					
						
							| 
									
										
										
										
											2012-02-15 17:39:47 +00:00
										 |  |  |       if offsets | 
					
						
							| 
									
										
										
										
											2019-03-17 16:36:34 +00:00
										 |  |  |         s = +'' | 
					
						
							| 
									
										
										
										
											2012-02-15 17:39:47 +00:00
										 |  |  |         unless offsets.first == 0
 | 
					
						
							|  |  |  |           s << CGI.escapeHTML(line[0..offsets.first-1]) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         s << '<span>' + CGI.escapeHTML(line[offsets.first..offsets.last]) + '</span>' | 
					
						
							|  |  |  |         unless offsets.last == -1
 | 
					
						
							|  |  |  |           s << CGI.escapeHTML(line[offsets.last+1..-1]) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         s | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         CGI.escapeHTML(line) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2008-06-08 16:28:42 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | end |