blib/lib/Statocles/Document.pm

filter: Statementstmt 97.8% 92 / 94 Branchbran 87.5% 28 / 32 Conditioncond 80.0% 12 / 15 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 92.7% 154 / 166 CC 24 SCAR 18.2
CC
24
Cov
94%
parse_content
11.0
deparse_content
8.6
__ANON__
3.0
CRAP 6.2
« blib/lib/Statocles/Plugin/Highlight.pm ↑ Summary blib/lib/Statocles/Page/List.pm »
1 package Statocles::Document;
2 101 our $VERSION = '0.099';
3 # ABSTRACT: Base class for all Statocles documents
4  
5 101 ▶use Statocles::Base 'Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 101 with 'Statocles::Role::PageAttrs';
7 101 ▶use Statocles::Image;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 101 ▶use Statocles::Util qw( derp );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 101 ▶use YAML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 101 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 #pod =attr path
13 #pod
14 #pod The path to this document. This is not settable from the frontmatter.
15 #pod
16 #pod =cut
17  
18 101 has path => (
19 is => 'rw',
20 isa => PagePath,
21 coerce => PagePath->coercion,
22 );
23  
24 #pod =attr store
25 #pod
26 #pod The Store this document comes from. This is not settable from the
27 #pod frontmatter.
28 #pod
29 #pod =cut
30  
31 101 has store => (
32 is => 'ro',
33 isa => StoreType,
34 coerce => StoreType->coercion,
35 );
36  
37 #pod =attr title
38 #pod
39 #pod ---
40 #pod title: My First Post
41 #pod ---
42 #pod
43 #pod The title of this document. Used in the template and the main page
44 #pod title. Any unsafe characters in the title (C<E<lt>>, C<E<gt>>, C<">, and
45 #pod C<&>) will be escaped by the template, so no HTML allowed.
46 #pod
47 #pod =cut
48  
49 #pod =attr author
50 #pod
51 #pod ---
52 #pod author: preaction <doug@example.com>
53 #pod ---
54 #pod
55 #pod The author of this document. Optional. Either a simple string containing
56 #pod the author's name and optionally, in E<gt>E<lt>, the author's e-mail address,
57 #pod or a hashref of L<Statocles::Person attributes|Statocles::Person/ATTRIBUTES>.
58 #pod
59 #pod ---
60 #pod # Using Statocles::Person attributes
61 #pod author:
62 #pod name: Doug Bell
63 #pod email: doug@example.com
64 #pod ---
65 #pod
66 #pod =cut
67  
68 12 ▶sub _build_author { }
Subroutine _build_author: called CC=1 SCAR=0.0
69  
70 #pod =attr status
71 #pod
72 #pod The publishing status of this document. Optional. Statocles apps can
73 #pod examine this to determine whether to turn a document into a page. The
74 #pod default value is C<published>; other reasonable values could include
75 #pod C<draft> or C<private>.
76 #pod
77 #pod =cut
78  
79 101 has status => (
80 is => 'rw',
81 isa => Str,
82 default => 'published',
83 );
84  
85 #pod =attr content
86 #pod
87 #pod The raw content of this document, in markdown. This is everything below
88 #pod the ending C<---> of the frontmatter.
89 #pod
90 #pod =cut
91  
92 101 has content => (
93 is => 'rw',
94 isa => Str,
95 );
96  
97 #pod =attr tags
98 #pod
99 #pod ---
100 #pod tags: recipe, beef, cheese
101 #pod tags:
102 #pod - recipe
103 #pod - beef
104 #pod - cheese
105 #pod ---
106 #pod
107 #pod The tags for this document. Tags are used to categorize documents.
108 #pod
109 #pod Tags may be specified as an array or as a comma-separated string of
110 #pod tags.
111 #pod
112 #pod =cut
113  
114 has tags => (
115 is => 'rw',
116 isa => ArrayRef,
117 88 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
118 113 ▶ coerce => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
119 113 ▶ return [] unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 3 110 113
120 110 ▶ if ( !ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 6 104 110
121 6 return [ split /\s*,\s*/, $_[0] ];
122 }
123 104 return $_[0];
124 },
125 101 );
126  
127 #pod =attr links
128 #pod
129 #pod ---
130 #pod links:
131 #pod stylesheet:
132 #pod - href: /theme/css/extra.css
133 #pod alternate:
134 #pod - href: http://example.com/blog/alternate
135 #pod title: A contributed blog
136 #pod ---
137 #pod
138 #pod Related links for this document. Links are used to build relationships
139 #pod to other web addresses. Link categories are named based on their
140 #pod relationship. Some possible categories are:
141 #pod
142 #pod =over 4
143 #pod
144 #pod =item stylesheet
145 #pod
146 #pod Additional stylesheets for the content of this document.
147 #pod
148 #pod =item script
149 #pod
150 #pod Additional scripts for the content of this document.
151 #pod
152 #pod =item alternate
153 #pod
154 #pod A link to the same document in another format or posted to another web site
155 #pod
156 #pod =back
157 #pod
158 #pod Each category contains an arrayref of hashrefs of L<link objects|Statocles::Link>.
159 #pod See the L<Statocles::Link|Statocles::Link> documentation for a full list of
160 #pod supported attributes. The most common attributes are:
161 #pod
162 #pod =over 4
163 #pod
164 #pod =item href
165 #pod
166 #pod The URL for the link.
167 #pod
168 #pod =item text
169 #pod
170 #pod The text of the link. Not needed for stylesheet or script links.
171 #pod
172 #pod =back
173 #pod
174 #pod =cut
175  
176 #pod =attr images
177 #pod
178 #pod ---
179 #pod images:
180 #pod title:
181 #pod src: title.jpg
182 #pod alt: A title image for this post
183 #pod banner: banner.jpg
184 #pod ---
185 #pod
186 #pod Related images for this document. These are used by themes to display
187 #pod images in appropriate templates. Each image has a category, like C<title>,
188 #pod C<banner>, or C<thumbnail>, mapped to an L<image object|Statocles::Image>.
189 #pod See the L<Statocles::Image|Statocles::Image> documentation for a full
190 #pod list of supported attributes. The most common attributes are:
191 #pod
192 #pod =over 4
193 #pod
194 #pod =item src
195 #pod
196 #pod The source path of the image. Relative paths will be resolved relative
197 #pod to this document.
198 #pod
199 #pod =item alt
200 #pod
201 #pod The alternative text to display if the image cannot be downloaded or
202 #pod rendered. Also the text to use for non-visual media.
203 #pod
204 #pod =back
205 #pod
206 #pod =cut
207  
208 #pod =attr date
209 #pod
210 #pod ---
211 #pod date: 2015-03-27
212 #pod date: 2015-03-27 12:04:00
213 #pod ---
214 #pod
215 #pod The date/time this document is for. For pages, this is the last modified date.
216 #pod For blog posts, this is the post's date.
217 #pod
218 #pod Should be in C<YYYY-MM-DD> or C<YYYY-MM-DD HH:MM:SS> format.
219 #pod
220 #pod =cut
221  
222 101 has date => (
223 is => 'rw',
224 isa => DateTimeObj,
225 coerce => DateTimeObj->coercion,
226 predicate => 'has_date',
227 );
228  
229 #pod =attr template
230 #pod
231 #pod ---
232 #pod template: /blog/recipe.html
233 #pod ---
234 #pod
235 #pod The path to a template override for this document. If set, the L<document
236 #pod page|Statocles::Page::Document> will use this instead of the template provided
237 #pod by the application.
238 #pod
239 #pod The template path should not have the final extention (by default C<.ep>).
240 #pod Different template parsers will have different extentions.
241 #pod
242 #pod =cut
243  
244 has template => (
245 is => 'rw',
246 isa => Maybe[ArrayRef[Str]],
247 6 ▶ coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
248 6 ▶ return $_[0] if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 6 6
249 6 return [ grep { $_ ne '' } split m{/}, $_[0] ];
250 },
251 101 predicate => 'has_template',
252 );
253  
254 #pod =attr layout
255 #pod
256 #pod ---
257 #pod layout: /site/layout-dark.html
258 #pod ---
259 #pod
260 #pod The path to a layout template override for this document. If set, the L<document
261 #pod page|Statocles::Page::Document> will use this instead of the layout provided
262 #pod by the application.
263 #pod
264 #pod The template path should not have the final extention (by default C<.ep>).
265 #pod Different template parsers will have different extentions.
266 #pod
267 #pod =cut
268  
269 has layout => (
270 is => 'rw',
271 isa => Maybe[ArrayRef[Str]],
272 6 ▶ coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
273 6 ▶ return $_[0] if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 6 6
274 6 return [ grep { $_ ne '' } split m{/}, $_[0] ];
275 },
276 101 predicate => 'has_layout',
277 );
278  
279 #pod =attr data
280 #pod
281 #pod ---
282 #pod data:
283 #pod ingredients:
284 #pod - Eggs
285 #pod - Milk
286 #pod - Cheese
287 #pod ---
288 #pod % for my $item ( @{ $self->data->{ingredients} } ) {
289 #pod <%= $item %>
290 #pod % }
291 #pod
292 #pod A hash of extra data to attach to this document. This is available
293 #pod immediately in the document content, and later in the page template.
294 #pod
295 #pod Every document's content is parsed as a template. The C<data> attribute can be
296 #pod used in the template to allow for some structured data that would be cumbersome
297 #pod to have to mark up time and again.
298 #pod
299 #pod =cut
300  
301 101 has data => (
302 is => 'rw',
303 );
304  
305 #pod =attr disable_content_template
306 #pod
307 #pod ---
308 #pod disable_content_template: true
309 #pod ---
310 #pod
311 #pod This disables processing the content as a template. This can speed up processing
312 #pod when the content is not using template directives.
313 #pod
314 #pod This can be also set in the application
315 #pod (L<Statocles::App/disable_content_template>), or for the entire site
316 #pod (L<Statocles::Site/disable_content_template>).
317 #pod
318 #pod =cut
319  
320 101 has disable_content_template => (
321 is => 'ro',
322 isa => Bool,
323 lazy => 1,
324 default => 0,
325 predicate => 'has_disable_content_template',
326 );
327  
328 115 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
329 115 my ( $orig, $self, @args ) = @_;
330 115 my $args = $self->$orig( @args );
331 115 ▶ if ( defined $args->{data} && ref $args->{data} ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"data"} and ref $args->{"data"} ne "HASH") 2 113 115
Condition100%3/3
defined $args->{"data"} and ref $args->{"data"} ne "HASH"
!ll&&!rl&&r
11212
MC/DC100%2/2
defined $args->{"data"} and ref $args->{"data"} ne "HASH"
defined $args->{"data"} ref $args->{"data"} ne "HASH"
Truth table100%3/3
A and B
A= defined $args->{"data"}B= ref $args->{"data"} ne "HASH"
ABresult
0X0
100
111
332 derp qq{Invalid data attribute in document "%s". Data attributes that are not hashes are deprecated and will be removed in v2.0. Please use a hash instead.},
333 2 $args->{path};
334 }
335 115 return $args;
336 101 };
337  
338 #pod =method parse_content
339 #pod
340 #pod my $doc = $class->parse_content(
341 #pod path => $path,
342 #pod store => $store,
343 #pod content => $content,
344 #pod );
345 #pod
346 #pod Construct a document the given content, with the given additional
347 #pod attributes. Returns a new C<Statocles::Document> object.
348 #pod
349 #pod This parses the YAML or JSON frontmatter into the document's attributes,
350 #pod putting the rest of the file after the YAML or JSON frontmatter in the
351 #pod C<content> attribute.
352 #pod
353 #pod Custom document classes can override this method to change how file content is
354 #pod parsed.
355 #pod
356 #pod =cut
357  
358 80 ▶sub parse_content {
Subroutine parse_content: called CC=11 SCAR=24.0
Pod documented
359 80 my ( $class, %args ) = @_;
360  
361 80 my %doc;
362 80 ▶ my $content = delete $args{content} or die "Content is required";
Branch: 1 branch
BranchTrueFalseTotal
my $content = delete $args{"content"} or die "Content is required" 80 0 80
363  
364 80 my @lines = split /\n/, $content;
365 # YAML frontmatter
366 80 ▶ if ( @lines && $lines[0] =~ /^---/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@lines and $lines[0] =~ /^---/) { } 60 20 80
elsif (@lines and $lines[0] =~ /^{/) { } 12 9 21
Condition100%3/3
@lines and $lines[0] =~ /^---/
!ll&&!rl&&r
11960
MC/DC100%2/2
@lines and $lines[0] =~ /^---/
@lines $lines[0] =~ /^---/
Truth table100%3/3
A and B
A= @linesB= $lines[0] =~ /^---/
ABresult
0X0
100
111
367 60 shift @lines;
368  
369 # The next --- is the end of the YAML frontmatter
370 60 my ( $i ) = grep { $lines[ $_ ] =~ /^---/ } 0..$#lines;
371  
372 # If we did not find the marker between YAML and Markdown
373 60 ▶ if ( !defined $i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $i) 1 59 60
374 1 die qq{Could not find end of YAML front matter (---)\n};
375 }
376  
377 # Before the marker is YAML
378 59 eval {
379 59 %doc = %{ YAML::Load( join "\n", splice( @lines, 0, $i ), "" ) };
380 };
381 59 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 58 59
382 1 die qq{Error parsing YAML in "$args{path}"\n$@};
383 }
384  
385 # Remove the last '---' mark
386 58 shift @lines;
387 }
388 # JSON frontmatter
389 ▶ elsif ( @lines && $lines[0] =~ /^{/ ) {
Condition100%3/3
@lines and $lines[0] =~ /^{/
!ll&&!rl&&r
1712
MC/DC100%2/2
@lines and $lines[0] =~ /^{/
@lines $lines[0] =~ /^{/
Truth table100%3/3
A and B
A= @linesB= $lines[0] =~ /^{/
ABresult
0X0
100
111
390 12 my $json;
391 12 ▶ if ( $lines[0] =~ /\}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[0] =~ /\}$/) { } 3 9 12
392 # The JSON is all on a single line
393 3 $json = shift @lines;
394 }
395 else {
396 # The } on a line by itself is the last line of JSON
397 9 my ( $i ) = grep { $lines[ $_ ] =~ /^}$/ } 0..$#lines;
398 # If we did not find the marker between YAML and Markdown
399 9 ▶ if ( !defined $i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $i) 1 8 9
400 1 die qq{Could not find end of JSON front matter (\})\n};
401 }
402 8 $json = join "\n", splice( @lines, 0, $i+1 );
403 }
404 11 eval {
405 11 %doc = %{ JSON::PP->new()->utf8(0)->decode( $json ) };
406 };
407 11 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 10 11
408 1 die qq{Error parsing JSON: $@\n};
409 }
410 }
411  
412 # The remaining lines are content
413 76 $doc{content} = join "\n", @lines, "";
414  
415 76 delete $doc{path};
416 76 delete $doc{store};
417  
418 76 return $class->new( %doc, %args );
419 }
420  
421 #pod =method deparse_content
422 #pod
423 #pod my $content = $doc->deparse_content;
424 #pod
425 #pod Deparse the document into a string ready to be stored in a file. This will
426 #pod serialize the document attributes into YAML frontmatter, and place the content
427 #pod after.
428 #pod
429 #pod =cut
430  
431 6 ▶sub deparse_content {
Subroutine deparse_content: called CC=8 SCAR=21.6
Pod documented
432 6 my ( $self ) = @_;
433 6 my %data = %$self;
434 6 delete $data{ store };
435 6 delete $data{ path };
436 6 my $content = delete $data{content};
437  
438 # Serialize date correctly
439 6 ▶ if ( exists $data{date} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data{"date"}) 2 4 6
440 2 $data{date} = $data{date}->strftime('%Y-%m-%d %H:%M:%S');
441 }
442  
443 # Don't save empty references
444 6 for my $hash_type ( qw( links images ) ) {
445 0 ▶ if ( exists $data{ $hash_type } && !keys %{ $data{ $hash_type } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data{$hash_type} and not keys %{$data{$hash_type};}) 0 12 12
Condition33%1/3
exists $data{$hash_type} and not keys %{$data{$hash_type};}
!ll&&!rl&&r
1200
MC/DC0%0/2
exists $data{$hash_type} and not keys %{$data{$hash_type};}
exists $data{$hash_type} not keys %{$data{$hash_type};}
Truth table33%1/3
A and not(B)
A= exists $data{$hash_type}B= not keys %{$data{$hash_type};}
ABresult
0X0
100
111
446 0 delete $data{ $hash_type };
447 }
448 }
449 6 for my $array_type ( qw( tags ) ) {
450 6 ▶ if ( exists $data{ $array_type } && !@{ $data{ $array_type } } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data{$array_type} and not @{$data{$array_type};}) 4 2 6
Condition66%2/3
exists $data{$array_type} and not @{$data{$array_type};}
!ll&&!rl&&r
024
MC/DC50%1/2
exists $data{$array_type} and not @{$data{$array_type};}
exists $data{$array_type} not @{$data{$array_type};}
Truth table66%2/3
A and not(B)
A= exists $data{$array_type}B= not @{$data{$array_type};}
ABresult
0X0
100
111
451 4 delete $data{ $array_type };
452 }
453 }
454  
455 6 return YAML::Dump( \%data ) . "---\n". $content;
456 }
457  
458 101 1;
459  
460 __END__
« blib/lib/Statocles/Plugin/Highlight.pm ↑ Summary blib/lib/Statocles/Page/List.pm »