blib/lib/Statocles/Page.pm

filter: Statementstmt 100% 58 / 58 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 18 / 18 Podpod 100% 5 / 5 totaltotal 98.8% 84 / 85 CC 3 SCAR 1.8
CC
3
Cov
98%
__ANON__
2.0
render
2.0
BEGIN
1.0
CRAP 1.2
« blib/lib/Statocles/Base.pm ↑ Summary bin/statocles »
1 package Statocles::Page;
2 59 our $VERSION = '0.099';
3 # ABSTRACT: Base role for rendering files
4  
5 59 ▶use Statocles::Base 'Role';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 59 with 'Statocles::Role::PageAttrs';
7 59 ▶use Statocles::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 59 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #pod =attr site
11 #pod
12 #pod The site this page is part of.
13 #pod
14 #pod =cut
15  
16 has site => (
17 is => 'rw',
18 isa => InstanceOf['Statocles::Site'],
19 lazy => 1,
20 168 ▶ default => sub { $Statocles::SITE },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 59 );
22  
23 #pod =attr app
24 #pod
25 #pod The application this page came from, so we can give it to the templates.
26 #pod
27 #pod =cut
28  
29 59 has app => (
30 is => 'rw',
31 isa => ConsumerOf['Statocles::App'],
32 );
33  
34 #pod =attr path
35 #pod
36 #pod The absolute URL path to save this page to.
37 #pod
38 #pod =cut
39  
40 59 has path => (
41 is => 'rw',
42 isa => PagePath,
43 coerce => PagePath->coercion,
44 required => 1,
45 );
46  
47 #pod =attr type
48 #pod
49 #pod The MIME type of this page. By default, will use the L<path's|/path> file extension
50 #pod to detect a likely type.
51 #pod
52 #pod =cut
53  
54 59 our %TYPES = (
55 # text
56 html => 'text/html',
57 markdown => 'text/markdown',
58 css => 'text/css',
59  
60 # image
61 jpg => 'image/jpeg',
62 jpeg => 'image/jpeg',
63 png => 'image/png',
64 gif => 'image/gif',
65  
66 # application
67 rss => 'application/rss+xml',
68 atom => 'application/atom+xml',
69 js => 'application/javascript',
70 json => 'application/json',
71 );
72  
73 has type => (
74 is => 'ro',
75 isa => Str,
76 lazy => 1,
77 16 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
78 16 my ( $self ) = @_;
79 16 my $filename = (split '/', $self->path.'', -1)[-1];
80 16 ▶ return $TYPES{html} if $filename eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $filename eq "" 0 16 16
81 16 my ( $ext ) = $filename =~ /[.]([^.]+)$/;
82 16 return $TYPES{ $ext };
83 },
84 59 );
85  
86 #pod =attr date
87 #pod
88 #pod The date of this page. Used for last updated date and blog post dates.
89 #pod
90 #pod =cut
91  
92 has date => (
93 is => 'rw',
94 isa => DateTimeObj,
95 coerce => DateTimeObj->coercion,
96 lazy => 1,
97 6 ▶ default => sub { DateTime::Moonpig->now( time_zone => 'local' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
98 59 );
99  
100 #pod =attr data
101 #pod
102 #pod A hash of additional template variables for this page.
103 #pod
104 #pod =cut
105  
106 # XXX: For now this is the only way to add arbitrary template vars to
107 # the page. In the Statocles::Page::Document class, it defaults to the
108 # data attribute of the Document object. I suspect this might create
109 # a conflict when both the document and the application need to add
110 # arbitrary template variables. If that happens, we will require a new,
111 # application-only attribute.
112 has data => (
113 is => 'ro',
114 isa => HashRef,
115 103 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
116 59 );
117  
118 #pod =attr markdown
119 #pod
120 #pod The markdown object to render document Markdown. Defaults to L<the markdown
121 #pod attribute from the Site object|Statocles::Site/markdown>.
122 #pod
123 #pod Any object with a "markdown" method will work.
124 #pod
125 #pod =cut
126  
127 has markdown => (
128 is => 'rw',
129 isa => HasMethods['markdown'],
130 168 ▶ default => sub { $_[0]->site->markdown },
Subroutine __ANON__: called CC=1 SCAR=0.0
131 59 );
132  
133 #pod =attr template
134 #pod
135 #pod The main L<template|Statocles::Template> for this page. The result will be
136 #pod wrapped in the L<layout template|/layout>.
137 #pod
138 #pod =cut
139  
140 my @template_attrs = (
141 is => 'rw',
142 isa => InstanceOf['Statocles::Template'],
143 coerce => Statocles::Template->coercion,
144 271 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
145 271 Statocles::Template->new( content => '<%= content %>' ),
146 },
147 59 );
148  
149 59 has template => @template_attrs;
150  
151 #pod =attr layout
152 #pod
153 #pod The layout L<template|Statocles::Template> for this page, which will wrap the content generated by the
154 #pod L<template|/template>.
155 #pod
156 #pod =cut
157  
158 59 has layout => @template_attrs;
159  
160 #pod =attr search_change_frequency
161 #pod
162 #pod How frequently a search engine should check this page for changes. This is used
163 #pod in the L<sitemap.xml|http://www.sitemaps.org> to give hints to search engines.
164 #pod
165 #pod Should be one of:
166 #pod
167 #pod always
168 #pod hourly
169 #pod daily
170 #pod weekly
171 #pod monthly
172 #pod yearly
173 #pod never
174 #pod
175 #pod Defaults to C<weekly>.
176 #pod
177 #pod B<NOTE:> This is only a hint to search engines, not a command. Pages marked C<hourly>
178 #pod may be checked less often, and pages marked C<never> may still be checked once in a
179 #pod while. C<never> is mainly used for archived pages or permanent links.
180 #pod
181 #pod =cut
182  
183 has search_change_frequency => (
184 is => 'rw',
185 isa => Enum[qw( always hourly daily weekly monthly yearly never )],
186 137 ▶ default => sub { 'weekly' },
Subroutine __ANON__: called CC=1 SCAR=0.0
187 59 );
188  
189 #pod =attr search_priority
190 #pod
191 #pod How high should this page rank in search results compared to similar pages on
192 #pod this site? This is used in the L<sitemap.xml|http://www.sitemaps.org> to rank
193 #pod individual, full pages more highly than aggregate, list pages.
194 #pod
195 #pod Value should be between C<0.0> and C<1.0>. The default is C<0.5>.
196 #pod
197 #pod This is only used to decide which pages are more important for the search
198 #pod engine to crawl, and which pages within your site should be given to users. It
199 #pod does not improve your rankings compared to other sites. See L<the sitemap
200 #pod protocol|http://sitemaps.org> for details.
201 #pod
202 #pod =cut
203  
204 has search_priority => (
205 is => 'rw',
206 isa => Num,
207 137 ▶ default => sub { 0.5 },
Subroutine __ANON__: called CC=1 SCAR=0.0
208 59 );
209  
210 # _content_sections
211 #
212 # The saved content sections from any rendered content templates. This
213 # is private for now. We might make this public later
214 has _content_sections => (
215 is => 'rw',
216 isa => HashRef,
217 165 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
218 59 );
219  
220 #pod =attr dom
221 #pod
222 #pod A L<Mojo::DOM> object containing the HTML DOM of the rendered content for
223 #pod this page. Any edits made to this object will be reflected in the file
224 #pod written.
225 #pod
226 #pod Editing this DOM object is the recommended way to edit pages.
227 #pod
228 #pod =cut
229  
230 has dom => (
231 is => 'ro',
232 isa => InstanceOf['Mojo::DOM'],
233 lazy => 1,
234 28 ▶ default => sub { Mojo::DOM->new( shift->render ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
235 59 );
236  
237 #pod =method has_dom
238 #pod
239 #pod Returns true if the page can render a DOM
240 #pod
241 #pod =cut
242  
243 28 ▶sub has_dom { 1 }
Subroutine has_dom: called CC=1 SCAR=0.0
Pod documented
244  
245 #pod =method vars
246 #pod
247 #pod my %vars = $page->vars;
248 #pod
249 #pod Get extra template variables for this page
250 #pod
251 #pod =cut
252  
253 56 ▶sub vars {
Subroutine vars: called CC=1 SCAR=0.0
Pod documented
254 56 my ( $self ) = @_;
255 return (
256 56 app => $self->app,
257 site => $self->site,
258 self => $self,
259 page => $self,
260 );
261 }
262  
263 #pod =method render
264 #pod
265 #pod my $html = $page->render( %vars );
266 #pod
267 #pod Render the page, using the L<template|Statocles::Page/template> and wrapping
268 #pod with the L<layout|Statocles::Page/layout>. Give any extra C<%vars> to the
269 #pod template, layout, and page C<content> method (if applicable).
270 #pod
271 #pod The result of this method is cached.
272 #pod
273 #pod =cut
274  
275 30 ▶sub render {
Subroutine render: called CC=2 SCAR=6.9
Pod documented
276 30 my ( $self ) = @_;
277  
278 30 $self->site->log->debug( 'Render page: ' . $self->path );
279  
280 my %vars = (
281 30 %{ $self->data },
282 $self->vars,
283 );
284  
285 30 ▶ my %tmpl_vars = (
Branch: 1 branch
BranchTrueFalseTotal
$self->can('content') ? : 29 1 30
286 # XXX: This is suboptimal. Isn't vars() enough?
287 ( $self->can( 'content' ) ? ( content => $self->content( %vars ) ) : () ),
288 %vars,
289 );
290  
291 30 my $content = $self->template->render( %tmpl_vars );
292  
293 30 my $html = $self->layout->render(
294 content => $content,
295 %vars,
296 );
297  
298 30 return $html;
299 }
300  
301 #pod =method basename
302 #pod
303 #pod my $name = $page->basename;
304 #pod
305 #pod Get the base file name of this page. Everything after the last C</>.
306 #pod
307 #pod =cut
308  
309 3 ▶sub basename {
Subroutine basename: called CC=1 SCAR=0.0
Pod documented
310 3 my ( $self ) = @_;
311 3 return $self->path->[-1];
312 }
313  
314 #pod =method dirname
315 #pod
316 #pod my $dir = $page->dirname;
317 #pod
318 #pod Get the full directory to this page. Anything that isn't part of L</basename>.
319 #pod
320 #pod There will not be a trailing slash unless it is the root directory.
321 #pod
322 #pod =cut
323  
324 14 ▶sub dirname {
Subroutine dirname: called CC=1 SCAR=0.0
Pod documented
325 14 my ( $self ) = @_;
326 14 my $clone = $self->path->clone;
327 14 pop @$clone;
328 14 return $clone.'';
329 }
330  
331 59 1;
332  
333 __END__
« blib/lib/Statocles/Base.pm ↑ Summary bin/statocles »