| 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 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use base 'Data::Pageset'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 22 | 2 | our $VERSION = '0.14'; | |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 25 | 3 | my ( $class, $cfg ) = @_; | |||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | 3 | my $self = shift->SUPER::new(@_); | |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | 3 | $self->{link_format} = $cfg->{link_format}; | |||||||||||||||||||||||||||||||||
| 30 | $self->{current_page_format} = $cfg->{current_page_format} | ||||||||||||||||||||||||||||||||||
| 31 | 3 | ▶ | || _strip_link_from_format( $cfg->{link_format} ); | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$cfg->{'current_page_format'} || _strip_link_from_format($cfg->{'link_format'})
MC/DC0%0/2
$cfg->{'current_page_format'} || _strip_link_from_format($cfg->{'link_format'})
$cfg->{'current_page_format'}
_strip_link_from_format($cfg->{'link_format'})
Truth table33%1/3
A || B
A= $cfg->{'current_page_format'}B= _strip_link_from_format($cfg->{'link_format'})
| |||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | 3 | return $self; | |||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | 12 | ▶ | sub html { | ||||||||||||||||||||||||||||||||
|
Subroutine
html: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 37 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 38 | 12 | ▶ | my $frmt = shift || $self->{link_format}; | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
shift() || $self->{'link_format'}
MC/DC0%0/2
shift() || $self->{'link_format'}
shift()
$self->{'link_format'}
Truth table66%2/3
A || B
A= shift()B= $self->{'link_format'}
| |||||||||||||||||||||||||||||||||||
| 39 | my $frmt_for_current = | ||||||||||||||||||||||||||||||||||
| 40 | shift | ||||||||||||||||||||||||||||||||||
| 41 | || $self->{current_page_format} | ||||||||||||||||||||||||||||||||||
| 42 | 12 | ▶ | || _strip_link_from_format($frmt); | ||||||||||||||||||||||||||||||||
|
Condition50%3/6
shift() || $self->{'current_page_format'} || _strip_link_from_format($frmt)
shift() or $self->{"current_page_format"}
MC/DC0%0/3
shift() || $self->{'current_page_format'} || _strip_link_from_format($frmt)
shift()
$self->{"current_page_format"}
_strip_link_from_format($frmt)
Truth table50%2/4
A or B || C
A= shift()B= $self->{"current_page_format"}C= _strip_link_from_format($frmt)
| |||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | 12 | my $txt; | |||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | 12 | ▶ | if ( $self->current_page > 1 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 47 | 7 | $txt .= _sprintf( $frmt, $self->current_page - 1, '<<' ); | |||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||
| 49 | 12 | ▶ | if ( $self->previous_set ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 50 | 8 | ▶ | if ( $self->previous_set < _min( @{ $self->pages_in_set } ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 51 | 6 | my $chunk = $frmt; | |||||||||||||||||||||||||||||||||
| 52 | 6 | $txt .= _sprintf( $frmt, 1 ); | |||||||||||||||||||||||||||||||||
| 53 | 6 | $txt .= _sprintf( $frmt, $self->previous_set, '...' ); | |||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||
| 56 | 12 | for my $num ( @{ $self->pages_in_set } ) { | |||||||||||||||||||||||||||||||||
| 57 | 70 | ▶ | if ( $num == $self->current_page ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 58 | 12 | $txt .= _sprintf( $frmt_for_current, $num ); | |||||||||||||||||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||||||||||||||||
| 60 | 58 | $txt .= _sprintf( $frmt, $num ); | |||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||
| 63 | 12 | ▶ | if ( $self->next_set ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 64 | 10 | ▶ | if ( $self->next_set > _max( @{ $self->pages_in_set } ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 65 | 8 | $txt .= _sprintf( $frmt, $self->next_set, '...' ); | |||||||||||||||||||||||||||||||||
| 66 | 8 | $txt .= _sprintf( $frmt, $self->last_page ); | |||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||
| 69 | 12 | ▶ | if ( $self->current_page < $self->last_page ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 70 | 10 | $txt .= _sprintf( $frmt, $self->current_page + 1, '>>' ); | |||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | 12 | return $txt; | |||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub link_format { | ||||||||||||||||||||||||||||||||
|
Subroutine
link_format: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 77 | 0 | my ( $self, $frmt ) = @_; | |||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $self->{link_format} = $frmt if defined $frmt; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 80 | 0 | return $self->{link_format}; | |||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub current_page_format { | ||||||||||||||||||||||||||||||||
|
Subroutine
current_page_format: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 84 | 0 | my ( $self, $frmt ) = @_; | |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | $self->{current_page_format} = $frmt if defined $frmt; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 87 | 0 | return $self->{current_page_format}; | |||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | 3 | ▶ | sub _strip_link_from_format { | ||||||||||||||||||||||||||||||||
|
Subroutine
_strip_link_from_format: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||
| 91 | 3 | my $frmt = shift; | |||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | 3 | ▶ | return unless defined $frmt; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | 3 | $frmt =~ s{<a[^>]*?>}{}; # strip the first <a> link | |||||||||||||||||||||||||||||||||
| 96 | 3 | $frmt =~ s{(.*)</a[^>]*?>}{$1}; # strip the last </a> link | |||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | 3 | return $frmt; | |||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | 115 | ▶ | sub _sprintf { | ||||||||||||||||||||||||||||||||
|
Subroutine
_sprintf: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||
| 102 | 115 | my $frmt = shift; | |||||||||||||||||||||||||||||||||
| 103 | 115 | my $p = shift; | |||||||||||||||||||||||||||||||||
| 104 | 115 | ▶ | my $l = shift || $p; # default for $a is current page num | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
shift() || $p
MC/DC0%0/2
shift() || $p
shift()
$p
Truth table66%2/3
A || B
A= shift()B= $p
| |||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||
| 106 | 115 | $frmt =~ s{ \%p }{$p}gx; # substitute the page number for %p | |||||||||||||||||||||||||||||||||
| 107 | 115 | $frmt =~ s{ \%a }{$l}gx; # substitute the text for %a | |||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | 115 | return $frmt; | |||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | 8 | ▶ | sub _min { | ||||||||||||||||||||||||||||||||
|
Subroutine
_min: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||
| 113 | 8 | my ( $min, @list ) = @_; | |||||||||||||||||||||||||||||||||
| 114 | 8 | for (@list) { | |||||||||||||||||||||||||||||||||
| 115 | 32 | ▶ | $min = $_ if $_ < $min; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||
| 117 | 8 | return $min; | |||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | 10 | ▶ | sub _max { | ||||||||||||||||||||||||||||||||
|
Subroutine
_max: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||
| 121 | 10 | my ( $max, @list ) = @_; | |||||||||||||||||||||||||||||||||
| 122 | 10 | for (@list) { | |||||||||||||||||||||||||||||||||
| 123 | 40 | ▶ | $max = $_ if $_ > $max; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||
| 125 | 10 | return $max; | |||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||
| 128 | 2 | 1; | |||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||
| 130 | __END__ | ||||||||||||||||||||||||||||||||||