docdiff 0.6.5 → 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +7 -7
  3. data/Guardfile +4 -4
  4. data/Makefile +1 -1
  5. data/Rakefile +6 -6
  6. data/bin/docdiff +1 -1
  7. data/devutil/Rakefile +12 -5
  8. data/devutil/char_by_charclass.rb +43 -20
  9. data/devutil/charclass_by_char.rb +40 -19
  10. data/devutil/jis0208.rb +263 -231
  11. data/devutil/jis0208_test.rb +196 -0
  12. data/doc/news.md +8 -0
  13. data/docdiff.gemspec +12 -10
  14. data/lib/doc_diff.rb +59 -60
  15. data/lib/docdiff/charstring.rb +225 -241
  16. data/lib/docdiff/cli.rb +285 -250
  17. data/lib/docdiff/diff/contours.rb +1 -1
  18. data/lib/docdiff/diff/editscript.rb +1 -1
  19. data/lib/docdiff/diff/rcsdiff.rb +1 -1
  20. data/lib/docdiff/diff/shortestpath.rb +1 -1
  21. data/lib/docdiff/diff/speculative.rb +1 -1
  22. data/lib/docdiff/diff/subsequence.rb +1 -1
  23. data/lib/docdiff/diff/unidiff.rb +1 -1
  24. data/lib/docdiff/diff.rb +1 -1
  25. data/lib/docdiff/difference.rb +71 -70
  26. data/lib/docdiff/document.rb +129 -109
  27. data/lib/docdiff/encoding/en_ascii.rb +64 -58
  28. data/lib/docdiff/encoding/ja_eucjp.rb +250 -235
  29. data/lib/docdiff/encoding/ja_sjis.rb +240 -226
  30. data/lib/docdiff/encoding/ja_utf8.rb +6952 -6939
  31. data/lib/docdiff/version.rb +1 -1
  32. data/lib/docdiff/view.rb +522 -438
  33. data/lib/docdiff.rb +2 -2
  34. data/test/charstring_test.rb +475 -351
  35. data/test/cli_test.rb +103 -101
  36. data/test/diff_test.rb +15 -16
  37. data/test/difference_test.rb +40 -31
  38. data/test/docdiff_test.rb +162 -136
  39. data/test/document_test.rb +280 -175
  40. data/test/test_helper.rb +2 -1
  41. data/test/view_test.rb +636 -497
  42. metadata +8 -8
  43. data/devutil/testjis0208.rb +0 -38
data/lib/docdiff/cli.rb CHANGED
@@ -1,281 +1,316 @@
1
- require 'optparse'
1
+ require "optparse"
2
2
 
3
3
  class DocDiff
4
4
  module CLI
5
- def self.parse_options!(args, base_options: {})
6
- o = base_options.dup
7
-
8
- option_parser = OptionParser.new do |parser|
9
- parser.on(
10
- '--resolution=RESOLUTION',
11
- resolutions = ['line', 'word', 'char'],
12
- 'specify resolution (granularity)',
13
- "#{resolutions.join('|')} (default: word)"
14
- ){|s| o[:resolution] = (s || "word")}
15
- parser.on('--line', 'same as --resolution=line'){o[:resolution] = "line"}
16
- parser.on('--word', 'same as --resolution=word'){o[:resolution] = "word"}
17
- parser.on('--char', 'same as --resolution=char'){o[:resolution] = "char"}
18
-
19
- parser.on(
20
- '--encoding=ENCODING',
21
- encodings = ['ASCII', 'EUC-JP', 'Shift_JIS', 'CP932', 'UTF-8', 'auto'],
22
- "specify character encoding",
23
- "#{encodings.join('|')} (default: auto)",
24
- "(try ASCII for single byte encodings such as ISO-8859)"
25
- ){|s| o[:encoding] = (s || "auto")}
26
- parser.on('--ascii', 'same as --encoding=ASCII'){o[:encoding] = "ASCII"}
27
- parser.on('--iso8859', 'same as --encoding=ASCII'){o[:encoding] = "ASCII"}
28
- parser.on('--iso8859x', 'same as --encoding=ASCII (deprecated)'){o[:encoding] = "ASCII"}
29
- parser.on('--eucjp', 'same as --encoding=EUC-JP'){o[:encoding] = "EUC-JP"}
30
- parser.on('--sjis', 'same as --encoding=Shift_JIS'){o[:encoding] = "Shift_JIS"}
31
- parser.on('--cp932', 'same as --encoding=CP932'){o[:encoding] = "CP932"}
32
- parser.on('--utf8', 'same as --encoding=UTF-8'){o[:encoding] = "UTF-8"}
33
-
34
- parser.on(
35
- '--eol=EOL',
36
- eols = ['CR','LF','CRLF','auto'],
37
- 'specify end-of-line character',
38
- "#{eols.join('|')} (default: auto)",
39
- ){|s| o[:eol] = (s || "auto")}
40
- parser.on('--cr', 'same as --eol=CR'){o[:eol] = "CR"}
41
- parser.on('--lf', 'same as --eol=LF'){o[:eol] = "LF"}
42
- parser.on('--crlf', 'same as --eol=CRLF'){o[:eol] = "CRLF"}
43
-
44
- parser.on(
45
- '--format=FORMAT',
46
- formats = ['tty', 'manued', 'html', 'wdiff', 'stat', 'user'],
47
- 'specify output format',
48
- "#{formats.join('|')} (default: html) (stat is deprecated)",
49
- '(user tags can be defined in config file)'
50
- ){|s| o[:format] = (s || "manued")}
51
- parser.on('--tty', 'same as --format=tty'){o[:format] = "tty"}
52
- parser.on('--manued', 'same as --format=manued'){o[:format] = "manued"}
53
- parser.on('--html', 'same as --format=html'){o[:format] = "html"}
54
- parser.on('--wdiff', 'same as --format=wdiff'){o[:format] = "wdiff"}
55
- parser.on('--stat', 'same as --format=stat (not implemented) (deprecated)'){o[:format] = "stat"}
56
-
57
- parser.on(
58
- '--label LABEL', '-L LABEL',
59
- 'use label instead of file name (not implemented; exists for compatibility with diff)'
60
- ){|s| o[:label] ||= []; o[:label] << s}
61
-
62
- parser.on('--digest', 'digest output, do not show all'){o[:digest] = true}
63
- parser.on('--summary', 'same as --digest'){o[:digest] = true}
64
-
65
- parser.on(
66
- '--display=DISPLAY',
67
- display_types = ['inline', 'block', 'multi'],
68
- 'specify presentation type (effective only with digest; experimental feature)',
69
- "#{display_types.join('|')} (default: inline) (multi is deprecated)",
70
- ){|s| o[:display] ||= s.downcase}
71
-
72
- parser.on('--cache', 'use file cache (not implemented) (deprecated)'){o[:cache] = true}
73
- parser.on(
74
- '--pager=PAGER', String,
75
- 'specify pager (if available, $DOCDIFF_PAGER is used by default)'
76
- ){|s| o[:pager] = s}
77
- parser.on('--no-pager', 'do not use pager'){o[:pager] = false}
78
- parser.on('--config-file=FILE', String, 'specify config file to read'){|s| o[:config_file] = s}
79
- parser.on('--no-config-file', 'do not read config files'){o[:no_config_file] = true}
80
- parser.on('--verbose', 'run verbosely (not well-supported) (deprecated)'){o[:verbose] = true}
81
-
82
- parser.on('--help', 'show this message'){puts parser; exit(0)}
83
- parser.on('--version', 'show version'){puts Docdiff::VERSION; exit(0)}
84
- parser.on('--license', 'show license (deprecated)'){puts DocDiff::License; exit(0)}
85
- parser.on('--author', 'show author(s) (deprecated)'){puts DocDiff::Author; exit(0)}
86
-
87
- parser.on_tail(
88
- "When invoked as worddiff or chardiff, resolution will be set accordingly.",
89
- "Config files: /etc/docdiff/docdiff.conf, ~/.config/docdiff/docdiff.conf (or ~/etc/docdiff/docdiff.conf (deprecated))"
90
- )
5
+ class << self
6
+ def parse_options!(args, base_options: {})
7
+ o = base_options.dup
8
+
9
+ option_parser = OptionParser.new do |parser|
10
+ parser.on(
11
+ "--resolution=RESOLUTION",
12
+ resolutions = ["line", "word", "char"],
13
+ "specify resolution (granularity)",
14
+ "#{resolutions.join("|")} (default: word)",
15
+ ) { |s| o[:resolution] = (s || "word") }
16
+ parser.on("--line", "same as --resolution=line") { o[:resolution] = "line" }
17
+ parser.on("--word", "same as --resolution=word") { o[:resolution] = "word" }
18
+ parser.on("--char", "same as --resolution=char") { o[:resolution] = "char" }
19
+
20
+ parser.on(
21
+ '--encoding=ENCODING',
22
+ encodings = ['ASCII', 'EUC-JP', 'Shift_JIS', 'CP932', 'UTF-8', 'auto'],
23
+ encoding_aliases = {
24
+ "ascii" => "ASCII",
25
+ "euc-jp" => "EUC-JP",
26
+ "shift_jis" => "Shift_JIS",
27
+ "cp932" => "CP932",
28
+ "utf-8" => "UTF-8",
29
+ },
30
+ "specify character encoding",
31
+ "#{encodings.join("|")} (default: auto)",
32
+ "(try ASCII for single byte encodings such as ISO-8859)",
33
+ ) { |s| o[:encoding] = (s || "auto") }
34
+ parser.on("--ascii", "same as --encoding=ASCII") { o[:encoding] = "ASCII" }
35
+ parser.on("--iso8859", "same as --encoding=ASCII") { o[:encoding] = "ASCII" }
36
+ parser.on("--iso8859x", "same as --encoding=ASCII (deprecated)") { o[:encoding] = "ASCII" }
37
+ parser.on("--eucjp", "same as --encoding=EUC-JP") { o[:encoding] = "EUC-JP" }
38
+ parser.on("--sjis", "same as --encoding=Shift_JIS") { o[:encoding] = "Shift_JIS" }
39
+ parser.on("--cp932", "same as --encoding=CP932") { o[:encoding] = "CP932" }
40
+ parser.on("--utf8", "same as --encoding=UTF-8") { o[:encoding] = "UTF-8" }
41
+
42
+ parser.on(
43
+ "--eol=EOL",
44
+ eols = ["CR", "LF", "CRLF", "auto"],
45
+ eol_aliases = { "cr" => "CR", "lf" => "LF", "crlf" => "CRLF" },
46
+ "specify end-of-line character",
47
+ "#{eols.join("|")} (default: auto)",
48
+ ) { |s| o[:eol] = (s || "auto") }
49
+ parser.on("--cr", "same as --eol=CR") { o[:eol] = "CR" }
50
+ parser.on("--lf", "same as --eol=LF") { o[:eol] = "LF" }
51
+ parser.on("--crlf", "same as --eol=CRLF") { o[:eol] = "CRLF" }
52
+
53
+ parser.on(
54
+ "--format=FORMAT",
55
+ formats = ["tty", "manued", "html", "wdiff", "stat", "user"],
56
+ "specify output format",
57
+ "#{formats.join("|")} (default: html) (stat is deprecated)",
58
+ "(user tags can be defined in config file)",
59
+ ) { |s| o[:format] = (s || "manued") }
60
+ parser.on("--tty", "same as --format=tty") { o[:format] = "tty" }
61
+ parser.on("--manued", "same as --format=manued") { o[:format] = "manued" }
62
+ parser.on("--html", "same as --format=html") { o[:format] = "html" }
63
+ parser.on("--wdiff", "same as --format=wdiff") { o[:format] = "wdiff" }
64
+ parser.on("--stat", "same as --format=stat (not implemented) (deprecated)") { o[:format] = "stat" }
65
+
66
+ parser.on(
67
+ "--label LABEL",
68
+ "-L LABEL",
69
+ "use label instead of file name (not implemented; exists for compatibility with diff)",
70
+ ) do |s|
71
+ o[:label] ||= []
72
+ o[:label] << s
73
+ end
74
+
75
+ parser.on("--digest", "digest output, do not show all") { o[:digest] = true }
76
+ parser.on("--summary", "same as --digest") { o[:digest] = true }
77
+
78
+ parser.on(
79
+ "--display=DISPLAY",
80
+ display_types = ["inline", "block", "multi"],
81
+ "specify presentation type (effective only with digest; experimental feature)",
82
+ "#{display_types.join("|")} (default: inline) (multi is deprecated)",
83
+ ) { |s| o[:display] ||= s.downcase }
84
+
85
+ parser.on("--cache", "use file cache (not implemented) (deprecated)") { o[:cache] = true }
86
+ parser.on(
87
+ "--pager=PAGER",
88
+ String,
89
+ "specify pager (if available, $DOCDIFF_PAGER is used by default)",
90
+ ) { |s| o[:pager] = s }
91
+ parser.on("--no-pager", "do not use pager") { o[:pager] = false }
92
+ parser.on("--config-file=FILE", String, "specify config file to read") { |s| o[:config_file] = s }
93
+ parser.on("--no-config-file", "do not read config files") { o[:no_config_file] = true }
94
+ parser.on("--verbose", "run verbosely (not well-supported) (deprecated)") { o[:verbose] = true }
95
+
96
+ parser.on("--help", "show this message") do
97
+ puts parser
98
+ exit(0)
99
+ end
100
+ parser.on("--version", "show version") do
101
+ puts Docdiff::VERSION
102
+ exit(0)
103
+ end
104
+ parser.on("--license", "show license (deprecated)") do
105
+ puts DocDiff::License
106
+ exit(0)
107
+ end
108
+ parser.on("--author", "show author(s) (deprecated)") do
109
+ puts DocDiff::Author
110
+ exit(0)
111
+ end
112
+
113
+ parser.on_tail(
114
+ "When invoked as worddiff or chardiff, resolution will be set accordingly.",
115
+ "Config files: /etc/docdiff/docdiff.conf, ~/.config/docdiff/docdiff.conf (or ~/etc/docdiff/docdiff.conf (deprecated))",
116
+ )
117
+ end
118
+
119
+ option_parser.parse!(args)
120
+ o
91
121
  end
92
122
 
93
- option_parser.parse!(args)
94
- o
95
- end
123
+ def parse_config_file_content(content)
124
+ result = {}
125
+ return result if content.size <= 0
96
126
 
97
- def self.parse_config_file_content(content)
98
- result = {}
99
- return result if content.size <= 0
100
- lines = content.dup.split(/\r\n|\r|\n/).compact
101
- lines.collect!{|line| line.sub(/#.*$/, '')}
102
- lines.collect!{|line| line.strip}
103
- lines.delete_if{|line| line == ""}
104
- lines.each{|line|
105
- raise 'line does not include " = ".' unless /[\s]+=[\s]+/.match line
106
- name_src, value_src = line.split(/[\s]+=[\s]+/)
107
- raise "Invalid name: #{name_src.inspect}" if (/\s/.match name_src)
108
- raise "Invalid value: #{value_src.inspect}" unless value_src.kind_of?(String)
109
- name = name_src.intern
110
- value = value_src
111
- value = true if ['on','yes','true'].include? value_src.downcase
112
- value = false if ['off','no','false'].include? value_src.downcase
113
- value = value_src.to_i if /^[0-9]+$/.match value_src
114
- result[name] = value
115
- }
116
- result
117
- end
127
+ lines = content.dup.split(/\r\n|\r|\n/).compact
128
+ lines.collect! { |line| line.sub(/#.*$/, "") }
129
+ lines.collect!(&:strip)
130
+ lines.delete_if { |line| line == "" }
131
+ lines.each do |line|
132
+ raise 'line does not include " = ".' unless /[\s]+=[\s]+/.match(line)
133
+
134
+ name_src, value_src = line.split(/[\s]+=[\s]+/)
135
+ raise "Invalid name: #{name_src.inspect}" if /\s/.match(name_src)
136
+ raise "Invalid value: #{value_src.inspect}" unless value_src.is_a?(String)
137
+
138
+ name = name_src.intern
139
+ value = value_src
140
+ value = true if ["on", "yes", "true"].include?(value_src.downcase)
141
+ value = false if ["off", "no", "false"].include?(value_src.downcase)
142
+ value = value_src.to_i if /^[0-9]+$/.match(value_src)
143
+ result[name] = value
144
+ end
145
+ result
146
+ end
118
147
 
119
- def self.read_config_from_file(filename)
120
- content = nil
121
- begin
122
- File.open(filename, "r"){|f| content = f.read}
123
- rescue => exception
124
- raise exception
125
- ensure
126
- message =
127
- case exception
128
- in Errno::ENOENT
129
- "config file not found: #{filename.inspect}"
130
- in Errno::EACCES
131
- "permission denied for reading: #{filename.inspect}"
148
+ def read_config_from_file(filename)
149
+ content = nil
150
+ begin
151
+ File.open(filename, "r") { |f| content = f.read }
152
+ rescue => exception
153
+ raise exception
154
+ ensure
155
+ message =
156
+ case exception
157
+ in Errno::ENOENT
158
+ "config file not found: #{filename.inspect}"
159
+ in Errno::EACCES
160
+ "permission denied for reading: #{filename.inspect}"
161
+ else
162
+ "something unexpected happened: #{filename.inspect}"
163
+ end
164
+ if content
165
+ config = parse_config_file_content(content)
132
166
  else
133
- "something unexpected happened: #{filename.inspect}"
167
+ message = "config file empty: #{filename.inspect}"
134
168
  end
135
- if content
136
- config = parse_config_file_content(content)
137
- else
138
- message = "config file empty: #{filename.inspect}"
139
169
  end
170
+ [config, message]
140
171
  end
141
- [config, message]
142
- end
143
172
 
144
- def self.print_or_write_to_pager(content, pager)
145
- if STDOUT.tty? && pager.is_a?(String) && !pager.empty?
146
- IO.popen(pager, "w"){|f| f.print content}
147
- else
148
- print content
173
+ def print_or_write_to_pager(content, pager)
174
+ if $stdout.tty? && pager.is_a?(String) && !pager.empty?
175
+ IO.popen(pager, "w") { |f| f.print(content) }
176
+ else
177
+ print(content)
178
+ end
149
179
  end
150
- end
151
180
 
152
- def self.run
153
- command_line_config = parse_options!(ARGV)
154
-
155
- system_config =
156
- unless command_line_config[:no_config_file]
157
- possible_system_config_file_names = [
158
- DocDiff::SystemConfigFileName,
159
- ]
160
- existing_system_config_file_names =
161
- possible_system_config_file_names.select{|fn| File.exist? fn}
162
- if existing_system_config_file_names.size >= 2
163
- raise <<~EOS
164
- More than one system config file found, using the first one: \
165
- #{existing_system_config_file_names.inspect}
166
- EOS
181
+ def run
182
+ command_line_config = parse_options!(ARGV)
183
+
184
+ system_config =
185
+ unless command_line_config[:no_config_file]
186
+ possible_system_config_file_names = [
187
+ DocDiff::SystemConfigFileName,
188
+ ]
189
+ existing_system_config_file_names =
190
+ possible_system_config_file_names.select { |fn| File.exist?(fn) }
191
+ if existing_system_config_file_names.size >= 2
192
+ raise <<~EOS
193
+ More than one system config file found, using the first one: \
194
+ #{existing_system_config_file_names.inspect}
195
+ EOS
196
+ end
197
+
198
+ filename = existing_system_config_file_names.first
199
+ config, message = read_config_from_file(filename)
200
+ $stderr.print(message) if command_line_config[:verbose]
201
+ config
167
202
  end
168
- filename = existing_system_config_file_names.first
169
- config, message = read_config_from_file(filename)
170
- STDERR.print message if command_line_config[:verbose]
171
- config
172
- end
173
203
 
174
- user_config =
175
- unless command_line_config[:no_config_file]
176
- possible_user_config_file_names = [
177
- DocDiff::UserConfigFileName,
178
- DocDiff::AltUserConfigFileName,
179
- DocDiff::XDGUserConfigFileName,
180
- ]
181
- existing_user_config_file_names =
182
- possible_user_config_file_names.select{|fn| File.exist? fn}
183
- if existing_user_config_file_names.size >= 2
184
- raise <<~EOS
185
- Only one user config file can be used at the same time. \
186
- Keep one and remove or rename the others: \
187
- #{existing_user_config_file_names.inspect}
188
- EOS
204
+ user_config =
205
+ unless command_line_config[:no_config_file]
206
+ possible_user_config_file_names = [
207
+ DocDiff::UserConfigFileName,
208
+ DocDiff::AltUserConfigFileName,
209
+ DocDiff::XDGUserConfigFileName,
210
+ ]
211
+ existing_user_config_file_names =
212
+ possible_user_config_file_names.select { |fn| File.exist?(fn) }
213
+ if existing_user_config_file_names.size >= 2
214
+ raise <<~EOS
215
+ Only one user config file can be used at the same time. \
216
+ Keep one and remove or rename the others: \
217
+ #{existing_user_config_file_names.inspect}
218
+ EOS
219
+ end
220
+
221
+ filename = existing_user_config_file_names.first
222
+ config, message = read_config_from_file(filename)
223
+ $stderr.print(message) if command_line_config[:verbose]
224
+ config
189
225
  end
190
- filename = existing_user_config_file_names.first
191
- config, message = read_config_from_file(filename)
192
- STDERR.print message if command_line_config[:verbose]
193
- config
194
- end
195
226
 
196
- config_from_specified_file =
197
- if filename = command_line_config[:config_file]
198
- config, message = read_config_from_file(filename)
199
- STDERR.print message if command_line_config[:verbose] == true
200
- config
201
- end
227
+ config_from_specified_file =
228
+ if (filename = command_line_config[:config_file])
229
+ config, message = read_config_from_file(filename)
230
+ $stderr.print(message) if command_line_config[:verbose] == true
231
+ config
232
+ end
233
+
234
+ config_from_program_name =
235
+ case File.basename($PROGRAM_NAME, ".*")
236
+ when "worddiff" then { resolution: "word" }
237
+ when "chardiff" then { resolution: "char" }
238
+ end
202
239
 
203
- config_from_program_name =
204
- case File.basename($PROGRAM_NAME, ".*")
205
- when "worddiff" then {:resolution => "word"}
206
- when "chardiff" then {:resolution => "char"}
240
+ config_from_env_vars = {}
241
+ if (pager = ENV["DOCDIFF_PAGER"]) && !pager.empty?
242
+ config_from_env_vars[:pager] = pager
207
243
  end
208
244
 
209
- config_from_env_vars = {}
210
- if (pager = ENV['DOCDIFF_PAGER']) && !pager.empty?
211
- config_from_env_vars[:pager] = pager
212
- end
245
+ config_in_effect = DocDiff::DEFAULT_CONFIG.dup
246
+ config_in_effect.merge!(config_from_program_name) if config_from_program_name
247
+ config_in_effect.merge!(system_config) if system_config
248
+ config_in_effect.merge!(user_config) if user_config
249
+ config_in_effect.merge!(config_from_env_vars) if config_from_env_vars
250
+ config_in_effect.merge!(config_from_specified_file) if config_from_specified_file
251
+ config_in_effect.merge!(command_line_config) if command_line_config
252
+
253
+ docdiff = DocDiff.new(config: config_in_effect)
254
+
255
+ file1_content = nil
256
+ file2_content = nil
257
+ raise "Try `#{File.basename($PROGRAM_NAME)} --help' for more information." if ARGV[0].nil?
258
+ raise "Specify at least 2 target files." unless ARGV[0] && ARGV[1]
259
+
260
+ ARGV[0] = "/dev/stdin" if ARGV[0] == "-"
261
+ ARGV[1] = "/dev/stdin" if ARGV[1] == "-"
262
+ raise "No such file: #{ARGV[0]}." unless FileTest.exist?(ARGV[0])
263
+ raise "No such file: #{ARGV[1]}." unless FileTest.exist?(ARGV[1])
264
+ raise "#{ARGV[0]} is not readable." unless FileTest.readable?(ARGV[0])
265
+ raise "#{ARGV[1]} is not readable." unless FileTest.readable?(ARGV[1])
266
+
267
+ File.open(ARGV[0], "r") { |f| file1_content = f.read }
268
+ File.open(ARGV[1], "r") { |f| file2_content = f.read }
213
269
 
214
- config_in_effect = DocDiff::DEFAULT_CONFIG.dup
215
- config_in_effect.merge!(config_from_program_name) if config_from_program_name
216
- config_in_effect.merge!(system_config) if system_config
217
- config_in_effect.merge!(user_config) if user_config
218
- config_in_effect.merge!(config_from_env_vars) if config_from_env_vars
219
- config_in_effect.merge!(config_from_specified_file) if config_from_specified_file
220
- config_in_effect.merge!(command_line_config) if command_line_config
221
-
222
- docdiff = DocDiff.new(config: config_in_effect)
223
-
224
- file1_content = nil
225
- file2_content = nil
226
- raise "Try `#{File.basename($0)} --help' for more information." if ARGV[0].nil?
227
- raise "Specify at least 2 target files." unless ARGV[0] && ARGV[1]
228
- ARGV[0] = "/dev/stdin" if ARGV[0] == "-"
229
- ARGV[1] = "/dev/stdin" if ARGV[1] == "-"
230
- raise "No such file: #{ARGV[0]}." unless FileTest.exist?(ARGV[0])
231
- raise "No such file: #{ARGV[1]}." unless FileTest.exist?(ARGV[1])
232
- raise "#{ARGV[0]} is not readable." unless FileTest.readable?(ARGV[0])
233
- raise "#{ARGV[1]} is not readable." unless FileTest.readable?(ARGV[1])
234
- File.open(ARGV[0], "r"){|f| file1_content = f.read}
235
- File.open(ARGV[1], "r"){|f| file2_content = f.read}
236
-
237
- doc1 = nil
238
- doc2 = nil
239
-
240
- encoding1 = docdiff.config[:encoding]
241
- encoding2 = docdiff.config[:encoding]
242
- eol1 = docdiff.config[:eol]
243
- eol2 = docdiff.config[:eol]
244
-
245
- if docdiff.config[:encoding] == "auto"
246
- encoding1 = DocDiff::CharString.guess_encoding(file1_content)
247
- encoding2 = DocDiff::CharString.guess_encoding(file2_content)
248
- case
249
- when (encoding1 == "UNKNOWN" or encoding2 == "UNKNOWN")
250
- raise "Document encoding unknown (#{encoding1}, #{encoding2})."
251
- when encoding1 != encoding2
252
- raise "Document encoding mismatch (#{encoding1}, #{encoding2})."
270
+ encoding1 = docdiff.config[:encoding]
271
+ encoding2 = docdiff.config[:encoding]
272
+ eol1 = docdiff.config[:eol]
273
+ eol2 = docdiff.config[:eol]
274
+
275
+ if docdiff.config[:encoding] == "auto"
276
+ encoding1 = DocDiff::CharString.guess_encoding(file1_content)
277
+ encoding2 = DocDiff::CharString.guess_encoding(file2_content)
278
+ if (encoding1 == "UNKNOWN") || (encoding2 == "UNKNOWN")
279
+ raise "Document encoding unknown (#{encoding1}, #{encoding2})."
280
+ elsif encoding1 != encoding2
281
+ raise "Document encoding mismatch (#{encoding1}, #{encoding2})."
282
+ end
253
283
  end
254
- end
255
284
 
256
- if docdiff.config[:eol] == "auto"
257
- eol1 = DocDiff::CharString.guess_eol(file1_content)
258
- eol2 = DocDiff::CharString.guess_eol(file2_content)
259
- case
260
- when (eol1.nil? or eol2.nil?)
261
- raise "Document eol is nil (#{eol1.inspect}, #{eol2.inspect}). The document might be empty."
262
- when (eol1 == 'UNKNOWN' or eol2 == 'UNKNOWN')
263
- raise "Document eol unknown (#{eol1.inspect}, #{eol2.inspect})."
264
- when (eol1 != eol2)
265
- raise "Document eol mismatch (#{eol1}, #{eol2})."
285
+ if docdiff.config[:eol] == "auto"
286
+ eol1 = DocDiff::CharString.guess_eol(file1_content)
287
+ eol2 = DocDiff::CharString.guess_eol(file2_content)
288
+ if eol1.nil? || eol2.nil?
289
+ raise "Document eol is nil (#{eol1.inspect}, #{eol2.inspect}). The document might be empty."
290
+ elsif (eol1 == "UNKNOWN") || (eol2 == "UNKNOWN")
291
+ raise "Document eol unknown (#{eol1.inspect}, #{eol2.inspect})."
292
+ elsif eol1 != eol2
293
+ raise "Document eol mismatch (#{eol1}, #{eol2})."
294
+ end
266
295
  end
267
- end
268
296
 
269
- doc1 = DocDiff::Document.new(file1_content, encoding1, eol1)
270
- doc2 = DocDiff::Document.new(file2_content, encoding2, eol2)
297
+ doc1 = DocDiff::Document.new(file1_content, encoding1, eol1)
298
+ doc2 = DocDiff::Document.new(file2_content, encoding2, eol2)
271
299
 
272
- output = docdiff.run(doc1, doc2,
273
- {:resolution => docdiff.config[:resolution],
274
- :format => docdiff.config[:format],
275
- :digest => docdiff.config[:digest],
276
- :display => docdiff.config[:display]})
300
+ output =
301
+ docdiff.run(
302
+ doc1,
303
+ doc2,
304
+ {
305
+ resolution: docdiff.config[:resolution],
306
+ format: docdiff.config[:format],
307
+ digest: docdiff.config[:digest],
308
+ display: docdiff.config[:display],
309
+ },
310
+ )
277
311
 
278
- print_or_write_to_pager(output, docdiff.config[:pager])
312
+ print_or_write_to_pager(output, docdiff.config[:pager])
313
+ end
279
314
  end
280
315
  end
281
316
  end
@@ -380,4 +380,4 @@ class Diff
380
380
  end
381
381
  end
382
382
  end
383
- end # class DocDiff
383
+ end
@@ -147,4 +147,4 @@ class Diff
147
147
  end
148
148
  end
149
149
  end
150
- end # class DocDiff
150
+ end
@@ -106,4 +106,4 @@ class Diff
106
106
  end
107
107
  end
108
108
  end
109
- end # class DocDiff
109
+ end
@@ -92,4 +92,4 @@ class Diff
92
92
  end
93
93
  end
94
94
  end
95
- end # class DocDiff
95
+ end
@@ -40,4 +40,4 @@ class Diff
40
40
  end
41
41
  end
42
42
  end
43
- end # class DocDiff
43
+ end
@@ -38,4 +38,4 @@ class Diff
38
38
  end
39
39
  end
40
40
  end
41
- end # class DocDiff
41
+ end
@@ -122,4 +122,4 @@ class Diff
122
122
  end
123
123
  end
124
124
  end
125
- end # class DocDiff
125
+ end
data/lib/docdiff/diff.rb CHANGED
@@ -216,4 +216,4 @@ class Diff
216
216
  end
217
217
  end
218
218
  end
219
- end # class DocDiff
219
+ end