#!/usr/bin/perl -w # Blosxom Plugin: blogroll # Author: Todd Larason (jtl@molehill.org) # Version: 0+1i # Blosxom Home/Docs/Licensing: http://www.raelity.org/blosxom # Blogroll plugin Home/Docs/Licensing: # http://molelog.molehill.org/blox/Computers/Internet/Web/Blosxom/Blogroll/ package blogroll; # --- Configuration Variables --- # files to read; should be either OPML or 'table' files (tab<url>\n) my @source_files = qw(/home/jtl/apps/amphetadesk-0.93.1/data/myChannels.opml /var/www/html/molelog/blog/flavours/nonrdfblogs.tab); # output format # 'ul' -> unordered list; simplest, suitable for most people # 'm4' -> m4 source for later postprocessing my $output_format = 'ul'; # --- End of Configuration Section --- use IO::File; # Output & formatting functions my @items; sub report_start { return qq!<ul>\n! if $output_format eq 'ul'; return qq!blogroll_start\n! if $output_format eq 'm4'; warn "Invalid output_format $output_format"; return qq!!; } sub report_item { my ($title, $htmlurl, $xmlurl) = @_; return qq!<li><a href="$htmlurl">$title</a>! . ($xmlurl ? qq! (<a href="$xmlurl">xml</a>)! : qq!!) . qq!</li>\n! if $output_format eq 'ul'; return qq!blogroll_item([[$htmlurl]],[[$title]],[[$xmlurl]])\n! if $output_format eq 'm4'; return qq!!; } sub report_end { return qq!</ul>\n! if $output_format eq 'ul'; return qq!blogroll_end\n! if $output_format eq 'm4'; } sub finish { my $results; $results = report_start(); foreach (sort {lc($a->[0]) cmp lc($b->[0])} @items) { $_->[2] ||= ''; $results .= report_item(@{$_}); } $results .= report_end(); return $results; } # input and parsing functions sub handle_item { my ($title, $htmlurl, $xmlurl) = @_; push @items,[$title, $htmlurl, $xmlurl]; } sub handle_opml_file { my ($filename) = @_; my $fh = new IO::File("< $filename"); if (!$fh) { warn "Couldn't open $filename"; return; } # XXX this should maybe do 'real' xml parsing # XML::Simple fast enough? worth requiring more # modules installed? while ($_ = $fh->getline) { next unless /^\s*<outline /; my ($htmlurl, $title, $xmlurl); ($htmlurl) = m:htmlurl="([^\"]+)":; ($title ) = m:title="([^\"]+)":; ($xmlurl ) = m:xmlurl="([^\"]+)":; handle_item($title, $htmlurl, $xmlurl); } $fh->close; } sub handle_tab_file { my ($filename) = @_; my $fh = new IO::File("< $filename"); if (!$fh) { warn "Couldn't open $filename"; return; } while ($_ = $fh->getline) { chomp; my ($title, $htmlurl) = split /\t+/; handle_item($title, $htmlurl); } close $fh; } sub handle_file { my ($filename) = @_; if ($filename =~ m:\.opml$:) { handle_opml_file($filename) } elsif ($filename =~ m:\.tab$:) { handle_tab_file($filename); } else { warn "Unrecognized filetype $filename"; } } sub handle_files { foreach (@source_files) { handle_file($_); } return finish(); } # blosxom plugin interface $blogroll = ''; sub start { return 1; } sub head { my ($pkg, $currentdir, $head_ref) = @_; $blogroll = handle_files(); 1; } #package main; # if called as a standalone, output to stdout -- double up to avoid # spurious warning #if (!defined($blog_title) && !defined($blog_title)) { # print blogroll::handle_files(); # exit 0; #} 1;