zaru 1.0.0 → 1.1
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 +4 -4
- data/lib/zaru.rb +35 -24
- metadata +5 -50
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 563b71271ed8169cd299499ef243e937f5fd3a04055377db87c0d821973a839e
|
|
4
|
+
data.tar.gz: a8f51824ca7a18598770f9a2a63c254565fe47ed4f616f87bcdd8c9e6d5f0201
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f4b51ce4dc951a7ecbf2867b2554505ca0837392cee8e1f8b0bc26fcfebdffb6d787006c9764bd2b1d1e8ca9dd71a8ce9114459ba2ea783b6e0ecd0e16c0aeac
|
|
7
|
+
data.tar.gz: 9470144c2ca5b9a5eb79b030bad989ee0fa3d638a1572e38a1e69799a1da1dac1e8daaa65b2fe97aebadb126e50c528dfcc7890d243afd0a73e146c441f4f24e
|
data/lib/zaru.rb
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# Zaru normalizes, sanitizes, and truncates filenames so they can be
|
|
4
|
+
# safely used across Linux, macOS, and Windows.
|
|
3
5
|
class Zaru
|
|
4
|
-
CHARACTER_FILTER =
|
|
5
|
-
UNICODE_WHITESPACE = /[[:space:]]+/u
|
|
6
|
+
CHARACTER_FILTER = %r{[\x00-\x1F/\\:*?"<>|]}u.freeze
|
|
7
|
+
UNICODE_WHITESPACE = /[[:space:]]+/u.freeze
|
|
6
8
|
WINDOWS_RESERVED_NAMES =
|
|
7
|
-
%w
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
%w[CON PRN AUX NUL COM1 COM2 COM3 COM4 COM5 COM6 COM7 COM8 COM9
|
|
10
|
+
COM COM² COM³ LPT1 LPT2 LPT3 LPT4 LPT5 LPT6 LPT7 LPT8 LPT9 LPT¹
|
|
11
|
+
LPT² LPT³].freeze
|
|
10
12
|
FALLBACK_FILENAME = 'file'
|
|
11
13
|
|
|
12
14
|
def initialize(filename, options = {})
|
|
@@ -18,7 +20,7 @@ class Zaru
|
|
|
18
20
|
# strip whitespace on beginning and end
|
|
19
21
|
# collapse intra-string whitespace into single spaces
|
|
20
22
|
def normalize
|
|
21
|
-
@
|
|
23
|
+
@normalize ||= @raw.strip.gsub(UNICODE_WHITESPACE, ' ')
|
|
22
24
|
end
|
|
23
25
|
|
|
24
26
|
# remove bad things!
|
|
@@ -29,15 +31,16 @@ class Zaru
|
|
|
29
31
|
#
|
|
30
32
|
# this renormalizes after filtering in order to collapse whitespace
|
|
31
33
|
def sanitize
|
|
32
|
-
@
|
|
33
|
-
filter(normalize.gsub(CHARACTER_FILTER,'')).gsub(UNICODE_WHITESPACE,' ')
|
|
34
|
+
@sanitize ||=
|
|
35
|
+
filter(normalize.gsub(CHARACTER_FILTER, '')).gsub(UNICODE_WHITESPACE, ' ')
|
|
34
36
|
end
|
|
35
37
|
|
|
36
38
|
# cut off at 255 characters
|
|
37
39
|
# optionally provide a padding, which is useful to
|
|
38
40
|
# make sure there is room to add a file extension later
|
|
39
41
|
def truncate
|
|
40
|
-
|
|
42
|
+
max_length = [254 - @padding, 0].max + 1
|
|
43
|
+
@truncate ||= sanitize.chars.to_a.slice(0...max_length).join
|
|
41
44
|
end
|
|
42
45
|
|
|
43
46
|
def to_s
|
|
@@ -51,24 +54,32 @@ class Zaru
|
|
|
51
54
|
|
|
52
55
|
private
|
|
53
56
|
|
|
54
|
-
|
|
57
|
+
attr_reader :fallback
|
|
55
58
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
def filter(filename)
|
|
60
|
+
filename = filter_windows_reserved_names(filename)
|
|
61
|
+
filename = filter_blank(filename)
|
|
62
|
+
filter_dot(filename)
|
|
63
|
+
end
|
|
61
64
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
end
|
|
65
|
+
def filter_windows_reserved_names(filename)
|
|
66
|
+
return '' if filename.empty?
|
|
65
67
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
end
|
|
68
|
+
parts = filename.split('.').reject(&:empty?)
|
|
69
|
+
return '' if parts.empty?
|
|
69
70
|
|
|
70
|
-
|
|
71
|
-
|
|
71
|
+
if WINDOWS_RESERVED_NAMES.include?(parts[0].upcase)
|
|
72
|
+
parts.fill(fallback, 0, 1).join('.')
|
|
73
|
+
else
|
|
74
|
+
filename
|
|
72
75
|
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def filter_blank(filename)
|
|
79
|
+
filename.empty? ? fallback : filename
|
|
80
|
+
end
|
|
73
81
|
|
|
82
|
+
def filter_dot(filename)
|
|
83
|
+
filename.start_with?('.') ? "#{fallback}#{filename}" : filename
|
|
84
|
+
end
|
|
74
85
|
end
|
metadata
CHANGED
|
@@ -1,57 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: zaru
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: '1.1'
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Thomas Fuchs
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
12
|
-
dependencies:
|
|
13
|
-
- !ruby/object:Gem::Dependency
|
|
14
|
-
name: bundler
|
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
|
16
|
-
requirements:
|
|
17
|
-
- - ">="
|
|
18
|
-
- !ruby/object:Gem::Version
|
|
19
|
-
version: '0'
|
|
20
|
-
type: :development
|
|
21
|
-
prerelease: false
|
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
-
requirements:
|
|
24
|
-
- - ">="
|
|
25
|
-
- !ruby/object:Gem::Version
|
|
26
|
-
version: '0'
|
|
27
|
-
- !ruby/object:Gem::Dependency
|
|
28
|
-
name: rake
|
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
|
30
|
-
requirements:
|
|
31
|
-
- - ">="
|
|
32
|
-
- !ruby/object:Gem::Version
|
|
33
|
-
version: '0'
|
|
34
|
-
type: :development
|
|
35
|
-
prerelease: false
|
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
-
requirements:
|
|
38
|
-
- - ">="
|
|
39
|
-
- !ruby/object:Gem::Version
|
|
40
|
-
version: '0'
|
|
41
|
-
- !ruby/object:Gem::Dependency
|
|
42
|
-
name: test-unit
|
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
|
44
|
-
requirements:
|
|
45
|
-
- - ">="
|
|
46
|
-
- !ruby/object:Gem::Version
|
|
47
|
-
version: '0'
|
|
48
|
-
type: :development
|
|
49
|
-
prerelease: false
|
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
-
requirements:
|
|
52
|
-
- - ">="
|
|
53
|
-
- !ruby/object:Gem::Version
|
|
54
|
-
version: '0'
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
55
12
|
description: Zaru takes a given filename (a string) and normalizes, filters and truncates
|
|
56
13
|
it, so it can be safely used as a filename in modern operating systems. Zaru doesn't
|
|
57
14
|
remove Unicode characters when not necessary.
|
|
@@ -65,7 +22,6 @@ homepage: https://github.com/madrobby/zaru
|
|
|
65
22
|
licenses:
|
|
66
23
|
- MIT
|
|
67
24
|
metadata: {}
|
|
68
|
-
post_install_message:
|
|
69
25
|
rdoc_options: []
|
|
70
26
|
require_paths:
|
|
71
27
|
- lib
|
|
@@ -73,15 +29,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
73
29
|
requirements:
|
|
74
30
|
- - ">="
|
|
75
31
|
- !ruby/object:Gem::Version
|
|
76
|
-
version: '
|
|
32
|
+
version: '2.0'
|
|
77
33
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
34
|
requirements:
|
|
79
35
|
- - ">="
|
|
80
36
|
- !ruby/object:Gem::Version
|
|
81
37
|
version: '0'
|
|
82
38
|
requirements: []
|
|
83
|
-
rubygems_version: 3.
|
|
84
|
-
signing_key:
|
|
39
|
+
rubygems_version: 3.6.9
|
|
85
40
|
specification_version: 4
|
|
86
41
|
summary: Filename sanitization for Ruby
|
|
87
42
|
test_files: []
|