Sha256: 7ca7b2b0020c1b555a62510f9c086589a4ed29425675389cf888cc268e7635f8

Contents?: true

Size: 818 Bytes

Versions: 3

Compression:

Stored size: 818 Bytes

Contents

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

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

		def self.to_quoted_string(string)
			'"' + string.gsub('"', '\\"').gsub(/\r/, "\\r").gsub(/\n/, "\\n") + '"'
		end
		
		# `value` must already be escaped.
		def self.to_attribute(key, value)
			%Q{#{key}="#{value}"}
		end
		
		def self.to_simple_attribute(key, strict)
			strict ? %Q{#{key}="#{key}"} : key.to_s
		end
		
		def self.to_title(string)
			string.gsub(/(^|[ \-_])(.)/){" " + $2.upcase}.strip
		end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
trenni-1.0.4 lib/trenni/strings.rb
trenni-1.0.3 lib/trenni/strings.rb
trenni-1.0.2 lib/trenni/strings.rb