-
Notifications
You must be signed in to change notification settings - Fork 1
/
paging
154 lines (118 loc) · 3.63 KB
/
paging
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Blosxom Plugin: paging
# Author(s): Kyo Nagashima <[email protected]>
# Version: 2009-05-21
# Blosxom Home/Docs/Licensing: http://www.blosxom.com/
package paging;
use strict;
use vars qw($navi $link_first $link_prev $link_next $link_last $meta_robots);
# --- Configurable variables -----------
# number of page indicators
# this variable supports only odd number or 0 currently.
my $num = 11;
# Do you want to output as XHTML?
my $as_xhtml = 1;
# --- Plug-in package variables --------
my $xhtml = $as_xhtml ? ' /' : '';
my($currentpath, $num_files, $pages, $page, $from, $to);
# --------------------------------------
use CGI qw(Delete param url);
sub start {
Delete(-name => '-quiet');
$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) = @_;
my $url;
# prefix
$navi .= qq!<div class="paging">\n<p>\n!;
$navi .= qq!Page $page of $pages:\n!;
# link to the first page
if ($page > 2) {
$url = url(-absolute => 1, -path_info => 1);
$link_first = qq!<link rel="first" href="$url"$xhtml>!;
$navi .= qq!<a rel="first" href="$url" title="Go to first page">«</a>\n!;
}
# link to previous page
if ($page > 1) {
if ($page == 2) {
$url = url(-absolute => 1, -path_info => 1);
} else {
param('page' => $page - 1);
$url = url(-absolute => 1, -path_info => 1, -query => 1);
}
$link_prev = qq!<link rel="previous" href="$url"$xhtml>!;
$navi .= qq!<a rel="previous" href="$url" title="Go to previous page">‹</a>\n!;
$meta_robots = qq!<meta name="robots" content="noindex, nofollow"$xhtml>!;
}
# 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!<em>$i</em>\n!;
} else {
# other page indicator is link
if ($i == 1) {
$url = url(-absolute => 1, -path_info => 1);
} else {
param('page' => $i);
$url = url(-absolute => 1, -path_info => 1, -query => 1);
}
$navi .= qq!<a href="$url" title="Go to page $i">$i</a>\n!;
}
}
# link to next page
if ($page < $pages) {
param('page' => $page + 1);
$url = url(-absolute => 1, -path_info => 1, -query => 1);
$link_next = qq!<link rel="next" href="$url"$xhtml>!;
$navi .= qq!<a rel="next" href="$url" title="Go to next page">›</a>\n!;
}
# link to the last page
if ($page < $pages - 1) {
param('page' => $pages);
$url = url(-absolute=>1, -path_info => 1, -query => 1);
$link_last = qq!<link rel="last" href="$url"$xhtml>!;
$navi .= qq!<a rel="last" href="$url" title="Go to last page">»</a>\n!;
}
# postfix
$navi .= qq!</p>\n</div>!;
return 1;
}
1;
# vim:ft=perl