Here's one I use...this is based off of some original code provided by a fellow monk. I'm drawing a blank at the moment, so if said monk could kindly speak up I'll gladly give credit (update: it was jeffa. thanks!). It's been rather modified since then.
Matt
package PerlMonks::PuffBracket;
use strict;
use warnings;
require Exporter;
use base qw(Exporter);
our @EXPORT = qw(puffbracket);
use Carp;
use URI;
use constant PM_URL => 'http://www.perlmonks.org';
use constant ABSOLUTE => 1;
my %SCHEME = (
ftp => 'ftp://',
http => 'http://',
https => 'https://',
kobe => 'http://theoryx5.uwinnipeg.ca/mod_perl/cpan-search?filety
+pe=+distribution+name+or+description&j&case=clike&search=',
kobes => 'http://theoryx5.uwinnipeg.ca/mod_perl/cpan-search?filety
+pe=+distribution+name+or+description&j&case=clike&search=',
cpan => 'http://search.cpan.org/search?mode=module&query=',
isbn => 'http://shop.barnesandnoble.com/booksearch/isbnInquiry.as
+p?isbn=',
google => 'http://www.google.com/search?q=',
lucky => 'http://www.google.com/search?btnI=I&q=',
jargon => 'http://www.science.uva.nl/cng/search/htsearch.CGI?restri
+ct=%2F%7Emes%2F&jargon%2Fwords=',
perldoc => 'http://www.perldoc.com/cgi-bin/htsearch?&words=',
dict => 'http://www.m-w.com/cgi-bin/dictionary?book=Dictionary&va
+=',
id => '/index.pl?node_id=',
pad => '/index.pl?node_id=108949&user=',
DEFAULT => '/index.pl?node=',
);
my $anch_with_alt = qr/\[(\w+):\/\/([^|\]]*?)\|([^\]]+?)\]/;
my $anch = qr/\[(\w+):\/\/([^\]]+?)\]/;
my $bracket_with_alt = qr/\[([^|\]]+)\|([^\]]+?)\]/;
my $bracket = qr/\[([^\]]+?)\]/;
if (ABSOLUTE) {
foreach (qw(id pad DEFAULT)) {
$SCHEME{$_} = PM_URL . $SCHEME{$_};
}
}
sub puffbracket {
my $line = shift;
while ($line =~ /$anch_with_alt/g) {
if ($SCHEME{$1}) {
my $tgt = URI->new($2)->canonical;
$line =~ s/$anch_with_alt/<a href="$SCHEME{$1}$tgt">$3<\/a>/
if $tgt;
}
}
while ($line =~ /$anch/g) {
my $scheme = $1;
my $tgt = $2;
my $txt = '';
if ($SCHEME{$scheme}) {
$tgt = URI->new($tgt)->canonical;
$txt = $scheme eq 'id' ? "node $tgt" : $tgt;
$line =~ s/$anch/<a href="$SCHEME{$scheme}$tgt">$txt<\/a>/;
}
}
while ($line =~ /$bracket_with_alt/g) {
my $tgt = URI->new("$SCHEME{DEFAULT}$1")->canonical;
$line =~ s/$bracket_with_alt/<a href="$tgt">$2<\/a>/
if $tgt;
}
while ($line =~ /$bracket/g) {
my $tgt = URI->new("$SCHEME{DEFAULT}$1")->canonical;
$line =~ s/$bracket/<a href="$tgt">$1<\/a>/ if $tgt;
}
$line;
}
1;
|