Convert a text file to use short Metamark urls

| | Comments (0)

This should be useful to authors of the Perl summaries and such, which was my main reason for making Metamark.

Beware that you need to install URI::Find and WWW::Shorten. WWW::Shorten requires about two billion other modules (mostly indirectly), so be sure to use the CPAN shell to install them.

   # perl -MCPAN -e shell
   install URI::Find WWW::Shorten
Oh, and you can of course use one of the other shortening services that WWW::Shorten supports.

#!/usr/bin/perl -w
use strict;

# (this was in pod; but MovableType strips my '=cut' so I changed it to a comment)
#
# Read a text line by line from standard input; convert long urls to short metamark urls
# and output to standard out.                       
                                                                                                                   
use WWW::Shorten 'Metamark', ':short';
use URI::Find;

my $finder = URI::Find->new(
   sub {
     my($uri, $orig_uri) = @_;
     if( length $uri > 35 or $uri =~ m![^\w./_:?%]!) {
       return short_link($orig_uri) || $orig_uri;
     }
     return $orig_uri;
   }
  );

while (<>) {
  my $text = $_;
  $finder->find(\$text);
  print $text;
}

1;

Leave a comment

About this Entry

This page contains a single entry by Ask Bjørn Hansen published on January 11, 2003 1:17 AM.

Everything is shaking ... was the previous entry in this blog.

New Safari Beta is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 4.11