5

Using Ruby/Rails does anyone know how to take a large string that may contain some HTML elements and make them into links?

This is an example:

"Check out my video on you tube http://youtu.be/OkCcD6cOKgs"

I am looking for something that will turn the HTML into a valid click-able link <a href ... but also leave the other text as is, just like this question did.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
John D
  • 79
  • 1
  • 3
  • What do you mean by "HTML elements"? Are they URLs, as in the sample string, or tags and markup? If they are only URLs, are they always at the end of a string, or can the URL be followed by punctuation or more words? You need to define these things in order to get good answers that solve your needs. It's easy to find a solution that works for the sentence above. Let's find one that finds a solution for all the strings you'll have. – the Tin Man Apr 15 '13 at 14:43
  • This question has been answered here: http://stackoverflow.com/questions/3095230/convert-plain-text-urls-to-html-hyperlinks-in-ruby-on-rails – Gideon Rosenthal Nov 05 '15 at 00:04

5 Answers5

10

I know it is too late, But if someone still have this question then he can do this way.

text = "Check out my video on you tube http://youtu.be/OkCcD6cOKgs"
html_text = text.gsub(URI.regexp, '<a href="\0">\0</a>').html_safe

This is best way I found from here Ruby: make plain text links clickable till now.

MUHAMMAD SOBAN
  • 403
  • 8
  • 20
  • 2
    This seem to work but I'm experiencing a bug. I have a text like this `"View full list on this link: https://google.com/"` it gives me `"View full list on this link: https://google.com/"` where any word with colon on the end seems to match the regex. – dcangulo Nov 21 '19 at 03:22
7

Rails used to have a built in "auto_link" function, which now can be brought in as an 'autolink' gem. This does other string sanitization too.

Alternatively the 'anchored' gem is more focused on this one task.

Without gems, here's the ruby code I have used:

  url_regexp = %r{
    (?:(?:https?|ftp|file):\/\/|www\.|ftp\.)
    (?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|
         [-A-Z0-9+&@#\/%=~_|$?!:,.])*
    (?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|
          [A-Z0-9+&@#\/%=~_|$])
  }ix

  text = text.gsub(url_regexp, '<a href="\0">\0</a>').html_safe

I recommend you carefully test some edge cases with whatever solution you go for. Looking around for answers to this you'll find people recommending the core library URI.regexp or URI::DEFAULT_PARSER, but this fails very simple tests. Try including a word followed by a colon ':' char in your text. You don't want that to become a link! (I don't really know why this built-in solution is so poor. My guess that is it's more intended for validating rather than finding URL-like substrings)

So my solution above is a regexp based on this answer and converted to be more rubyish. But when you start looking, there are many different regexps for URLs out there. You can play the game of finding the awkward edge case that any given regexp fails on. Generally longer regexps catch more edge cases. But there's also a challenge to convert it to work in ruby. This website presents many language solutions including a (quite long) ruby example: http://urlregex.com

Harry Wood
  • 2,220
  • 2
  • 24
  • 46
0

Split by last delimiter: How to split a string into only two parts, by the last occurrence of the split char?

text, uri = "Check out my video on you tube http://youtu.be/OkCcD6cOKgs".split(/ (?=\S+$)/)

<%= link_to text, uri %>
Community
  • 1
  • 1
Zippie
  • 6,018
  • 6
  • 31
  • 46
-2

You can mark the string as html_safe. I believe all you need to do is string.html_safe.

This is if you are asking how to display the HTML elements in the string correctly when they already exists in the string.

If you want to change a URL to an HTML link, you can always use a helper method to check if http(s) exists in the string, and add the HTML element accordingly.

TheDude
  • 3,796
  • 2
  • 28
  • 51
  • That allows html tags to work, but I am looking for a way to turn the string with a url i.e. "http..." into a clickable link automatically so the user does not have to type bar – John D Apr 15 '13 at 00:49
-4

HTML may be easier. In your view:

<h2>    <!--Or whatever tag you're using-->
  Check out my video on  
  <a href="http://http://youtu.be/OkCcD6cOKgs/">YouTube</a>
</h2>
aceofbassgreg
  • 3,837
  • 2
  • 32
  • 42
  • 1
    How is this easier? How does the OP get from a string containing a URL to HTML? That's what the question is about. – the Tin Man Apr 15 '13 at 03:28
  • The way the question is worded, I wasn't exactly clear on what it's about (i.e. if this is an element added to his site in a comments section, or if it's something he's adding to his views, or if this is really just a straight Ruby question even though he mentions Rails). – aceofbassgreg Apr 15 '13 at 03:32