Forum: Ruby on Rails Absolutize URLs in a string

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A2b2f4ee23989dc68529baef9cbddcd6?d=identicon&s=25 Julian 'Julik' Tarkhanov (Guest)
on 2006-06-04 19:55
(Received via mailing list)
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
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-06-04 20:17
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
A2b2f4ee23989dc68529baef9cbddcd6?d=identicon&s=25 Julian 'Julik' Tarkhanov (Guest)
on 2006-06-04 21:16
(Received via mailing list)
On 4-jun-2006, at 20:17, Alex Wayne 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
A2b2f4ee23989dc68529baef9cbddcd6?d=identicon&s=25 Julian 'Julik' Tarkhanov (Guest)
on 2006-06-07 11:37
(Received via mailing list)
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 Fernandez :-P 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
This topic is locked and can not be replied to.