hq-tools 0.1.1 → 0.2.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.
Files changed (2) hide show
  1. data/lib/hq/tools/escape.rb +74 -0
  2. metadata +3 -2
@@ -0,0 +1,74 @@
1
+ require "cgi"
2
+
3
+ module HQ
4
+ module Tools
5
+ module Escape
6
+
7
+ # various escape functions
8
+
9
+ def self.html str
10
+ return CGI::escapeHTML str
11
+ end
12
+
13
+ def self.url str
14
+ return CGI::escape str
15
+ end
16
+
17
+ def self.xpath str
18
+ return "'" + str.gsub("'", "''") + "'"
19
+ end
20
+
21
+ def self.shell str
22
+
23
+ # recurse into arrays and join with space
24
+
25
+ return str.map { |a| shell a }.join(" ") \
26
+ if str.is_a?(Array)
27
+
28
+ # simple strings require no encoding
29
+
30
+ return str \
31
+ if str =~ /^[-a-zA-Z0-9_\/:.=@]+$/
32
+
33
+ # single quotes preferred
34
+
35
+ unless str =~ /'/
36
+ return \
37
+ "'" +
38
+ str.gsub("'", "'\\\\''") +
39
+ "'"
40
+ end
41
+
42
+ # else double quotes
43
+
44
+ return \
45
+ "\"" +
46
+ str.gsub("\\", "\\\\\\\\")
47
+ .gsub("\"", "\\\\\"")
48
+ .gsub("`", "\\\\`")
49
+ .gsub("$", "\\\\$") +
50
+ "\""
51
+
52
+ end
53
+
54
+ # also function as a handy mixin
55
+
56
+ def esc_ht str
57
+ return Escape.html str
58
+ end
59
+
60
+ def esc_ue str
61
+ return Escape.url str
62
+ end
63
+
64
+ def esc_xp str
65
+ return Escape.xpath str
66
+ end
67
+
68
+ def esc_shell str
69
+ return Escape.shell str
70
+ end
71
+
72
+ end
73
+ end
74
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hq-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-19 00:00:00.000000000 Z
12
+ date: 2013-03-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -99,6 +99,7 @@ extensions: []
99
99
  extra_rdoc_files: []
100
100
  files:
101
101
  - lib/hq/tools/getopt.rb
102
+ - lib/hq/tools/escape.rb
102
103
  - spec/hq/tools/getopt-spec.rb
103
104
  homepage: https://github.com/jamespharaoh/hq-tools
104
105
  licenses: []