2

Many of my users are typing in plain URL's and not using the Textile format for creating links. I would like Textile to just make the URL's linkable to the URL's. I don't really need to worry about XSS or anything malicious because it is an internal network with a very small group of users. What would be the best way to go about achieving this functionality?

I am using the Ruby version of RedCloth.

Sixty4Bit
  • 12,852
  • 13
  • 48
  • 62

2 Answers2

3

How about this?

def urls2links( s )
  s.gsub(
    /([^:])((https?|ftp)\:\/\/[^\s)'"]+[^.,)\s])(. |, |[\s]|\) | )/, 
    '\1<a href="\2" target="_blank">\2</a>\4')
end

def textile2html( s )
  textilize(urls2links(s)).html_safe
end

Use this text to test it:

Plain urls converted to external links by my textile2html helper:
* http://www.google.com 
* http://www.google.com, 
* http://www.google.com. 
Mike Blake
  • 61
  • 6
  • Edit your answer for others to see the full answer at once and there is no need to read all comments – Yaroslav Oct 10 '12 at 11:49
1

I'm not sure if RedCloth can turn URLs into clickable links, but there's a Rails text helper called auto_link that can.

Zargony
  • 9,615
  • 3
  • 44
  • 44
  • Interesting. I am already sending the data through Textile so sending it through another parser isn't exactly what I want to do. – Sixty4Bit Aug 05 '10 at 15:27