blib/lib/Statocles/Page/Document.pm

filter: Statementstmt 90.3% 84 / 93 Branchbran 68.7% 22 / 32 Conditioncond 33.3% 9 / 27 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 95.2% 20 / 21 Podpod 60.0% 3 / 5 totaltotal 71.2% 139 / 195 CC 27 SCAR 22.9
CC
27
Cov
76%
__ANON__
37.7
__ANON__
3.7
__ANON__
3.7
CRAP 9.9
« blib/lib/Statocles.pm ↑ Summary blib/lib/Statocles/Command/status.pm »
1 package Statocles::Page::Document;
2 53 our $VERSION = '0.099';
3 # ABSTRACT: Render document objects into HTML
4  
5 53 ▶use Statocles::Base 'Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 53 with 'Statocles::Page';
7 53 ▶use Statocles::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 53 ▶use Statocles::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #pod =attr document
11 #pod
12 #pod The L<document|Statocles::Document> this page will render.
13 #pod
14 #pod =cut
15  
16 53 has document => (
17 is => 'ro',
18 isa => InstanceOf['Statocles::Document'],
19 required => 1,
20 );
21  
22 #pod =attr title
23 #pod
24 #pod The title of the page.
25 #pod
26 #pod =cut
27  
28 has '+title' => (
29 lazy => 1,
30 16 ▶ default => sub { $_[0]->document->title || '' },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->document->title or "" 4 12 16
31 53 );
32  
33 #pod =attr author
34 #pod
35 #pod The author of the page.
36 #pod
37 #pod =cut
38  
39 14 ▶around _build_author => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
40 14 my ( $orig, $self ) = @_;
41 14 ▶ return $self->document->author || $self->$orig;
Condition66%2/3
$self->document->author || $self->$orig
l!l&&r!l&&!r
2120
MC/DC0%0/2
$self->document->author || $self->$orig
$self->document->author $self->$orig
Truth table66%2/3
A || B
A= $self->document->authorB= $self->$orig
ABresult
1X1
011
000
42 53 };
43  
44 #pod =attr date
45 #pod
46 #pod Get the date of this page by checking the document.
47 #pod
48 #pod =cut
49  
50 has '+date' => (
51 lazy => 1,
52 9 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
53 9 my ( $self ) = @_;
54 9 ▶ $self->document->date || DateTime::Moonpig->now( time_zone => 'local' );
Branch: 1 branch
BranchTrueFalseTotal
$self->document->date or "DateTime::Moonpig"->now("time_zone", "local") 3 6 9
55 },
56 53 );
57  
58 #pod =attr tags
59 #pod
60 #pod The tag links for this document. An array of L<link objects|Statocles::Link>. The
61 #pod most important attributes are:
62 #pod
63 #pod text - The text of the link
64 #pod href - The page of the link
65 #pod
66 #pod =cut
67  
68 has _tags => (
69 is => 'rw',
70 isa => LinkArray,
71 44 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
72 53 coerce => LinkArray->coercion,
73 init_arg => 'tags',
74 );
75  
76 115 ▶sub links {
Subroutine links: called CC=1 SCAR=0.0
Pod undocumented
77 115 shift->document->links( @_ );
78 }
79  
80 0 ▶sub images {
Subroutine images: not called CC=1 SCAR=6.9
Pod undocumented
81 0 shift->document->images( @_ );
82 }
83  
84 #pod =attr data
85 #pod
86 #pod The C<data> hash for this page. Defaults to the C<data> attribute from the Document.
87 #pod
88 #pod =cut
89  
90 has '+data' => (
91 lazy => 1,
92 13 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
93 13 my ( $self ) = @_;
94 # Only allow hashref data attributes to come through.
95 # Non-hashref data attributes are deprecated and will be removed
96 # in v2.0. When that happens, remove this check as well
97 13 my $data = $self->document->data;
98 13 ▶ if ( $data && ref $data eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and ref $data eq "HASH") 1 12 13
Condition66%2/3
$data and ref $data eq "HASH"
!ll&&!rl&&r
1201
MC/DC50%1/2
$data and ref $data eq "HASH"
$data ref $data eq "HASH"
Truth table66%2/3
A and B
A= $dataB= ref $data eq "HASH"
ABresult
0X0
100
111
99 1 return $data;
100 }
101 12 return {};
102 },
103 53 );
104  
105 #pod =attr disable_content_template
106 #pod
107 #pod If true, disables the processing of the content as a template. This will
108 #pod improve performance if you're not using any template directives in your content.
109 #pod
110 #pod This can be set in the document (L<Statocles::Document/disable_content_template>),
111 #pod the application (L<Statocles::App/disable_content_template>), or the site
112 #pod (L<Statocles::Site/disable_content_template>).
113 #pod
114 #pod =cut
115  
116 has disable_content_template => (
117 is => 'ro',
118 isa => Str,
119 lazy => 1,
120 18 ▶ default => sub {
Subroutine __ANON__: called CC=10 SCAR=36.3
121 18 my ( $self ) = @_;
122 18 ▶ return $self->document && $self->document->has_disable_content_template ? $self->document->disable_content_template && "document"
Branch: 3 branches
BranchTrueFalseTotal
$self->document && $self->document->has_disable_content_template ? : 0 18 18
$self->app && $self->app->has_disable_content_template ? : 0 18 18
$self->site && $self->site->has_disable_content_template ? : 0 18 18
Condition20%3/15
$self->document and $self->document->has_disable_content_template
!ll&&!rl&&r
0180
$self->document->disable_content_template and "document"
!ll
00
$self->app and $self->app->has_disable_content_template
!ll&&!rl&&r
1800
$self->app->disable_content_template and "application"
!ll
00
$self->site and $self->site->has_disable_content_template
!ll&&!rl&&r
0180
$self->site->disable_content_template and "site"
!ll
00
MC/DC0%0/2
$self->document and $self->document->has_disable_content_template
$self->document $self->document->has_disable_content_template
MC/DC0%0/1
$self->document->disable_content_template and "document"
$self->document->disable_content_template
MC/DC0%0/2
$self->app and $self->app->has_disable_content_template
$self->app $self->app->has_disable_content_template
MC/DC0%0/1
$self->app->disable_content_template and "application"
$self->app->disable_content_template
MC/DC0%0/2
$self->site and $self->site->has_disable_content_template
$self->site $self->site->has_disable_content_template
MC/DC0%0/1
$self->site->disable_content_template and "site"
$self->site->disable_content_template
Truth table33%1/3
A and B
A= $self->documentB= $self->document->has_disable_content_template
ABresult
0X0
100
111
Truth table0%0/2
A
A= $self->document->disable_content_template
Aresult
00
11
Truth table33%1/3
A and B
A= $self->appB= $self->app->has_disable_content_template
ABresult
0X0
100
111
Truth table0%0/2
A
A= $self->app->disable_content_template
Aresult
00
11
Truth table33%1/3
A and B
A= $self->siteB= $self->site->has_disable_content_template
ABresult
0X0
100
111
Truth table0%0/2
A
A= $self->site->disable_content_template
Aresult
00
11
123 : $self->app && $self->app->has_disable_content_template ? $self->app->disable_content_template && "application"
124 : $self->site && $self->site->has_disable_content_template ? $self->site->disable_content_template && "site"
125 : "";
126 },
127 53 );
128  
129 26 ▶sub _render_content_template {
Subroutine _render_content_template: called CC=3 SCAR=11.1
130 26 my ( $self, $content, $vars ) = @_;
131 26 ▶ if ( my $by = $self->disable_content_template ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $by = $self->disable_content_template) 0 26 26
132 0 $self->site->log->debug( $self->path . ' content template processing disabled by ' . $by );
133 0 return $content;
134 }
135 26 $self->site->log->debug( $self->path . ' processing content template ' );
136 26 my $tmpl = $self->site->theme->build_template( $self->path, $content );
137 26 my $doc = $self->document;
138 26 ▶ if ( $doc->store ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc->store) 11 15 26
139 11 my $document_path = $doc->store->path->child( $doc->path )->parent;
140 11 push @{ $tmpl->include_paths }, $document_path;
141 }
142 26 my $rendered = $tmpl->render( %$vars, $self->vars, self => $doc, page => $self );
143 26 return $rendered;
144 }
145  
146 #pod =method content
147 #pod
148 #pod my $html = $page->content( %vars );
149 #pod
150 #pod Generate the document HTML by processing template directives and converting
151 #pod Markdown. C<vars> is a set of name-value pairs to give to the template.
152 #pod
153 #pod =cut
154  
155 15 ▶sub content {
Subroutine content: called CC=1 SCAR=0.0
Pod documented
156 15 my ( $self, %vars ) = @_;
157 15 my $content = $self->document->content;
158 15 my $rendered = $self->_render_content_template( $content, \%vars );
159 15 return $self->markdown->markdown( $rendered );
160 }
161  
162 #pod =method vars
163 #pod
164 #pod my %vars = $page->vars;
165 #pod
166 #pod Get the template variables for this page.
167 #pod
168 #pod =cut
169  
170 38 ▶around vars => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
171 38 my ( $orig, $self ) = @_;
172 return (
173 38 $self->$orig,
174 doc => $self->document,
175 );
176 53 };
177  
178 #pod =method sections
179 #pod
180 #pod my @sections = $page->sections;
181 #pod my $number_of_sections = $page->sections;
182 #pod my @first_sections = $page->sections( 0, 1 );
183 #pod
184 #pod Get a list of rendered HTML content divided into sections. The Markdown "---"
185 #pod marker divides sections. In scalar context, returns the number of sections.
186 #pod You can also pass the indexes of the sections you want as arguments.
187 #pod
188 #pod For example, to loop over sections in the template:
189 #pod
190 #pod % for my $i ( 0..$page->sections ) {
191 #pod <%= $page->sections( $i ) %>
192 #pod % }
193 #pod
194 #pod =cut
195  
196 53 has _rendered_sections => (
197 is => 'rw',
198 isa => ArrayRef,
199 predicate => '_has_rendered_sections',
200 );
201  
202 35 ▶sub sections {
Subroutine sections: called CC=3 SCAR=11.0
Pod documented
203 35 my ( $self, @indexes ) = @_;
204  
205 35 my @sections;
206 35 ▶ if ( $self->_has_rendered_sections ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_rendered_sections) { } 29 6 35
207 29 @sections = @{ $self->_rendered_sections };
208 }
209 else {
210 @sections =
211 11 map { $self->markdown->markdown( $_ ) }
212 6 map { $self->_render_content_template( $_, {} ) }
213 split /\R---\R/,
214 $self->document->content;
215  
216 6 $self->_rendered_sections( \@sections );
217 }
218  
219 35 ▶ return @indexes ? @sections[ @indexes ] : @sections;
Branch: 1 branch
BranchTrueFalseTotal
@indexes ? : 2 33 35
220 }
221  
222 #pod =method tags
223 #pod
224 #pod my @tags = $page->tags;
225 #pod
226 #pod Get the list of tags for this page.
227 #pod
228 #pod =cut
229  
230 28 ▶sub tags {
Subroutine tags: called CC=2 SCAR=7.4
Pod documented
231 28 my ( $self, $new_tags ) = @_;
232 28 ▶ if ( $new_tags ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_tags) 0 28 28
233 0 return $self->_tags( $new_tags );
234 }
235 28 return @{ $self->_tags };
236 }
237  
238 #pod =method template
239 #pod
240 #pod my $tmpl = $page->template;
241 #pod
242 #pod The L<template object|Statocles::Template> for this page. If the document has a template,
243 #pod it will be used. Otherwise, the L<template attribute|Statocles::Page/template> will
244 #pod be used.
245 #pod
246 #pod =cut
247  
248 13 ▶around template => sub {
Subroutine __ANON__: called CC=2 SCAR=8.4
249 13 my ( $orig, $self, @args ) = @_;
250 13 ▶ if ( $self->document->has_template ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->document->has_template) 0 13 13
251 0 return $self->site->theme->template( @{ $self->document->template } );
252 }
253 13 return $self->$orig( @args );
254 53 };
255  
256 #pod =method layout
257 #pod
258 #pod my $tmpl = $page->layout;
259 #pod
260 #pod The L<layout template object|Statocles::Template> for this page. If the document has a layout,
261 #pod it will be used. Otherwise, the L<layout attribute|Statocles::Page/layout> will
262 #pod be used.
263 #pod
264 #pod =cut
265  
266 13 ▶around layout => sub {
Subroutine __ANON__: called CC=2 SCAR=8.4
267 13 my ( $orig, $self, @args ) = @_;
268 13 ▶ if ( $self->document->has_layout ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->document->has_layout) 0 13 13
269 0 return $self->site->theme->template( @{ $self->document->layout } );
270 }
271 13 return $self->$orig( @args );
272 53 };
273  
274 #pod =attr next
275 #pod
276 #pod The path to the next document if it is part of a list.
277 #pod Defaults to the L<Statocles::Document/path> from L</next_page> if it exists.
278 #pod
279 #pod =cut
280  
281 has next => (
282 is => 'rw',
283 lazy => 1,
284 isa => PagePath|Undef,
285 coerce => PagePath->coercion,
286 12 ▶ default => sub { my $p = $_[0]->_page_path('next_page'); $p && $p !~ /^\// ? "/$p" : $p },
Subroutine __ANON__: called CC=3 SCAR=13.1
Branch: 1 branch
BranchTrueFalseTotal
$p && !($p =~ m[^/]) ? : 0 12 12
Condition33%1/3
$p and not $p =~ m[^/]
!ll&&!rl&&r
1200
MC/DC0%0/2
$p and not $p =~ m[^/]
$p not $p =~ m[^/]
Truth table33%1/3
A and not(B)
A= $pB= not $p =~ m[^/]
ABresult
0X0
100
111
287 53 );
288  
289 #pod =attr prev
290 #pod
291 #pod The path to the previous document if it is part of a list.
292 #pod Defaults to the L<Statocles::Document/path> from L</prev_page> if it exists.
293 #pod
294 #pod =cut
295  
296 has prev => (
297 is => 'rw',
298 lazy => 1,
299 isa => PagePath|Undef,
300 coerce => PagePath->coercion,
301 12 ▶ default => sub { my $p = $_[0]->_page_path('prev_page'); $p && $p !~ /^\// ? "/$p" : $p },
Subroutine __ANON__: called CC=3 SCAR=13.1
Branch: 1 branch
BranchTrueFalseTotal
$p && !($p =~ m[^/]) ? : 0 12 12
Condition33%1/3
$p and not $p =~ m[^/]
!ll&&!rl&&r
1200
MC/DC0%0/2
$p and not $p =~ m[^/]
$p not $p =~ m[^/]
Truth table33%1/3
A and not(B)
A= $pB= not $p =~ m[^/]
ABresult
0X0
100
111
302 53 );
303  
304 24 ▶sub _page_path {
Subroutine _page_path: called CC=2 SCAR=7.6
305 24 my ( $self, $method ) = @_;
306 24 ▶ if ( my $page = $self->$method() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $page = $self->$method) 0 24 24
307 0 return $page->path;
308 }
309 24 return undef;
310 }
311  
312 #pod =attr next_page
313 #pod
314 #pod The L<Statocles::Page::Document> instance of the next document if it is part of a list.
315 #pod
316 #pod =attr prev_page
317 #pod
318 #pod The L<Statocles::Page::Document> instance of the previous document if it is part of a list.
319 #pod
320 #pod =cut
321  
322 53 has [qw( next_page prev_page )] => (
323 is => 'rw',
324 isa => InstanceOf['Statocles::Page::Document'],
325 );
326  
327 53 1;
328  
329 __END__
« blib/lib/Statocles.pm ↑ Summary blib/lib/Statocles/Command/status.pm »