File Coverage

blib/lib/Statocles/Document.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Statocles::Document;
2             our $VERSION = '0.084';
3             # ABSTRACT: Base class for all Statocles documents
4              
5 68     68   437 use Statocles::Base 'Class';
  68         149  
  68         531  
6 68     68   499650 use Statocles::Image;
  68         252  
  68         2736  
7 68     68   579 use Statocles::Util qw( derp );
  68         143  
  68         50598  
8              
9             #pod =attr path
10             #pod
11             #pod The path to this document. This is not settable from the frontmatter.
12             #pod
13             #pod =cut
14              
15             has path => (
16             is => 'rw',
17             isa => Path,
18             coerce => Path->coercion,
19             );
20              
21             #pod =attr store
22             #pod
23             #pod The Store this document comes from. This is not settable from the
24             #pod frontmatter.
25             #pod
26             #pod =cut
27              
28             has store => (
29             is => 'ro',
30             isa => Store,
31             coerce => Store->coercion,
32             );
33              
34             #pod =attr title
35             #pod
36             #pod ---
37             #pod title: My First Post
38             #pod ---
39             #pod
40             #pod The title of this document. Used in the template and the main page
41             #pod title. Any unsafe characters in the title (C<E<lt>>, C<E<gt>>, C<">, and
42             #pod C<&>) will be escaped by the template, so no HTML allowed.
43             #pod
44             #pod =cut
45              
46             has title => (
47             is => 'rw',
48             isa => Str,
49             );
50              
51             #pod =attr author
52             #pod
53             #pod ---
54             #pod author: preaction <doug@example.com>
55             #pod ---
56             #pod
57             #pod The author of this document. Optional. Either a simple string containing
58             #pod the author's name and optionally, in E<gt>E<lt>, the author's e-mail address,
59             #pod or a hashref of L<Statocles::Person attributes|Statocles::Person/ATTRIBUTES>.
60             #pod
61             #pod ---
62             #pod # Using Statocles::Person attributes
63             #pod author:
64             #pod name: Doug Bell
65             #pod email: doug@example.com
66             #pod ---
67             #pod
68             #pod =cut
69              
70             has author => (
71             is => 'rw',
72             isa => Person,
73             coerce => Person->coercion,
74             );
75              
76             #pod =attr status
77             #pod
78             #pod The publishing status of this document. Optional. Statocles apps can
79             #pod examine this to determine whether to turn a document into a page. The
80             #pod default value is C<published>; other reasonable values could include
81             #pod C<draft> or C<private>.
82             #pod
83             #pod =cut
84              
85             has status => (
86             is => 'rw',
87             isa => Str,
88             default => 'published',
89             );
90              
91             #pod =attr content
92             #pod
93             #pod The raw content of this document, in markdown. This is everything below
94             #pod the ending C<---> of the frontmatter.
95             #pod
96             #pod =cut
97              
98             has content => (
99             is => 'rw',
100             isa => Str,
101             );
102              
103             #pod =attr tags
104             #pod
105             #pod ---
106             #pod tags: recipe, beef, cheese
107             #pod tags:
108             #pod - recipe
109             #pod - beef
110             #pod - cheese
111             #pod ---
112             #pod
113             #pod The tags for this document. Tags are used to categorize documents.
114             #pod
115             #pod Tags may be specified as an array or as a comma-separated string of
116             #pod tags.
117             #pod
118             #pod =cut
119              
120             has tags => (
121             is => 'rw',
122             isa => ArrayRef,
123             default => sub { [] },
124             coerce => sub {
125             return [] unless $_[0];
126             if ( !ref $_[0] ) {
127             return [ split /\s*,\s*/, $_[0] ];
128             }
129             return $_[0];
130             },
131             );
132              
133             #pod =attr links
134             #pod
135             #pod ---
136             #pod links:
137             #pod stylesheet:
138             #pod - href: /theme/css/extra.css
139             #pod alternate:
140             #pod - href: http://example.com/blog/alternate
141             #pod title: A contributed blog
142             #pod ---
143             #pod
144             #pod Related links for this document. Links are used to build relationships
145             #pod to other web addresses. Link categories are named based on their
146             #pod relationship. Some possible categories are:
147             #pod
148             #pod =over 4
149             #pod
150             #pod =item stylesheet
151             #pod
152             #pod Additional stylesheets for the content of this document.
153             #pod
154             #pod =item script
155             #pod
156             #pod Additional scripts for the content of this document.
157             #pod
158             #pod =item alternate
159             #pod
160             #pod A link to the same document in another format or posted to another web site
161             #pod
162             #pod =back
163             #pod
164             #pod Each category contains an arrayref of hashrefs of L<link objects|Statocles::Link>.
165             #pod See the L<Statocles::Link|Statocles::Link> documentation for a full list of
166             #pod supported attributes. The most common attributes are:
167             #pod
168             #pod =over 4
169             #pod
170             #pod =item href
171             #pod
172             #pod The URL for the link.
173             #pod
174             #pod =item text
175             #pod
176             #pod The text of the link. Not needed for stylesheet or script links.
177             #pod
178             #pod =back
179             #pod
180             #pod =cut
181              
182             has links => (
183             is => 'rw',
184             isa => LinkHash,
185             default => sub { +{} },
186             coerce => LinkHash->coercion,
187             );
188              
189             #pod =attr images
190             #pod
191             #pod ---
192             #pod images:
193             #pod title:
194             #pod src: title.jpg
195             #pod alt: A title image for this post
196             #pod banner: banner.jpg
197             #pod ---
198             #pod
199             #pod Related images for this document. These are used by themes to display
200             #pod images in appropriate templates. Each image has a category, like C<title>,
201             #pod C<banner>, or C<thumbnail>, mapped to an L<image object|Statocles::Image>.
202             #pod See the L<Statocles::Image|Statocles::Image> documentation for a full
203             #pod list of supported attributes. The most common attributes are:
204             #pod
205             #pod =over 4
206             #pod
207             #pod =item src
208             #pod
209             #pod The source path of the image. Relative paths will be resolved relative
210             #pod to this document.
211             #pod
212             #pod =item alt
213             #pod
214             #pod The alternative text to display if the image cannot be downloaded or
215             #pod rendered. Also the text to use for non-visual media.
216             #pod
217             #pod =back
218             #pod
219             #pod =cut
220              
221             has images => (
222             is => 'ro',
223             isa => HashRef[InstanceOf['Statocles::Image']],
224             default => sub { +{} },
225             coerce => sub {
226             my ( $ref ) = @_;
227             my %img;
228             for my $name ( keys %$ref ) {
229             my $attrs = $ref->{ $name };
230             if ( !ref $attrs ) {
231             $attrs = { src => $attrs };
232             }
233             $img{ $name } = Statocles::Image->new(
234             %{ $attrs },
235             );
236             }
237             return \%img;
238             },
239             );
240              
241             #pod =attr date
242             #pod
243             #pod ---
244             #pod date: 2015-03-27
245             #pod date: 2015-03-27 12:04:00
246             #pod ---
247             #pod
248             #pod The date/time this document is for. For pages, this is the last modified date.
249             #pod For blog posts, this is the post's date.
250             #pod
251             #pod Should be in C<YYYY-MM-DD> or C<YYYY-MM-DD HH:MM:SS> format.
252             #pod
253             #pod =cut
254              
255             has date => (
256             is => 'rw',
257             isa => DateTimeObj,
258             coerce => DateTimeObj->coercion,
259             predicate => 'has_date',
260             );
261              
262             #pod =attr template
263             #pod
264             #pod ---
265             #pod template: /blog/recipe.html
266             #pod ---
267             #pod
268             #pod The path to a template override for this document. If set, the L<document
269             #pod page|Statocles::Page::Document> will use this instead of the template provided
270             #pod by the application.
271             #pod
272             #pod The template path should not have the final extention (by default C<.ep>).
273             #pod Different template parsers will have different extentions.
274             #pod
275             #pod =cut
276              
277             has template => (
278             is => 'rw',
279             isa => Maybe[ArrayRef[Str]],
280             coerce => sub {
281             return $_[0] if ref $_[0];
282             return [ grep { $_ ne '' } split m{/}, $_[0] ];
283             },
284             predicate => 'has_template',
285             );
286              
287             #pod =attr layout
288             #pod
289             #pod ---
290             #pod layout: /site/layout-dark.html
291             #pod ---
292             #pod
293             #pod The path to a layout template override for this document. If set, the L<document
294             #pod page|Statocles::Page::Document> will use this instead of the layout provided
295             #pod by the application.
296             #pod
297             #pod The template path should not have the final extention (by default C<.ep>).
298             #pod Different template parsers will have different extentions.
299             #pod
300             #pod =cut
301              
302             has layout => (
303             is => 'rw',
304             isa => Maybe[ArrayRef[Str]],
305             coerce => sub {
306             return $_[0] if ref $_[0];
307             return [ grep { $_ ne '' } split m{/}, $_[0] ];
308             },
309             predicate => 'has_layout',
310             );
311              
312             #pod =attr data
313             #pod
314             #pod ---
315             #pod data:
316             #pod ingredients:
317             #pod - Eggs
318             #pod - Milk
319             #pod - Cheese
320             #pod ---
321             #pod % for my $item ( @{ $self->data->{ingredients} } ) {
322             #pod <%= $item %>
323             #pod % }
324             #pod
325             #pod A hash of extra data to attach to this document. This is available
326             #pod immediately in the document content, and later in the page template.
327             #pod
328             #pod Every document's content is parsed as a template. The C<data> attribute can be
329             #pod used in the template to allow for some structured data that would be cumbersome
330             #pod to have to mark up time and again.
331             #pod
332             #pod =cut
333              
334             has data => (
335             is => 'rw',
336             );
337              
338             around BUILDARGS => sub {
339             my ( $orig, $self, @args ) = @_;
340             my $args = $self->$orig( @args );
341             if ( defined $args->{data} && ref $args->{data} ne 'HASH' ) {
342             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.},
343             $args->{path};
344             }
345             return $args;
346             };
347              
348             1;
349              
350             __END__
351              
352             =pod
353              
354             =encoding UTF-8
355              
356             =head1 NAME
357              
358             Statocles::Document - Base class for all Statocles documents
359              
360             =head1 VERSION
361              
362             version 0.084
363              
364             =head1 DESCRIPTION
365              
366             A Statocles::Document is the base unit of content in Statocles.
367             L<Applications|Statocles::App> take documents to build
368             L<pages|Statocles::Page>.
369              
370             Documents are usually written as files, with the L<content|/content> in Markdown,
371             and the other attributes as frontmatter, a block of YAML at the top of the file.
372              
373             An example file with frontmatter looks like:
374              
375             ---
376             title: My Blog Post
377             author: preaction
378             links:
379             stylesheet:
380             - href: /theme/css/extra.css
381             ---
382             In my younger and more vulnerable years, my father gave me some
383              
384             =head1 ATTRIBUTES
385              
386             =head2 path
387              
388             The path to this document. This is not settable from the frontmatter.
389              
390             =head2 store
391              
392             The Store this document comes from. This is not settable from the
393             frontmatter.
394              
395             =head2 title
396              
397             ---
398             title: My First Post
399             ---
400              
401             The title of this document. Used in the template and the main page
402             title. Any unsafe characters in the title (C<E<lt>>, C<E<gt>>, C<">, and
403             C<&>) will be escaped by the template, so no HTML allowed.
404              
405             =head2 author
406              
407             ---
408             author: preaction <doug@example.com>
409             ---
410              
411             The author of this document. Optional. Either a simple string containing
412             the author's name and optionally, in E<gt>E<lt>, the author's e-mail address,
413             or a hashref of L<Statocles::Person attributes|Statocles::Person/ATTRIBUTES>.
414              
415             ---
416             # Using Statocles::Person attributes
417             author:
418             name: Doug Bell
419             email: doug@example.com
420             ---
421              
422             =head2 status
423              
424             The publishing status of this document. Optional. Statocles apps can
425             examine this to determine whether to turn a document into a page. The
426             default value is C<published>; other reasonable values could include
427             C<draft> or C<private>.
428              
429             =head2 content
430              
431             The raw content of this document, in markdown. This is everything below
432             the ending C<---> of the frontmatter.
433              
434             =head2 tags
435              
436             ---
437             tags: recipe, beef, cheese
438             tags:
439             - recipe
440             - beef
441             - cheese
442             ---
443              
444             The tags for this document. Tags are used to categorize documents.
445              
446             Tags may be specified as an array or as a comma-separated string of
447             tags.
448              
449             =head2 links
450              
451             ---
452             links:
453             stylesheet:
454             - href: /theme/css/extra.css
455             alternate:
456             - href: http://example.com/blog/alternate
457             title: A contributed blog
458             ---
459              
460             Related links for this document. Links are used to build relationships
461             to other web addresses. Link categories are named based on their
462             relationship. Some possible categories are:
463              
464             =over 4
465              
466             =item stylesheet
467              
468             Additional stylesheets for the content of this document.
469              
470             =item script
471              
472             Additional scripts for the content of this document.
473              
474             =item alternate
475              
476             A link to the same document in another format or posted to another web site
477              
478             =back
479              
480             Each category contains an arrayref of hashrefs of L<link objects|Statocles::Link>.
481             See the L<Statocles::Link|Statocles::Link> documentation for a full list of
482             supported attributes. The most common attributes are:
483              
484             =over 4
485              
486             =item href
487              
488             The URL for the link.
489              
490             =item text
491              
492             The text of the link. Not needed for stylesheet or script links.
493              
494             =back
495              
496             =head2 images
497              
498             ---
499             images:
500             title:
501             src: title.jpg
502             alt: A title image for this post
503             banner: banner.jpg
504             ---
505              
506             Related images for this document. These are used by themes to display
507             images in appropriate templates. Each image has a category, like C<title>,
508             C<banner>, or C<thumbnail>, mapped to an L<image object|Statocles::Image>.
509             See the L<Statocles::Image|Statocles::Image> documentation for a full
510             list of supported attributes. The most common attributes are:
511              
512             =over 4
513              
514             =item src
515              
516             The source path of the image. Relative paths will be resolved relative
517             to this document.
518              
519             =item alt
520              
521             The alternative text to display if the image cannot be downloaded or
522             rendered. Also the text to use for non-visual media.
523              
524             =back
525              
526             =head2 date
527              
528             ---
529             date: 2015-03-27
530             date: 2015-03-27 12:04:00
531             ---
532              
533             The date/time this document is for. For pages, this is the last modified date.
534             For blog posts, this is the post's date.
535              
536             Should be in C<YYYY-MM-DD> or C<YYYY-MM-DD HH:MM:SS> format.
537              
538             =head2 template
539              
540             ---
541             template: /blog/recipe.html
542             ---
543              
544             The path to a template override for this document. If set, the L<document
545             page|Statocles::Page::Document> will use this instead of the template provided
546             by the application.
547              
548             The template path should not have the final extention (by default C<.ep>).
549             Different template parsers will have different extentions.
550              
551             =head2 layout
552              
553             ---
554             layout: /site/layout-dark.html
555             ---
556              
557             The path to a layout template override for this document. If set, the L<document
558             page|Statocles::Page::Document> will use this instead of the layout provided
559             by the application.
560              
561             The template path should not have the final extention (by default C<.ep>).
562             Different template parsers will have different extentions.
563              
564             =head2 data
565              
566             ---
567             data:
568             ingredients:
569             - Eggs
570             - Milk
571             - Cheese
572             ---
573             % for my $item ( @{ $self->data->{ingredients} } ) {
574             <%= $item %>
575             % }
576              
577             A hash of extra data to attach to this document. This is available
578             immediately in the document content, and later in the page template.
579              
580             Every document's content is parsed as a template. The C<data> attribute can be
581             used in the template to allow for some structured data that would be cumbersome
582             to have to mark up time and again.
583              
584             =head1 SEE ALSO
585              
586             =over 4
587              
588             =item L<Statocles::Help::Content>
589              
590             The content guide describes how to edit content in Statocles sites, which are
591             represented by Document objects.
592              
593             =back
594              
595             =head1 AUTHOR
596              
597             Doug Bell <preaction@cpan.org>
598              
599             =head1 COPYRIGHT AND LICENSE
600              
601             This software is copyright (c) 2016 by Doug Bell.
602              
603             This is free software; you can redistribute it and/or modify it under
604             the same terms as the Perl 5 programming language system itself.
605              
606             =cut