#!/usr/bin/perl -w
# Blosxom Plugin: archives
# Author: Brian Akins (bakins@web.turner.com)
# Version: 0+5i
# Blosxom Home/Docs/Licensing: http://www.raelity.org/blosxom

package archives;

# --- Configurable Variables ---
# Do you want the list sorted in reverse chronological order?
my $reversechron=1;

# Set to whatever you want your HTML to be indented with. Leave blank for nothing.
my $indent="\t";

# Customize your month names here if you want to.
my @monthname=('01', '02', '03', '04', '05', '06', '07',
               '08', '09', '10', '11', '12');

$include_months = 0; # include months? or only years.
# ------------------------------

$archives = '';

sub start {
    return 1;
}

sub filter {
    my ($pkg, $files) = @_;
    my %archive;
    
    foreach (keys %{$files}) {
	    my @date = localtime($files->{$_});
        my $month = $date[4];
    	my $year  = $date[5] + 1900;

        $archive{$year}{'count'}++;
        $archive{$year}{$month}{'count'}++;
    }
    
    my $results = qq{<ul class="archives">\n};

    foreach my $year(sort {$reversechron?$b<=>$a:$a<=>$b} keys(%archive)) {

        $results .= qq~

$indent<li><a href="$blosxom::url/$year/">$year</a> ($archive{$year}{'count'})
	~;
	$results .= qq~$indent$indent<ul>~ if ($include_months);

        delete $archive{$year}{'count'};
        
								# loop for each month found; one LI per month.
        if ($include_months){
	    foreach my $month(sort {$reversechron?$b<=>$a:$a<=>$b} keys(%{$archive{$year}})) {
	            my $mnum = sprintf("%02d", $month+1);
	            $results .= qq{$indent$indent$indent<li><a href="$blosxom::url/$year/$mnum/index.$blosxom::flavour">$monthname[$month]</a> ($archive{$year}{$month}{'count'})</li>\n};
	        }
	    $results .= "$indent$indent</ul>\n$indent</li>\n";
	}
                          
    }

    $results .= "</ul>\n";
        
    $archives = $results;
}

1;