Absolutize URLs in a string


#1

I wonder - do we have some helper/processor/gem to automatically
convert all URLs in a passed string to their canonical equivalent -
i.e. with the protocol, host and such prepended based on the Rails
environment. Super-duper infty for RSS feeds (I hate their
requirement for canonical URLs everywhere).

Julian ‘Julik’ Tarkhanov
please send all personal mail to
me at julik.nl


#2

Julian ‘Julik’ Tarkhanov wrote:

I wonder - do we have some helper/processor/gem to automatically
convert all URLs in a passed string to their canonical equivalent -
i.e. with the protocol, host and such prepended based on the Rails
environment. Super-duper infty for RSS feeds (I hate their
requirement for canonical URLs everywhere).

Julian ‘Julik’ Tarkhanov
please send all personal mail to
me at julik.nl

I’m not sure about strings, but the url_helpers have an :only_path
parameter that should do what you want. The following line will give a
url with host protocol and port.

url_for :action => ‘foo’, :only_path => false

You can even set up your own defaults for the url helpers. Add the
default_url_options method to your application_controller

def default_url_options(options)
{:only_path => false}
end

All urls in your entire app will now be absolute, unless you pass in
:only_path=>true when creating the url


#3

On 4-jun-2006, at 20:17, Alex W. wrote:

I’m not sure about strings, but the url_helpers have an :only_path
parameter that should do what you want. The following line will
give a
url with host protocol and port.

I am talking about URLs typed in by users, and relating to the site
root - as in a Markdown-formatted entry:

We are proud to announce our new [bodily parts enlargment

contraption](/products/magic-device).

After being processed with Markdown I would like to process all href=
and src= attributes so that they include the host, port and protocol.


Julian ‘Julik’ Tarkhanov
please send all personal mail to
me at julik.nl


#4

On 4-jun-2006, at 21:14, Julian ‘Julik’ Tarkhanov wrote:

I am talking about URLs typed in by users, and relating to the site
root - as in a Markdown-formatted entry:

Ok, thanks to some ruby-foo of Mauricio F. :stuck_out_tongue: I don’t have to
write this myself, all rejoice

http://eigenclass.org/hiki.rb?Turning+Hiki+into+a+simple+CMS

require ‘cgi’
require ‘uri’

def rss_make_content_relative_to(htmlcontent, root)
htmlcontent.gsub(/(<(img|a)\b[^>](src|href)=)(["’])(.?)\4/) do
md = $~

 url = URI.parse CGI.unescapeHTML(md[5])

 if url.relative? && url.path !~ /^\//
   md[1] + md[4] + root + "/" + md[5] + md[4]
 else
   md.to_s
 end

end
end


Julian ‘Julik’ Tarkhanov
please send all personal mail to
me at julik.nl