Professional Web Applications Themes

hyperlinking URLs (and emails?) in text - Ruby

Ugly: class String def hyperlink gsub(/(http:\/\/|https:\/\/|mailto:)([[:alnum:]\/!\#$\%&'()*+,.:;=?~-]+)([[:alnum:]\/!\#$\%&'()*+:;=?~-])/, '<a href="\1\2\3">\1\2\3</a>') end end ... but it generally works, from my few tests. Anyone got a better version, or care to improve on this one? Just trying to hyperlink URLs in text (and in future, email addresses, without needing mailto:)....

  1. #1

    Default hyperlinking URLs (and emails?) in text


    Ugly:

    class String
    def hyperlink
    gsub(/(http:\/\/|https:\/\/|mailto:)([[:alnum:]\/!\#$\%&'()*+,.:;=?~-]+)([[:alnum:]\/!\#$\%&'()*+:;=?~-])/, '<a href="\\1\\2\\3">\\1\\2\\3</a>')
    end
    end

    ... but it generally works, from my few tests.


    Anyone got a better version, or care to improve on this one?

    Just trying to hyperlink URLs in text (and in future, email addresses, without needing mailto:).


    Ruby Guest

  2. #2

    Default Re: hyperlinking URLs (and emails?) in text

    This is a little bit more readable, while still being somewhat compact.
    It also gets plain email addresses:

    class String
    def hyperlink
    # define url parts:
    chars = Regexp.escape "/!#$\\%&'()*+,.:;=?~-_"
    url = "https?:\\/\\/[#{chars}[:alnum:]]+"
    email = "(?:mailto:)?[^\s]+[[:alnum:]._-]+\\.[[:alnum:]._-]+"
    # do the substitution:
    gsub(/#{url}|#{email}/) do |link|
    if link =~ /^[a-zA-Z]+:/
    %Q(<a href="#{link}">#{link}</a>)
    else
    %Q(<a href="mailto:#{link}">#{link}</a>)
    end
    end
    end
    end

    Note that this will not get *all* email addresses, and it will let a
    some urls spill through. But it should get most stuff. Getting it exact
    would require more lines of code than I want to look at tonight :)



    On Feb 15, 2004, at 1:27 AM, Ruby Baby wrote:
     



    Mark Guest

  3. #3

    Default Re: hyperlinking URLs (and emails?) in text

    In Message-Id: <hitmedia.com>
    Ruby Baby <com> writes:
     

    Do you like the following? uri.rb is bundled with current stable releases.

    require "uri"

    URI.extract("foo bar http://foo.bar.com/foobar mailto:com")
    #=> ["http://foo.bar.com/foobar", "mailto:com"]


    --
    to February 15, 2004
    Out of sight, out of mind.



    YANAGAWA Guest

  4. #4

    Default Re: hyperlinking URLs (and emails?) in text

    Hi,

    In mail "hyperlinking URLs (and emails?) in text"
    Ruby Baby <com> wrote:
     

    If you have ruby 1.8.1:

    require 'uri'

    text.gsub(URI.regexp(%w(http https ftp))) {|urlstr|
    %Q[<a href="#{urlstr}">#{urlstr}</a>]
    }

    Also, you will want to escape '<' '>' '&' in text:

    require 'uri'
    require 'cgi'

    text.gsub(/[<&>"']|#{URI.regexp(%w(http https ftp))}/) {|matched|
    case matched
    when /[<&>"']/
    CGI.escapeHTML(matched)
    else
    url = CGI.escapeHTML(matched)
    %Q[<a href="#{url}">#{url}</a>]
    end
    }


    Regards,
    Minero Aoki


    Minero Guest

Similar Threads

  1. Outlook converting emails to text?
    By flavio in forum Coldfusion - Getting Started
    Replies: 6
    Last Post: December 23rd, 06:03 AM
  2. Creating web links from URLs in text
    By Marlene_Hochberg@adobeforums.com in forum Adobe Acrobat Windows
    Replies: 8
    Last Post: May 4th, 05:39 PM
  3. dynamic URLS convert to static URLS for search engines
    By Steve T. in forum ASP.NET Web Services
    Replies: 7
    Last Post: March 4th, 03:16 PM
  4. Attaching web URLs to text / PDF export in MX
    By Rob S. in forum Macromedia Freehand
    Replies: 0
    Last Post: January 20th, 03:49 AM
  5. Changing button's URLs based upon text field
    By cooped webforumsuser@macromedia.com in forum Macromedia Dreamweaver
    Replies: 1
    Last Post: July 21st, 06:03 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139