Module: Trenni::Strings

Defined in:
lib/trenni/strings.rb

Constant Summary collapse

HTML_ESCAPE =
{"&" => "&amp;", "<" => "&lt;", ">" => "&gt;", "\"" => "&quot;"}
HTML_ESCAPE_PATTERN =
Regexp.new("[" + Regexp.quote(HTML_ESCAPE.keys.join) + "]")

Class Method Summary collapse

Class Method Details

.to_attribute(key, value) ⇒ Object

‘value` must already be escaped.



41
42
43
# File 'lib/trenni/strings.rb', line 41

def self.to_attribute(key, value)
	%Q{#{key}="#{value}"}
end

.to_html(string) ⇒ Object



28
29
30
# File 'lib/trenni/strings.rb', line 28

def self.to_html(string)
	string.gsub(HTML_ESCAPE_PATTERN){|c| HTML_ESCAPE[c]}
end

.to_quoted_string(string) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/trenni/strings.rb', line 32

def self.to_quoted_string(string)
	string = string.gsub('"', '\\"')
	string.gsub!(/\r/, "\\r")
	string.gsub!(/\n/, "\\n")
	
	return "\"#{string}\""
end

.to_simple_attribute(key, strict) ⇒ Object



45
46
47
# File 'lib/trenni/strings.rb', line 45

def self.to_simple_attribute(key, strict)
	strict ? %Q{#{key}="#{key}"} : key.to_s
end

.to_snake(string) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/trenni/strings.rb', line 56

def self.to_snake(string)
	string = string.gsub("::", "")
	string.gsub!(/([A-Z]+)/){"_" + $1.downcase}
	string.sub!(/^_+/, "")
	
	return string
end

.to_title(string) ⇒ Object



49
50
51
52
53
54
# File 'lib/trenni/strings.rb', line 49

def self.to_title(string)
	string = string.gsub(/(^|[ \-_])(.)/){" " + $2.upcase}
	string.strip!
	
	return string
end