Automatically link urls and images inside html string
Hi, I have a line like this:
"<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>" I need to convert it to a string like this:
"<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>" so how can i get each item and convert it?
thanks
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> You can use the automatic anchor function to convert links to actual anchor labels.
auto_link (text_to_convert)
* Note: the method is deprecated or moved This method is deprecated or ported to the latest stable version. The latest existing version (v3.0.9) is shown in the link.
If you have more specific use cases, you probably want to use gsub with regex. For example:
text.gsub (/ \ <p \ s + class = \ 'image \' \> (. *?) \ <\ / p \> /, "<p class = 'image'> <img src = '\\ 1 '/> </p> ")
Instead of writing a regular expression, use complex Nokogiri. The solution below ideally converts links and images.
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
Try this simple best gem to convert the entire URL from text or string to links. It also converts the image URL into an image tag.
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"/> Or try another one
Assistant
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))%> The new big gem I want to offer you for converting an entire URL from text to links is gem link_url . He also works for www, where there is no such gem.
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>