File Coverage

blib/lib/Data/Pageset/Render.pm
Criterion Covered Total %
statement 58 64 90.6
branch 17 24 70.8
condition 6 12 50.0
subroutine 9 11 81.8
pod 4 4 100.0
total 94 115 81.7


line stmt bran cond sub pod time code
1             package Data::Pageset::Render;
2              
3             ###########################################################################
4             # Data::Pageset::Render
5             # Mark Grimes
6             # $Id: Render.pm 464 2009-06-05 19:49:45Z mgrimes $
7             #
8             # Extension to the Data::Pageset module that simplifies the creation of HTML
9             # page navigation
10             # Copyright (c) 2008 Mark Grimes (mgrimes@cpan.org).
11             # All rights reserved. This program is free software; you can redistribute
12             # it and/or modify it under the same terms as Perl itself.
13             #
14             # Thanks!
15             #
16             ###########################################################################
17              
18 2     2   83901 use strict;
  2         3  
  2         90  
19 2     2   11 use warnings;
  2         3  
  2         68  
20              
21 2     2   11 use base 'Data::Pageset';
  2         21  
  2         1865  
22             our $VERSION = '0.14';
23              
24             sub new {
25 3     3 1 44 my ( $class, $cfg ) = @_;
26              
27 3         25 my $self = shift->SUPER::new(@_);
28              
29 3         671 $self->{link_format} = $cfg->{link_format};
30 3   33     20 $self->{current_page_format} = $cfg->{current_page_format}
31             || _strip_link_from_format( $cfg->{link_format} );
32              
33 3         9 return $self;
34             }
35              
36             sub html {
37 12     12 1 1314 my $self = shift;
38 12   66     50 my $frmt = shift || $self->{link_format};
39 12   33     52 my $frmt_for_current =
40             shift
41             || $self->{current_page_format}
42             || _strip_link_from_format($frmt);
43              
44 12         13 my $txt;
45              
46 12 100       28 if ( $self->current_page > 1 ) {
47 7         265 $txt .= _sprintf( $frmt, $self->current_page - 1, '<<' );
48             }
49 12 100       217 if ( $self->previous_set ) {
50 8 100       52 if ( $self->previous_set < _min( @{ $self->pages_in_set } ) ) {
  8         37  
51 6         9 my $chunk = $frmt;
52 6         11 $txt .= _sprintf( $frmt, 1 );
53 6         19 $txt .= _sprintf( $frmt, $self->previous_set, '...' );
54             }
55             }
56 12         32 for my $num ( @{ $self->pages_in_set } ) {
  12         40  
57 70 100       183 if ( $num == $self->current_page ) {
58 12         427 $txt .= _sprintf( $frmt_for_current, $num );
59             } else {
60 58         1993 $txt .= _sprintf( $frmt, $num );
61             }
62             }
63 12 100       554 if ( $self->next_set ) {
64 10 100       64 if ( $self->next_set > _max( @{ $self->pages_in_set } ) ) {
  10         46  
65 8         22 $txt .= _sprintf( $frmt, $self->next_set, '...' );
66 8         20 $txt .= _sprintf( $frmt, $self->last_page );
67             }
68             }
69 12 100       42 if ( $self->current_page < $self->last_page ) {
70 10         512 $txt .= _sprintf( $frmt, $self->current_page + 1, '>>' );
71             }
72              
73 12         148 return $txt;
74             }
75              
76             sub link_format {
77 0     0 1 0 my ( $self, $frmt ) = @_;
78              
79 0 0       0 $self->{link_format} = $frmt if defined $frmt;
80 0         0 return $self->{link_format};
81             }
82              
83             sub current_page_format {
84 0     0 1 0 my ( $self, $frmt ) = @_;
85              
86 0 0       0 $self->{current_page_format} = $frmt if defined $frmt;
87 0         0 return $self->{current_page_format};
88             }
89              
90             sub _strip_link_from_format {
91 3     3   6 my $frmt = shift;
92              
93 3 50       8 return unless defined $frmt;
94              
95 3         9 $frmt =~ s{]*?>}{}; # strip the first link
96 3         5 $frmt =~ s{(.*)]*?>}{$1}; # strip the last link
97              
98 3         17 return $frmt;
99             }
100              
101             sub _sprintf {
102 115     115   873 my $frmt = shift;
103 115         120 my $p = shift;
104 115   66     310 my $l = shift || $p; # default for $a is current page num
105              
106 115         252 $frmt =~ s{ \%p }{$p}gx; # substitute the page number for %p
107 115         239 $frmt =~ s{ \%a }{$l}gx; # substitute the text for %a
108              
109 115         270 return $frmt;
110             }
111              
112             sub _min {
113 8     8   106 my ( $min, @list ) = @_;
114 8         27 for (@list) {
115 32 50       65 $min = $_ if $_ < $min;
116             }
117 8         22 return $min;
118             }
119              
120             sub _max {
121 10     10   50 my ( $max, @list ) = @_;
122 10         14 for (@list) {
123 40 50       77 $max = $_ if $_ > $max;
124             }
125 10         27 return $max;
126             }
127              
128             1;
129              
130             __END__