# Blosxom Plugin: paging # Author(s): Kyo Nagashima # Version: 2004-09-11 # Blosxom Home/Docs/Licensing: http://www.blosxom.com/ package paging; use strict; use vars qw($navi $link_first $link_prev $link_next $link_last); # --- Configurable variables ----------- # number of page indicators # this variable supports only odd number or 0 currently. my $num = 9; # --- Plug-in package variables -------- my($currentpath, $num_files, $pages, $page, $from, $to); # -------------------------------------- sub start { $currentpath = "$blosxom::datadir/$blosxom::path_info"; if (!-d $currentpath or $blosxom::path_info_yr) { return 0; } return 1; } sub filter { my($pkg, $files_ref, $others_ref) = @_; foreach (keys %$files_ref) { delete $files_ref->{$_} unless /^$currentpath/; } $num_files = scalar(keys %$files_ref); $pages = $num_files / $blosxom::num_entries; $pages = int($pages + 1) unless $pages == int($pages); $page = param('page'); $page = 1 unless $page =~ /\d+/; $from = $blosxom::num_entries * ($page - 1); $to = $num_files - 1; return 1; } sub sort { return sub { my($files_ref) = @_; my @sorted = sort { $files_ref->{$b} <=> $files_ref->{$a} } keys %$files_ref; return @sorted[$from .. $to]; }; } sub head { my ($pkg, $currentdir, $head_ref) = @_; $currentdir =~ s!^/*!!; $currentdir &&= "/$currentdir"; my $url; # prefix $navi .= qq!
\n

\n!; $navi .= qq!Page $page/$pages:\n!; # link to the first page if ($page > 2) { $url = "$blosxom::url$currentdir/"; $link_first = qq!!; $navi .= qq!«\n!; } # link to previous page if ($page > 1) { if ($page == 2) { $url = "$blosxom::url$currentdir/"; } else { my $i = $page - 1; $url = "$blosxom::url$currentdir/?page=$i"; } $link_prev = qq!!; $navi .= qq!\n!; } # links my $j = $page; my $k = ($num == 0) ? $pages : int($num / 2); if (($j - $k) < 0) { $j = $k + 1; } elsif (($j + $k) > $pages) { $j = $pages - $k; } for (my $i = $j - $k; $i <= $j + $k; $i++) { next if $i < 1; last if $i > $pages; if ($i == $page) { # current page indicator is plain text $navi .= qq!$i\n!; } else { # other page indicator is link if ($i == 1) { $url = "$blosxom::url$currentdir/"; } else { $url = "$blosxom::url$currentdir/?page=$i"; } $navi .= qq!$i\n!; } } # link to next page if ($page < $pages) { my $i = $page + 1; $url = "$blosxom::url$currentdir/?page=$i"; $link_next = qq!!; $navi .= qq!\n!; } # link to the last page if ($page < $pages - 1) { $url = "$blosxom::url$currentdir/?page=$pages"; $link_last = qq!!; $navi .= qq!»\n!; } # postfix $navi .= qq!

\n
!; return 1; } 1;