Just a short little sub I pulled from my discussion board code that provides pseudo-PerlMonk style bracket links. I'm sure the regexes could be improved (and I'd love suggestions), but it works for the daily use my friends and I put through it.
sub linkify { my $text = shift; # translate the square bracket URL links to real (HTML) links 1 while $text =~ s/\[((?:(?:(?:(?:(?:http|ftp|gopher|news):\/\/)|m +ailto:)|\/)[^\|]+?)|\/)\]/<a href="$1">$1<\/a>/; 1 while $text =~ s/\[((?:(?:(?:(?:(?:http|ftp|gopher|news):\/\/)|m +ailto:)|\/)[^\|]+?)|\/)\|(.+?)\]/<a href="$1">$2<\/a>/; # translate the square bracket select (com, net, org only) email l +inks to # real (HTML), mailto: links 1 while $text =~ s/\[(.+?@.+?\.(com|net|org))\|(.+?)\]/<a href="ma +ilto:$1">$3<\/a>/; 1 while $text =~ s/\[(.+?@.+?\.(com|net|org))\]/<a href="mailto:$1 +">$1<\/a>/; # Google, for instance. Others become easy by following this exam +ple. 1 while $text =~ s/\[(google):\/\/([^|]+)\]/<a href="http:\/\/goog +le.com\/search?q=$1">$1<\/a>/; 1 while $text =~ s/\[(google):\/\/([^|]+)\|(.+?)\]/<a href="http:\ +/\/google.com\/search?q=$1">$2<\/a>/; return $text; }

Replies are listed 'Best First'.
PerlMonks::PuffBracket
by mojotoad (Monsignor) on May 13, 2003 at 21:07 UTC
    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=+distribut­ion+name+or+description&j&case=clike&search=', kobes => 'http://theoryx5.uwinnipeg.ca/mod_perl/cpan-search?filety +pe=+distribut­ion+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;
•Re: Psuedo-PerlMonk Style Links
by merlyn (Sage) on May 13, 2003 at 21:25 UTC

      Personally, I'd love to see the POD parsers handle some Wiki formatting idioms like the bullet lists.

      Any line following a blank line and starting with an asterisk should be seen as an implied =item * instead, using an =over 4 if the preceding paragraph wasn't also a bulleted item.

      Any line following such a bullet item and starting with an asterisk should also be seen as an implied =item.

      If there is leading whitespace, and the previous line is a bullet item, then inject another =over 4.

      To avoid conflict between indented code areas and numbered lists, nested lists have to follow their parents without a blank line. First-column lists can have blank lines above them. Unlike Wiki, you wouldn't need to terminate a list: just start something with less indent or with no asterisk.

      An indented line without bullet but immediately following a bulleted line would be seen as a new flowing paragraph at the same indent level, not as the start of a preformatted indented code block.

      Same goes for the # for numbered lists; since it's in POD, there's no overlap with Perl comments.

      Roughly:

      =head1 METHODS * new() # This is a code block with a Perl comment since # it's indented and follows a blank line. my $foo = new MyClass(parameters); Creates a new instance of MyClass. Valid parameters: # --style Sets the initial style parameter. Valid styles: * dotted * dashed * stippled # --text Sets the initial text parameter. =cut

      A lot briefer and more readable than the =over4/=item*/=item2 equivalent, I think. And since it can still be expressed AS the above items, maybe it can just be done as a preprocessor step.

      --
      [ e d @ h a l l e y . c c ]