Module: HQ::Tools::Escape

Defined in:
lib/hq/tools/escape.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.html(str) ⇒ Object

various escape functions



9
10
11
# File 'lib/hq/tools/escape.rb', line 9

def self.html str
   return CGI::escapeHTML str
end

.shell(str) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hq/tools/escape.rb', line 21

def self.shell str

	# recurse into arrays and join with space

	return str.map { |a| shell a }.join(" ") \
		if str.is_a?(Array)

	# simple strings require no encoding

	return str \
		if str =~ /^[-a-zA-Z0-9_\/:.=@]+$/

	# single quotes preferred

	unless str =~ /'/
		return \
			"'" +
			str.gsub("'", "'\\\\''") +
		"'"
	end

	# else double quotes

	return \
		"\"" +
		str.gsub("\\", "\\\\\\\\")
			.gsub("\"", "\\\\\"")
			.gsub("`", "\\\\`")
			.gsub("$", "\\\\$") +
		"\""

end

.url(str) ⇒ Object



13
14
15
# File 'lib/hq/tools/escape.rb', line 13

def self.url str
   return CGI::escape str
end

.xpath(str) ⇒ Object



17
18
19
# File 'lib/hq/tools/escape.rb', line 17

def self.xpath str
	return "'" + str.gsub("'", "''") + "'"
end

Instance Method Details

#esc_ht(str) ⇒ Object

also function as a handy mixin



56
57
58
# File 'lib/hq/tools/escape.rb', line 56

def esc_ht str
	return Escape.html str
end

#esc_shell(str) ⇒ Object



68
69
70
# File 'lib/hq/tools/escape.rb', line 68

def esc_shell str
	return Escape.shell str
end

#esc_ue(str) ⇒ Object



60
61
62
# File 'lib/hq/tools/escape.rb', line 60

def esc_ue str
	return Escape.url str
end

#esc_xp(str) ⇒ Object



64
65
66
# File 'lib/hq/tools/escape.rb', line 64

def esc_xp str
	return Escape.xpath str
end