filename_cleaner 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/lib/filename_cleaner/cli.rb +1 -1
- data/lib/filename_cleaner/filename_cleaner.rb +48 -0
- data/lib/filename_cleaner/version.rb +1 -1
- data/lib/filename_cleaner.rb +1 -1
- data/test/lib/filename_cleaner/test_filename_cleaner.rb +46 -0
- data/test/test_helper.rb +0 -1
- metadata +5 -5
- data/lib/filename_cleaner/utils.rb +0 -50
- data/test/lib/filename_cleaner/test_utils.rb +0 -43
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0bd5137e77cc80734366d70ab66ff7b4bff25c74
|
4
|
+
data.tar.gz: b31ecd51ae531972fd2a9421513a8ef02475b40f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b76d5199034e669a1b257018f7b17ac0340c7e129bcc85bd360a894749fcb334eb55575a8d2d4ab45f62b820ec2cf152c3e6ac82a047cef9f06dcf574e5b498
|
7
|
+
data.tar.gz: 506c76b7f13c56d937f8bed94d1ee43d26577beda2ca733d4b3b1e20e40dae817d3b2bad2cc4560f5551183f322a213e506eaca4b4c86fb3157b330eb7115853
|
data/README.md
CHANGED
@@ -95,10 +95,14 @@ puts clean_name # => 'some_b_d_fil_name.txt'
|
|
95
95
|
|
96
96
|
### Changelogs
|
97
97
|
|
98
|
+
#### 0.0.3
|
99
|
+
|
100
|
+
- Remove the namespace and code refactoring
|
101
|
+
|
98
102
|
#### 0.0.2
|
99
103
|
|
100
104
|
- Update gem dependencies to the latest version
|
101
|
-
- Update gemspec to reflect
|
105
|
+
- Update gemspec to reflect the actual feature of the gem
|
102
106
|
- Update and cleanup README.md
|
103
107
|
|
104
108
|
#### 0.0.1
|
data/lib/filename_cleaner/cli.rb
CHANGED
@@ -71,7 +71,7 @@ Options:
|
|
71
71
|
files.each do |file|
|
72
72
|
dirname = File.dirname(File.expand_path(file))
|
73
73
|
filename = File.basename(file)
|
74
|
-
sanitized_name = FilenameCleaner::
|
74
|
+
sanitized_name = FilenameCleaner::sanitize_filename(filename, options[:sep_char])
|
75
75
|
old_name = File.expand_path(file)
|
76
76
|
new_name = File.expand_path([dirname, sanitized_name].join(File::SEPARATOR))
|
77
77
|
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module FilenameCleaner
|
2
|
+
DOT = '.'
|
3
|
+
class << self
|
4
|
+
# Sanitize filename that have the extension
|
5
|
+
#
|
6
|
+
# @param [String] filename the input filename with extension
|
7
|
+
# @retyrn [String] the output file with special characters replaced.
|
8
|
+
def sanitize_filename(filename, sep_char = nil)
|
9
|
+
extension = File.extname(filename)
|
10
|
+
|
11
|
+
if extension.empty?
|
12
|
+
replace_dot!(sanitize(filename), sep_char)
|
13
|
+
else
|
14
|
+
name_only = File.basename(filename, ".*")
|
15
|
+
name_only = replace_dot!(sanitize(name_only), sep_char)
|
16
|
+
"#{name_only}#{extension}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Clean the the input string to remove the special characters
|
21
|
+
#
|
22
|
+
# @param [String] filename input file
|
23
|
+
# @return [String] the new file name with special characters replaced or removed.
|
24
|
+
def sanitize(filename)
|
25
|
+
# remove anything that is not letters, numbers, dash, underscore or space
|
26
|
+
# Note: we intentionally ignore dot from the list
|
27
|
+
filename.gsub!(/[^0-9A-Za-z\-_ ]/, DOT)
|
28
|
+
|
29
|
+
# replace multiple occurrences of a given char with a dot
|
30
|
+
['-', '_', ' '].each do |c|
|
31
|
+
filename.gsub!(/#{Regexp.quote(c)}+/, DOT)
|
32
|
+
end
|
33
|
+
|
34
|
+
# replace multiple occurrence of dot with one dot
|
35
|
+
filename.gsub!(/#{Regexp.quote(DOT)}+/, DOT)
|
36
|
+
|
37
|
+
filename
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# replace 'dot' string with a agiven string if any
|
43
|
+
def replace_dot!(string, replace = nil)
|
44
|
+
string.gsub!(/#{Regexp.quote(DOT)}+/, replace) if replace
|
45
|
+
string
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/filename_cleaner.rb
CHANGED
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative '../../test_helper'
|
2
|
+
describe FilenameCleaner do
|
3
|
+
context '#sanitize' do
|
4
|
+
it 'works with simple input' do
|
5
|
+
FilenameCleaner.sanitize('any txt').must_equal 'any.txt'
|
6
|
+
FilenameCleaner.sanitize('this is a long filename.txt').must_equal 'this.is.a.long.filename.txt'
|
7
|
+
end
|
8
|
+
it 'works with text with extension' do
|
9
|
+
FilenameCleaner.sanitize('filename.txt').must_equal 'filename.txt'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
context '#sanitize_filename' do
|
13
|
+
describe 'file with extension' do
|
14
|
+
it 'replaces mutilple consecutive chars with one' do
|
15
|
+
FilenameCleaner.sanitize_filename('some!!!$file$:%.txt').must_equal 'some.file..txt'
|
16
|
+
end
|
17
|
+
it 'works with default separator' do
|
18
|
+
FilenameCleaner.sanitize_filename('some file.txt').must_equal 'some.file.txt'
|
19
|
+
end
|
20
|
+
it 'works with non-default separator' do
|
21
|
+
FilenameCleaner.sanitize_filename('some file.txt', '_').must_equal 'some_file.txt'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
describe 'file without extension' do
|
25
|
+
it 'replaces mutilple consecutive chars with one' do
|
26
|
+
FilenameCleaner.sanitize_filename('some!!!$file$:%.').must_equal 'some.file.'
|
27
|
+
end
|
28
|
+
context 'using default separator' do
|
29
|
+
it 'works with simple input' do
|
30
|
+
FilenameCleaner.sanitize_filename('Gemfile').must_equal 'Gemfile'
|
31
|
+
end
|
32
|
+
it 'works with complex input' do
|
33
|
+
FilenameCleaner.sanitize_filename('File$without!extension').must_equal 'File.without.extension'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
context 'with non-default separator char' do
|
37
|
+
it 'works with simple input' do
|
38
|
+
FilenameCleaner.sanitize_filename('Gemfile', '_').must_equal 'Gemfile'
|
39
|
+
end
|
40
|
+
it 'works with complex input' do
|
41
|
+
FilenameCleaner.sanitize_filename('File$without!extension', '-').must_equal 'File-without-extension'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filename_cleaner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Burin Choomnuan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -214,10 +214,10 @@ files:
|
|
214
214
|
- filename_cleaner.gemspec
|
215
215
|
- lib/filename_cleaner.rb
|
216
216
|
- lib/filename_cleaner/cli.rb
|
217
|
-
- lib/filename_cleaner/
|
217
|
+
- lib/filename_cleaner/filename_cleaner.rb
|
218
218
|
- lib/filename_cleaner/version.rb
|
219
219
|
- rubocop-todo.yml
|
220
|
-
- test/lib/filename_cleaner/
|
220
|
+
- test/lib/filename_cleaner/test_filename_cleaner.rb
|
221
221
|
- test/test_helper.rb
|
222
222
|
homepage: https://github.com/agilecreativity/filename_cleaner
|
223
223
|
licenses:
|
@@ -245,6 +245,6 @@ specification_version: 4
|
|
245
245
|
summary: Bulk rename/remove unwanted characters from list of files in any directory
|
246
246
|
recursively
|
247
247
|
test_files:
|
248
|
-
- test/lib/filename_cleaner/
|
248
|
+
- test/lib/filename_cleaner/test_filename_cleaner.rb
|
249
249
|
- test/test_helper.rb
|
250
250
|
has_rdoc:
|
@@ -1,50 +0,0 @@
|
|
1
|
-
module FilenameCleaner
|
2
|
-
class Utils
|
3
|
-
DOT = '.'
|
4
|
-
class << self
|
5
|
-
# Sanitize filename that have the extension
|
6
|
-
#
|
7
|
-
# @param [String] filename the input filename with extension
|
8
|
-
# @retyrn [String] the output file with special characters replaced.
|
9
|
-
def sanitize_filename(filename, sep_char = nil)
|
10
|
-
extension = File.extname(filename)
|
11
|
-
|
12
|
-
if extension.empty?
|
13
|
-
replace_dot!(sanitize(filename), sep_char)
|
14
|
-
else
|
15
|
-
name_only = File.basename(filename, ".*")
|
16
|
-
name_only = replace_dot!(sanitize(name_only), sep_char)
|
17
|
-
"#{name_only}#{extension}"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
# Clean the the input string to remove the special characters
|
22
|
-
#
|
23
|
-
# @param [String] filename input file
|
24
|
-
# @return [String] the new file name with special characters replaced or removed.
|
25
|
-
def sanitize(filename)
|
26
|
-
# remove anything that is not letters, numbers, dash, underscore or space
|
27
|
-
# Note: we intentionally ignore dot from the list
|
28
|
-
filename.gsub!(/[^0-9A-Za-z\-_ ]/, DOT)
|
29
|
-
|
30
|
-
# replace multiple occurrences of a given char with a dot
|
31
|
-
['-', '_', ' '].each do |c|
|
32
|
-
filename.gsub!(/#{Regexp.quote(c)}+/, DOT)
|
33
|
-
end
|
34
|
-
|
35
|
-
# replace multiple occurrence of dot with one dot
|
36
|
-
filename.gsub!(/#{Regexp.quote(DOT)}+/, DOT)
|
37
|
-
|
38
|
-
filename
|
39
|
-
end
|
40
|
-
|
41
|
-
private
|
42
|
-
|
43
|
-
# replace 'dot' string with a agiven string if any
|
44
|
-
def replace_dot!(string, replace = nil)
|
45
|
-
string.gsub!(/#{Regexp.quote(DOT)}+/, replace) if replace
|
46
|
-
string
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
require_relative '../../test_helper'
|
2
|
-
describe FilenameCleaner::Utils do
|
3
|
-
context '#sanitize' do
|
4
|
-
it 'works with simple input' do
|
5
|
-
FilenameCleaner::Utils.sanitize('any txt').must_equal 'any.txt'
|
6
|
-
FilenameCleaner::Utils.sanitize('this is a long filename.txt').must_equal 'this.is.a.long.filename.txt'
|
7
|
-
end
|
8
|
-
|
9
|
-
it 'works with text with extension' do
|
10
|
-
FilenameCleaner::Utils.sanitize('filename.txt').must_equal 'filename.txt'
|
11
|
-
end
|
12
|
-
end
|
13
|
-
context '#sanitize_filename' do
|
14
|
-
describe 'file with extension' do
|
15
|
-
it 'works with default separator' do
|
16
|
-
FilenameCleaner::Utils.sanitize_filename('some file.txt').must_equal 'some.file.txt'
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'works with non-default separator' do
|
20
|
-
FilenameCleaner::Utils.sanitize_filename('some file.txt', '_').must_equal 'some_file.txt'
|
21
|
-
end
|
22
|
-
end
|
23
|
-
describe 'file without extension' do
|
24
|
-
context 'using default separator' do
|
25
|
-
it 'works with simple input' do
|
26
|
-
FilenameCleaner::Utils.sanitize_filename('Gemfile').must_equal 'Gemfile'
|
27
|
-
end
|
28
|
-
it 'works with complex input' do
|
29
|
-
FilenameCleaner::Utils.sanitize_filename('File$without!extension').must_equal 'File.without.extension'
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
context 'with non-default separator char' do
|
34
|
-
it 'works with simple input' do
|
35
|
-
FilenameCleaner::Utils.sanitize_filename('Gemfile', '_').must_equal 'Gemfile'
|
36
|
-
end
|
37
|
-
it 'works with complex input' do
|
38
|
-
FilenameCleaner::Utils.sanitize_filename('File$without!extension', '-').must_equal 'File-without-extension'
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|