Skip to content

Instantly share code, notes, and snippets.

@jqr
Created May 6, 2010 20:03
Show Gist options
  • Save jqr/392632 to your computer and use it in GitHub Desktop.
Save jqr/392632 to your computer and use it in GitHub Desktop.
# Returns a link to url with the specified content, automatically adds
# rel="nofollow" and the external class to the link.
def link_to_external(content, url, options = {})
url = httpify_url(url)
link_to content, url, options.merge(:rel => :nofollow, :class => "#{options[:class].to_s} external")
end
# Just like link_to_external, but uses the dehttpified url as the content.
def link_to_external_url(url, options = {})
link_to_external(h(dehttpify_url(url)), url, options)
end
# Adds http:// to a URL if missing.
def httpify_url(url)
if url.match(/^https?\:\/\//i)
url
else
"http://#{url}"
end
end
# Removes http:// from a URL if present.
def dehttpify_url(url)
if url.match(/^https?\:\/\//i)
url.gsub(/^https?\:\/\//i, '')
else
url
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment