URL-ification

Tom Christiansen (tchrist@mox.perl.com)
Tue, 09 Jan 1996 18:39:32 -0700


I've been thinking about how to convert generally convert
URLs in text like:

 Been to http://www.xor.com/? Then try http://internet-plaza.net/. 

to something like

 Been to <A HREF="http://www.xor.com/">http://www.xor.com/</A>? 
 Then try <A HREF="http://internet-plaza.net/">http://internet-plaza.net/</A>. 

It's actually somewhat tough.  I have this (as a filter) so far.
Is there a lovely module that does this already?  Is this the
right approach?

--tom

#!/usr/bin/perl -p

BEGIN { 
    $urls = '(' . join ('|', qw{
		    http
		    telnet
		    gopher
		    file
		    wais
		    ftp
		} ) 
	    . ')';

    $ltrs = '\w';
    $gunk = '/#~:.?+=&%@!\-';
    $punc = '.:?\-';
    $any  = "${ltrs}${gunk}${punc}";

}

s{
    \b                          # start at word boundary
    (                           # begin $1  {
      $urls     :               # need resource and a colon
      [$any] +?                 # followed by on or more
				#  of any valid character, but
				#  be conservative and take only
				#  what you need to....
    )                           # end   $1  }
    (?=                         # look-ahead non-comsumptive assertion
	    [$punc]+            # either 0 or more puntuation
	    [^$any]             #   followed by a non-url char
	|                       # or else
	    $                   #   then end of the string
    )
}{<A HREF="$1">$1</A>}igox;