| 1 | package Statocles::Page::List; | ||||||||||||||||||||||||||||
| 2 | 16 | our $VERSION = '0.099'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: A page presenting a list of other pages | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 16 | ▶ | use Statocles::Base 'Class'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 16 | with 'Statocles::Page'; | |||||||||||||||||||||||||||
| 7 | 16 | ▶ | use List::Util qw( reduce ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 16 | ▶ | use Statocles::Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 16 | ▶ | use Statocles::Page::ListItem; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 16 | ▶ | use Statocles::Util qw( uniq_by ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | #pod =attr pages | ||||||||||||||||||||||||||||
| 13 | #pod | ||||||||||||||||||||||||||||
| 14 | #pod The pages that should be shown in this list. | ||||||||||||||||||||||||||||
| 15 | #pod | ||||||||||||||||||||||||||||
| 16 | #pod =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 16 | has _pages => ( | |||||||||||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||||||||||
| 20 | isa => ArrayRef[ConsumerOf['Statocles::Page']], | ||||||||||||||||||||||||||||
| 21 | init_arg => 'pages', | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 7 | ▶ | sub pages { | ||||||||||||||||||||||||||
|
Subroutine
pages: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 7 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 7 | my %rewrite; | |||||||||||||||||||||||||||
| 28 | 7 | ▶ | if ( $self->type eq 'application/rss+xml' || $self->type eq 'application/atom+xml' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->type eq "application/rss+xml" or $self->type eq "application/atom+xml"
MC/DC50%1/2
$self->type eq "application/rss+xml" or $self->type eq "application/atom+xml"
$self->type eq "application/rss+xml"
$self->type eq "application/atom+xml"
Truth table66%2/3
A or B
A= $self->type eq "application/rss+xml"B= $self->type eq "application/atom+xml"
| |||||||||||||||||||||||||||||
| 29 | 1 | %rewrite = ( rewrite_mode => 'full' ); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 7 | my @pages; | |||||||||||||||||||||||||||
| 33 | 7 | for my $page ( @{ $self->_pages } ) { | |||||||||||||||||||||||||||
| 34 | # Always re-wrap the page, even if it's already wrapped, | ||||||||||||||||||||||||||||
| 35 | # to change the rewrite_mode | ||||||||||||||||||||||||||||
| 36 | 18 | ▶ | push @pages, Statocles::Page::ListItem->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | %rewrite, | ||||||||||||||||||||||||||||
| 38 | page => $page->isa( 'Statocles::Page::ListItem' ) ? $page->page : $page, | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 7 | return \@pages; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | #pod =attr next | ||||||||||||||||||||||||||||
| 46 | #pod | ||||||||||||||||||||||||||||
| 47 | #pod The path to the next page in the pagination series. | ||||||||||||||||||||||||||||
| 48 | #pod | ||||||||||||||||||||||||||||
| 49 | #pod =attr prev | ||||||||||||||||||||||||||||
| 50 | #pod | ||||||||||||||||||||||||||||
| 51 | #pod The path to the previous page in the pagination series. | ||||||||||||||||||||||||||||
| 52 | #pod | ||||||||||||||||||||||||||||
| 53 | #pod =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 16 | has [qw( next prev )] => ( | |||||||||||||||||||||||||||
| 56 | is => 'rw', | ||||||||||||||||||||||||||||
| 57 | isa => PagePath, | ||||||||||||||||||||||||||||
| 58 | coerce => PagePath->coercion, | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | #pod =attr date | ||||||||||||||||||||||||||||
| 62 | #pod | ||||||||||||||||||||||||||||
| 63 | #pod Get the date of this list. By default, this is the latest date of the first | ||||||||||||||||||||||||||||
| 64 | #pod page in the list of pages. | ||||||||||||||||||||||||||||
| 65 | #pod | ||||||||||||||||||||||||||||
| 66 | #pod =cut | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | has '+date' => ( | ||||||||||||||||||||||||||||
| 69 | lazy => 1, | ||||||||||||||||||||||||||||
| 70 | 3 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | 3 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 72 | 4 | ▶ | my $date = reduce { $a->epoch gt $b->epoch ? $a : $b } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 7 | map { $_->date } | |||||||||||||||||||||||||||
| 74 | 3 | @{ $self->pages }; | |||||||||||||||||||||||||||
| 75 | 3 | return $date; | |||||||||||||||||||||||||||
| 76 | }, | ||||||||||||||||||||||||||||
| 77 | 16 | ); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | #pod =attr search_change_frequency | ||||||||||||||||||||||||||||
| 80 | #pod | ||||||||||||||||||||||||||||
| 81 | #pod Override the default L<search_change_frequency|Statocles::Page/search_change_frequency> | ||||||||||||||||||||||||||||
| 82 | #pod to C<daily>, because these pages aggregate other pages. | ||||||||||||||||||||||||||||
| 83 | #pod | ||||||||||||||||||||||||||||
| 84 | #pod =cut | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | has '+search_change_frequency' => ( | ||||||||||||||||||||||||||||
| 87 | 31 | ▶ | default => sub { 'daily' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | 16 | ); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | #pod =attr search_priority | ||||||||||||||||||||||||||||
| 91 | #pod | ||||||||||||||||||||||||||||
| 92 | #pod Override the default L<search_priority|Statocles::Page/search_priority> to reduce | ||||||||||||||||||||||||||||
| 93 | #pod the rank of list pages to C<0.3>. | ||||||||||||||||||||||||||||
| 94 | #pod | ||||||||||||||||||||||||||||
| 95 | #pod It is more important for users to get to the full page than | ||||||||||||||||||||||||||||
| 96 | #pod to get to this list page, which may contain truncated content, and whose relevant | ||||||||||||||||||||||||||||
| 97 | #pod content may appear 3-4 items down the page. | ||||||||||||||||||||||||||||
| 98 | #pod | ||||||||||||||||||||||||||||
| 99 | #pod =cut | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | has '+search_priority' => ( | ||||||||||||||||||||||||||||
| 102 | 31 | ▶ | default => sub { 0.3 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | 16 | ); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | #pod =method paginate | ||||||||||||||||||||||||||||
| 106 | #pod | ||||||||||||||||||||||||||||
| 107 | #pod my @pages = Statocles::Page::List->paginate( %args ); | ||||||||||||||||||||||||||||
| 108 | #pod | ||||||||||||||||||||||||||||
| 109 | #pod Build a paginated list of L<Statocles::Page::List> objects. | ||||||||||||||||||||||||||||
| 110 | #pod | ||||||||||||||||||||||||||||
| 111 | #pod Takes a list of key-value pairs with the following keys: | ||||||||||||||||||||||||||||
| 112 | #pod | ||||||||||||||||||||||||||||
| 113 | #pod path - An sprintf format string to build the path, like '/page-%i.html'. | ||||||||||||||||||||||||||||
| 114 | #pod Pages are indexed started at 1. | ||||||||||||||||||||||||||||
| 115 | #pod index - The special, unique path for the first page. Optional. | ||||||||||||||||||||||||||||
| 116 | #pod pages - The arrayref of Statocles::Page::Document objects to paginate. | ||||||||||||||||||||||||||||
| 117 | #pod after - The number of items per page. Defaults to 5. | ||||||||||||||||||||||||||||
| 118 | #pod | ||||||||||||||||||||||||||||
| 119 | #pod Return a list of Statocles::Page::List objects in numerical order, the index | ||||||||||||||||||||||||||||
| 120 | #pod page first (if any). | ||||||||||||||||||||||||||||
| 121 | #pod | ||||||||||||||||||||||||||||
| 122 | #pod =cut | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 6 | ▶ | sub paginate { | ||||||||||||||||||||||||||
|
Subroutine
paginate: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 6 | my ( $class, %args ) = @_; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Unpack the args so we can pass the rest to new() | ||||||||||||||||||||||||||||
| 128 | 6 | ▶ | my $after = delete $args{after} // 5; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $args{"after"} // 5
MC/DC0%0/1
delete $args{"after"} // 5
delete $args{"after"}
Truth table50%1/2
A
A= delete $args{"after"}
| |||||||||||||||||||||||||||||
| 129 | 6 | my $pages = delete $args{pages}; | |||||||||||||||||||||||||||
| 130 | 6 | my $path_format = delete $args{path}; | |||||||||||||||||||||||||||
| 131 | 6 | my $index = delete $args{index}; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # The date is the max of all input pages, since input pages get moved between | ||||||||||||||||||||||||||||
| 134 | # all the list pages | ||||||||||||||||||||||||||||
| 135 | 12 | ▶ | my $date = reduce { $a->epoch gt $b->epoch ? $a : $b } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 6 | map { $_->date } | |||||||||||||||||||||||||||
| 137 | @$pages; | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 6 | my @sets; | |||||||||||||||||||||||||||
| 140 | 6 | for my $i ( 0..$#{$pages} ) { | |||||||||||||||||||||||||||
| 141 | 18 | push @{ $sets[ int( $i / $after ) ] }, $pages->[ $i ]; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 6 | my @retval; | |||||||||||||||||||||||||||
| 145 | 6 | for my $i ( 0..$#sets ) { | |||||||||||||||||||||||||||
| 146 | 12 | ▶ | my $path = $index && $i == 0 ? $index : sprintf( $path_format, $i + 1 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$index and $i == 0
MC/DC100%2/2
$index and $i == 0
$index
$i == 0
Truth table100%3/3
A and B
A= $indexB= $i == 0
| |||||||||||||||||||||||||||||
| 147 | 12 | ▶ | my $prev = $index && $i == 1 ? $index : sprintf( $path_format, $i ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$index and $i == 1
MC/DC100%2/2
$index and $i == 1
$index
$i == 1
Truth table100%3/3
A and B
A= $indexB= $i == 1
| |||||||||||||||||||||||||||||
| 148 | 12 | ▶ | my $next = $i != $#sets ? sprintf( $path_format, $i + 2 ) : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # Remove index.html from link URLs | ||||||||||||||||||||||||||||
| 151 | 12 | s{/index[.]html$}{/} for ( $prev, $next ); | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 12 | ▶ | push @retval, $class->new( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 154 | path => $path, | ||||||||||||||||||||||||||||
| 155 | pages => $sets[$i], | ||||||||||||||||||||||||||||
| 156 | ( $next ? ( next => $next ) : () ), | ||||||||||||||||||||||||||||
| 157 | ( $i > 0 ? ( prev => $prev ) : () ), | ||||||||||||||||||||||||||||
| 158 | date => $date, | ||||||||||||||||||||||||||||
| 159 | %args, | ||||||||||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 6 | return @retval; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | #pod =method vars | ||||||||||||||||||||||||||||
| 167 | #pod | ||||||||||||||||||||||||||||
| 168 | #pod my %vars = $page->vars; | ||||||||||||||||||||||||||||
| 169 | #pod | ||||||||||||||||||||||||||||
| 170 | #pod Get the template variables for this page. | ||||||||||||||||||||||||||||
| 171 | #pod | ||||||||||||||||||||||||||||
| 172 | #pod =cut | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 1 | ▶ | around vars => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 175 | 1 | my ( $orig, $self ) = @_; | |||||||||||||||||||||||||||
| 176 | return ( | ||||||||||||||||||||||||||||
| 177 | 1 | $self->$orig, | |||||||||||||||||||||||||||
| 178 | pages => $self->pages, | ||||||||||||||||||||||||||||
| 179 | ); | ||||||||||||||||||||||||||||
| 180 | 16 | }; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | #pod =method links | ||||||||||||||||||||||||||||
| 183 | #pod | ||||||||||||||||||||||||||||
| 184 | #pod my @links = $page->links( $key ); | ||||||||||||||||||||||||||||
| 185 | #pod | ||||||||||||||||||||||||||||
| 186 | #pod Get the given set of links for this page. See L<the links | ||||||||||||||||||||||||||||
| 187 | #pod attribute|Statocles::Page/links> for some commonly-used keys. | ||||||||||||||||||||||||||||
| 188 | #pod | ||||||||||||||||||||||||||||
| 189 | #pod For List pages, C<stylesheet> and C<script> links are also collected | ||||||||||||||||||||||||||||
| 190 | #pod from the L<inner pages|/pages>, to ensure that content in those pages | ||||||||||||||||||||||||||||
| 191 | #pod works correctly. | ||||||||||||||||||||||||||||
| 192 | #pod | ||||||||||||||||||||||||||||
| 193 | #pod =cut | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 14 | ▶ | around links => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 196 | 14 | my ( $orig, $self, @args ) = @_; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 14 | ▶ | if ( @args > 1 || $args[0] !~ /^(?:stylesheet|script)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@args > 1 or not $args[0] =~ /^(?:stylesheet|script)$/
MC/DC100%2/2
@args > 1 or not $args[0] =~ /^(?:stylesheet|script)$/
@args > 1
not $args[0] =~ /^(?:stylesheet|script)$/
Truth table100%3/3
A or not(B)
A= @args > 1B= not $args[0] =~ /^(?:stylesheet|script)$/
| |||||||||||||||||||||||||||||
| 199 | 12 | return $self->$orig( @args ); | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 2 | my @links; | |||||||||||||||||||||||||||
| 203 | 2 | for my $page ( @{ $self->pages } ) { | |||||||||||||||||||||||||||
| 204 | 6 | push @links, $page->links( @args ); | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | 2 | push @links, $self->$orig( @args ); | |||||||||||||||||||||||||||
| 207 | 2 | ▶ | return uniq_by { $_->href } @links; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 208 | 16 | }; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 16 | 1; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | __END__ | ||||||||||||||||||||||||||||