Created
March 13, 2014 00:29
-
-
Save etrepum/9519609 to your computer and use it in GitHub Desktop.
Sinatra helper to add a url_escape method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
helpers do | |
def escape(text) | |
Rack::Utils.escape_html(text) | |
end | |
def url_escape(text) | |
chunks = [] | |
after = text | |
while after != "" do | |
before, match, after = after.partition(URI.regexp) | |
if before != "" | |
chunks.push(escape(before)) | |
end | |
if match != "" | |
chunks.push("<a href=\"#{match}\">#{match}</a>") | |
end | |
end | |
chunks.join | |
end | |
alias_method :h, :escape | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment