Mercurial > hg > soundsoftware-site
comparison lib/redmine/unified_diff.rb @ 245:051f544170fe
* Update to SVN trunk revision 4993
author | Chris Cannam |
---|---|
date | Thu, 03 Mar 2011 11:42:28 +0000 |
parents | 513646585e45 |
children | cbce1fd3b1b7 |
comparison
equal
deleted
inserted
replaced
244:8972b600f4fb | 245:051f544170fe |
---|---|
20 class UnifiedDiff < Array | 20 class UnifiedDiff < Array |
21 def initialize(diff, options={}) | 21 def initialize(diff, options={}) |
22 options.assert_valid_keys(:type, :max_lines) | 22 options.assert_valid_keys(:type, :max_lines) |
23 diff = diff.split("\n") if diff.is_a?(String) | 23 diff = diff.split("\n") if diff.is_a?(String) |
24 diff_type = options[:type] || 'inline' | 24 diff_type = options[:type] || 'inline' |
25 | |
26 lines = 0 | 25 lines = 0 |
27 @truncated = false | 26 @truncated = false |
28 diff_table = DiffTable.new(diff_type) | 27 diff_table = DiffTable.new(diff_type) |
29 diff.each do |line| | 28 diff.each do |line| |
29 line_encoding = nil | |
30 if line.respond_to?(:force_encoding) | |
31 line_encoding = line.encoding | |
32 # TODO: UTF-16 and Japanese CP932 which is imcompatible with ASCII | |
33 # In Japan, diffrence between file path encoding | |
34 # and file contents encoding is popular. | |
35 line.force_encoding('ASCII-8BIT') | |
36 end | |
30 unless diff_table.add_line line | 37 unless diff_table.add_line line |
31 self << diff_table if diff_table.length > 1 | 38 line.force_encoding(line_encoding) if line_encoding |
39 self << diff_table if diff_table.length > 0 | |
32 diff_table = DiffTable.new(diff_type) | 40 diff_table = DiffTable.new(diff_type) |
33 end | 41 end |
34 lines += 1 | 42 lines += 1 |
35 if options[:max_lines] && lines > options[:max_lines] | 43 if options[:max_lines] && lines > options[:max_lines] |
36 @truncated = true | 44 @truncated = true |
38 end | 46 end |
39 end | 47 end |
40 self << diff_table unless diff_table.empty? | 48 self << diff_table unless diff_table.empty? |
41 self | 49 self |
42 end | 50 end |
43 | 51 |
44 def truncated?; @truncated; end | 52 def truncated?; @truncated; end |
45 end | 53 end |
46 | 54 |
47 # Class that represents a file diff | 55 # Class that represents a file diff |
48 class DiffTable < Hash | 56 class DiffTable < Hash |