docdiff 0.6.5 → 0.6.7

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 +15 -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 +291 -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,322 @@
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
+ in NilClass
162
+ nil
163
+ else
164
+ "#{exception}: something unexpected happened: #{filename.inspect}"
165
+ end
166
+ if content
167
+ config = parse_config_file_content(content)
132
168
  else
133
- "something unexpected happened: #{filename.inspect}"
169
+ message = "config file empty: #{filename.inspect}"
134
170
  end
135
- if content
136
- config = parse_config_file_content(content)
137
- else
138
- message = "config file empty: #{filename.inspect}"
139
171
  end
172
+ [config, message]
140
173
  end
141
- [config, message]
142
- end
143
174
 
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
175
+ def print_or_write_to_pager(content, pager)
176
+ if $stdout.tty? && pager.is_a?(String) && !pager.empty?
177
+ IO.popen(pager, "w") { |f| f.print(content) }
178
+ else
179
+ print(content)
180
+ end
149
181
  end
150
- end
151
182
 
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
183
+ def run
184
+ command_line_config = parse_options!(ARGV)
185
+
186
+ system_config =
187
+ unless command_line_config[:no_config_file]
188
+ possible_system_config_file_names = [
189
+ DocDiff::SystemConfigFileName,
190
+ ]
191
+ existing_system_config_file_names =
192
+ possible_system_config_file_names.select { |fn| File.exist?(fn) }
193
+ if existing_system_config_file_names.size >= 2
194
+ raise <<~EOS
195
+ More than one system config file found, using the first one: \
196
+ #{existing_system_config_file_names.inspect}
197
+ EOS
198
+ end
199
+
200
+ unless existing_system_config_file_names.empty?
201
+ filename = existing_system_config_file_names.first
202
+ config, message = read_config_from_file(filename)
203
+ $stderr.print(message) if command_line_config[:verbose]
204
+ config
205
+ end
167
206
  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
207
 
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
208
+ user_config =
209
+ unless command_line_config[:no_config_file]
210
+ possible_user_config_file_names = [
211
+ DocDiff::UserConfigFileName,
212
+ DocDiff::AltUserConfigFileName,
213
+ DocDiff::XDGUserConfigFileName,
214
+ ]
215
+ existing_user_config_file_names =
216
+ possible_user_config_file_names.select { |fn| File.exist?(fn) }
217
+ if existing_user_config_file_names.size >= 2
218
+ raise <<~EOS
219
+ Only one user config file can be used at the same time. \
220
+ Keep one and remove or rename the others: \
221
+ #{existing_user_config_file_names.inspect}
222
+ EOS
223
+ end
224
+
225
+ unless existing_user_config_file_names.empty?
226
+ filename = existing_user_config_file_names.first
227
+ config, message = read_config_from_file(filename)
228
+ $stderr.print(message) if command_line_config[:verbose]
229
+ config
230
+ end
189
231
  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
232
 
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
233
+ config_from_specified_file =
234
+ if (filename = command_line_config[:config_file])
235
+ config, message = read_config_from_file(filename)
236
+ $stderr.print(message) if command_line_config[:verbose] == true
237
+ config
238
+ end
239
+
240
+ config_from_program_name =
241
+ case File.basename($PROGRAM_NAME, ".*")
242
+ when "worddiff" then { resolution: "word" }
243
+ when "chardiff" then { resolution: "char" }
244
+ end
202
245
 
203
- config_from_program_name =
204
- case File.basename($PROGRAM_NAME, ".*")
205
- when "worddiff" then {:resolution => "word"}
206
- when "chardiff" then {:resolution => "char"}
246
+ config_from_env_vars = {}
247
+ if (pager = ENV["DOCDIFF_PAGER"]) && !pager.empty?
248
+ config_from_env_vars[:pager] = pager
207
249
  end
208
250
 
209
- config_from_env_vars = {}
210
- if (pager = ENV['DOCDIFF_PAGER']) && !pager.empty?
211
- config_from_env_vars[:pager] = pager
212
- end
251
+ config_in_effect = DocDiff::DEFAULT_CONFIG.dup
252
+ config_in_effect.merge!(config_from_program_name) if config_from_program_name
253
+ config_in_effect.merge!(system_config) if system_config
254
+ config_in_effect.merge!(user_config) if user_config
255
+ config_in_effect.merge!(config_from_env_vars) if config_from_env_vars
256
+ config_in_effect.merge!(config_from_specified_file) if config_from_specified_file
257
+ config_in_effect.merge!(command_line_config) if command_line_config
258
+
259
+ docdiff = DocDiff.new(config: config_in_effect)
260
+
261
+ file1_content = nil
262
+ file2_content = nil
263
+ raise "Try `#{File.basename($PROGRAM_NAME)} --help' for more information." if ARGV[0].nil?
264
+ raise "Specify at least 2 target files." unless ARGV[0] && ARGV[1]
265
+
266
+ ARGV[0] = "/dev/stdin" if ARGV[0] == "-"
267
+ ARGV[1] = "/dev/stdin" if ARGV[1] == "-"
268
+ raise "No such file: #{ARGV[0]}." unless FileTest.exist?(ARGV[0])
269
+ raise "No such file: #{ARGV[1]}." unless FileTest.exist?(ARGV[1])
270
+ raise "#{ARGV[0]} is not readable." unless FileTest.readable?(ARGV[0])
271
+ raise "#{ARGV[1]} is not readable." unless FileTest.readable?(ARGV[1])
272
+
273
+ File.open(ARGV[0], "r") { |f| file1_content = f.read }
274
+ File.open(ARGV[1], "r") { |f| file2_content = f.read }
213
275
 
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})."
276
+ encoding1 = docdiff.config[:encoding]
277
+ encoding2 = docdiff.config[:encoding]
278
+ eol1 = docdiff.config[:eol]
279
+ eol2 = docdiff.config[:eol]
280
+
281
+ if docdiff.config[:encoding] == "auto"
282
+ encoding1 = DocDiff::CharString.guess_encoding(file1_content)
283
+ encoding2 = DocDiff::CharString.guess_encoding(file2_content)
284
+ if (encoding1 == "UNKNOWN") || (encoding2 == "UNKNOWN")
285
+ raise "Document encoding unknown (#{encoding1}, #{encoding2})."
286
+ elsif encoding1 != encoding2
287
+ raise "Document encoding mismatch (#{encoding1}, #{encoding2})."
288
+ end
253
289
  end
254
- end
255
290
 
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})."
291
+ if docdiff.config[:eol] == "auto"
292
+ eol1 = DocDiff::CharString.guess_eol(file1_content)
293
+ eol2 = DocDiff::CharString.guess_eol(file2_content)
294
+ if eol1.nil? || eol2.nil?
295
+ raise "Document eol is nil (#{eol1.inspect}, #{eol2.inspect}). The document might be empty."
296
+ elsif (eol1 == "UNKNOWN") || (eol2 == "UNKNOWN")
297
+ raise "Document eol unknown (#{eol1.inspect}, #{eol2.inspect})."
298
+ elsif eol1 != eol2
299
+ raise "Document eol mismatch (#{eol1}, #{eol2})."
300
+ end
266
301
  end
267
- end
268
302
 
269
- doc1 = DocDiff::Document.new(file1_content, encoding1, eol1)
270
- doc2 = DocDiff::Document.new(file2_content, encoding2, eol2)
303
+ doc1 = DocDiff::Document.new(file1_content, encoding1, eol1)
304
+ doc2 = DocDiff::Document.new(file2_content, encoding2, eol2)
271
305
 
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]})
306
+ output =
307
+ docdiff.run(
308
+ doc1,
309
+ doc2,
310
+ {
311
+ resolution: docdiff.config[:resolution],
312
+ format: docdiff.config[:format],
313
+ digest: docdiff.config[:digest],
314
+ display: docdiff.config[:display],
315
+ },
316
+ )
277
317
 
278
- print_or_write_to_pager(output, docdiff.config[:pager])
318
+ print_or_write_to_pager(output, docdiff.config[:pager])
319
+ end
279
320
  end
280
321
  end
281
322
  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