开发者

Adding html break to each line in Ruby

开发者 https://www.devze.com 2023-03-16 12:11 出处:网络
What I basically have here is a short ruby script (I just started learning) and the purpose of it is to spider-crawl a website and return all the links it finds.

What I basically have here is a short ruby script (I just started learning) and the purpose of it is to spider-crawl a website and return all the links it finds.

@sites = Array.new
Anemone.crawl("http://www.nemecisco.com/") do |anemone|
  anemone.on_every_page do |page|
    puts page.url
    @sites<<page.url+""  #heres where i add something to give it a line break i think
  end
  anemone.after_crawl { puts @sites }
end
end

It does this fine, however when Its being outputted to the HTML, they are all clustered together as it trys to jam them on the one line. The HTML is just the array inside the ruby script tag.

<%= @sites %>

You may notice that the array is composed of the pages name + something else. I figured a break tag would be enough but Ruby doesn't like this and gives a bad URI exception.

Has anyone any ideas on how to put a line break after each site statement. In the HTML declaration? in the array? Any and a开发者_如何转开发ll help appreciated.


Since you're trying to output an array, it's joining them with a newline \n instead of an HTML break. You can force an HTML break by joining with a br tag:

<%= @sites.join("<br />") %>

In Rails 3, you may need to turn off escaping:

<%= raw(@sites.join("<br />")) %>


Probably the safest thing to do is to do it manually in the ERB:

<% @sites.each do |site| %>
    <%= site %><br>
<% end %>

Or in Rails 2:

<% @sites.each do |site| %>
    <%=h site %><br>
<% end %>

This way you don't have to worry about possible HTML issues the @sites entries.


I'm not familiar with Amemone, but I'm assuming page.url returns an instance of the URI class. You need to convert the url to a string (page.url.to_s) before appending to it.

I would probably leave the urls as URI instances though, and format them when I print. Change your after_crawl method to be { puts @sites.join("\n") } (the URI instances will be converted to strings automatically).

0

精彩评论

暂无评论...
验证码 换一张
取 消