自动链接html字符串中的URL和图像

时间:2011-01-26 22:01:16

标签: ruby-on-rails ruby regex

嗨,我有一个这样的字符串:

"<p class='video'>http://vimeo/2342343</p><p class='image'>http://nerto.it/logo.png</p><p class='text'>try to write</p><p class='video'>http://vimeo/2234923</p>"

我必须用这样的字符串转换它:

"<p class='video'><a href='http://vimeo/2342343'>http://vimeo/2342343</a></p><p class='image'><img src='http://nerto.it/logo.png' /></p><p class='text'>try to write</p><p class='video'><a href='http://vimeo/2234923'>http://vimeo/2234923</a></p>"

那么我如何获得每一个元素并对其进行转换?

感谢

6 个答案:

答案 0 :(得分:6)

您可以使用auto-link function将链接转换为实际的锚标记。

auto_link(text_to_convert)

*注意:不推荐使用或移动了方法 不推荐使用此方法或在最新的稳定版本上移动此方法。最后一个现有版本(v3.0.9)显示在链接中。

如果您有更具体的用例,您可能希望将gsub与正则表达式一起使用。例如:

text.gsub(/\<p\s+class=\'image\'\>(.*?)\<\/p\>/, "<p class='image'><img src='\\1' /></p>")

答案 1 :(得分:5)

html = "<p class='video'>http://vimeo/2342343</p>
        <p class='image'>http://nerto.it/logo.png</p>
        <p class='text'>try to write</p>
        <p class='video'>http://vimeo/2234923</p>"

linked = html.gsub( %r{http://[^\s<]+} ) do |url|
  if url[/(?:png|jpe?g|gif|svg)$/]
    "<img src='#{url}' />"
  else
    "<a href='#{url}'>#{url}</a>"
  end
end

puts linked
#=> <p class='video'><a href='http://vimeo/2342343'>http://vimeo/2342343</a></p>
#=> <p class='image'><img src='http://nerto.it/logo.png' /></p>
#=> <p class='text'>try to write</p>
#=> <p class='video'><a href='http://vimeo/2234923'>http://vimeo/2234923</a></p>

答案 2 :(得分:5)

auto_link函数已移至单独的gem here

答案 3 :(得分:2)

不要编写复杂正则表达式,而是使用Nokogiri。下面的解决方案将完美地转换链接和图像。

require 'rubygems'
require 'nokogiri'

#replace with your string
str = "...."

doc = Nokogiri::HTML.parse(str)
video_nodes = doc.css('.video')
video_nodes.each do |v|
  content = v.content
  link_node = Nokogiri::XML::Node.new('a',doc)
  link_node['href'] = content
  link_node.content = content 
  v.add_child(link_node)
end

img_nodes = doc.css('.image')

img_nodes.each do |img|
  content = img.content
  image_node = Nokogiri::XML::Node.new('img',doc)
  image_node['src'] = content
  img.add_child(image_node)
end
puts doc.to_html

答案 4 :(得分:0)

尝试这个简单的最佳宝石将文本或字符串中的所有网址转换为链接。它还将图像URL转换为图像标记。

gem install url_link

string =  "Welcome to my website http://www.mywebsite.com"
url_link(format(string))
result => welcome to my website <a href='http://www.mywebsite.com'>http://www.mywebsite.com</a>

image_string = "http://blogspot.com/images/screenshot.png"
url_link(format(image_string))
result => <img src="http://blogspot.com/images/screenshot.png"/>

string = "Welcome to my website http://www.mywebsite.com see the picture http://media.smashingmagazine.com/images/introduction-to-rails/rails.jpg"
url_link(format(string))
result => welcome to my website <a href='http://www.mywebsite.com'>http://www.mywebsite.com</a>see the picture <img src="http://media.smashingmagazine.com/images/introduction-to-rails/rails.jpg"/>

或尝试另一个

辅助

def proper_url_link(url_link)
  unless url_link.blank?
   url_link.gsub( %r{(http|https)://[^\s<]+} ) do |url|
      if url[/(?:png|jpe?g|gif|svg)$/]
        "<img src='#{url}' />"
      else
        "<a href='#{url}' target='_blank'>#{url}</a> "
      end
    end
  end
end

def proper_html(html_format)
  unless html_format.blank?
    html_format.html_safe
  end
end

View

html = "<p class='video'>http://www.vimeo.com/2342343</p>

<=proper_html(proper_url_link(html))%>

答案 5 :(得分:0)

我想建议您将所有网址从文本转换为链接的新宝石 gem link_url 。它也适用于没有宝石的www。

gem install link_url

Example 1: 
LinkUrl.convert('hello I am on www.stackoverflow.com')
Result => hello I am on <a href='http://www.stackoverflow.com'>www.stackoverflow.com</a>
Example 2:
LinkUrl.convert('hello I am on www.stackoverflow.com and my blog is http://www.clecotech.in')
Result => hello I am on <a href='http://www.stackoverflow.com'>www.stackoverflow.com</a> and my blog is <a href='http://www.clecotech.in'>http://www.clecotech.in</a>