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.



35
36
37
# File 'lib/trenni/strings.rb', line 35

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

.to_html(string) ⇒ Object



26
27
28
# File 'lib/trenni/strings.rb', line 26

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

.to_quoted_string(string) ⇒ Object



30
31
32
# File 'lib/trenni/strings.rb', line 30

def self.to_quoted_string(string)
	'"' + string.gsub('"', '\\"').gsub(/\r/, "\\r").gsub(/\n/, "\\n") + '"'
end

.to_simple_attribute(key, strict) ⇒ Object



39
40
41
# File 'lib/trenni/strings.rb', line 39

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

.to_snake(string) ⇒ Object



47
48
49
# File 'lib/trenni/strings.rb', line 47

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

.to_title(string) ⇒ Object



43
44
45
# File 'lib/trenni/strings.rb', line 43

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