zaru 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/zaru.rb +22 -16
  2. metadata +4 -4
@@ -3,12 +3,14 @@
3
3
  class Zaru
4
4
  CHARACTER_FILTER = /[\x00-\x1F\/\\:\*\?\"<>\|]/u
5
5
  UNICODE_WHITESPACE = /[[:space:]]+/u
6
- WINDOWS_RESERVED_NAMES =
6
+ WINDOWS_RESERVED_NAMES =
7
7
  %w{CON PRN AUX NUL COM1 COM2 COM3 COM4 COM5
8
- COM6 COM7 COM8 COM9 LPT1 LPT2 LPT3 LPT4
8
+ COM6 COM7 COM8 COM9 LPT1 LPT2 LPT3 LPT4
9
9
  LPT5 LPT6 LPT7 LPT8 LPT9}
10
+ FALLBACK_FILENAME = 'file'
10
11
 
11
- def initialize(filename)
12
+ def initialize(filename, options = {})
13
+ @padding = options[:padding] || 0
12
14
  @raw = filename.to_s.freeze
13
15
  end
14
16
 
@@ -18,28 +20,32 @@ class Zaru
18
20
  @normalized ||= @raw.strip.gsub(UNICODE_WHITESPACE,' ')
19
21
  end
20
22
 
21
- # remove characters that aren't allowed cross-OS
23
+ # remove bad things!
24
+ # - remove characters that aren't allowed cross-OS
25
+ # - don't allow certain special filenames (issue on Windows)
26
+ # - don't allow filenames to start with a dot
27
+ # - don't allow empty filenames
22
28
  def sanitize
23
- @sanitized ||=
29
+ @sanitized ||=
24
30
  filter(normalize.gsub(CHARACTER_FILTER,''))
25
31
  end
26
32
 
27
- # normalize unicode string and cut off at 255 characters
28
- # TODO
33
+ # cut off at 255 characters
34
+ # optionally provide a padding, which is useful to
35
+ # make sure there is room to add a file extension later
29
36
  def truncate
30
- @truncated ||= sanitize.chars.to_a.slice(0..254).join
37
+ @truncated ||= sanitize.chars.to_a.slice(0..254-@padding).join
31
38
  end
32
39
 
33
- # convert back from multibyte string
34
40
  def to_s
35
41
  truncate
36
42
  end
37
-
43
+
38
44
  # convenience method
39
- def self.sanitize!(filename)
40
- new(filename).to_s
45
+ def self.sanitize!(filename, options = {})
46
+ new(filename, options).to_s
41
47
  end
42
-
48
+
43
49
  private
44
50
 
45
51
  def filter(filename)
@@ -53,11 +59,11 @@ class Zaru
53
59
  end
54
60
 
55
61
  def filter_blank(filename)
56
- filename == '' ? 'file': filename
62
+ filename.empty?? FALLBACK_FILENAME : filename
57
63
  end
58
64
 
59
65
  def filter_dot(filename)
60
- filename.start_with?('.') ? "file#{filename}" : filename
66
+ filename.start_with?('.')? "#{FALLBACK_FILENAME}#{filename}" : filename
61
67
  end
62
-
68
+
63
69
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zaru
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 3
10
- version: 0.0.3
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Thomas Fuchs
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-02-05 00:00:00 Z
18
+ date: 2014-04-26 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: Zaru takes a given filename (a string) and normalizes, filters and truncates it, so it can be safely used as a filename in modern operating systems. Zaru doesn't remove Unicode characters when not necessary.