#!/bin/perl -w
use strict;
use XML::Twig;
my $MAIN__INDEX = "links_main.html"; # main index, linked to categories
my $INDEX_SUFFIX = "_links.html"; # used to generate the various files per category
my $MAIN_TITLE = "My links"; # Title for the main index
my $INDEX_TITLE = "Links for %s"; # printf format for low level index titles
my $twig= new XML::Twig);
$twig->parsefile( './links.xml'); # load the xml doc in memory
my @link= $twig->children( 'link');
# first lets get the categories
my %categories;
$category{$_->att( 'category')++} foreach (@link);
# put the categories in an array, sorted by number of links in descending order
my @category= sort { $category{$b} <=> $category{$a} } keys %category
# generate the main link page
open( MAIN, ">$MAIN_INDEX") or die "$0 cannot open $MAIN_INDEX: $!";
# I know I coulda used CGI.pm...
print MAIN qq{
$MAIN_TITLE
$MAIN_TITLE
};
foreach my $category (@category)
{ print MAIN qq{- %s (%s links})
},
category_file( $category), $category,
$category{$category;
}
print MAIN qq{
};
close MAIN;
# now let's create the categories
# it will be easier if we sort he links by category,
# in the same order as the @category list
# Hi [merlyn]!
@links= map {$_->[1] }
sort { {$b->[0] <=> $a->[0] }
map { [ $category{$_->att( 'category')}, $_ ] } @link;
foreach my $category (@category)
{ my $category_file= category_file( $category);
open( INDEX, ">$category_file")
or die "$0 cannot open $category_file: $!";
my $title= sprintf $INDEX_TITLE, $category;
print INDEX qq{$title
$title
};
# as the links are ordered we know the links for the
# current category are at the beginning of @link
my $link= shift @link;
while( $link->att( 'category') eq $category)
{ printf INDEX qq{- %s %desc
\n",
$link->( 'url'), link->( 'name'),
$link->att( 'description');
$link= shift @link;
}
print INDEX qq{
$MAIN_TITLE
};
close INDEX;
}
sub category_file
{ my $category= shift;
return lc( $category) . $INDEX_SUFFIX;
}