Thanks guys, not quite what I was after but it did give me a few ideas. Looking through the forum this has come up a couple of times without a real answer. By 'borrowing' a few ideas from various places I've come up with the following. It's not exactly pretty but seems to work. Perl isn't my first language so be gentle with me ;)
In
nph-build.cgi replace the
build_category_pages sub with the following
Code:
sub build_category_pages {
# --------------------------------------------------------
# This routine builds all the category pages. Each category uses
# the same template which is defined in &site_html_category.
my $build_single = shift;
my ($cat, $url, $dir, @related, $relation, $page_num, $next_page, $numlinks);
local ($category, $links, $title_linked, $title, $total, $category_name, $category_name_escaped, $subcat_links);
local ($description, $related, $meta_name, $meta_keywords, $header, $footer, $next, $prev);
# Go through each category and build the appropriate page.
CATEGORY: foreach $cat (sort keys %category) {
next CATEGORY if ($cat =~ /^\s*$/); # How'd that get in here? =)
next CATEGORY if ($build_single and ($build_single ne $cat));
$url = "$build_root_url/" . &urlencode($cat) . "/";
$use_html ?
print qq|Building Category: <A HREF="$url" TARGET="_blank">$cat</A>\n| :
print qq|Building Category: $cat\n|;
print "\tSubcategories: " . ($#{$subcategories{$cat}} + 1) . "\n";
print "\tLinks: " . (($#{$links{$cat}}+1) / ($#db_cols+1)) . "\n";
# Let's make sure the directory exists, build it if it doesn't.
$dir = &build_dir ($cat);
print "\tDirectory: $dir\n";
print "\tFilename : $dir/$build_index\n";
# We set up all the variables people can use in &site_html.pl.
($description, $related, $meta_name, $meta_keywords, $header, $footer) = @{$category{$cat}}[2..7];
# Get the header and footer from file if it exists, otherwise assume it is html.
if ($header && (length($header) < 20) && ($header !~ /\s+/) && (-e "$db_header_path/$header")) {
open (HEAD, "<$db_header_path/$header") or &cgierr ("Unable to open header file: $db_header_path/$header. Reason: $!");
$header = "";
while (<HEAD>) {
$header .= $_;
}
close HEAD;
}
if ($footer && (length($footer) < 20) && ($footer !~ /\s+/) && (-e "$db_footer_path/$footer")) {
open (FOOT, "<$db_footer_path/$footer") or &cgierr ("Unable to open footer file: $db_footer_path/$footer. Reason: $!");
$footer = "";
while (<FOOT>) {
$footer .= $_;
}
close FOOT;
}
$title_linked = &build_linked_title ($cat);
$title = &build_unlinked_title ($cat);
$total = ($#{$links{$cat}} + 1) / ($#db_cols + 1);
$category_name = $cat;
$category_name_escaped = &urlencode ($cat);
$category_clean = &build_clean ($cat);
# Get list of links
$numlinks = ($#{$links{$cat}} + 1) / ($#db_cols + 1);
$next = $prev = $links = "";
for ($i = 0; $i < $numlinks; $i++) {
%tmp = &array_to_hash ($i, @{$links{$cat}});
$links .= &site_html_link (%tmp);
}
# Get sub-category links
if ($#{$subcategories{$cat}} >= 0) {
$subcat_links = &site_html_subcat_links (@{$subcategories{$cat}});
}
else {
$subcat_links = "";
}
$links .= $subcat_links;
# Create the main page.
open (CAT, ">$dir/$build_index") or &cgierr ("unable to open category page: $dir/$build_index. Reason: $!");
print CAT &site_html_category;
close CAT;
print "\n";
}
}
Then add a new sub called
site_html_subcat_links Code:
sub site_html_subcat_links {
# --------------------------------------------------------
my (@subcat) = @_;
my ($url, $numlinks, $mod, $subcat, $category_name, $description, $output, $i);
local ($links);
foreach $subcat (sort @subcat) {
$numlinks = ($#{$links{$subcat}} + 1) / ($#db_cols + 1);
# Start with the sub-category title, don't show if no links
if ($numlinks > 0 ) {
$output .= "<P><FONT SIZE=2><B>";
$output .= &build_unlinked_title ($subcat);
$output .= "</B></FONT><BR>";
}
# Loop though the sub-catgeory links
for ($i = 0; $i < $numlinks; $i++) {
%tmp = &array_to_hash ($i, @{$links{$subcat}});
$output .= &site_html_link (%tmp);
}
# Get sub-category links
if ($#{$subcategories{$subcat}} >= 0) {
$subcat_links = &site_html_subcat_links (@{$subcategories{$subcat}});
}
else {
$subcat_links = "";
}
$output .= $subcat_links;
}
return $output;
}
In
build_category_pages I've removed the related categories and sub-categories sections as well as span pages as these are no longer relevent.
site_html_subcat_links will call itself for each subsequent sub-category. So far I've tested it 3 levels down (ie. Cat/Sub_Cat/Sub_Cat/). In theory it should continue to work regardless of the number of levels involved.
This will build an index page for every sub-category, which is a bit over-kill. There's not really any point in building Cat/Sub-cat/Sub-cat/index.html as it will never be seen. Perhaps add something to the start of build_category_pages to only build if we are starting from a top-level category.
I'd really like to get this into a 2 column layout. The total number of links should be know but I can't find an easy way of keeping track of how many links have been built so far.