line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::TagHelpers::Pagination; |
2
|
1
|
|
|
1
|
|
1107
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
3
|
1
|
|
|
1
|
|
220
|
use Mojo::ByteStream 'b'; |
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
56
|
|
4
|
1
|
|
|
1
|
|
7
|
use Scalar::Util 'blessed'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
47
|
|
5
|
1
|
|
|
1
|
|
5
|
use POSIX 'ceil'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = 0.08; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @value_list = |
10
|
|
|
|
|
|
|
qw/prev |
11
|
|
|
|
|
|
|
next |
12
|
|
|
|
|
|
|
current_start |
13
|
|
|
|
|
|
|
current_end |
14
|
|
|
|
|
|
|
page_start |
15
|
|
|
|
|
|
|
page_end |
16
|
|
|
|
|
|
|
separator |
17
|
|
|
|
|
|
|
ellipsis |
18
|
|
|
|
|
|
|
placeholder/; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Register plugin |
21
|
|
|
|
|
|
|
sub register { |
22
|
3
|
|
|
3
|
1
|
5905
|
my ($plugin, $mojo, $param) = @_; |
23
|
|
|
|
|
|
|
|
24
|
3
|
|
50
|
|
|
19
|
$param ||= {}; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# Load parameter from Config file |
27
|
3
|
50
|
|
|
|
22
|
if (my $config_param = $mojo->config('TagHelpers-Pagination')) { |
28
|
0
|
|
|
|
|
0
|
$param = { %$param, %$config_param }; |
29
|
|
|
|
|
|
|
}; |
30
|
|
|
|
|
|
|
|
31
|
3
|
|
|
|
|
69
|
foreach (@value_list) { |
32
|
27
|
100
|
|
|
|
71
|
$plugin->{$_} = $param->{$_} if defined $param->{$_}; |
33
|
|
|
|
|
|
|
}; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Set 'current_start' and 'current_end' symbols, |
36
|
|
|
|
|
|
|
# if 'current' template is available. |
37
|
|
|
|
|
|
|
# Same for 'page'. |
38
|
3
|
|
|
|
|
10
|
foreach (qw/page current/) { |
39
|
6
|
100
|
|
|
|
20
|
if (defined $param->{$_}) { |
40
|
1
|
|
|
|
|
34
|
@{$plugin}{"${_}_start", "${_}_end"} = split("{$_}", $param->{$_}); |
|
1
|
|
|
|
|
5
|
|
41
|
1
|
|
50
|
|
|
7
|
$plugin->{"${_}_end"} ||= ''; |
42
|
|
|
|
|
|
|
}; |
43
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# Default current start and current end symbols |
46
|
3
|
|
|
|
|
10
|
for ($plugin) { |
47
|
3
|
|
100
|
|
|
26
|
$_->{current_start} //= '['; |
48
|
3
|
|
100
|
|
|
17
|
$_->{current_end} //= ']'; |
49
|
3
|
|
50
|
|
|
22
|
$_->{page_start} //= ''; |
50
|
3
|
|
50
|
|
|
24
|
$_->{page_end} //= ''; |
51
|
3
|
|
100
|
|
|
17
|
$_->{prev} //= '<'; |
52
|
3
|
|
100
|
|
|
18
|
$_->{next} //= '>'; |
53
|
3
|
|
100
|
|
|
16
|
$_->{separator} //= ' '; |
54
|
3
|
|
100
|
|
|
17
|
$_->{ellipsis} //= '...'; |
55
|
3
|
|
100
|
|
|
16
|
$_->{placeholder} //= 'page'; |
56
|
|
|
|
|
|
|
}; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Establish pagination helper |
59
|
|
|
|
|
|
|
$mojo->helper( |
60
|
|
|
|
|
|
|
pagination => sub { |
61
|
39
|
|
|
39
|
|
26831
|
return b( $plugin->pagination( @_ ) ); |
62
|
3
|
|
|
|
|
30
|
}); |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# Pagination helper |
67
|
|
|
|
|
|
|
sub pagination { |
68
|
39
|
|
|
39
|
1
|
85
|
my $self = shift; |
69
|
39
|
|
|
|
|
69
|
my $c = shift; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# $_[0] = current page |
72
|
|
|
|
|
|
|
# $_[1] = page count or -1 |
73
|
|
|
|
|
|
|
# $_[2] = template or Mojo::URL |
74
|
|
|
|
|
|
|
|
75
|
39
|
100
|
|
|
|
115
|
return '' unless $_[1]; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# No valid count given |
78
|
36
|
50
|
|
|
|
204
|
local $_[1] = !$_[1] ? 1 : ceil($_[1]); |
79
|
36
|
|
100
|
|
|
107
|
local $_[0] = $_[0] // 1; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# New parameter hash |
82
|
|
|
|
|
|
|
my %values = |
83
|
36
|
|
|
|
|
85
|
map { $_ => $self->{$_} } @value_list; |
|
324
|
|
|
|
|
808
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Overwrite plugin defaults |
86
|
36
|
100
|
66
|
|
|
160
|
if ($_[3] && ref $_[3] eq 'HASH') { |
87
|
11
|
|
|
|
|
21
|
my $overwrite = $_[3]; |
88
|
11
|
|
|
|
|
26
|
foreach (@value_list) { |
89
|
99
|
100
|
|
|
|
291
|
$values{$_} = $overwrite->{$_} if defined $overwrite->{$_}; |
90
|
|
|
|
|
|
|
}; |
91
|
|
|
|
|
|
|
|
92
|
11
|
|
|
|
|
26
|
foreach (qw/page current/) { |
93
|
22
|
100
|
|
|
|
65
|
if (defined $overwrite->{$_}) { |
94
|
10
|
|
|
|
|
188
|
@values{$_ . '_start', $_ . '_end'} = split("{$_}", $overwrite->{$_}); |
95
|
10
|
|
50
|
|
|
54
|
$values{$_ . '_end'} ||= ''; |
96
|
|
|
|
|
|
|
}; |
97
|
|
|
|
|
|
|
}; |
98
|
|
|
|
|
|
|
}; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# Establish string variables |
101
|
36
|
|
|
|
|
155
|
my ($p, $n, $cs, $ce, $ps, $pe, $s, $el, $ph) = @values{@value_list}; |
102
|
|
|
|
|
|
|
# prev next current_start current_end |
103
|
|
|
|
|
|
|
# page_start page_end separator ellipsis placeholder |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# Template |
106
|
36
|
|
|
|
|
108
|
my $t = $_[2]; |
107
|
36
|
100
|
66
|
|
|
145
|
if (blessed $t && blessed $t eq 'Mojo::URL') { |
108
|
1
|
|
|
|
|
6
|
$t = $t->to_string; |
109
|
1
|
|
|
|
|
857
|
$t =~ s/\%7[bB]$ph\%7[dD]/{$ph}/g; |
110
|
|
|
|
|
|
|
}; |
111
|
|
|
|
|
|
|
|
112
|
36
|
50
|
|
|
|
93
|
my $sub = sublink_gen($c, $t, $ps, $pe, $ph) or return ''; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# Pagination string |
115
|
36
|
|
|
|
|
71
|
my $e; |
116
|
36
|
|
|
|
|
60
|
my $counter = 1; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# More than seven pages |
119
|
36
|
100
|
100
|
|
|
240
|
if ($_[1] >= 7 || |
|
|
|
100
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# Or the number of pages is unknown |
122
|
|
|
|
|
|
|
($_[1] == -1 && $_[0] > 4)){ |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# < [1] #2 #3 |
125
|
|
|
|
|
|
|
# The current page is 1 |
126
|
15
|
100
|
|
|
|
54
|
if ($_[0] == 1){ |
|
|
100
|
|
|
|
|
|
127
|
3
|
|
|
|
|
92
|
$e .= $sub->(undef, [$p, 'prev']) . $s . |
128
|
|
|
|
|
|
|
$sub->(undef, [$cs . 1 . $ce, 'self']) . $s . |
129
|
|
|
|
|
|
|
$sub->('2') . $s . |
130
|
|
|
|
|
|
|
$sub->('3') . $s; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# < #1 #2 #3 |
134
|
|
|
|
|
|
|
# The current page is 0 |
135
|
|
|
|
|
|
|
elsif ($_[0] == 0) { |
136
|
1
|
|
|
|
|
31
|
$e .= $sub->(undef, [$p, 'prev']) . $s; |
137
|
1
|
|
|
|
|
31
|
$e .= $sub->($_) . $s foreach (1 .. 3); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# #< #1 |
141
|
|
|
|
|
|
|
# The current page is anywhere |
142
|
|
|
|
|
|
|
else { |
143
|
11
|
|
|
|
|
403
|
$e .= $sub->(($_[0] - 1), [$p, 'prev']) . $s . |
144
|
|
|
|
|
|
|
$sub->('1') . $s; |
145
|
|
|
|
|
|
|
}; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# [2] #3 |
148
|
|
|
|
|
|
|
# The current page is 2 |
149
|
15
|
50
|
|
|
|
71
|
if ($_[0] == 2) { |
|
|
100
|
|
|
|
|
|
150
|
0
|
|
|
|
|
0
|
$e .= $sub->(undef, [$cs . 2 . $ce, 'self']) . $s . |
151
|
|
|
|
|
|
|
$sub->('3') . $s; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# ... |
155
|
|
|
|
|
|
|
# The current page is beyond 3 |
156
|
|
|
|
|
|
|
elsif ($_[0] > 3) { |
157
|
10
|
|
|
|
|
37
|
$e .= $el . $s; |
158
|
|
|
|
|
|
|
}; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# #x-1 [x] #x+1 |
161
|
|
|
|
|
|
|
# The current page is beyond 2 and there are at least 2 pages to go |
162
|
15
|
100
|
100
|
|
|
69
|
if (($_[0] >= 3) && ($_[0] <= ($_[1] - 2))) { |
163
|
6
|
|
|
|
|
173
|
$e .= $sub->($_[0] - 1) . $s . |
164
|
|
|
|
|
|
|
$sub->(undef, [$cs .$_[0] . $ce, 'self']) . $s . |
165
|
|
|
|
|
|
|
$sub->($_[0] + 1) . $s; |
166
|
|
|
|
|
|
|
}; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# ... |
169
|
|
|
|
|
|
|
# There are at least 2 pages following the current page |
170
|
15
|
100
|
|
|
|
52
|
if ($_[0] < ($_[1] - 2)){ |
171
|
10
|
|
|
|
|
25
|
$e .= $el . $s; |
172
|
|
|
|
|
|
|
}; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# The current page is prefinal |
175
|
15
|
100
|
|
|
|
69
|
if ($_[0] == ($_[1] - 1)){ |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
176
|
1
|
|
|
|
|
30
|
$e .= $sub->($_[1] - 2) . $s . |
177
|
|
|
|
|
|
|
$sub->(undef, [$cs . $_[0] . $ce, 'self']) . $s . |
178
|
|
|
|
|
|
|
$sub->($_[1]) . $s . |
179
|
|
|
|
|
|
|
$sub->($_[1], [$n, 'next']); |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
# The current page is final |
183
|
|
|
|
|
|
|
elsif ($_[0] == $_[1]) { |
184
|
0
|
|
|
|
|
0
|
$e .= $sub->($_[0] - 1) . $s . |
185
|
|
|
|
|
|
|
$sub->(undef, [$cs . $_[0] . $ce, 'self']) . $s . |
186
|
|
|
|
|
|
|
$sub->(undef, [$n, 'next']); |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
# Number is unknown |
190
|
|
|
|
|
|
|
elsif ($_[1] == -1) { |
191
|
4
|
|
|
|
|
119
|
$e .= $sub->($_[0] - 1) . $s . |
192
|
|
|
|
|
|
|
$sub->(undef, [$cs . $_[0] . $ce, 'self']) . $s . |
193
|
|
|
|
|
|
|
$el . $s . |
194
|
|
|
|
|
|
|
$sub->($_[0] + 1, [$n, 'next']); |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
# Number is anywhere in between |
198
|
|
|
|
|
|
|
else { |
199
|
10
|
|
|
|
|
292
|
$e .= $sub->($_[1]) . $s . |
200
|
|
|
|
|
|
|
$sub->(($_[0] + 1), [$n, 'next']); |
201
|
|
|
|
|
|
|
}; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
# Counter < 7 |
205
|
|
|
|
|
|
|
else { |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
# Previous |
208
|
21
|
100
|
|
|
|
62
|
if ($_[0] > 1){ |
209
|
8
|
|
|
|
|
245
|
$e .= $sub->(($_[0] - 1), [$p, 'prev']) . $s; |
210
|
|
|
|
|
|
|
} else { |
211
|
13
|
|
|
|
|
390
|
$e .= $sub->(undef, [$p, 'prev']) . $s; |
212
|
|
|
|
|
|
|
}; |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
# All numbers in between |
215
|
21
|
|
100
|
|
|
111
|
while ($counter <= $_[1] || ($_[1] == -1 && $counter <= $_[0])){ |
|
|
|
100
|
|
|
|
|
216
|
46
|
100
|
|
|
|
104
|
if ($_[0] != $counter) { |
217
|
33
|
|
|
|
|
869
|
$e .= $sub->($counter) . $s; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
# Current |
221
|
|
|
|
|
|
|
else { |
222
|
13
|
|
|
|
|
365
|
$e .= $sub->(undef, [$cs . $counter . $ce, 'self']) . $s; |
223
|
|
|
|
|
|
|
}; |
224
|
|
|
|
|
|
|
|
225
|
46
|
|
|
|
|
193
|
$counter++; |
226
|
|
|
|
|
|
|
}; |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
# Ellipsis in case the number is not known |
229
|
21
|
100
|
|
|
|
53
|
$e .= $el . $s if $_[1] == -1; |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
# Next |
232
|
21
|
100
|
|
|
|
53
|
if ($_[0] != $_[1]){ |
233
|
17
|
|
|
|
|
475
|
$e .= $sub->(($_[0] + 1), [$n, 'next']); |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
else { |
237
|
4
|
|
|
|
|
111
|
$e .= $sub->(undef, [$n, 'next']); |
238
|
|
|
|
|
|
|
}; |
239
|
|
|
|
|
|
|
}; |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
# Pagination string |
242
|
36
|
|
|
|
|
937
|
$e; |
243
|
|
|
|
|
|
|
}; |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
# Sublink function generator |
247
|
|
|
|
|
|
|
sub sublink_gen { |
248
|
36
|
|
|
36
|
0
|
67
|
my $c = shift; |
249
|
36
|
|
|
|
|
91
|
my ($url, $ps, $pe, $ph) = @_; |
250
|
|
|
|
|
|
|
|
251
|
36
|
|
|
|
|
73
|
my $s = 'sub{'; |
252
|
|
|
|
|
|
|
# $_[0] = number |
253
|
|
|
|
|
|
|
# $_[1] = number_shown |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
# Url is template |
256
|
36
|
100
|
66
|
|
|
136
|
if ($url && length($url) > 0) { |
257
|
19
|
|
|
|
|
52
|
$s .= 'my $url=' . _quote($url) . ';'; |
258
|
19
|
|
|
|
|
56
|
$s .= 'if($_[0]){$url=~s/\{' . $ph . '\}/$_[0]/g}else{$url=undef};'; |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
# No template given |
262
|
|
|
|
|
|
|
else { |
263
|
17
|
|
|
|
|
53
|
$s .= 'my $url = $_[0];'; |
264
|
|
|
|
|
|
|
}; |
265
|
|
|
|
|
|
|
|
266
|
36
|
|
|
|
|
84
|
$s .= q!my$n=$_[1]||! . _quote($ps) . '.$_[0].' . _quote($pe) . ';' . |
267
|
|
|
|
|
|
|
q{my $rel='';} . |
268
|
|
|
|
|
|
|
q{if(ref $n){$rel=' rel="'.$n->[1].'"';$n=$n->[0]};} . |
269
|
|
|
|
|
|
|
q!if($url){$url=~s/&/&/g;! . |
270
|
|
|
|
|
|
|
q{$url=~s/</g;} . |
271
|
|
|
|
|
|
|
q{$url=~s/>/>/g;} . |
272
|
|
|
|
|
|
|
q{$url=~s/"/"/g;} . |
273
|
|
|
|
|
|
|
q!$url=~s/'/'/g;};!; |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
# Create sublink |
276
|
36
|
|
|
|
|
86
|
$s .= q!return '' . $n . '';}!; |
277
|
|
|
|
|
|
|
|
278
|
36
|
|
|
|
|
11320
|
my $x = eval $s; |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
# Log evaluation error and return |
281
|
36
|
50
|
0
|
|
|
222
|
$c->app->log->warn($@) and return if $@; |
282
|
|
|
|
|
|
|
|
283
|
36
|
|
|
|
|
140
|
$x; |
284
|
|
|
|
|
|
|
}; |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
# Quote with a single quote |
288
|
|
|
|
|
|
|
sub _quote { |
289
|
91
|
|
|
91
|
|
167
|
my $str = shift; |
290
|
91
|
|
|
|
|
196
|
$str =~ s/(['\\])/\\$1/g; |
291
|
91
|
|
|
|
|
297
|
return qq{'$str'}; |
292
|
|
|
|
|
|
|
}; |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
1; |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
__END__ |