blib/lib/Statocles/App/Blog.pm

filter: Statementstmt 48.1% 103 / 214 Branchbran 33.3% 18 / 54 Conditioncond 38.8% 7 / 18 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 80.9% 17 / 21 Podpod 100% 8 / 8 totaltotal 47.5% 155 / 326 CC 49 SCAR 37.9
CC
49
Cov
45%
pages
113.3
index
90.0
recent_posts
72.0
CRAP 44.5
« blib/lib/Statocles/Test.pm ↑ Summary blib/lib/Statocles/App/Basic.pm »
1 package Statocles::App::Blog;
2 14 our $VERSION = '0.099';
3 # ABSTRACT: A blog application
4  
5 14 ▶use Text::Unidecode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use Statocles::Base 'Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use Getopt::Long qw( GetOptionsFromArray );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Statocles::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Statocles::Page::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Statocles::Page::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Statocles::Util qw( run_editor read_stdin );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 with 'Statocles::App';
13  
14 #pod =attr tag_text
15 #pod
16 #pod # site.yml
17 #pod blog:
18 #pod class: Statocles::App::Blog
19 #pod args:
20 #pod tag_text:
21 #pod software: Posts about software and development
22 #pod travel: My travelogue around the world!
23 #pod
24 #pod A hash of tag and introductory Markdown that will be shown on the tag's main
25 #pod page. Having a description is optional.
26 #pod
27 #pod Using L<Beam::Wire's $config directive|Beam::Wire/Config Services>, you can
28 #pod put the tag text in an external file:
29 #pod
30 #pod # site.yml
31 #pod blog:
32 #pod class: Statocles::App::Blog
33 #pod args:
34 #pod tag_text:
35 #pod $config: tags.yml
36 #pod
37 #pod # tags.yml
38 #pod software: |-
39 #pod # Software
40 #pod
41 #pod Posts about software development, mostly in [Perl](http://perl.org)
42 #pod
43 #pod travel: |-
44 #pod # Travel
45 #pod
46 #pod My travelogue around the world! [Also visit my Instagram!](http://example.com)
47 #pod
48 #pod =cut
49  
50 has tag_text => (
51 is => 'ro',
52 isa => HashRef,
53 22 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 14 );
55  
56 #pod =attr page_size
57 #pod
58 #pod # site.yml
59 #pod blog:
60 #pod class: Statocles::App::Blog
61 #pod args:
62 #pod page_size: 5
63 #pod
64 #pod The number of posts to put in a page (the main page and the tag pages). Defaults
65 #pod to 5.
66 #pod
67 #pod =cut
68  
69 has page_size => (
70 is => 'ro',
71 isa => Int,
72 23 ▶ default => sub { 5 },
Subroutine __ANON__: called CC=1 SCAR=0.0
73 14 );
74  
75 #pod =attr index_tags
76 #pod
77 #pod # site.yml
78 #pod blog:
79 #pod class: Statocles::App::Blog
80 #pod args:
81 #pod index_tags: [ '-private', '+important' ]
82 #pod
83 #pod Filter the tags shown in the index page. An array of tags prefixed with either
84 #pod a + or a -. By prefixing the tag with a "-", it will be removed from the index,
85 #pod unless a later tag prefixed with a "+" also matches.
86 #pod
87 #pod By default, all tags are shown on the index page.
88 #pod
89 #pod So, given a document with tags "foo", and "bar":
90 #pod
91 #pod index_tags: [ ] # document will be included
92 #pod index_tags: [ '-foo' ] # document will not be included
93 #pod index_tags: [ '-foo', '+bar' ] # document will be included
94 #pod
95 #pod =cut
96  
97 has index_tags => (
98 is => 'ro',
99 isa => ArrayRef[Str],
100 23 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
101 14 );
102  
103 #pod =attr template_dir
104 #pod
105 #pod The directory (inside the theme directory) to use for this app's templates.
106 #pod Defaults to C<blog>.
107 #pod
108 #pod =cut
109  
110 14 has '+template_dir' => (
111 default => 'blog',
112 );
113  
114 # A cache of the last set of post pages we have
115 # XXX: We need to allow apps to have a "clear" the way that Store and Theme do
116 14 has _post_pages => (
117 is => 'rw',
118 isa => ArrayRef,
119 predicate => '_has_cached_post_pages',
120 );
121  
122 # The default post information hash
123 has _default_post => (
124 is => 'rw',
125 isa => HashRef,
126 lazy => 1,
127 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
128 {
129 1 tags => undef,
130 content => "Markdown content goes here.\n",
131 }
132 },
133 14 );
134  
135 #pod =method command
136 #pod
137 #pod my $exitval = $app->command( $app_name, @args );
138 #pod
139 #pod Run a command on this app. The app name is used to build the help, so
140 #pod users get exactly what they need to run.
141 #pod
142 #pod =cut
143  
144 14 my $USAGE_INFO = <<'ENDHELP';
145 Usage:
146 $name help -- This help file
147 $name post [--date YYYY-MM-DD] <title> -- Create a new blog post with the given title
148 ENDHELP
149  
150 8 ▶sub command {
Subroutine command: called CC=16 SCAR=29.5
Pod documented
151 8 my ( $self, $name, @argv ) = @_;
152  
153 8 ▶ if ( !$argv[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($argv[0]) 1 7 8
154 1 say STDERR "ERROR: Missing command";
155 1 say STDERR eval "qq{$USAGE_INFO}";
156 1 return 1;
157 }
158  
159 7 ▶ if ( $argv[0] eq 'help' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($argv[0] eq 'help') { } 1 6 7
elsif ($argv[0] eq 'post') { } 5 1 6
160 1 say eval "qq{$USAGE_INFO}";
161 }
162 elsif ( $argv[0] eq 'post' ) {
163 5 my %opt;
164 5 my @doc_opts = qw(author layout status tags template);
165 GetOptionsFromArray( \@argv, \%opt,
166 'date:s',
167 5 map { "$_:s" } @doc_opts,
168 );
169  
170 5 my $doc;
171  
172 # Read post content on STDIN
173 5 ▶ if ( my $content = read_stdin() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $content = read_stdin()) { } 3 2 5
174 $doc = Statocles::Document->parse_content(
175 3 ▶ (map { defined $opt{$_} ? ( $_, $opt{$_} ) : () } @doc_opts),
Branch: 2 branches
BranchTrueFalseTotal
defined $opt{$_} ? : 0 15 15
@argv > 1 ? : 2 1 3
176 ( @argv > 1 ? ( title => join( " ", @argv[1..$#argv] ) ) : () ),
177 content => $content,
178 );
179 }
180 else {
181 $doc = Statocles::Document->new(
182 2 %{ $self->_default_post },
183 2 ▶ (map { defined $opt{$_} ? ( $_, $opt{$_} ) : () } @doc_opts),
Branch: 2 branches
BranchTrueFalseTotal
defined $opt{$_} ? : 0 10 10
@argv > 1 ? : 2 0 2
184 ( @argv > 1 ? ( title => join( " ", @argv[1..$#argv] ) ) : () ),
185 );
186 }
187  
188 5 ▶ if ( !$ENV{EDITOR} && !$doc->title ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"EDITOR"} or $doc->title) 1 4 5
Condition100%3/3
$ENV{"EDITOR"} or $doc->title
l!l&&r!l&&!r
221
MC/DC100%2/2
$ENV{"EDITOR"} or $doc->title
$ENV{"EDITOR"} $doc->title
Truth table100%3/3
A or B
A= $ENV{"EDITOR"}B= $doc->title
ABresult
1X1
011
000
189 1 say STDERR <<"ENDHELP";
190 Title is required when \$EDITOR is not set.
191  
192 Usage: $name post <title>
193 ENDHELP
194 1 return 1;
195 }
196  
197 4 my ( $year, $mon, $day );
198 4 ▶ if ( $opt{ date } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'date'}) { } 1 3 4
199 1 ( $year, $mon, $day ) = split /-/, $opt{date};
200 }
201 else {
202 3 ( undef, undef, undef, $day, $mon, $year ) = localtime;
203 3 $year += 1900;
204 3 $mon += 1;
205 }
206  
207 4 my @date_parts = (
208 sprintf( '%04i', $year ),
209 sprintf( '%02i', $mon ),
210 sprintf( '%02i', $day ),
211 );
212  
213 4 ▶ my $slug = $self->make_slug( $doc->title || "new post" );
Condition50%1/2
$doc->title || 'new post'
l!l
40
MC/DC0%0/1
$doc->title || 'new post'
$doc->title
Truth table50%1/2
A
A= $doc->title
Aresult
11
00
214 4 my $url_root = $self->url_root;
215 4 $url_root =~ s{^/}{}g;
216 4 my @partsdir = ($url_root, @date_parts, $slug);
217 4 my @partsfile = (@partsdir, "index.markdown");
218 4 my $path = Mojo::Path->new->parts(\@partsfile);
219 4 $self->site->store->write_file( $path => $doc );
220 4 my $full_path = $self->site->store->path->child( @partsfile );
221  
222 4 ▶ if ( my $content = run_editor( $full_path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $content = run_editor($full_path)) 0 4 4
223 0 my $old_title = $doc->title;
224 0 my $doc = Statocles::Document->parse_content(
225 path => $path.'',
226 store => $self->site->store,
227 content => $content,
228 );
229 0 ▶ if ( $doc->title ne $old_title ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->title ne $old_title) 0 0 0
230 0 $self->site->store->path->child( @partsdir )->remove_tree;
231 0 ▶ $slug = $self->make_slug( $doc->title || "new post" );
Condition0%0/2
$doc->title || 'new post'
l!l
00
MC/DC0%0/1
$doc->title || 'new post'
$doc->title
Truth table0%0/2
A
A= $doc->title
Aresult
11
00
232 0 @partsdir = ($url_root, @date_parts, $slug);
233 0 @partsfile = (@partsdir, "index.markdown");
234 0 $path = Mojo::Path->new->parts(\@partsfile);
235 0 $self->site->store->write_file( $path => $doc );
236 0 $full_path = $self->site->store->path->child( @partsfile );
237 }
238 }
239  
240 4 say "New post at: $full_path";
241  
242 }
243 else {
244 1 say STDERR qq{ERROR: Unknown command "$argv[0]"};
245 1 say STDERR eval "qq{$USAGE_INFO}";
246 1 return 1;
247 }
248  
249 5 return 0;
250 }
251  
252 #pod =method make_slug
253 #pod
254 #pod my $slug = $app->make_slug( $title );
255 #pod
256 #pod Given a post title, remove special characters to create a slug.
257 #pod
258 #pod =cut
259  
260 32 ▶sub make_slug {
Subroutine make_slug: called CC=1 SCAR=0.0
Pod documented
261 32 my ( $self, $slug ) = @_;
262 32 $slug = unidecode($slug);
263 32 $slug =~ s/'//g;
264 32 $slug =~ s/[\W]+/-/g;
265 32 $slug =~ s/^-|-$//g;
266 32 return lc $slug;
267 }
268  
269 #pod =method index
270 #pod
271 #pod my @pages = $app->index( \@post_pages );
272 #pod
273 #pod Build the index page (a L<list page|Statocles::Page::List>) and all related
274 #pod feed pages out of the given array reference of post pages.
275 #pod
276 #pod =cut
277  
278 14 my %FEEDS = (
279 rss => {
280 text => 'RSS',
281 template => 'index.rss',
282 },
283 atom => {
284 text => 'Atom',
285 template => 'index.atom',
286 },
287 );
288  
289 0 ▶sub index {
Subroutine index: not called CC=9 SCAR=45.0
Pod documented
290 0 my ( $self, $all_post_pages ) = @_;
291  
292 0 my @index_tags;
293 my %tag_flag;
294 0 for my $tag_spec ( map lc, @{ $self->index_tags } ) {
295 0 my $tag = substr $tag_spec, 1;
296 0 push @index_tags, $tag;
297 0 $tag_flag{$tag} = substr $tag_spec, 0, 1;
298 }
299  
300 0 my @index_post_pages;
301 0 PAGE: for my $page ( @$all_post_pages ) {
302 0 my $page_flag = '+';
303 0 my %page_tags;
304 0 @page_tags{ map lc, @{ $page->document->tags } } = 1; # we use exists(), so value doesn't matter
305 0 for my $tag ( map lc, @index_tags ) {
306 0 ▶ if ( exists $page_tags{ $tag } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $page_tags{$tag}) 0 0 0
307 0 $page_flag = $tag_flag{ $tag };
308 }
309 }
310 0 ▶ push @index_post_pages, $page if $page_flag eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $page_flag eq "+" 0 0 0
311 }
312  
313 0 my @pages = Statocles::Page::List->paginate(
314 after => $self->page_size,
315 path => $self->url( 'page/%i/index.html', 1 ),
316 index => $self->url( 'index.html' ),
317 pages => [ _sort_page_list( @index_post_pages ) ],
318 app => $self,
319 template => $self->template( 'index.html' ),
320 layout => $self->template( 'layout.html' ),
321 );
322  
323 0 ▶ return unless @pages; # Only build feeds if we have pages
Branch: 1 branch
BranchTrueFalseTotal
unless @pages 0 0 0
324  
325 0 my $index = $pages[0];
326 0 my @feed_pages;
327 my @feed_links;
328 0 for my $feed ( sort keys %FEEDS ) {
329 my $page = Statocles::Page::List->new(
330 app => $self,
331 site => $self->site,
332 pages => $index->pages,
333 path => $self->url( 'index.' . $feed ),
334 0 template => $self->template( $FEEDS{$feed}{template} ),
335 links => {
336 alternate => [
337 $self->link(
338 href => $index->path,
339 title => 'index',
340 type => $index->type,
341 ),
342 ],
343 },
344 );
345  
346 0 push @feed_pages, $page;
347 push @feed_links, $self->link(
348 text => $FEEDS{ $feed }{ text },
349 0 href => $page->path.'',
350 type => $page->type,
351 );
352 }
353  
354 # Add the feeds to all the pages
355 0 for my $page ( @pages ) {
356 0 $page->links( feed => @feed_links );
357 }
358  
359 0 return ( @pages, @feed_pages );
360 }
361  
362 #pod =method tag_pages
363 #pod
364 #pod my @pages = $app->tag_pages( \%tag_pages );
365 #pod
366 #pod Get L<pages|Statocles::Page> for the tags in the given blog post documents
367 #pod (build from L<the post_pages method|/post_pages>, including relevant feed
368 #pod pages.
369 #pod
370 #pod =cut
371  
372 0 ▶sub tag_pages {
Subroutine tag_pages: not called CC=4 SCAR=30.0
Pod documented
373 0 my ( $self, $tagged_docs ) = @_;
374  
375 0 my @pages;
376 0 for my $tag ( keys %$tagged_docs ) {
377 0 my $tagroot = $self->url( join "/", 'tag', $self->_tag_url( $tag ) );
378 my @tag_pages = Statocles::Page::List->paginate(
379 after => $self->page_size,
380 path => join( "/", $tagroot, 'page/%i/index.html' ),
381 index => join( "/", $tagroot, 'index.html' ),
382 0 pages => [ _sort_page_list( @{ $tagged_docs->{ $tag } } ) ],
383 app => $self,
384 template => $self->template( 'index.html' ),
385 layout => $self->template( 'layout.html' ),
386 data => {
387 tag => $tag,
388 0 tag_text => $self->tag_text->{ $tag },
389 },
390 );
391  
392 0 my $index = $tag_pages[0];
393 0 my @feed_pages;
394 my @feed_links;
395 0 for my $feed ( sort keys %FEEDS ) {
396 0 my $tag_file = $self->_tag_url( $tag ) . '.' . $feed;
397  
398 my $page = Statocles::Page::List->new(
399 app => $self,
400 site => $self->site,
401 pages => $index->pages,
402 path => $self->url( join( "/", 'tag', $tag_file ) ),
403 0 template => $self->template( $FEEDS{$feed}{template} ),
404 links => {
405 alternate => [
406 $self->link(
407 href => $index->path,
408 title => $tag,
409 type => $index->type,
410 ),
411 ],
412 },
413 );
414  
415 0 push @feed_pages, $page;
416 push @feed_links, $self->link(
417 text => $FEEDS{ $feed }{ text },
418 0 href => $page->path.'',
419 type => $page->type,
420 );
421 }
422  
423 # Add the feeds to all the pages
424 0 for my $page ( @tag_pages ) {
425 0 $page->links( feed => @feed_links );
426 }
427  
428 0 push @pages, @tag_pages, @feed_pages;
429 }
430  
431 0 return @pages;
432 }
433  
434 #pod =method pages
435 #pod
436 #pod my @pages = $app->pages( %options );
437 #pod
438 #pod Get all the L<pages|Statocles::Page> for this application. Available options
439 #pod are:
440 #pod
441 #pod =over 4
442 #pod
443 #pod =item date
444 #pod
445 #pod The date to build for. Only posts on or before this date will be built.
446 #pod Defaults to the current date.
447 #pod
448 #pod =back
449 #pod
450 #pod =cut
451  
452 9 ▶sub pages {
Subroutine pages: called CC=11 SCAR=47.3
Pod documented
453 9 my ( $self, $pages, %opt ) = @_;
454 9 ▶ $opt{date} ||= DateTime::Moonpig->now( time_zone => 'local' )->ymd;
Condition33%1/3
$opt{"date"} ||= "DateTime::Moonpig"->now("time_zone", "local")->ymd
l!l&&r!l&&!r
009
MC/DC0%0/2
$opt{"date"} ||= "DateTime::Moonpig"->now("time_zone", "local")->ymd
$opt{"date"} "DateTime::Moonpig"->now("time_zone", "local")->ymd
Truth table33%1/3
A ||= B
A= $opt{"date"}B= "DateTime::Moonpig"->now("time_zone", "local")->ymd
ABresult
1X1
011
000
455 0 my $root = $self->url_root;
456 0 $root =~ s{^/}{}g;
457 0 my $is_dated_path = qr{^/?$root/?(\d{4})/(\d{2})/(\d{2})/};
458 0 my @parent_pages = @$pages;
459 my @pages =
460 0 map { $_->[0] }
461 # Only pages today or before
462 0 grep { $_->[1] le $opt{date} }
463 # Create the page's date
464 0 map { [ $_, join "-", $_->path =~ $is_dated_path ] }
465 # Only dated pages
466 0 grep { $_->path =~ $is_dated_path }
467 @parent_pages;
468 0 @pages = _sort_page_list( @pages );
469  
470 0 my @post_pages;
471 my %tag_pages;
472  
473 #; say "Got pages: " . join ", ", map { $_->path } @pages;
474  
475 0 for my $page ( @pages ) {
476 0 $page->app( $self );
477  
478 0 ▶ if ( $page->isa( 'Statocles::Page::Document' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->isa("Statocles::Page::Document")) 0 0 0
479  
480 0 ▶ if ( $page->path =~ m{$is_dated_path [^/]+ (?:/index)? [.]html$}x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->path =~ m[$is_dated_path [^/]+ (?:/index)? [.]html$]x) 0 0 0
481 0 my ( $year, $month, $day ) = ( $1, $2, $3 );
482  
483 0 push @post_pages, $page;
484  
485 0 my $doc = $page->document;
486 0 ▶ $page->date( $doc->has_date ? $doc->date : DateTime::Moonpig->new( year => $year, month => $month, day => $day ) );
Branch: 1 branch
BranchTrueFalseTotal
$doc->has_date ? : 0 0 0
487  
488 0 my @tags;
489 0 for my $tag ( @{ $doc->tags } ) {
490 0 push @{ $tag_pages{ lc $tag } }, $page;
491 0 push @tags, $self->link(
492 text => $tag,
493 href => join( "/", 'tag', $self->_tag_url( $tag ), '' ),
494 );
495 }
496 0 $page->tags( \@tags );
497  
498 0 $page->template( $self->template( 'post.html' ) );
499 }
500 }
501 }
502  
503 0 ▶ for ( my $i = 0; $i < @post_pages; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @post_pages) 0 0 0
504  
505 0 my $page = $post_pages[$i];
506 0 ▶ my $prev_page = $i ? $post_pages[$i-1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 0 0 0
507 0 my $next_page = $post_pages[$i+1];
508 0 ▶ $page->prev_page( $prev_page ) if $prev_page;
Branch: 1 branch
BranchTrueFalseTotal
if $prev_page 0 0 0
509 0 ▶ $page->next_page( $next_page ) if $next_page;
Branch: 1 branch
BranchTrueFalseTotal
if $next_page 0 0 0
510 }
511  
512 # Cache the post pages for this build
513 # XXX: This needs to be handled more intelligently with proper dependencies
514 0 $self->_post_pages( \@post_pages );
515  
516 0 my @all_pages = ( $self->index( \@post_pages ), $self->tag_pages( \%tag_pages ) );
517 0 return @all_pages;
518 }
519  
520 #pod =method tags
521 #pod
522 #pod my @links = $app->tags;
523 #pod
524 #pod Get a set of L<link objects|Statocles::Link> suitable for creating a list of
525 #pod tag links. The common attributes are:
526 #pod
527 #pod text => 'The tag text'
528 #pod href => 'The URL to the tag page'
529 #pod
530 #pod =cut
531  
532 8 ▶sub tags {
Subroutine tags: called CC=5 SCAR=16.4
Pod documented
533 8 my ( $self ) = @_;
534 8 my %tags;
535 8 ▶ my @pages = @{ $self->_post_pages || [] };
Condition50%1/2
$self->_post_pages || []
l!l
80
MC/DC0%0/1
$self->_post_pages || []
$self->_post_pages
Truth table50%1/2
A
A= $self->_post_pages
Aresult
11
00
536 8 for my $page ( @pages ) {
537 8 for my $tag ( @{ $page->document->tags } ) {
538 24 ▶ $tags{ lc $tag } ||= $tag;
Condition33%1/3
$tags{lc $tag} ||= $tag
l!l&&r!l&&!r
0240
MC/DC0%0/2
$tags{lc $tag} ||= $tag
$tags{lc $tag} $tag
Truth table33%1/3
A ||= B
A= $tags{lc $tag}B= $tag
ABresult
1X1
011
000
539 }
540 }
541 24 return map {; $self->link( text => $_, href => join( "/", 'tag', $self->_tag_url( $_ ), '' ) ) }
542 8 map { $tags{ $_ } }
543 sort keys %tags;
544 }
545  
546 24 ▶sub _tag_url {
Subroutine _tag_url: called CC=1 SCAR=0.0
547 24 my ( $self, $tag ) = @_;
548 24 return lc $self->make_slug( $tag );
549 }
550  
551 #pod =method recent_posts
552 #pod
553 #pod my @pages = $app->recent_posts( $count, %filter );
554 #pod
555 #pod Get the last $count recent posts for this blog. Useful for templates and site
556 #pod index pages.
557 #pod
558 #pod %filter is an optional set of filters to apply to only show recent posts
559 #pod matching the given criteria. The following filters are available:
560 #pod
561 #pod =over 4
562 #pod
563 #pod =item tags
564 #pod
565 #pod (string) Only show posts with the given tag
566 #pod
567 #pod =back
568 #pod
569 #pod =cut
570  
571 0 ▶sub recent_posts {
Subroutine recent_posts: not called CC=8 SCAR=42.8
Pod documented
572 0 my ( $self, $count, %filter ) = @_;
573  
574 0 my $root = $self->url_root;
575 0 ▶ my @pages = $self->_has_cached_post_pages ? @{ $self->_post_pages } : $self->pages;
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_cached_post_pages ? : 0 0 0
576 0 my @found_pages;
577 0 PAGE: for my $page ( @pages ) {
578 0 ▶ next PAGE unless $page->path =~ qr{^$root/?(\d{4})/(\d{2})/(\d{2})/[^/]+(?:/index)?[.]html$};
Branch: 1 branch
BranchTrueFalseTotal
unless $page->path =~ qr"^$root/?(\d{4})/(\d{2})/(\d{2})/[^/]+(?:/index)?[.]html$" 0 0 0
579  
580 0 QUERY: for my $attr ( keys %filter ) {
581 0 my $value = $filter{ $attr };
582 0 ▶ if ( $attr eq 'tags' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr eq "tags") 0 0 0
583 0 ▶ next PAGE unless grep { $_ eq $value } @{ $page->document->tags };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $value;} @{$page->document->tags;} 0 0 0
584 }
585 }
586  
587 0 push @found_pages, $page;
588 0 ▶ last if @found_pages >= $count;
Branch: 1 branch
BranchTrueFalseTotal
if @found_pages >= $count 0 0 0
589 }
590  
591 0 return @found_pages;
592 }
593  
594 #pod =method page_url
595 #pod
596 #pod my $url = $app->page_url( $page )
597 #pod
598 #pod Return the absolute URL to this L<page object|Statocles::Page>, removing the
599 #pod "/index.html" if necessary.
600 #pod
601 #pod =cut
602  
603 # XXX This is TERRIBLE. We need to do this better. Perhaps a "url()" helper in the
604 # template? And a full_url() helper? Or perhaps the template knows whether it should
605 # use absolute (/whatever) or full (http://www.example.com/whatever) URLs?
606  
607 10 ▶sub page_url {
Subroutine page_url: called CC=1 SCAR=0.0
Pod documented
608 10 my ( $self, $page ) = @_;
609 10 my $url = "".$page->path;
610 10 $url =~ s{/index[.]html$}{/};
611 10 return $url;
612 }
613  
614 #=sub _sort_list
615 #
616 # my @sorted_pages = _sort_page_list( @unsorted_pages );
617 #
618 # Sort a list of blog post pages into buckets according to the date
619 # component of their path, and then sort the buckets according to the
620 # date field in the document.
621 #
622 # This allows a user to order the posts in a single day themselves,
623 # predictably and consistently.
624  
625 0 ▶sub _sort_page_list {
Subroutine _sort_page_list: not called CC=2 SCAR=17.9
626 0 return map { $_->[0] }
627 0 ▶ sort { $b->[1] cmp $a->[1] || $b->[2] cmp $a->[2] }
Condition0%0/3
$Statocles::App::Blog::b->[1] cmp $Statocles::App::Blog::a->[1] || $Statocles::App::Blog::b->[2] cmp $Statocles::App::Blog::a->[2]
l!l&&r!l&&!r
000
MC/DC0%0/2
$Statocles::App::Blog::b->[1] cmp $Statocles::App::Blog::a->[1] || $Statocles::App::Blog::b->[2] cmp $Statocles::App::Blog::a->[2]
$Statocles::App::Blog::b->[1] cmp $Statocles::App::Blog::a->[1] $Statocles::App::Blog::b->[2] cmp $Statocles::App::Blog::a->[2]
Truth table0%0/3
A || B
A= $Statocles::App::Blog::b->[1] cmp $Statocles::App::Blog::a->[1]B= $Statocles::App::Blog::b->[2] cmp $Statocles::App::Blog::a->[2]
ABresult
1X1
011
000
628 0 map { [ $_, $_->path =~ m{/(\d{4}/\d{2}/\d{2})}, $_->date ] }
629 @_;
630 }
631  
632 14 1;
633  
634 __END__
« blib/lib/Statocles/Test.pm ↑ Summary blib/lib/Statocles/App/Basic.pm »