blib/lib/Statocles/Site.pm

filter: Statementstmt 92.4% 209 / 226 Branchbran 72.0% 36 / 50 Conditioncond 66.6% 20 / 30 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 95.2% 40 / 42 Podpod 85.7% 6 / 7 totaltotal 84.9% 315 / 371 CC 45 SCAR 20.9
CC
45
Cov
87%
_fix_internal_links
27.2
template
8.1
_build_pages
5.0
CRAP 8.1
« blib/lib/Statocles/Store.pm ↑ Summary blib/lib/Statocles/Deploy/Git.pm »
1 package Statocles::Site;
2 50 our $VERSION = '0.099';
3 # ABSTRACT: An entire, configured website
4  
5 50 ▶use Statocles::Base 'Class', 'Emitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 50 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 50 ▶use Text::Markdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 50 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 50 ▶use Mojo::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 50 ▶use Statocles::Page::Plain;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 50 ▶use Statocles::Page::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 50 ▶use Statocles::Page::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 50 ▶use Statocles::Util qw( derp );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 50 ▶use Statocles::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 50 ▶use List::UtilsBy qw( uniq_by );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #pod =attr store
18 #pod
19 #pod The L<Store object|Statocles::Store> for the site.
20 #pod
21 #pod =cut
22  
23 has store => (
24 is => 'ro',
25 isa => StoreType,
26 coerce => StoreType->coercion,
27 28 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
28 28 return StoreType->coercion->( '.' );
29 },
30 50 );
31  
32 #pod =attr title
33 #pod
34 #pod The site title, used in templates.
35 #pod
36 #pod =cut
37  
38 has title => (
39 is => 'ro',
40 isa => Str,
41 24 ▶ default => sub { '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 50 );
43  
44 #pod =attr author
45 #pod
46 #pod author: Doug Bell <doug@example.com>
47 #pod author:
48 #pod name: Doug Bell
49 #pod email: doug@example.com
50 #pod
51 #pod The primary author of the site, which will be used as the default author
52 #pod for all content. This can be a string with the author's name, and an
53 #pod optional e-mail address wrapped in E<lt>E<gt>, or a hashref of
54 #pod L<Statocles::Person attributes|Statocles::Person/ATTRIBUTES>.
55 #pod
56 #pod Individual documents can have their own authors. See
57 #pod L<Statocles::Document/author>.
58 #pod
59 #pod =cut
60  
61 50 has author => (
62 is => 'ro',
63 isa => PersonType,
64 coerce => PersonType->coercion,
65 );
66  
67 #pod =attr base_url
68 #pod
69 #pod The base URL of the site, including protocol and domain. Used mostly for feeds.
70 #pod
71 #pod This can be overridden by L<base_url in Deploy|Statocles::Deploy/base_url>.
72 #pod
73 #pod =cut
74  
75 has base_url => (
76 is => 'rw',
77 isa => Str,
78 59 ▶ default => sub { '/' },
Subroutine __ANON__: called CC=1 SCAR=0.0
79 50 );
80  
81 #pod =attr theme
82 #pod
83 #pod The L<theme|Statocles::Theme> for this site. All apps share the same theme.
84 #pod
85 #pod =cut
86  
87 has theme => (
88 is => 'ro',
89 isa => ThemeType,
90 coerce => ThemeType->coercion,
91 27 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
92 27 my ( $self ) = @_;
93 27 require Statocles::Theme;
94 27 Statocles::Theme->new( path => '::default' );
95 },
96 50 );
97  
98 #pod =attr apps
99 #pod
100 #pod The applications in this site. Each application has a name
101 #pod that can be used later.
102 #pod
103 #pod =cut
104  
105 has apps => (
106 is => 'ro',
107 isa => HashRef[ConsumerOf['Statocles::App']],
108 47 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
109 50 );
110  
111 #pod =attr plugins
112 #pod
113 #pod The plugins in this site. Each plugin has a name that can be used later.
114 #pod
115 #pod =cut
116  
117 has plugins => (
118 is => 'ro',
119 isa => HashRef[ConsumerOf['Statocles::Plugin']],
120 58 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
121 50 );
122  
123 #pod =attr index
124 #pod
125 #pod The page path to use for the site index. Make sure to include the leading slash
126 #pod (but C</index.html> is optional). Defaults to C</>, so any app with C<url_root>
127 #pod of C</> will be the index.
128 #pod
129 #pod =cut
130  
131 has index => (
132 is => 'ro',
133 isa => Str,
134 55 ▶ default => sub { '/' },
Subroutine __ANON__: called CC=1 SCAR=0.0
135 50 );
136  
137 #pod =attr nav
138 #pod
139 #pod Named navigation lists. A hash of arrays of hashes with the following keys:
140 #pod
141 #pod title - The title of the link
142 #pod href - The href of the link
143 #pod
144 #pod The most likely name for your navigation will be C<main>. Navigation names
145 #pod are defined by your L<theme|Statocles::Theme>. For example:
146 #pod
147 #pod {
148 #pod main => [
149 #pod {
150 #pod title => 'Blog',
151 #pod href => '/blog',
152 #pod },
153 #pod {
154 #pod title => 'Contact',
155 #pod href => '/contact.html',
156 #pod },
157 #pod ],
158 #pod }
159 #pod
160 #pod =cut
161  
162 has _nav => (
163 is => 'ro',
164 isa => LinkHash,
165 coerce => LinkHash->coercion,
166 64 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
167 50 init_arg => 'nav',
168 );
169  
170 #pod =attr links
171 #pod
172 #pod # site.yml
173 #pod links:
174 #pod stylesheet:
175 #pod - href: /theme/css/site.css
176 #pod script:
177 #pod - href: /theme/js/site.js
178 #pod
179 #pod Related links for this site. Links are used to build relationships
180 #pod to other web addresses. Link categories are named based on their
181 #pod relationship. Some possible categories are:
182 #pod
183 #pod =over 4
184 #pod
185 #pod =item stylesheet
186 #pod
187 #pod Additional stylesheets for this site.
188 #pod
189 #pod =item script
190 #pod
191 #pod Additional scripts for this site.
192 #pod
193 #pod =back
194 #pod
195 #pod Each category contains an arrayref of hashrefs of L<link objects|Statocles::Link>.
196 #pod See the L<Statocles::Link|Statocles::Link> documentation for a full list of
197 #pod supported attributes. The most common attributes are:
198 #pod
199 #pod =over 4
200 #pod
201 #pod =item href
202 #pod
203 #pod The URL for the link.
204 #pod
205 #pod =item text
206 #pod
207 #pod The text of the link. Not needed for stylesheet or script links.
208 #pod
209 #pod =back
210 #pod
211 #pod =cut
212  
213 has _links => (
214 is => 'ro',
215 isa => LinkHash,
216 69 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
217 50 coerce => LinkHash->coercion,
218 init_arg => 'links',
219 );
220  
221 #pod =attr images
222 #pod
223 #pod # site.yml
224 #pod images:
225 #pod icon: /images/icon.png
226 #pod
227 #pod Related images for this document. These are used by themes to display
228 #pod images in appropriate templates. Each image has a category, like
229 #pod C<title>, C<banner>, or C<icon>, mapped to an L<image
230 #pod object|Statocles::Image>. See the L<Statocles::Image|Statocles::Image>
231 #pod documentation for a full list of supported attributes. The most common
232 #pod attributes are:
233 #pod
234 #pod =over 4
235 #pod
236 #pod =item src
237 #pod
238 #pod The source path of the image. Relative paths will be resolved relative
239 #pod to this document.
240 #pod
241 #pod =item alt
242 #pod
243 #pod The alternative text to display if the image cannot be downloaded or
244 #pod rendered. Also the text to use for non-visual media.
245 #pod
246 #pod =back
247 #pod
248 #pod Useful image names are:
249 #pod
250 #pod =over 4
251 #pod
252 #pod =item icon
253 #pod
254 #pod The shortcut icon for the site.
255 #pod
256 #pod =back
257 #pod
258 #pod =cut
259  
260 has images => (
261 is => 'ro',
262 isa => HashRef[InstanceOf['Statocles::Image']],
263 69 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
264 71 ▶ coerce => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
265 71 my ( $ref ) = @_;
266 71 my %img;
267 71 for my $name ( keys %$ref ) {
268 2 my $attrs = $ref->{ $name };
269 2 ▶ if ( !ref $attrs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $attrs) 0 2 2
270 0 $attrs = { src => $attrs };
271 }
272 $img{ $name } = Statocles::Image->new(
273 2 %{ $attrs },
274 );
275 }
276 71 return \%img;
277 },
278 50 );
279  
280 #pod =attr templates
281 #pod
282 #pod # site.yml
283 #pod templates:
284 #pod sitemap.xml: custom/sitemap.xml
285 #pod layout.html: custom/layout.html
286 #pod
287 #pod The custom templates to use for the site meta-template like
288 #pod C<sitemap.xml> and C<robots.txt>, or the site-wide default layout
289 #pod template. A mapping of template names to template paths (relative to the
290 #pod theme root directory).
291 #pod
292 #pod Developers should get site templates using L<the C<template>
293 #pod method|/template>.
294 #pod
295 #pod =cut
296  
297 has _templates => (
298 is => 'ro',
299 isa => HashRef,
300 70 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
301 50 init_arg => 'templates',
302 );
303  
304 #pod =attr template_dir
305 #pod
306 #pod The directory (inside the theme directory) to use for the site meta-templates.
307 #pod
308 #pod =cut
309  
310 has template_dir => (
311 is => 'ro',
312 isa => Str,
313 71 ▶ default => sub { 'site' },
Subroutine __ANON__: called CC=1 SCAR=0.0
314 50 );
315  
316 #pod =attr deploy
317 #pod
318 #pod The L<deploy object|Statocles::Deploy> to use for C<deploy()>. This is
319 #pod intended to be the production deployment of the site. A build gets promoted to
320 #pod production by using the C<deploy> command.
321 #pod
322 #pod =cut
323  
324 has deploy => (
325 is => 'ro',
326 isa => ConsumerOf['Statocles::Deploy'],
327 required => 1,
328 71 ▶ coerce => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
329 71 ▶ if ( ( blessed $_[0] && $_[0]->isa( 'Path::Tiny' ) ) || !ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->isa("Path::Tiny") or not ref $_[0]) 45 26 71
Condition100%6/6
builtin::blessed($_[0]) and $_[0]->isa("Path::Tiny") or not ref $_[0]
l!l&&r!l&&!r
39626
builtin::blessed($_[0]) and $_[0]->isa("Path::Tiny")
!ll&&!rl&&r
62639
MC/DC33%1/3
builtin::blessed($_[0]) and $_[0]->isa("Path::Tiny") or not ref $_[0]
builtin::blessed($_[0]) $_[0]->isa("Path::Tiny") not ref $_[0]
Truth table60%3/5
A and B or not(C)
A= builtin::blessed($_[0])B= $_[0]->isa("Path::Tiny")C= not ref $_[0]
ABCresult
11X1
0X11
1011
0X00
1000
330 45 require Statocles::Deploy::File;
331 45 return Statocles::Deploy::File->new(
332 path => $_[0],
333 );
334 }
335 26 return $_[0];
336 },
337 50 );
338  
339 #pod =attr data
340 #pod
341 #pod A hash of arbitrary data available to theme templates. This is a good place to
342 #pod put extra structured data like social network links or make easy customizations
343 #pod to themes like header image URLs.
344 #pod
345 #pod =cut
346  
347 has data => (
348 is => 'ro',
349 isa => HashRef,
350 70 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
351 50 );
352  
353 #pod =attr log
354 #pod
355 #pod A L<Mojo::Log> object to write logs to. Defaults to STDERR.
356 #pod
357 #pod =cut
358  
359 has log => (
360 is => 'ro',
361 isa => InstanceOf['Mojo::Log'],
362 lazy => 1,
363 12 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
364 12 Mojo::Log->new( level => 'warn' );
365 },
366 50 );
367  
368 #pod =attr markdown
369 #pod
370 #pod The Text::Markdown object to use to turn Markdown into HTML. Defaults to a
371 #pod plain Text::Markdown object.
372 #pod
373 #pod Any object with a "markdown" method will work here.
374 #pod
375 #pod =cut
376  
377 has markdown => (
378 is => 'ro',
379 isa => HasMethods['markdown'],
380 71 ▶ default => sub { Text::Markdown->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
381 50 );
382  
383 #pod =attr disable_content_template
384 #pod
385 #pod This disables processing the content as a template. This can speed up processing
386 #pod when the content is not using template directives.
387 #pod
388 #pod This can be also set in the application
389 #pod (L<Statocles::App/disable_content_template>), or for each document
390 #pod (L<Statocles::Document/disable_content_template>).
391 #pod
392 #pod =cut
393  
394 50 has disable_content_template => (
395 is => 'ro',
396 isa => Bool,
397 lazy => 1,
398 default => 0,
399 predicate => 'has_disable_content_template',
400 );
401  
402 #pod =attr _pages
403 #pod
404 #pod A cache of all the pages that the site contains. This is generated
405 #pod during the C<build> phase and is available to all the templates
406 #pod while they are being rendered.
407 #pod
408 #pod =cut
409  
410 has _pages => (
411 is => 'rw',
412 isa => ArrayRef[ConsumerOf['Statocles::Page']],
413 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
414 50 lazy => 1,
415 predicate => 'has_pages',
416 clearer => 'clear_pages',
417 );
418  
419 #pod =method BUILD
420 #pod
421 #pod Register this site as the global site.
422 #pod
423 #pod =cut
424  
425 71 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod documented
426 71 my ( $self ) = @_;
427  
428 71 $Statocles::SITE = $self;
429 71 for my $app ( values %{ $self->apps } ) {
430 25 $app->site( $self );
431 }
432 71 for my $plugin ( values %{ $self->plugins } ) {
433 13 $plugin->register( $self );
434 }
435 }
436  
437 #pod =method app
438 #pod
439 #pod my $app = $site->app( $name );
440 #pod
441 #pod Get the app with the given C<name>.
442 #pod
443 #pod =cut
444  
445 0 ▶sub app {
Subroutine app: not called CC=1 SCAR=6.9
Pod documented
446 0 my ( $self, $name ) = @_;
447 0 return $self->apps->{ $name };
448 }
449  
450 #pod =method nav
451 #pod
452 #pod my @links = $site->nav( $key );
453 #pod
454 #pod Get the list of links for the given nav C<key>. Each link is a
455 #pod L<Statocles::Link> object.
456 #pod
457 #pod title - The title of the link
458 #pod href - The href of the link
459 #pod
460 #pod If the named nav does not exist, returns an empty list.
461 #pod
462 #pod =cut
463  
464 37 ▶sub nav {
Subroutine nav: called CC=2 SCAR=8.1
Pod documented
465 37 my ( $self, $name ) = @_;
466 0 ▶ return $self->_nav->{ $name } ? @{ $self->_nav->{ $name } } : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->_nav->{$name} ? : 0 37 37
467 }
468  
469 #pod =method build
470 #pod
471 #pod $site->build( %options );
472 #pod
473 #pod Build the site in its build location. The C<%options> hash is passed in to every
474 #pod app's C<pages> method, allowing for customization of app behavior based on
475 #pod command-line.
476 #pod
477 #pod =cut
478  
479 50 our %PAGE_PRIORITY = (
480 'Statocles::Page::File' => -100,
481 );
482  
483 20 ▶sub pages {
Subroutine pages: called CC=4 SCAR=13.9
Pod undocumented
484 20 my ( $self, %options ) = @_;
485  
486 20 ▶ if ( $self->has_pages ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_pages) 0 20 20
487 #; say "Returning cached pages";
488 0 return @{ $self->_pages };
489 }
490  
491 # Load all the files/documents from the site directory
492 20 ▶ my $drain = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
493 20 my $iter = shift;
494 20 my @f;
495 20 ▶ while ( my $obj = $iter->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = &$iter()) 92 20 112
496 92 ▶ next if $obj->path =~ /[.]ep$/;
Branch: 1 branch
BranchTrueFalseTotal
if $obj->path =~ /[.]ep$/ 32 60 92
497 60 push @f, $obj;
498 }
499 20 return @f;
500 20 };
501 20 my @files = $drain->( $self->store->iterator );
502 # $self->emit(
503 # 'read_files',
504 # class => 'Statocles::Event::Files',
505 # files => \@files,
506 # );
507  
508 # Build Page objects for each document
509 20 my @pages = $self->_build_pages( \@files );
510 #; say "Got all pages: " . join ", ", map { $_->path } @pages;
511 # $self->emit(
512 # 'read_pages',
513 # class => 'Statocles::Event::Pages',
514 # pages => \@pages,
515 # );
516  
517 # Apply applications to specific subsets of pages
518 20 my $apps = $self->apps;
519 20 my %seen_paths;
520 my @app_pages;
521 20 for my $app_name ( keys %{ $apps } ) {
522 16 my $app = $apps->{$app_name};
523 16 my $app_root = $app->url_root;
524 16 $app_root =~ s{^/}{}g;
525 16 my $pages_for_app = [ grep { $_->path =~ m{^/?$app_root} } @pages ];
526 16 push @app_pages, $app->pages(
527 $pages_for_app,
528 %options,
529 );
530 }
531  
532 11 push @pages, @app_pages;
533 11 $self->emit(
534 'collect_pages',
535 class => 'Statocles::Event::Pages',
536 pages => \@pages,
537 );
538  
539 # @pages should not change after this, because it is being cached
540 11 $self->_pages( \@pages );
541  
542 11 $self->emit(
543 'before_build_write',
544 class => 'Statocles::Event::Pages',
545 pages => \@pages,
546 );
547  
548 11 $self->_fix_internal_links( \@pages, \%options );
549  
550 11 push @pages,
551 $self->_build_sitemap( \@pages ),
552 $self->_build_robots,
553 ;
554  
555 # Add the theme
556 2 my $theme_root = $self->theme->url_root;
557 2 $theme_root =~ s{^/}{}g;
558 2 my $pages_for_theme = [ grep { $_->path =~ m{^/?$theme_root} } @pages ];
559 2 $self->theme->site( $self );
560 2 for my $page ( $self->theme->pages ) {
561 2 push @pages, $page;
562 }
563  
564 # @pages should not change after this, because it is being cached
565 2 $self->_pages( \@pages );
566  
567 2 $self->emit(
568 'build',
569 class => 'Statocles::Event::Pages',
570 pages => \@pages,
571 );
572  
573 2 return @pages;
574 }
575  
576 20 ▶sub _build_pages {
Subroutine _build_pages: called CC=5 SCAR=16.1
577 20 my ( $self, $files ) = @_;
578 20 my @pages;
579 20 for my $obj ( @$files ) {
580 60 ▶ if ( $obj->isa( 'Statocles::Document' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->isa('Statocles::Document')) { } 34 26 60
581 34 my $page_path = $obj->path.'';
582 34 $page_path =~ s{[.]\w+$}{.html};
583  
584 34 my %args = (
585 path => $page_path,
586 site => $self,
587 layout => $self->template( 'layout.html' ),
588 document => $obj,
589 );
590  
591 34 push @pages, Statocles::Page::Document->new( %args );
592 }
593 else {
594 # If there's a markdown file, don't keep the html file, since
595 # we'll be building it from the markdown
596 26 ▶ if ( $obj->path =~ /[.]html$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->path =~ /[.]html$/) 2 24 26
597 2 my $doc_path = $obj->path."";
598 2 $doc_path =~ s/[.]html$/.markdown/;
599 2 ▶ next if $self->store->has_file( $doc_path );
Branch: 1 branch
BranchTrueFalseTotal
if $self->store->has_file($doc_path) 0 2 2
600 }
601  
602 26 push @pages, Statocles::Page::File->new(
603 site => $self,
604 path => $obj->path->stringify,
605 file_path => $self->store->path->child( $obj->path ),
606 );
607 }
608 }
609 20 return @pages;
610 }
611  
612 11 ▶sub _fix_internal_links {
Subroutine _fix_internal_links: called CC=18 SCAR=33.0
613 11 my ( $self, $pages, $options ) = @_;
614  
615 # Site-level munging of pages
616 11 ▶ my $base_url = $options->{ base_url } || $self->base_url;
Condition33%1/3
$options->{'base_url'} || $self->base_url
l!l&&r!l&&!r
0110
MC/DC0%0/2
$options->{'base_url'} || $self->base_url
$options->{'base_url'} $self->base_url
Truth table33%1/3
A || B
A= $options->{'base_url'}B= $self->base_url
ABresult
1X1
011
000
617 11 $self->base_url( $base_url );
618 11 my $base_path = Mojo::URL->new( $base_url )->path;
619 11 $base_path =~ s{/$}{};
620  
621 # XXX: Should we allow sites without indexes?
622 11 my $index_path = $self->index;
623 11 ▶ if ( $index_path && $index_path !~ m{^/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index_path and not $index_path =~ m[^/]) 0 11 11
Condition33%1/3
$index_path and not $index_path =~ m[^/]
!ll&&!rl&&r
0110
MC/DC0%0/2
$index_path and not $index_path =~ m[^/]
$index_path not $index_path =~ m[^/]
Truth table33%1/3
A and not(B)
A= $index_pathB= not $index_path =~ m[^/]
ABresult
0X0
100
111
624 0 $self->log->warn(
625 sprintf 'site "index" property should be absolute path to index page (got "%s")',
626 $self->index,
627 );
628 }
629 # Allow index to be path to document and not the resulting page
630 # (so, ending in ".markdown" or ".md")
631 11 my $doc_path = $index_path;
632 11 my $doc_ext = join '|', @{ $self->store->document_extensions };
633 11 $doc_path =~ s/$doc_ext/html/;
634 11 my $index_path_re = qr{^$doc_path(?:/index[.]html)?$};
635  
636 # DEPRECATED: Index without leading / is an index app
637 my $index_root = $self->index =~ m{^/} ? $self->index
638 11 ▶ : $self->index ? $self->apps->{ $self->index }->url_root : '';
Branch: 2 branches
BranchTrueFalseTotal
$self->index =~ m[^/] ? : 11 0 11
$self->index ? : 0 0 0
639 11 $index_root =~ s{/index[.]html$}{};
640  
641 #; say "Got pages: \n" . join "\n", map { $_->path } @$pages;
642 #; say "Looking for: $index_path_re";
643  
644 # First find the index file
645 11 my ( $index_page ) = grep { $_->path =~ $index_path_re } @$pages;
646 11 my $index_orig_path;
647 11 ▶ if ( $index_page ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index_page) 0 11 11
648 #; say "Found index: " . $index_page->path;
649 0 $index_orig_path = $index_page->path;
650 0 $index_page->path( '/index.html' );
651 }
652  
653 11 for my $page ( @$pages ) {
654 34 my $is_index = $page->path eq '/index.html';
655  
656 34 ▶ if ( !$page->has_dom ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($page->has_dom) 6 28 34
657 6 next;
658 }
659  
660 28 my $dom = $page->dom;
661 28 for my $attr ( qw( src href ) ) {
662 56 for my $el ( $dom->find( "[$attr]" )->each ) {
663 172 my $url = $el->attr( $attr );
664  
665 # Fix relative non-anchor links on the index page
666 172 ▶ if ( $is_index && $index_orig_path && $url !~ m{^([A-Za-z]+:|/|#)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_index and $index_orig_path and not $url =~ m[^([A-Za-z]+:|/|#)]) 0 172 172
Condition50%3/6
$is_index and $index_orig_path and not $url =~ m[^([A-Za-z]+:|/|#)]
!ll&&!rl&&r
17200
$is_index and $index_orig_path
!ll&&!rl&&r
133390
MC/DC0%0/3
$is_index and $index_orig_path and not $url =~ m[^([A-Za-z]+:|/|#)]
$is_index $index_orig_path not $url =~ m[^([A-Za-z]+:|/|#)]
Truth table50%2/4
A and B and not(C)
A= $is_indexB= $index_orig_pathC= not $url =~ m[^([A-Za-z]+:|/|#)]
ABCresult
0XX0
10X0
1100
1111
667 0 my $clone = $index_orig_path->clone;
668 0 pop @$clone;
669 0 $url = join "/", $clone, $url;
670 }
671  
672 172 ▶ next unless $url =~ m{^/(?:[^/]|$)};
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^/(?:[^/]|$)] 65 107 172
673  
674 # Rewrite links to the index app's index page
675 107 ▶ if ( $index_root && $url =~ m{^$index_root(?:/index[.]html)?$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index_root and $url =~ m[^$index_root(?:/index[.]html)?$]) 26 81 107
Condition66%2/3
$index_root and $url =~ m[^$index_root(?:/index[.]html)?$]
!ll&&!rl&&r
08126
MC/DC50%1/2
$index_root and $url =~ m[^$index_root(?:/index[.]html)?$]
$index_root $url =~ m[^$index_root(?:/index[.]html)?$]
Truth table66%2/3
A and B
A= $index_rootB= $url =~ m[^$index_root(?:/index[.]html)?$]
ABresult
0X0
100
111
676 26 $url = '/';
677 }
678  
679 107 ▶ if ( $base_path =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_path =~ /\S/) 0 107 107
680 0 $url = join "", $base_path, $url;
681 }
682  
683 107 $el->attr( $attr, $url );
684 }
685 }
686 }
687 }
688  
689 11 ▶sub _build_sitemap {
Subroutine _build_sitemap: called CC=1 SCAR=0.0
690 11 my ( $self, $pages ) = @_;
691  
692 # Build the sitemap.xml
693 # html files only
694 # sorted by path to keep order and prevent spurious deploy commits
695 28 my @indexed_pages = map { $_->[0] }
696 32 sort { $a->[1] cmp $b->[1] }
697 28 map { [ $_, $self->url( $_->path ) ] }
698 11 grep { $_->path =~ /[.]html?$/ }
699 @$pages;
700 11 my $tmpl = $self->template( 'sitemap.xml' );
701 11 my $sitemap = Statocles::Page::Plain->new(
702 path => '/sitemap.xml',
703 content => $tmpl->render( site => $self, pages => \@indexed_pages ),
704 );
705 2 return $sitemap;
706 }
707  
708 2 ▶sub _build_robots {
Subroutine _build_robots: called CC=1 SCAR=0.0
709 2 my ( $self ) = @_;
710 # robots.txt is the best way for crawlers to automatically discover sitemap.xml
711 # We should do more with this later...
712 2 my $robots_tmpl = $self->template( 'robots.txt' );
713 2 my $robots = Statocles::Page::Plain->new(
714 path => '/robots.txt',
715 content => $robots_tmpl->render( site => $self ),
716 );
717 2 return $robots;
718 }
719  
720 #pod =method links
721 #pod
722 #pod my @links = $site->links( $key );
723 #pod my $link = $site->links( $key );
724 #pod $site->links( $key => $add_link );
725 #pod
726 #pod Get or append to the links set for the given key. See L<the links
727 #pod attribute|/links> for some commonly-used keys.
728 #pod
729 #pod If only one argument is given, returns a list of L<link
730 #pod objects|Statocles::Link>. In scalar context, returns the first link in
731 #pod the list.
732 #pod
733 #pod If two arguments are given, append the new link to the given key.
734 #pod C<$add_link> may be a URL string, a hash reference of L<link
735 #pod attributes|Statocles::Link/ATTRIBUTES>, or a L<Statocles::Link
736 #pod object|Statocles::Link>. When adding links, nothing is returned.
737 #pod
738 #pod =cut
739  
740 86 ▶sub links {
Subroutine links: called CC=4 SCAR=15.2
Pod documented
741 86 my ( $self, $name, $add_link ) = @_;
742 86 ▶ if ( $add_link ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add_link) 0 86 86
743 0 push @{ $self->_links->{ $name } }, LinkType->coerce( $add_link );
744 0 return;
745 }
746 36 ▶ my @links = uniq_by { $_->href }
Subroutine __ANON__: called CC=1 SCAR=0.0
747 86 ▶ $self->_links->{ $name } ? @{ $self->_links->{ $name } } : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->_links->{$name} ? : 36 50 86
748 86 ▶ return wantarray ? @links : $links[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 86 0 86
749 }
750  
751 #pod =method url
752 #pod
753 #pod my $url = $site->url( $page_url );
754 #pod
755 #pod Get the full URL to the given path by prepending the C<base_url>.
756 #pod
757 #pod =cut
758  
759 97 ▶sub url {
Subroutine url: called CC=1 SCAR=0.0
Pod documented
760 97 my ( $self, $path ) = @_;
761 97 my $base = $self->base_url;
762  
763 # Remove index.html from the end of the path, since it's redundant
764 97 $path =~ s{/index[.]html$}{/};
765  
766 # Remove the / from both sides of the join so we don't double up
767 97 $base =~ s{/$}{};
768 97 $path =~ s{^/}{};
769  
770 97 return join "/", $base, $path;
771 }
772  
773 #pod =method template
774 #pod
775 #pod my $template = $app->template( $tmpl_name );
776 #pod
777 #pod Get a L<template object|Statocles::Template> for the given template
778 #pod name. The default template is determined by the app's class name and the
779 #pod template name passed in.
780 #pod
781 #pod Applications should list the templates they have and describe what L<page
782 #pod class|Statocles::Page> they use.
783 #pod
784 #pod =cut
785  
786 65 ▶sub template {
Subroutine template: called CC=8 SCAR=20.9
Pod documented
787 65 my ( $self, @parts ) = @_;
788  
789 65 ▶ if ( @parts == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts == 1) 65 0 65
790 @parts = $self->_templates->{ $parts[0] }
791 65 ▶ ? $self->_templates->{ $parts[0] }
Branch: 2 branches
BranchTrueFalseTotal
$self->_templates->{$parts[0]} ? : 1 64 65
$parts[0] eq 'layout.html' ? : 51 13 64
792 : $parts[0] eq 'layout.html'
793 ? ( 'layout', 'default.html' )
794 : ( $self->template_dir, @parts );
795 }
796  
797 # If the default layout doesn't exist, use the old default.
798 # Remove this in v2.0
799 65 ▶ if ( $parts[0] eq 'layout' && $parts[1] eq 'default.html'
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[0] eq "layout" and $parts[1] eq "default.html" and not $self->theme->path->child(@parts)->is_file and $self->theme->path->child("site", "layout.html.ep")->is_file) 2 63 65
Condition77%7/9
$parts[0] eq "layout" and $parts[1] eq "default.html" and not $self->theme->path->child(@parts)->is_file and $self->theme->path->child("site", "layout.html.ep")->is_file
!ll&&!rl&&r
14492
$parts[0] eq "layout" and $parts[1] eq "default.html" and not $self->theme->path->child(@parts)->is_file
!ll&&!rl&&r
14051
$parts[0] eq "layout" and $parts[1] eq "default.html"
!ll&&!rl&&r
14051
MC/DC50%2/4
$parts[0] eq "layout" and $parts[1] eq "default.html" and not $self->theme->path->child(@parts)->is_file and $self->theme->path->child("site", "layout.html.ep")->is_file
$parts[0] eq "layout" $parts[1] eq "default.html" not $self->theme->path->child(@parts)->is_file $self->theme->path->child("site", "layout.html.ep")->is_file
Truth table60%3/5
A and B and not(C) and D
A= $parts[0] eq "layout"B= $parts[1] eq "default.html"C= not $self->theme->path->child(@parts)->is_fileD= $self->theme->path->child("site", "layout.html.ep")->is_file
ABCDresult
0XXX0
10XX0
110X0
11100
11111
800 && !$self->theme->path->child( @parts )->is_file
801 && $self->theme->path->child( site => 'layout.html.ep' )->is_file
802 ) {
803 2 derp qq{Using default layout "site/layout.html.ep" is deprecated and will be removed in v2.0. Move your default layout to "layout/default.html.ep" to fix this warning.};
804 2 return $self->theme->template( qw( site layout.html ) );
805 }
806  
807 63 return $self->theme->template( @parts );
808 }
809  
810 50 1;
811  
812 __END__
« blib/lib/Statocles/Store.pm ↑ Summary blib/lib/Statocles/Deploy/Git.pm »