lib/Plerd/Init.pm

filter: Statementstmt 83.9% 47 / 56 Branchbran 42.8% 6 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 3 totaltotal 75.0% 63 / 84 CC 10 SCAR 16.0
CC
10
Cov
76%
initialize
10.5
__ANON__
4.0
__ANON__
2.0
CRAP 5.0
« lib/Plerd.pm ↑ Summary bin/plerdall »
1 package Plerd::Init;
2  
3 13 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Path::Class::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Plerd;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 my %file_content;
11  
12 14 ▶sub initialize ( $$ ) {
Subroutine initialize: called CC=7 SCAR=23.5
Pod undocumented
13 14 my ($init_path, $is_using_default) = @_;
14  
15 14 my @messages;
16  
17 14 my $dir = Path::Class::Dir->new( $init_path )->absolute;
18  
19 14 ▶ if ( $is_using_default ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_using_default) 1 13 14
20 1 push @messages,
21 "No directory provided, so using default location ($dir).\n"
22 }
23  
24 14 ▶ if (-e $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dir) { } 0 14 14
25 0 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 0 0
26 return [
27 0 @messages,
28 "$dir exists, but it's not a directory!\nExiting."
29 ];
30 }
31 0 ▶ if ( $dir->children ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir->children) 0 0 0
32 return [
33 0 @messages,
34 "$dir exists, but it's not empty!\nExiting."
35 ];
36 }
37 }
38 else {
39 14 ▶ unless (mkdir $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (mkdir $dir) 0 14 14
40 return [
41 0 @messages,
42 "Cannot create $dir: $!"
43 ];
44 }
45 }
46  
47 14 my $success = populate_directory( $dir, \@messages );
48  
49 14 ▶ if ( $success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) 14 0 14
50 14 my $config_file = Path::Class::File->new( $dir, 'conf', 'plerd.conf' );
51 14 push @messages,
52 "I have created and populated a new Plerd working directory at "
53 . "$dir. Your next step involves updating the configuration file "
54 . "at $config_file.\n"
55 . "For full documentation and links to user resources, "
56 . "please visit http://plerd.jmac.org/. Enjoy!";
57 }
58 14 return \@messages;
59 }
60  
61 14 ▶sub populate_directory ( $$ ) {
Subroutine populate_directory: called CC=1 SCAR=0.2
Pod undocumented
62 14 my ( $dir, $messages ) = @_;
63  
64 14 my %file_content = file_content( $dir );
65  
66 14 ▶ try {
Subroutine __ANON__: called CC=4 SCAR=13.9
67 14 foreach ( qw( docroot source templates log run db conf ) ) {
68 98 my $subdir = Path::Class::Dir->new( $dir, $_ );
69 98 ▶ mkdir $subdir or die "Can't create subdir $subdir: $!";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $subdir or die "Can't create subdir ${subdir}: $!" 98 0 98
70 }
71  
72 14 foreach ( qw( archive atom jsonfeed post wrapper tags ) ) {
73 84 my $template = Path::Class::File->new(
74 $dir, 'templates', "$_.tt",
75 );
76 84 $template->spew( iomode=>'>:encoding(utf8)', $file_content{ $_ } );
77 }
78  
79 14 my $config = Path::Class::File->new(
80 $dir, 'conf', 'plerd.conf',
81 );
82  
83 14 $config->spew( iomode=>'>:encoding(utf8)', $file_content{ config } );
84  
85 }
86 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 push @$messages, $_;
88 0 push @$messages, "I am cowardly declining to clean up $dir. You might "
89 . "need to empty or remove it yourself before trying "
90 . "this command again.";
91 0 push @$messages, "Exiting.";
92 0 return 0;
93 14 };
94  
95 14 return 1;
96  
97 }
98  
99 14 ▶sub file_content ( $ ) {
Subroutine file_content: called CC=1 SCAR=0.0
Pod undocumented
100 14 my ( $dir ) = @_;
101 14 %file_content = (
102 archive => <<EOF,
103 [% WRAPPER wrapper.tt title = plerd.title _ ': Archives ' %]
104  
105 <div class="page-header">
106 <h1>Archive</h1>
107 </div>
108  
109 [% current_month = 0 %]
110 [% FOREACH post IN posts %]
111 [% post_month = post.year _ post.month %]
112 [% IF !current_month || current_month != post_month %]
113 [% IF current_month %]
114 </ul>
115 [% END %]
116 <h2>[% post.month_name %] [% post.year %]</h2>
117 <ul>
118 [% END %]
119 <li><a href="[% post.uri %]">[% post.title %]</a></li>
120 [% current_month = post_month %]
121 [% END %]
122  
123 [% IF current_month %]
124 </ul>
125 [% END %]
126  
127 [% END %]
128 EOF
129 atom => <<EOF,
130 <?xml version="1.0" encoding="utf-8"?>
131 <feed xmlns="http://www.w3.org/2005/Atom">
132  
133 <title><![CDATA[[% plerd.title %]]]></title>
134 <link href="[% plerd.base_uri %]/atom.xml" rel="self" />
135 <link href="[% plerd.base_uri %]" />
136 <updated>[% timestamp %]</updated>
137 <id>[% plerd.base_uri %]</id>
138 <author>
139 <name><![CDATA[[% plerd.author_name %]]]></name>
140 <email><![CDATA[[% plerd.author_email %]]]></email>
141 </author>
142 <generator uri="https://github.com/jmacdotorg/plerd">Plerd</generator>
143  
144 [% FOR post IN posts %]
145 <entry>
146 <title type="html"><![CDATA[[% post.title %]]]></title>
147 <link href="[% post.uri %]"/>
148 <published>[% post.published_timestamp %]</published>
149 <updated>[% post.updated_timestamp %]</updated>
150 <id>[% post.uri %]</id>
151 <content type="html"><![CDATA[[% post.body %]]]></content>
152 </entry>
153 [% END %]
154  
155 </feed>
156 EOF
157 jsonfeed => <<EOF,
158 {
159 "version": "https://jsonfeed.org/version/1",
160 "title": "[% plerd.title %]",
161 "home_page_url": "[% plerd.base_uri %]",
162 "feed_url": "[% plerd.base_uri %]/feed.json",
163 "user_comment": "This feed allows you to read the posts from this site in any feed reader that supports the JSON Feed format. To add this feed to your reader, copy the following URL — [% plerd.base_uri %]/feed.json — and add it your reader.",
164 "author": {
165 "name": "[% plerd.author_name %]"[% IF plerd.author_email %],
166 "url": "mailto:[% plerd.author_email %]"[% END %]
167 },
168 "items": [
169 [% post_count = 0 %]
170 [% FOR post IN posts %]
171 [% post_count = post_count + 1 %]
172 {
173 "id": "[% post.uri %]",
174 "url": "[% post.uri %]",
175 "title": "[% post.stripped_title %]",
176 "content_html": "[% post.body | json %]",
177 "date_published": "[% post.published_timestamp %]",
178 "date_modified": "[% post.updated_timestamp %]"
179 }[% IF post_count < posts.size %],[% END %]
180 [% END %]
181 ]
182 }
183 EOF
184 post => <<EOF,
185 [% WRAPPER wrapper.tt %]
186  
187 [% FOREACH post IN posts %]
188 <div class="post h-entry">
189 <div class="title page-header"><h1><a href="[% post.uri %]"><span class="p-name">[% post.title %]</span><br /><small>[% post.month_name %] [% post.day %], [% post.year %]</small></a></h1></div>
190  
191 <data class="dt-published" value="[% post.ymd %] [% post.hms %]"></data>
192 <data class="p-author h-card">
193 <data class="p-name" value="[% post.plerd.author_name | html %]"></data>
194 </data>
195 <data class="p-summary" value="[% post.description | html %]"></data>
196 <data class="u-url u-uid" value="[% post.uri %]"></data>
197  
198 <div class="body e-content">[% post.body %]</div>
199 [% IF post.tags.size > 0 %]
200 <ul class="list-inline tag-list">
201 <li class="list-inline-item">Tags:</li>
202 [% FOREACH tag = post.tags.sort %]
203 <li class="list-inline-item p-category"><a href="[% plerd.tag_uri(tag) %]">[% tag %]</a></li>
204 [% END %]
205 </ul>
206 [% END %]
207 </div>
208 [% END %]
209  
210 [% IF context_post %]
211  
212 <div>
213 <hr />
214 [% IF post.newer_post %]
215 <p>Next post: <a href="[% post.newer_post.uri %]">[% post.newer_post.title %]</a></p>
216 [% END %]
217 [% IF post.older_post %]
218 <p>Previous post: <a href="[% post.older_post.uri %]">[% post.older_post.title %]</a></p>
219 [% END %]
220 </div>
221  
222 [% ELSIF post_counter == posts.size %]
223 <div>
224 <hr />
225 [% IF post.older_post %]
226 <p>Previous post: <a href="[% post.older_post.uri %]">[% post.older_post.title %]</a></p>
227 [% END %]
228 </div>
229 [% END %]
230  
231 [% END %]
232  
233  
234 <style>
235 /* img.media-object { max-width: 64px } */
236 </style>
237 EOF
238 wrapper => <<EOF,
239 <!DOCTYPE html>
240 <html>
241 <head>
242 <title>[% title %]</title>
243 <meta name="viewport" content="width=device-width, initial-scale=1.0">
244 <link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
245 <link href="atom.xml" rel="alternate" title="Atom feed" type="application/atom+xml">
246 <link href="feed.json" rel="alternate" title="JSON feed" type="application/json">
247  
248 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
249 [% IF context_post %]
250 [% context_post.social_meta_tags %]
251 [% END %]
252 <style>
253 .page-header h1 :link, .page-header h1 :visited {
254 color: black;
255 }
256 .sidebar h1 {
257 font-size: 1.5em;
258 }
259 img.author-link {
260 width: 20px;
261 }
262 .sidebar section {
263 margin-top: 2em;
264 }
265 img {
266 width: 100%;
267 }
268 </style>
269 </head>
270 <body>
271 <div class="navbar navbar-default" role="navigation">
272 <div class="container">
273 <div class="navbar-header">
274 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
275 <span class="sr-only">Toggle navigation</span>
276 <span class="icon-bar"></span>
277 <span class="icon-bar"></span>
278 <span class="icon-bar"></span>
279 </button>
280 <a class="navbar-brand" href="recent.html">[% plerd.title %]</a>
281 </div>
282 <div class="navbar-collapse collapse">
283 <ul class="nav navbar-nav">
284 <li><a href="archive.html">Archive</a></li>
285 <li><a href="atom.xml">RSS</a></li>
286 </ul>
287 </div><!--/.navbar-collapse -->
288 </div>
289 </div>
290  
291 <div class="container">
292 <div class="row">
293 <div class="col-sm-9">
294 [% content %]
295 </div>
296 <div class="col-sm-3 sidebar">
297 <section>
298 <h1>Hello</h1>
299 <p>
300 This is a blog by <a href="mailto:[% plerd.author_email %]">[% plerd.author_name %]</a>.
301 </p>
302 </section>
303 <section>
304 <h1>Search</h1>
305 <form action="https://duckduckgo.com" method="get">
306 <input name="q" type="text" placeholder="Search this blog" />
307 <input name="sites" type="hidden" value="[% plerd.base_uri %]" />
308 <input type="submit" value="Go" />
309 </form>
310 </section>
311 <section>
312 <h1>Recent Posts</h1>
313 <ul>
314 [% FOR post IN plerd.recent_posts %]
315 <li><a href="[% post.uri %]">[% post.title %]</a></li>
316 [% END %]
317 </ul>
318 </section>
319 </div>
320 </div>
321 <footer style="font-size:small; font-style:italic" class="container">
322 <hr />
323 <p>Powered by <a href="http://jmac.org/plerd">Plerd</a>.</p>
324 </footer>
325 </div>
326  
327 <script type="text/javascript" src="https://code.jquery.com/jquery.js"></script>
328 <script type="text/javascript" src="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
329 </body>
330 </html>
331 EOF
332 config => <<"EOF",
333 # This is a configuration file for a single Plerd-based blog!
334 #
335 # Update the values below to best suit your blogging needs. After that,
336 # you can then either copy (or symlink) this file to .plerd in your home
337 # directory for use as a system-wide default, or you can specify this
338 # file when running the `plerd` or `plerdwatcher` programs through their
339 # --config command-line option. (See these programs' respective man
340 # pages for more details.)
341  
342 #####################
343 # Required setup
344 #####################
345 # Values for these fields are all required for Plerd to work properly.
346  
347 # base_uri: The URI base that this blog will use when generating various
348 # self-pointing links. Generally, this should be the same as your
349 # blog's "front page" URL.
350 base_uri: http://blog.example.com/
351  
352 # path: The path on your filesystem where Plerd will find this blog's
353 # source, docroot, and other important sub-directories. (If you don't
354 # want to keep all these under a single master directory, see
355 # "Split-location setup", below.)
356 path: $dir
357  
358 # title: This blog's title.
359 title: My Cool Blog
360  
361 # author_name: The name of this blog's author.
362 author_name: Sam Handwich
363  
364 # author_email: The email address of this blog's author.
365 author_email: s.handwich\@example.com
366  
367 ######################
368 # Social-media setup
369 ######################
370 # Plerd can add Open Graph metadata tags to each post, so that services like
371 # Discord, Slack, and Mastodon can present an attractive summary when someone
372 # shares a link to one of your posts. Plerd generates these tags for any post
373 # that has an image associated with it -- either its own, or the blog-wide
374 # fallback image configured here.
375  
376 # image: Your blog's "logo", used as a fallback image for posts that do not
377 # define an image themselves.
378 # Optional, but if you don't define this, then posts without their
379 # own image attributes won't get any social-media metatags.
380 image: http://blog.example.com/images/blog_logo.png
381  
382 # image_alt: A text description of your image, equivalent to the "alt" attribute
383 # in HTML <img> tags, and useful for visually impaired visitors to
384 # your blog.
385 # Optional, but if you define an image, you should define this too.
386 image_alt: "My Cool Blog's logo -- a photograph of Fido, the author's gray tabby cat."
387  
388 ######################
389 # Split-location setup
390 ######################
391 # If you don't want to keep all of Plerd's required directories under a single
392 # master directory (configured withe the "path" directive, as seen above), then
393 # you can define the directories with these separate directives instead:
394 #
395 # source_path: /home/me/Dropbox/plerd/source
396 # publication_path: /var/www/html/blog/
397 # template_path: /yet/another/path/templates
398 # database_path: /opt/plerd/db
399 # run_path: /tmp/plerd/run
400 # log_path: /var/log/plerd/
401 EOF
402 tags => <<EOF,
403 [% WRAPPER wrapper.tt title = 'Tags' %]
404  
405 [% IF is_tags_index_page %]
406 <section>
407 <h1>All Tags</h1>
408  
409 <ul>
410 [% FOREACH tag = tags.keys.sort %]
411 <li><a href="[% plerd.tag_uri(tag) %]">[% tag %]</a> ([% tags.\$tag.size %])</li>
412 [% END %]
413 </ul>
414 </section>
415 [% ELSE %]
416  
417 [% FOREACH tag = tags.keys %]
418 <h1>Tag: [% tag %]</h1>
419 <ul>
420 [% FOREACH post = tags.\$tag %]
421 <li><a href="[% post.uri %]">[% post.title %]</a></li>
422 [% END %]
423 </ul>
424 [% END %]
425  
426 [% END %]
427  
428 [% END %]
429  
430 EOF
431 );
432 14 return %file_content;
433 }
434  
435 13 1;
436  
437 =head1 NAME
438  
439 Plerd::Init
440  
441 =head1 DESCRIPTION
442  
443 This module just defines a bunch of utility classes used by plerdall's
444 "init" verb. It offers no public API.
445  
446 =head1 SEE ALSO
447  
448 Plerd
449  
450 =head1 AUTHOR
451  
452 Jason McIntosh <jmac@jmac.org>
« lib/Plerd.pm ↑ Summary bin/plerdall »