Changing href attributes with nokogiri and ruby on rails
Asked Answered
D

2

19

I Have a HTML document with links links, for exemple:

<html>
  <body>
   <ul>
     <li><a href="http://someurl.com/etc/etc">teste1</a></li>
     <li><a href="http://someurl.com/etc/etc">teste2</a></li>
     <li><a href="http://someurl.com/etc/etc">teste3</a></li>
   <ul>
  </body>
</html>

I want with Ruby on Rails, with nokogiri or some other method, to have a final doc like this:

<html>
  <body>
    <ul>
      <li><a href="http://myproxy.com/?url=http://someurl.com/etc/etc">teste1</a></li>
      <li><a href="http://myproxy.com/?url=http://someurl.com/etc/etc">teste2</a></li>
      <li><a href="http://myproxy.com/?url=http://someurl.com/etc/etc">teste3</a></li>
    <ul>
  </body>
</html>

What's the best strategy to achieve this?

Divulgate answered 22/4, 2010 at 23:21 Comment(2)
Are you dynamically building the html page in a rails template (ie .html.erb) or has it already been built and you want to re-scan it (using Nokogiri etc) after the fact?Halcyon
The document is already built.Divulgate
G
36

If you choose to use Nokogiri, I think this should work:

require 'cgi'
require 'rubygems' rescue nil
require 'nokogiri'

file_path = "your_page.html"
doc = Nokogiri::HTML(open(file_path))
doc.css("a").each do |link|
  link.attributes["href"].value = "http://myproxy.com/?url=#{CGI.escape link.attributes["href"].value}"
end
doc.write_to(open(file_path, 'w'))

If I'm not mistaken rails loads REXML up by default, depending on what you're trying to do you could use this also.

Gilder answered 23/4, 2010 at 15:53 Comment(2)
link['href'] is a shortcut for link.attributes["href"].valuePrate
what if I want to output the changed to a variable ?Ageold
L
3

Here is what I did for replacing images src attributes:

      doc = Nokogiri::HTML(html)
       doc.xpath("//img").each do |img|
         img.attributes["src"].value = Absolute_asset_path(img.attributes["src"].value)
      end
      doc.to_html                  // simply use .to_html to re-convert to html
Lachus answered 18/10, 2017 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.