deblank 0.0.1 → 0.1.0
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.
- checksums.yaml +7 -0
- data/README.md +4 -4
- data/Rakefile +1 -2
- data/bin/deblank +1 -1
- data/deblank.gemspec +6 -5
- data/lib/deblank.rb +91 -67
- data/man/deblank.1 +2 -2
- data/test/test_nameconverter.rb +16 -3
- data/test/test_optionparser.rb +1 -2
- metadata +11 -16
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 39b5293f35eba93c2a7b59401af1484d9959df73
|
4
|
+
data.tar.gz: d9e64611e4839b69a0fe1cfc5fa238156dbddfac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9c2f513577c3047515d67525590729e8f47d493592a6a5888e0b53b309cd7c04486df92bb7db510e9ac5222bba9709461e813329bf692a4af2abcc746712211a
|
7
|
+
data.tar.gz: a02f35e87252e960c005b035bfe7a5949ad790b55d6cec21d12a5896dabc86009c44bc98b825c6433e39d3e0d3bb1462ca2ba52fd841a8418d3c4ca3ea0b4925
|
data/README.md
CHANGED
@@ -37,7 +37,7 @@ Installation
|
|
37
37
|
|
38
38
|
You can either
|
39
39
|
|
40
|
-
- use `gem install deblank
|
40
|
+
- use `gem install deblank` to install from RubyGems.org,
|
41
41
|
|
42
42
|
- copy `lib/deblank.rb` under the name `deblank` into your search path,
|
43
43
|
|
@@ -51,8 +51,8 @@ Requirements
|
|
51
51
|
|
52
52
|
- No additional Ruby gems are needed to run `deblank`.
|
53
53
|
|
54
|
-
- `deblank` has been tested with Ruby 1.9.3
|
55
|
-
and on Windows (command prompt with code page 850).
|
54
|
+
- `deblank` has been tested with Ruby 1.9.3 and Ruby 2.0.0
|
55
|
+
on Linux and on Windows (command prompt with code page 850).
|
56
56
|
|
57
57
|
Documentation
|
58
58
|
-------------
|
@@ -77,7 +77,7 @@ Report bugs on the `deblank` home page: <https://github.com/stomar/deblank/>
|
|
77
77
|
License
|
78
78
|
-------
|
79
79
|
|
80
|
-
Copyright © 2012
|
80
|
+
Copyright © 2012-2013 Marcus Stollsteimer
|
81
81
|
|
82
82
|
`deblank` is free software: you can redistribute it and/or modify
|
83
83
|
it under the terms of the GNU General Public License version 3 or later (GPLv3+),
|
data/Rakefile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# rakefile for the deblank script.
|
2
2
|
#
|
3
|
-
# Copyright (C) 2012 Marcus Stollsteimer
|
3
|
+
# Copyright (C) 2012-2013 Marcus Stollsteimer
|
4
4
|
|
5
5
|
require 'rake/testtask'
|
6
6
|
|
@@ -30,7 +30,6 @@ end
|
|
30
30
|
task :default => [:test]
|
31
31
|
|
32
32
|
Rake::TestTask.new do |t|
|
33
|
-
t.libs << '.'
|
34
33
|
t.pattern = 'test/**/test_*.rb'
|
35
34
|
t.ruby_opts << '-rubygems'
|
36
35
|
t.verbose = true
|
data/bin/deblank
CHANGED
data/deblank.gemspec
CHANGED
@@ -9,7 +9,6 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.name = 'deblank'
|
10
10
|
s.version = version
|
11
11
|
s.date = date
|
12
|
-
s.rubyforge_project = 'deblank'
|
13
12
|
|
14
13
|
s.description = 'deblank is a command line tool that ' +
|
15
14
|
'renames files and replaces or removes special characters ' +
|
@@ -22,14 +21,16 @@ Gem::Specification.new do |s|
|
|
22
21
|
|
23
22
|
s.license = 'GPL-3'
|
24
23
|
|
25
|
-
|
24
|
+
s.required_ruby_version = '>=1.9.2'
|
25
|
+
|
26
|
+
s.add_development_dependency('rake')
|
26
27
|
|
27
28
|
s.executables = ['deblank']
|
28
29
|
s.bindir = 'bin'
|
30
|
+
|
29
31
|
s.require_path = 'lib'
|
30
|
-
s.test_files = Dir.glob('test/**/test_*.rb')
|
31
32
|
|
32
|
-
s.
|
33
|
+
s.test_files = Dir.glob('test/**/test_*.rb')
|
33
34
|
|
34
35
|
s.files = %w{
|
35
36
|
README.md
|
@@ -39,5 +40,5 @@ Gem::Specification.new do |s|
|
|
39
40
|
} +
|
40
41
|
Dir.glob('{bin,lib,man,test}/**/*')
|
41
42
|
|
42
|
-
s.
|
43
|
+
s.rdoc_options = ['--charset=UTF-8']
|
43
44
|
end
|
data/lib/deblank.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#!/usr/bin/ruby
|
1
|
+
#!/usr/bin/env ruby
|
2
2
|
# encoding: UTF-8
|
3
3
|
# == Name
|
4
4
|
#
|
@@ -15,7 +15,7 @@
|
|
15
15
|
#
|
16
16
|
# == Author
|
17
17
|
#
|
18
|
-
# Copyright (C) 2012 Marcus Stollsteimer
|
18
|
+
# Copyright (C) 2012-2013 Marcus Stollsteimer
|
19
19
|
#
|
20
20
|
# License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
|
21
21
|
|
@@ -25,15 +25,17 @@ require 'optparse'
|
|
25
25
|
module Deblank
|
26
26
|
|
27
27
|
PROGNAME = 'deblank'
|
28
|
-
VERSION = '0.0
|
29
|
-
DATE = '
|
28
|
+
VERSION = '0.1.0'
|
29
|
+
DATE = '2013-10-27'
|
30
30
|
HOMEPAGE = 'https://github.com/stomar/deblank'
|
31
31
|
TAGLINE = 'remove special characters from filenames'
|
32
32
|
|
33
|
-
COPYRIGHT =
|
34
|
-
|
35
|
-
|
36
|
-
|
33
|
+
COPYRIGHT = <<-copyright.gsub(/^ +/, '')
|
34
|
+
Copyright (C) 2012-2013 Marcus Stollsteimer.
|
35
|
+
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
|
36
|
+
This is free software: you are free to change and redistribute it.
|
37
|
+
There is NO WARRANTY, to the extent permitted by law.
|
38
|
+
copyright
|
37
39
|
|
38
40
|
# Parser for the command line options.
|
39
41
|
# The class method parse! does the job.
|
@@ -50,23 +52,23 @@ module Deblank
|
|
50
52
|
|
51
53
|
options = {
|
52
54
|
:files => nil,
|
53
|
-
:simulate => false
|
55
|
+
:simulate => false
|
54
56
|
}
|
55
57
|
|
56
58
|
opt_parser = OptionParser.new do |opt|
|
57
59
|
opt.banner = "Usage: #{PROGNAME} [options] file[s]"
|
58
|
-
opt.separator
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
60
|
+
opt.separator %Q{
|
61
|
+
deblank renames files and replaces or removes special characters
|
62
|
+
like spaces, parentheses, or umlauts.
|
63
|
+
The new filename will only contain the following characters:
|
64
|
+
|
65
|
+
____#{NameConverter.default_valid_chars_to_s}
|
66
|
+
|
67
|
+
Spaces are replaced by underscores, German umlauts and eszett are
|
68
|
+
transliterated, all other invalid characters are removed.
|
69
|
+
|
70
|
+
Options
|
71
|
+
}.gsub(/^ +/, '').gsub(/^____/, ' ')
|
70
72
|
|
71
73
|
# process --version and --help first,
|
72
74
|
# exit successfully (GNU Coding Standards)
|
@@ -85,7 +87,7 @@ module Deblank
|
|
85
87
|
|
86
88
|
opt.on('-l', '--list',
|
87
89
|
'List the used character substitutions.') do
|
88
|
-
puts NameConverter.
|
90
|
+
puts NameConverter.default_substitutions_to_s
|
89
91
|
exit
|
90
92
|
end
|
91
93
|
|
@@ -124,11 +126,11 @@ module Deblank
|
|
124
126
|
end
|
125
127
|
end
|
126
128
|
|
127
|
-
# This
|
129
|
+
# This class provides a converter method for filenames
|
128
130
|
# (only the base name is modified).
|
129
|
-
|
131
|
+
class NameConverter
|
130
132
|
|
131
|
-
VALID_CHARS = 'A-
|
133
|
+
VALID_CHARS = 'A-Za-z0-9._-' # `-' must be last
|
132
134
|
|
133
135
|
SUBSTITUTIONS = {
|
134
136
|
' ' => '_',
|
@@ -141,26 +143,36 @@ module Deblank
|
|
141
143
|
'ß' => 'ss'
|
142
144
|
}
|
143
145
|
|
144
|
-
def
|
145
|
-
|
146
|
-
|
147
|
-
|
146
|
+
def initialize
|
147
|
+
@valid_characters = VALID_CHARS
|
148
|
+
@substitutions = SUBSTITUTIONS
|
149
|
+
end
|
148
150
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
basename.gsub!(
|
151
|
+
def convert(filename)
|
152
|
+
dir, basename = File.dirname(filename), File.basename(filename)
|
153
|
+
|
154
|
+
@substitutions.each {|from, to| basename.gsub!(/#{from}/, to) }
|
155
|
+
basename.gsub!(invalid_characters, '')
|
153
156
|
|
154
157
|
dir == '.' ? basename : "#{dir}/#{basename}"
|
155
158
|
end
|
156
159
|
|
157
|
-
def
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
160
|
+
def invalid?(filename)
|
161
|
+
invalid_characters === filename
|
162
|
+
end
|
163
|
+
|
164
|
+
def self.default_valid_chars_to_s
|
165
|
+
VALID_CHARS.scan(/.-.|./).join(' ')
|
166
|
+
end
|
167
|
+
|
168
|
+
def self.default_substitutions_to_s
|
169
|
+
SUBSTITUTIONS.map {|from, to| "#{from} => #{to}\n" }.join
|
170
|
+
end
|
171
|
+
|
172
|
+
private
|
162
173
|
|
163
|
-
|
174
|
+
def invalid_characters
|
175
|
+
/[^#{@valid_characters}]/
|
164
176
|
end
|
165
177
|
end
|
166
178
|
|
@@ -174,11 +186,12 @@ module Deblank
|
|
174
186
|
def initialize
|
175
187
|
begin
|
176
188
|
options = Optionparser.parse!(ARGV)
|
177
|
-
@files = options[:files]
|
178
|
-
@simulate = options[:simulate]
|
179
189
|
rescue => e
|
180
190
|
usage_fail(e.message)
|
181
191
|
end
|
192
|
+
@files = options[:files]
|
193
|
+
@simulate = options[:simulate]
|
194
|
+
@converter = NameConverter.new
|
182
195
|
end
|
183
196
|
|
184
197
|
# The main program.
|
@@ -187,28 +200,41 @@ module Deblank
|
|
187
200
|
warn "#{message}\n#{'-' * message.size}\n" if @simulate
|
188
201
|
|
189
202
|
@files.each do |filename|
|
190
|
-
unless
|
191
|
-
|
192
|
-
next
|
193
|
-
end
|
203
|
+
next unless file_exist?(filename)
|
204
|
+
next unless invalid?(filename)
|
194
205
|
|
195
|
-
new_filename =
|
206
|
+
new_filename = @converter.convert(filename)
|
207
|
+
secure_rename(filename, new_filename)
|
208
|
+
end
|
209
|
+
end
|
196
210
|
|
197
|
-
|
198
|
-
if new_filename == filename
|
199
|
-
warn("`#{filename}' and `#{new_filename}' are the same file. (Skipped.)")
|
200
|
-
next
|
201
|
-
end
|
211
|
+
private
|
202
212
|
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
213
|
+
def skip_warn(message)
|
214
|
+
warn "#{message} (Skipped.)"
|
215
|
+
end
|
216
|
+
|
217
|
+
def file_exist?(filename)
|
218
|
+
fail_message = "There is no file `#{filename}'."
|
219
|
+
|
220
|
+
File.exist?(filename) or skip_warn(fail_message)
|
221
|
+
end
|
208
222
|
|
209
|
-
|
210
|
-
|
211
|
-
|
223
|
+
def invalid?(filename)
|
224
|
+
fail_message = "`#{filename}' already is a valid filename."
|
225
|
+
|
226
|
+
@converter.invalid?(filename) or skip_warn(fail_message)
|
227
|
+
end
|
228
|
+
|
229
|
+
def secure_rename(old_filename, new_filename)
|
230
|
+
return if File.exist?(new_filename) && !overwrite?(new_filename)
|
231
|
+
|
232
|
+
warn "Moving from `#{old_filename}' to `#{new_filename}'."
|
233
|
+
File.rename(old_filename, new_filename) unless @simulate
|
234
|
+
end
|
235
|
+
|
236
|
+
def overwrite?(filename)
|
237
|
+
confirm("File `#{filename}' already exists. Overwrite?")
|
212
238
|
end
|
213
239
|
|
214
240
|
# Asks for yes or no (y/n).
|
@@ -216,28 +242,26 @@ module Deblank
|
|
216
242
|
# +question+ - string to be printed
|
217
243
|
#
|
218
244
|
# Returns +true+ if the answer is yes.
|
219
|
-
def
|
245
|
+
def confirm(question)
|
220
246
|
loop do
|
221
247
|
$stderr.print "#{question} [y/n] "
|
222
|
-
reply = $stdin.gets.chomp.downcase # $stdin
|
223
|
-
return
|
224
|
-
return false if reply == 'n'
|
248
|
+
reply = $stdin.gets.chomp.downcase # $stdin avoids gets/ARGV problem
|
249
|
+
return reply == 'y' if /\A[yn]\Z/ =~ reply
|
225
250
|
warn "Please answer `y' or `n'."
|
226
251
|
end
|
227
252
|
end
|
228
253
|
|
229
254
|
# Prints an error message and a short help information, then exits.
|
230
|
-
def usage_fail(message)
|
255
|
+
def usage_fail(message)
|
231
256
|
warn "#{PROGNAME}: #{message}"
|
232
257
|
warn "Use `#{PROGNAME} --help' for valid options."
|
233
258
|
exit ERRORCODE[:usage]
|
234
259
|
end
|
235
260
|
end
|
261
|
+
end # module
|
236
262
|
|
237
263
|
### call main method only if called on command line
|
238
264
|
|
239
265
|
if __FILE__ == $0
|
240
|
-
Application.new.run!
|
266
|
+
Deblank::Application.new.run!
|
241
267
|
end
|
242
|
-
|
243
|
-
end # module
|
data/man/deblank.1
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
.\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.40.4.
|
2
|
-
.TH DEBLANK "1" "
|
2
|
+
.TH DEBLANK "1" "October 2013" "deblank 0.1.0" "User Commands"
|
3
3
|
.SH NAME
|
4
4
|
deblank \- remove special characters from filenames
|
5
5
|
.SH SYNOPSIS
|
@@ -30,7 +30,7 @@ Print a brief version information and exit.
|
|
30
30
|
.SH "REPORTING BUGS"
|
31
31
|
Report bugs on the deblank home page: <https://github.com/stomar/deblank>
|
32
32
|
.SH COPYRIGHT
|
33
|
-
Copyright \(co 2012 Marcus Stollsteimer.
|
33
|
+
Copyright \(co 2012\-2013 Marcus Stollsteimer.
|
34
34
|
.br
|
35
35
|
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
|
36
36
|
.br
|
data/test/test_nameconverter.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
|
-
#!/usr/bin/ruby -w
|
2
1
|
# encoding: UTF-8
|
3
2
|
#
|
4
3
|
# test_nameconverter.rb: Unit tests for the deblank script.
|
5
4
|
#
|
6
|
-
# Copyright (C) 2012 Marcus Stollsteimer
|
5
|
+
# Copyright (C) 2012-2013 Marcus Stollsteimer
|
7
6
|
|
8
7
|
require 'minitest/spec'
|
9
8
|
require 'minitest/autorun'
|
@@ -13,7 +12,13 @@ require 'deblank'
|
|
13
12
|
describe Deblank::NameConverter do
|
14
13
|
|
15
14
|
before do
|
16
|
-
@nc = Deblank::NameConverter
|
15
|
+
@nc = Deblank::NameConverter.new
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'recognizes invalid filenames' do
|
19
|
+
@nc.invalid?('path/to/filename with spaces.txt').must_equal true
|
20
|
+
@nc.invalid?('filename_with_ä.txt').must_equal true
|
21
|
+
@nc.invalid?('Valid_filename-1.txt').must_equal false
|
17
22
|
end
|
18
23
|
|
19
24
|
it 'does not change the path name' do
|
@@ -31,4 +36,12 @@ describe Deblank::NameConverter do
|
|
31
36
|
it 'transliterates umlauts and eszett' do
|
32
37
|
@nc.convert('Ä_Ö_Ü_ä_ö_ü_ß.txt').must_equal 'Ae_Oe_Ue_ae_oe_ue_ss.txt'
|
33
38
|
end
|
39
|
+
|
40
|
+
it 'can return the default valid characters as string' do
|
41
|
+
Deblank::NameConverter.default_valid_chars_to_s.must_equal 'A-Z a-z 0-9 . _ -'
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'can return the default substitutions as string' do
|
45
|
+
Deblank::NameConverter.default_substitutions_to_s.split("\n")[0].must_equal ' => _'
|
46
|
+
end
|
34
47
|
end
|
data/test/test_optionparser.rb
CHANGED
metadata
CHANGED
@@ -1,30 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deblank
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Marcus Stollsteimer
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-10-27 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rake
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
description: deblank is a command line tool that renames files and replaces or removes
|
@@ -47,30 +44,28 @@ files:
|
|
47
44
|
homepage: https://github.com/stomar/deblank
|
48
45
|
licenses:
|
49
46
|
- GPL-3
|
47
|
+
metadata: {}
|
50
48
|
post_install_message:
|
51
49
|
rdoc_options:
|
52
50
|
- --charset=UTF-8
|
53
51
|
require_paths:
|
54
52
|
- lib
|
55
53
|
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
-
none: false
|
57
54
|
requirements:
|
58
|
-
- -
|
55
|
+
- - '>='
|
59
56
|
- !ruby/object:Gem::Version
|
60
|
-
version:
|
57
|
+
version: 1.9.2
|
61
58
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
-
none: false
|
63
59
|
requirements:
|
64
|
-
- -
|
60
|
+
- - '>='
|
65
61
|
- !ruby/object:Gem::Version
|
66
62
|
version: '0'
|
67
63
|
requirements: []
|
68
|
-
rubyforge_project:
|
69
|
-
rubygems_version: 1.
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 2.1.9
|
70
66
|
signing_key:
|
71
|
-
specification_version:
|
67
|
+
specification_version: 4
|
72
68
|
summary: deblank - remove special characters from filenames
|
73
69
|
test_files:
|
74
70
|
- test/test_nameconverter.rb
|
75
71
|
- test/test_optionparser.rb
|
76
|
-
has_rdoc:
|