lib/App/Dapper.pm

filter: Statementstmt 18.6% 98 / 526 Branchbran 3.3% 8 / 242 Conditioncond 0.0% 0 / 63 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 44.9% 31 / 69 Podpod 35.4% 11 / 31 totaltotal 15.3% 148 / 967 CC 167 SCAR 57.2
CC
167
Cov
13%
build_inventory
1482.0
clean_output
306.0
render_page
210.0
CRAP 306.4
« lib/App/Dapper/Agents.pm ↑ Summary lib/App/Dapper/Deploy.pm »
1 package App::Dapper;
2  
3 =head1 NAME
4  
5 App::Dapper - A publishing tool for static websites.
6  
7 =cut
8  
9 32 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use open ':std', ':encoding(UTF-8)';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 32 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 32 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 32 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 32 ▶use Template::Alloy;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 32 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 32 ▶use App::Dapper::Markdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 32 ▶use App::Dapper::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 32 ▶use App::Dapper::Publish;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 32 ▶use App::Dapper::Collections;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 32 ▶use App::Dapper::Check;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 32 ▶use App::Dapper::MCP;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 32 ▶use App::Dapper::Agents;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 32 ▶use App::Dapper::Deploy;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 32 ▶use YAML::PP qw/ LoadFile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 32 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 32 ▶use File::Spec::Functions qw/ canonpath catfile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 32 ▶use File::Basename qw/ basename /;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 32 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 32 ▶use Cwd qw/ abs_path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 32 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 32 ▶use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 32 ▶use App::Dapper::Init;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 32 ▶use App::Dapper::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 32 ▶use App::Dapper::Defaults;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 32 ▶use App::Dapper::Filters;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 32 my $DEFAULT_PORT = 8000;
47  
48 =head1 VERSION
49  
50 Version 1.00
51  
52 =cut
53  
54 32 our $VERSION = '1.00';
55  
56 32 our @EXPORT = qw($VERSION);
57  
58 =head1 SYNOPSIS
59  
60 B<Dapper> allows you to transform simple text files into static websites. By installing the App::Dapper Perl module, an executable named C<dapper> will be available to you in your terminal window. You can use this executable in a number of ways:
61  
62 # Initialize the current directory with a fresh skeleton of a site
63 $ dapper [-solc] init
64  
65 # ...or with a complete starter blog
66 $ dapper init --template blog
67  
68 # Build the site
69 $ dapper [-solc] build
70  
71 # Serve the site locally at http://localhost:8000
72 $ dapper [-solcp] serve
73  
74 # Rebuild the site if anything (source, layout dirs; config file) changes
75 $ dapper [-solc] watch
76  
77 # Check the site for problems, without touching the output directory
78 $ dapper [-solc] check [--json]
79  
80 # Build, check and publish the site (to S3, GitHub Pages, or a command)
81 $ dapper deploy [target] [--dry-run]
82  
83 # Let AI assistants read, write, check and build the site over MCP
84 $ dapper -C /path/to/site mcp
85  
86 # Get help on usage and switches
87 $ dapper -h
88  
89 # Print the version
90 $ dapper -v
91  
92 Additionally, B<Dapper> may be used as a perl module directly from a script. Examples:
93  
94 use App::Dapper;
95  
96 # Create a Dapper object
97 my $d = App::Dapper->new();
98  
99 # Initialize a new website in the current directory
100 $d->init();
101  
102 # Build the site
103 $d->build();
104  
105 # Serve the site locally at http://localhost:8000
106 $d->serve();
107  
108 =head1 DESCRIPTION
109  
110 Dapper helps you build static websites. To get you started, you can use the
111 C<dapper init> command to initialize a directory. After running this command,
112 the following directory structure will be created:
113  
114 _config.yml
115 _layout/
116 index.html
117 _source/
118 index.md
119  
120 In that same directory, you may then build the site using the C<dapper build>
121 command, which will combine the source files and the layout files and place
122 the results in the output directory (default: C<_output>). After you build
123 the default site, you'll then have the following directory structure:
124  
125 _config.yml
126 _layout/
127 index.html
128 _source/
129 index.md
130 _output/
131 index.html
132  
133 To see what your website looks like, run the C<dapper serve> command which
134 will spin up a development webserver and serve the static files located in
135 the output directory (default: C<_output>) at L<http://localhost:8000>.
136  
137 Now, let's walk through each file:
138  
139 =over 4
140  
141 =item B<_config.yml>
142  
143 The configuration file is a YAML file that specifies key configuration
144 elements for your static website. The default configuration file is as
145 follows:
146  
147 ---
148 name : My Site
149  
150 If you want to use a separate source, layout, or output directory, you may
151 specify it in this file. For instance:
152  
153 ---
154 name : My Site
155 source : _source
156 layout : _layout
157 output : _output
158  
159 Directories given on the command line take precedence over the ones in the
160 configuration file, which take precedence over the defaults.
161  
162 All of the configurations in this file are available in layout templates
163 under the C<site> namespace. For instance, C<name> in the configuration file
164 may be used in a template as follows:
165  
166 [% site.name %]
167  
168 =item B<_source/index.md>
169  
170 A sample markdown file is available in the _source directory. Contents:
171  
172 ---
173 layout: index
174 title: Welcome
175 ---
176  
177 Hello world.
178  
179 There are a few things to note about this file:
180  
181 =over 4
182  
183 =item 1. There is a YAML configuration block (the "front matter") at the start of the file.
184  
185 =item 2. The I<layout> configuration specifies which layout to use.
186  
187 =item 3. The C<index> layout indicates that C<_layout/index.html> should be used.
188  
189 =item 4. The C<title> configuration is the name of the post/page. It is optional.
190  
191 =item 5. All of the configurations may be used in the corresponding layout file.
192  
193 <!-- Example use of "title" in a layout file -->
194 <h1>[% page.title %]</h1>
195  
196 =back
197  
198 Files in the source directory that do not start with front matter (images,
199 PDFs, plain text files and so on) are copied to the output directory as they
200 are.
201  
202 A page's content is not run as template code unless the page sets
203 C<template: true>; layouts always are. C<templates_in_content: true> in
204 C<_config.yml> restores the behavior of Dapper before 1.0, where all content
205 and C<_config.yml> values were templates too.
206  
207 A C<_schema.yml> file in a source directory describes the front matter its
208 pages must have; see L<App::Dapper::Schema>. YAML and JSON files in C<_data>
209 are available as C<site.data>.
210  
211 =item B<_layout/index.html>
212  
213 Layout files are processed using the template language provided by
214 L<Template::Alloy> (the Template Toolkit mini-language). The initial layout
215 file that is given after you run the C<dapper init> command, is this:
216  
217 <!DOCTYPE html>
218 <html>
219 <head>
220 <meta charset="utf-8">
221 <title>[% page.title %]</title>
222 </head>
223  
224 <body>
225  
226 [% page.content %]
227  
228 </body>
229 </html>
230  
231 The main content of the text file that is being rendered with this template
232 is available using C<[% page.content %]>.
233  
234 Definitions specified in the C<_config.yml> file can be referenced under the
235 "site" namespace (e.g. C<[% site.name %]>). Definitions specified in the
236 front matter of source files can be referenced under the "page" namespace
237 (e.g. C<[% page.title %]>).
238  
239 =item B<_output/index.html>
240  
241 The output file that is created is a mix of the input file and the layout that
242 is specified by the input file. For the default site, the following output
243 file is created:
244  
245 <!DOCTYPE html>
246 <html>
247 <head>
248 <meta charset="utf-8">
249 <title>Welcome</title>
250 </head>
251  
252 <body>
253  
254 <p>Hello world.</p>
255  
256 </body>
257 </html>
258  
259 =back
260  
261 =head2 Variables
262  
263 The following variables are available in layouts and source files.
264  
265 =over 4
266  
267 =item B<site.*>
268  
269 Everything defined in C<_config.yml>, plus:
270  
271 site.time The time the build started (a DateTime)
272 site.pages Every page, in source path order
273 site.categories A hash of category name to its pages, sorted by date
274 site.tags A hash of tag to its pages, sorted by date
275 site.data The contents of the files in _data
276 site.category_pages, site.tag_pages URLs of generated list pages
277 site.source The resolved source directory
278 site.output The resolved output directory
279 site.layout The resolved layout directory
280 site.urlpattern Default URL pattern (/:category/:year/:month/:slug/)
281 site.extension Default output extension (.html)
282 site.ignore Patterns of top-level files not copied to the output
283  
284 =item B<page.*>
285  
286 Everything defined in the page's front matter, plus:
287  
288 page.content The rendered body of the page
289 page.excerpt The first paragraph of a markdown page
290 page.url The URL of the page, relative to the site root
291 page.slug The title, slugified (or the file name if no title)
292 page.id A number unique to the page within a build
293 page.date The page date (front matter, or the file's mtime)
294 page.year, page.month, page.day,
295 page.hour, page.minute, page.second Parts of page.date, zero padded
296 page.filename The output file the page is written to
297 page.dirname The directory of page.filename
298 page.source_file_extension The source file's extension (e.g. .md)
299 page.seo Tags for <head>: description, canonical, Open Graph, JSON-LD
300 page.markdown_url The URL of the page's Markdown copy, if it has one
301 page.posts On a generated category or tag page: its pages
302 paginator On a paginated page; see App::Dapper::Collections
303  
304 =back
305  
306 =head2 URLs and output files
307  
308 Pages with C<categories> or their own C<urlpattern> are written to the path
309 their URL describes. The pattern may use C<:category>, C<:year>, C<:month>,
310 C<:day>, C<:hour>, C<:minute>, C<:second> and C<:slug>. A URL ending in C</>
311 is written as C<index> plus the page extension inside that directory. Other
312 pages keep the path of their source file, with the page extension.
313  
314 =head2 Generated files
315  
316 After the pages, Dapper writes a sitemap, a feed (if C<feed> is set),
317 Markdown copies of pages and C<llms.txt> (see L<App::Dapper::Publish>), can
318 generate category and tag pages and paginate lists (see
319 L<App::Dapper::Collections>), and can build a search index with Pagefind.
320 Files that an earlier build wrote and this one did not are removed.
321  
322 =head2 Options
323  
324 B<Dapper> provides a number of optional command line switches:
325  
326 =over 4
327  
328 =item B<-s>, B<--source>=I<source directory>
329  
330 Specify the directory containing source files to process. If this command line option is not present, it defaults to the C<source> setting in the config file, or "_source".
331  
332 =item B<-o>, B<--output>=I<output directory>
333  
334 Specify the directory to place the output files in. If this command line option is not present, it defaults to the C<output> setting in the config file, or "_output".
335  
336 =item B<-l>, B<--layout>=I<layout directory>
337  
338 Specify the directory containing layout files. If this command line option is not present, it defaults to the C<layout> setting in the config file, or "_layout".
339  
340 =item B<-c>, B<--config>=I<config file>
341  
342 Specify the config file name to use. If this command line option is not present, it defaults to "_config.yml".
343  
344 =item B<-p>, B<--port>=I<port>
345  
346 Specify the port to use for the serve command. Defaults to 8000.
347  
348 =item B<-q>, B<--quiet>
349  
350 Only print errors.
351  
352 =item B<-C>, B<--directory>=I<directory>
353  
354 Run in this directory instead of the current one.
355  
356 =item B<-t>, B<--template>=I<name>
357  
358 For the init command: the starter project to create, C<default> or C<blog>.
359  
360 =item B<--json>
361  
362 For the check command: print the report as JSON.
363  
364 =item B<-h>, B<--help>
365  
366 Get help on available commands and options.
367  
368 =item B<-v>, B<--version>
369  
370 Print the version and exit.
371  
372 =back
373  
374 =cut
375  
376 =head1 METHODS
377  
378 Dapper may be used directly from a script as well. The following methods are available:
379  
380 =head2 new
381  
382 Create a new B<Dapper> object. Example:
383  
384 my $d = App::Dapper->new();
385  
386 Alternatively, the source dir, output dir, layout dir, and configuration file
387 may be specified. Any of them may be C<undef>, in which case the value from
388 the configuration file (or the default) is used. Example:
389  
390 my $d = App::Dapper->new("_source", "_output", "_layout", "_config.yml");
391  
392 After creating a B<Dapper> object, the following hash elements may be accessed:
393  
394 use App::Dapper;
395  
396 my $d = App::Dapper->new();
397  
398 print "Source directory: $d->{source}\n";
399 print "Output directory: $d->{output}\n";
400 print "Layout directory: $d->{layout}\n";
401 print "Config file: $d->{config}\n";
402 print "Object instantiation time: $d->{site}->{time}\n";
403  
404 The directories are updated from the configuration file when the site is
405 built or served. Set C<< $d->{quiet} >> to a true value to suppress progress
406 messages.
407  
408 =cut
409  
410 26 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
411 26 my $class = shift;
412 26 my ($source, $output, $layout, $config) = @_;
413  
414 26 ▶ my $self = {
Branch: 4 branches
BranchTrueFalseTotal
defined $source ? : 1 25 26
defined $output ? : 3 23 26
defined $layout ? : 1 25 26
defined $config ? : 1 25 26
415 created => 1,
416 quiet => 0,
417 explicit => {
418 source => defined $source,
419 output => defined $output,
420 layout => defined $layout,
421 config => defined $config,
422 },
423 source => defined $source ? $source : "_source",
424 output => defined $output ? $output : "_output",
425 layout => defined $layout ? $layout : "_layout",
426 config => defined $config ? $config : "_config.yml",
427 };
428  
429 26 bless $self, $class;
430 26 $self->reset_site();
431 0 return $self;
432 }
433  
434 =head2 init
435  
436 Initializes a new skeleton project in the current directory of the calling
437 script, using the source dir, layout dir and config file given to L</new>.
438 Nothing is written if any of the files already exist. Example usage:
439  
440 use App::Dapper;
441  
442 my $d = App::Dapper->new();
443 $d->init();
444  
445 Set C<< $d->{template} >> to C<blog> first for a complete starter blog (see
446 L<App::Dapper::Init>).
447  
448 =cut
449  
450 0 ▶sub init {
Subroutine init: not called CC=2 SCAR=17.9
Pod documented
451 0 my ($self) = @_;
452  
453 0 local $App::Dapper::Utils::QUIET = $self->{quiet};
454  
455 my $notes = App::Dapper::Init::init(
456 $self->{source},
457 $self->{output},
458 $self->{layout},
459 $self->{config},
460 $self->{template}
461 0 );
462  
463 # Explain the new project to AI coding agents
464 0 for my $file (App::Dapper::Agents::update($self, $notes)) {
465 0 $self->_log("Creating $file");
466 }
467  
468 0 $self->_log("Project initialized.");
469 }
470  
471 =head2 build
472  
473 Build the site. Example:
474  
475 use App::Dapper;
476  
477 my $d = App::Dapper->new();
478 $d->build();
479  
480 When the site is built, it is done in three steps:
481  
482 1. Parse. In this step, the configuration file is read. In addition, all the source files in the source directory as well as the layout files in the layout directory are read and stored in the site hash.
483  
484 2. Transform. Combine source and layout files.
485  
486 3. Render. Save output files to the output directory.
487  
488 A Dapper object may be built any number of times; each build starts fresh.
489  
490 =cut
491  
492 0 ▶sub build {
Subroutine build: not called CC=1 SCAR=6.9
Pod documented
493 0 my($self) = @_;
494  
495 0 $self->{problems} = [];
496 0 $self->{written} = {};
497 0 $self->{page_owner} = {};
498  
499 0 $self->parse();
500 0 $self->transform();
501 0 $self->render();
502  
503 0 $self->_log("Project built.");
504 0 return $self;
505 }
506  
507 # sub parse - Parse the source directory, config file, and templates.
508  
509 0 ▶sub parse {
Subroutine parse: not called CC=1 SCAR=6.9
Pod undocumented
510 0 my ($self) = @_;
511  
512 # load the defaults and the project file, and resolve the directories
513 0 $self->read_project();
514  
515 # read the layouts, expanding layouts that are wrapped in other layouts
516 0 $self->read_templates();
517 }
518  
519 # sub transform - Walk the source directory and record every page (and every
520 # file to copy as it is) in the site hash.
521  
522 0 ▶sub transform {
Subroutine transform: not called CC=4 SCAR=30.0
Pod undocumented
523 0 my ($self) = @_;
524  
525 0 ▶ die "error: source directory \"$self->{source}\" does not exist\n" unless -d $self->{source};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->{"source"} 0 0 0
526  
527 0 $self->{schema_errors} = [];
528  
529 # recurse through the source tree, mapping each file to its output path
530 0 $self->walk($self->{source}, $self->{output});
531  
532 0 my @errors = @{$self->{schema_errors}};
533 die "error: " . scalar(@errors) . " problem" . (@errors == 1 ? '' : 's') . " with front matter (see the _schema.yml files):\n"
534 0 ▶ . join('', map { " $_\n" } @errors) if @errors;
Branch: 2 branches
BranchTrueFalseTotal
if @errors 0 0 0
@errors == 1 ? : 0 0 0
535  
536 # category pages, tag pages and pagination
537 0 ▶ $self->attempt($self->{config}, sub { App::Dapper::Collections::generate($self) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
538 }
539  
540 # sub render - Combine each page with its layout and write it to the output
541 # directory, then copy the files that are not pages.
542  
543 0 ▶sub render {
Subroutine render: not called CC=9 SCAR=45.0
Pod undocumented
544 0 my ($self) = @_;
545  
546 # Template::Alloy's built-in json method returns UTF-8 bytes, which would
547 # be encoded a second time when the page is written. Give it encoders
548 # that return characters instead.
549 0 $Template::Alloy::VMethod::JSON = JSON->new->allow_nonref->allow_unknown->allow_blessed->convert_blessed->canonical;
550 0 $Template::Alloy::VMethod::JSONP = JSON->new->allow_nonref->allow_unknown->allow_blessed->convert_blessed->canonical->pretty;
551  
552 my $tt = Template::Alloy->new({
553 INCLUDE_PATH => $self->{layout},
554 ANYCASE => 1,
555 ENCODING => 'utf8',
556 FILTERS => App::Dapper::Filters::filters(),
557 0 ▶ EVAL_PERL => $ENV{EVAL_PERL} || 0,
Condition0%0/2
$ENV{'EVAL_PERL'} || 0
l!l
00
MC/DC0%0/1
$ENV{'EVAL_PERL'} || 0
$ENV{'EVAL_PERL'}
Truth table0%0/2
A
A= $ENV{'EVAL_PERL'}
Aresult
11
00
558 });
559  
560 # Work out what layouts can show about each page before any are rendered
561 0 for my $page (@{$self->{site}->{pages}}) {
562 $page->{markdown_url} = App::Dapper::Publish::twin_url($page)
563 0 ▶ if App::Dapper::Publish::has_twin($self->{site}, $page);
Branch: 1 branch
BranchTrueFalseTotal
if App::Dapper::Publish::has_twin($self->{"site"}, $page) 0 0 0
564 0 $page->{seo} = App::Dapper::Publish::seo($self->{site}, $page);
565 }
566  
567 0 for my $page (@{$self->{site}->{pages}}) {
568 0 ▶ $self->attempt($page->{source}, sub { $self->render_page($tt, $page) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
569 }
570  
571 # copy source files that have no front matter (images, etc.) as they are
572 0 for my $file (@{$self->{static_files}}) {
573 0 $self->record_written(App::Dapper::Utils::copy_recursive($file->{source}, $file->{output}));
574 0 $self->_log("Copied $file->{source}");
575 }
576  
577 # copy additional files and directories
578 0 $self->copy(".", $self->{output});
579  
580 # sitemap, feed and llms.txt
581 0 ▶ $self->attempt($self->{config}, sub { $self->write_generated() });
Subroutine __ANON__: not called CC=1 SCAR=6.9
582  
583 # with live reload on, a new value here tells open pages to reload
584 0 ▶ if ($self->{live_reload}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"live_reload"}) 0 0 0
585 0 $self->write_output(catfile($self->{output}, '.dapper-reload'), time() . '-' . int(rand(1_000_000)) . "\n");
586 }
587  
588 # remove what earlier builds wrote that this one did not
589 0 ▶ $self->clean_output() unless $self->{collect};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"collect"} 0 0 0
590  
591 # search index
592 0 ▶ $self->run_search() unless $self->{collect};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"collect"} 0 0 0
593 }
594  
595 # sub run_search - With "search: true", run Pagefind (https://pagefind.app) over
596 # the output directory to build a search index. "search_command" can name a
597 # different command, such as "npx -y pagefind".
598  
599 0 ▶sub run_search {
Subroutine run_search: not called CC=8 SCAR=42.8
Pod undocumented
600 0 my ($self) = @_;
601  
602 0 my $site = $self->{site};
603 0 ▶ return unless $site->{search} and App::Dapper::Publish::enabled($site, 'search');
Branch: 1 branch
BranchTrueFalseTotal
unless $site->{"search"} and App::Dapper::Publish::enabled($site, "search") 0 0 0
Condition0%0/3
$site->{"search"} and App::Dapper::Publish::enabled($site, "search")
!ll&&!rl&&r
000
MC/DC0%0/2
$site->{"search"} and App::Dapper::Publish::enabled($site, "search")
$site->{"search"} App::Dapper::Publish::enabled($site, "search")
Truth table0%0/3
A and B
A= $site->{"search"}B= App::Dapper::Publish::enabled($site, "search")
ABresult
0X0
100
111
604  
605 0 ▶ my @command = defined $site->{search_command} ? split(' ', $site->{search_command}) : ('pagefind');
Branch: 1 branch
BranchTrueFalseTotal
defined $site->{'search_command'} ? : 0 0 0
606 0 ▶ my $found = $command[0] =~ m{/} ? -x $command[0] : grep { -x catfile($_, $command[0]) } File::Spec->path();
Branch: 1 branch
BranchTrueFalseTotal
$command[0] =~ m[/] ? : 0 0 0
607 0 ▶ if (not $found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 0 0
608 $self->problem('warning', $self->{config},
609 0 "search: true needs Pagefind; install it (see https://pagefind.app) or set search_command");
610 0 return;
611 }
612  
613 0 $self->_log("Building the search index with @command");
614 0 ▶ my @quiet = $self->{quiet} ? ('--quiet') : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->{'quiet'} ? : 0 0 0
615 system(@command, '--site', $self->{output}, @quiet) == 0
616 0 ▶ or $self->problem('warning', $self->{config}, "@command did not finish (exit status " . ($? >> 8) . ")");
Branch: 1 branch
BranchTrueFalseTotal
system(@command, "--site", $self->{"output"}, @quiet) == 0 or $self->problem("warning", $self->{"config"}, "@command did not finish (exit status " . ($? >> 8) . ')') 0 0 0
617 }
618  
619 # sub clean_output - Remove files that an earlier build wrote to the output
620 # directory but this build did not (the output of a page that was renamed or
621 # deleted, say). Only files Dapper itself wrote are ever removed: the list is
622 # kept in .dapper/manifest.json in the project directory. "clean: false" in
623 # the project file turns this off.
624  
625 0 ▶sub clean_output {
Subroutine clean_output: not called CC=17 SCAR=57.2
Pod undocumented
626 0 my ($self) = @_;
627  
628 0 ▶ return unless App::Dapper::Publish::enabled($self->{site}, 'clean');
Branch: 1 branch
BranchTrueFalseTotal
unless App::Dapper::Publish::enabled($self->{"site"}, "clean") 0 0 0
629  
630 0 my $manifest_file = catfile('.dapper', 'manifest.json');
631 0 my $output = abs_path($self->{output});
632 0 ▶ return unless defined $output;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output 0 0 0
633  
634 0 my $manifest = {};
635 0 ▶ if (-f $manifest_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $manifest_file) 0 0 0
636 0 ▶ $manifest = eval { JSON->new->decode(App::Dapper::Utils::read_file($manifest_file)) } || {};
Condition0%0/2
eval { do { 'JSON'->new->decode(App::Dapper::Utils::read_file($manifest_file)) } } || {}
l!l
00
MC/DC0%0/1
eval { do { 'JSON'->new->decode(App::Dapper::Utils::read_file($manifest_file)) } } || {}
eval { do { 'JSON'->new->decode(App::Dapper::Utils::read_file($manifest_file)) } }
Truth table0%0/2
A
A= eval { do { 'JSON'->new->decode(App::Dapper::Utils::read_file($manifest_file)) } }
Aresult
11
00
637 0 ▶ $manifest = {} unless ref $manifest eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $manifest eq "HASH" 0 0 0
638 }
639  
640 0 my $old = $manifest->{$output};
641 0 my $removed = 0;
642 0 my %dirs;
643 0 ▶ if (ref $old eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $old eq "ARRAY") 0 0 0
644 0 for my $path (@$old) {
645 0 ▶ next if $self->{written}->{$path};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"written"}{$path} 0 0 0
646 0 ▶ next if $path =~ m{(?:^|/)\.\.(?:/|$)} or $path =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[(?:^|/)\.\.(?:/|$)] or $path =~ m[^/] 0 0 0
Condition0%0/3
$path =~ m[(?:^|/)\.\.(?:/|$)] or $path =~ m[^/]
l!l&&r!l&&!r
000
MC/DC0%0/2
$path =~ m[(?:^|/)\.\.(?:/|$)] or $path =~ m[^/]
$path =~ m[(?:^|/)\.\.(?:/|$)] $path =~ m[^/]
Truth table0%0/3
A or B
A= $path =~ m[(?:^|/)\.\.(?:/|$)]B= $path =~ m[^/]
ABresult
1X1
011
000
647 0 my $file = catfile($self->{output}, $path);
648 0 ▶ next unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
649 0 ▶ unlink $file or next;
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or next 0 0 0
650 0 $removed++;
651 0 (my $dir = $path) =~ s{/?[^/]*$}{};
652 0 ▶ while (length $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dir) 0 0 0
653 0 $dirs{$dir} = 1;
654 0 $dir =~ s{/?[^/]*$}{};
655 }
656 }
657 }
658  
659 # Remove directories the removed files leave empty, deepest first
660 0 for my $dir (sort { length $b <=> length $a } keys %dirs) {
661 0 rmdir catfile($self->{output}, $dir);
662 }
663  
664 0 ▶ $self->_log("Removed $removed file" . ($removed == 1 ? '' : 's') . " left over from an earlier build") if $removed;
Branch: 2 branches
BranchTrueFalseTotal
if $removed 0 0 0
$removed == 1 ? : 0 0 0
665  
666 0 $manifest->{$output} = [ sort keys %{$self->{written}} ];
667 0 App::Dapper::Utils::write_file($manifest_file, JSON->new->canonical->pretty->encode($manifest));
668 }
669  
670 # sub live_reload_script - The script added to pages while "dapper serve" is
671 # running. It asks for /.dapper-reload every second and reloads the page when
672 # a new build changes it.
673  
674 0 ▶sub live_reload_script {
Subroutine live_reload_script: not called CC=1 SCAR=6.9
Pod undocumented
675 0 return <<'HTML';
676 <script>
677 (function () {
678 var last = null;
679 function poll() {
680 fetch('/.dapper-reload?' + Date.now(), { cache: 'no-store' })
681 .then(function (r) { return r.ok ? r.text() : last; })
682 .then(function (t) { if (last !== null && t !== last) { location.reload(); } last = t; })
683 .catch(function () {})
684 .then(function () { setTimeout(poll, 1000); });
685 }
686 poll();
687 })();
688 </script>
689 HTML
690 }
691  
692 # sub write_generated - Write the files Dapper makes for the whole site:
693 # redirects, the sitemap, the feed, and llms.txt and llms-full.txt. A page that writes the
694 # same file wins, with a warning.
695  
696 0 ▶sub write_generated {
Subroutine write_generated: not called CC=11 SCAR=48.8
Pod undocumented
697 0 my ($self) = @_;
698  
699 0 my $site = $self->{site};
700 0 my @pages = @{$site->{pages}};
701 0 my %taken = map { $_->{filename} => 1 } @pages;
702 0 ▶ my $has_url = defined $site->{url} && length $site->{url};
Condition0%0/3
defined $site->{'url'} && length $site->{'url'}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $site->{'url'} && length $site->{'url'}
defined $site->{'url'} length $site->{'url'}
Truth table0%0/3
A && B
A= defined $site->{'url'}B= length $site->{'url'}
ABresult
0X0
100
111
703  
704 0 ▶ my $write = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
705 0 my ($path, $content) = @_;
706 0 my $file = canonpath(catfile($self->{output}, $path));
707 0 ▶ if ($taken{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taken{$file}) 0 0 0
708 0 $self->problem('warning', $file, "a page in the source directory already writes $file, so Dapper did not generate one");
709 0 return;
710 }
711 0 $self->write_output($file, $content);
712 0 $self->_log("Wrote $file");
713 0 };
714  
715 0 my $redirects = App::Dapper::Publish::redirects($site);
716 0 for my $from (sort keys %$redirects) {
717 0 my $file = canonpath(catfile($self->{output}, App::Dapper::Publish::redirect_path($from)));
718 0 ▶ if ($taken{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taken{$file}) 0 0 0
719 0 $self->problem('warning', $file, "a page is still at $from, so the redirect from it to $redirects->{$from} was not written");
720 0 next;
721 }
722 0 $self->write_output($file, App::Dapper::Publish::redirect_page($site, $redirects->{$from}));
723 0 $self->{page_owner}->{$file} = "the redirect from $from";
724 0 $taken{$file} = 1;
725 }
726  
727 0 ▶ if (App::Dapper::Publish::enabled($site, 'llms_txt')) {
Branch: 1 branch
BranchTrueFalseTotal
if (App::Dapper::Publish::enabled($site, "llms_txt")) 0 0 0
728 0 my @twins = grep { $_->{markdown_url} } @pages;
729 0 ▶ if (@twins) {
Branch: 1 branch
BranchTrueFalseTotal
if (@twins) 0 0 0
730 0 $write->('llms.txt', App::Dapper::Publish::llms_txt($site, \@twins));
731 0 $write->('llms-full.txt', App::Dapper::Publish::llms_full_txt($site, \@twins));
732 }
733 }
734  
735 0 ▶ if (App::Dapper::Publish::enabled($site, 'sitemap')) {
Branch: 1 branch
BranchTrueFalseTotal
if (App::Dapper::Publish::enabled($site, "sitemap")) 0 0 0
736 0 ▶ if ($has_url) {
Branch: 2 branches
BranchTrueFalseTotal
if ($has_url) { } 0 0 0
elsif ($self->{'configured'}{'sitemap'}) { } 0 0 0
737 0 $write->('sitemap.xml', App::Dapper::Publish::sitemap($site, \@pages));
738 }
739 elsif ($self->{configured}->{sitemap}) {
740 0 $self->problem('warning', $self->{config}, "the sitemap needs the site's address; set url in $self->{config}");
741 }
742 }
743  
744 0 ▶ if (my $settings = App::Dapper::Publish::feed_settings($site)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $settings = App::Dapper::Publish::feed_settings($site)) 0 0 0
745 0 ▶ if ($has_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_url) { } 0 0 0
746 0 my $pages = App::Dapper::Publish::feed_pages($site, $settings);
747 0 $write->($settings->{path}, App::Dapper::Publish::feed($site, $settings, $pages));
748 }
749 else {
750 0 $self->problem('warning', $self->{config}, "the feed needs the site's address; set url in $self->{config}");
751 }
752 }
753 }
754  
755 # sub render_page - Render one page with its layout and write it to disk.
756 #
757 # Layouts are always processed as templates. The page's content is processed
758 # as a template only if the page sets "template: true". With
759 # "templates_in_content: true" in the project file, Dapper instead works as it
760 # did before 1.0: the layout is rendered, and then the result is rendered a
761 # second time, so that template code in content and in site variables runs.
762  
763 0 ▶sub render_page {
Subroutine render_page: not called CC=14 SCAR=53.5
Pod undocumented
764 0 my ($self, $tt, $page) = @_;
765  
766 0 ▶ $page->{layout} = "index" unless $page->{layout};
Branch: 1 branch
BranchTrueFalseTotal
unless $page->{"layout"} 0 0 0
767  
768 0 my $layout = $self->{layout_content}->{$page->{layout}};
769 0 ▶ die "error: $page->{source} uses the layout \"$page->{layout}\", but there is no such file in $self->{layout}\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $layout 0 0 0
770 unless defined $layout;
771  
772 my %vars = (
773 site => $self->{site},
774 page => $page,
775 paginator => $page->{paginator},
776 0 );
777  
778 0 my $output;
779  
780 0 ▶ if ($self->{site}->{templates_in_content}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'site'}{'templates_in_content'}) { } 0 0 0
781 0 my $first;
782 0 ▶ $tt->process(\$layout, \%vars, \$first)
Branch: 1 branch
BranchTrueFalseTotal
$tt->process(\$layout, \%vars, \$first) or die qq[error: could not process $page->{"source"} with layout $page->{"layout"}: ] . $tt->error . "\n" 0 0 0
783 || die "error: could not process $page->{source} with layout $page->{layout}: " . $tt->error() . "\n";
784 0 ▶ $tt->process(\$first, \%vars, \$output)
Branch: 1 branch
BranchTrueFalseTotal
$tt->process(\$first, \%vars, \$output) or die qq[error: could not process $page->{"source"} with layout $page->{"layout"} (2nd pass): ] . $tt->error . "\n" 0 0 0
785 || die "error: could not process $page->{source} with layout $page->{layout} (2nd pass): " . $tt->error() . "\n";
786 }
787 else {
788 0 ▶ if ($page->{template}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->{"template"}) 0 0 0
789 0 my $content;
790 0 ▶ $tt->process(\$page->{content}, \%vars, \$content)
Branch: 1 branch
BranchTrueFalseTotal
$tt->process(\$page->{"content"}, \%vars, \$content) or die qq[error: could not process the content of $page->{"source"}: ] . $tt->error . "\n" 0 0 0
791 || die "error: could not process the content of $page->{source}: " . $tt->error() . "\n";
792 0 $page->{content} = $content;
793 }
794 0 ▶ $tt->process(\$layout, \%vars, \$output)
Branch: 1 branch
BranchTrueFalseTotal
$tt->process(\$layout, \%vars, \$output) or die qq[error: could not process $page->{"source"} with layout $page->{"layout"}: ] . $tt->error . "\n" 0 0 0
795 || die "error: could not process $page->{source} with layout $page->{layout}: " . $tt->error() . "\n";
796 }
797  
798 0 ▶ if ($self->{live_reload} and App::Dapper::Publish::is_html($page)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"live_reload"} and App::Dapper::Publish::is_html($page)) 0 0 0
Condition0%0/3
$self->{"live_reload"} and App::Dapper::Publish::is_html($page)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"live_reload"} and App::Dapper::Publish::is_html($page)
$self->{"live_reload"} App::Dapper::Publish::is_html($page)
Truth table0%0/3
A and B
A= $self->{"live_reload"}B= App::Dapper::Publish::is_html($page)
ABresult
0X0
100
111
799 0 my $script = live_reload_script();
800 0 ▶ $output =~ s{(</body>)}{$script$1}i or $output .= $script;
Branch: 1 branch
BranchTrueFalseTotal
$output =~ s[(</body>)][$script$1]i or $output .= $script 0 0 0
801 }
802  
803 0 my $owner = $self->{page_owner}->{$page->{filename}};
804 0 ▶ die "error: $page->{source} and $owner both write $page->{filename}\n" if defined $owner;
Branch: 1 branch
BranchTrueFalseTotal
if defined $owner 0 0 0
805 0 $self->{page_owner}->{$page->{filename}} = $page->{source};
806  
807 0 $self->write_output($page->{filename}, $output);
808 0 $self->_log("Wrote $page->{filename}");
809  
810 0 ▶ if ($page->{markdown_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->{"markdown_url"}) 0 0 0
811 0 my $twin = App::Dapper::Publish::twin_path($page);
812 0 $self->write_output($twin, App::Dapper::Publish::twin($self->{site}, $page));
813 0 $self->_log("Wrote $twin");
814 }
815 }
816  
817 # sub write_output - Write a file to the output directory and remember that
818 # Dapper wrote it.
819  
820 0 ▶sub write_output {
Subroutine write_output: not called CC=1 SCAR=6.9
Pod undocumented
821 0 my ($self, $filename, $content) = @_;
822  
823 0 App::Dapper::Utils::write_file($filename, $content);
824 0 $self->record_written($filename);
825 }
826  
827 # sub record_written - Remember files written to the output directory, so that
828 # files left over from earlier builds can be found.
829  
830 0 ▶sub record_written {
Subroutine record_written: not called CC=2 SCAR=17.9
Pod undocumented
831 0 my ($self, @files) = @_;
832  
833 0 for my $file (@files) {
834 0 my $relative = File::Spec->abs2rel($file, $self->{output});
835 0 $self->{written}->{ join('/', File::Spec->splitdir($relative)) } = 1;
836 }
837 }
838  
839 # sub attempt - Run a step of the build for a file. Normally an error stops the
840 # build. When collecting problems (as "dapper check" does), the error is
841 # recorded and the build carries on.
842  
843 0 ▶sub attempt {
Subroutine attempt: not called CC=3 SCAR=24.8
Pod undocumented
844 0 my ($self, $file, $code) = @_;
845  
846 0 ▶ return $code->() unless $self->{collect};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"collect"} 0 0 0
847  
848 0 ▶ eval { $code->(); 1 } or $self->problem('error', $file, $@);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$code(); 1 } } or $self->problem("error", $file, $@) 0 0 0
849 }
850  
851 # sub problem - Record a problem found during the build. Errors stop the build
852 # unless problems are being collected; warnings are printed.
853  
854 0 ▶sub problem {
Subroutine problem: not called CC=3 SCAR=24.8
Pod undocumented
855 0 my ($self, $severity, $file, $message) = @_;
856  
857 0 $message =~ s/^(?:error|warning): //;
858 0 $message =~ s/\s+\z//;
859  
860 0 ▶ if ($self->{collect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"collect"}) 0 0 0
861 0 push @{$self->{problems}}, { severity => $severity, file => $file, message => $message };
862 0 return;
863 }
864  
865 0 ▶ die "error: $message\n" if $severity eq 'error';
Branch: 1 branch
BranchTrueFalseTotal
if $severity eq "error" 0 0 0
866 0 warn "warning: $message\n";
867 }
868  
869 =head2 check
870  
871 Build the site into a temporary directory and return a report of every
872 problem found, without touching the output directory. See
873 L<App::Dapper::Check>.
874  
875 my $report = $d->check();
876 print "ok\n" if $report->{ok};
877  
878 =cut
879  
880 0 ▶sub check {
Subroutine check: not called CC=1 SCAR=6.9
Pod documented
881 0 my ($self) = @_;
882 0 return App::Dapper::Check::run($self);
883 }
884  
885 =head2 agents
886  
887 Write or update C<AGENTS.md> (and a C<CLAUDE.md> if there isn't one), which
888 explains the project to AI coding agents. See L<App::Dapper::Agents>.
889  
890 =cut
891  
892 0 ▶sub agents {
Subroutine agents: not called CC=2 SCAR=17.9
Pod documented
893 0 my ($self) = @_;
894 0 for my $file (App::Dapper::Agents::update($self)) {
895 0 $self->_log("Wrote $file");
896 }
897 }
898  
899 =head2 deploy
900  
901 Build, check and publish the site to a target set under C<deploy> in
902 C<_config.yml> (Amazon S3, GitHub Pages, or a command). Takes the target name
903 (optional if there is only one) and options: C<dry_run>, C<force>,
904 C<no_build>. See L<App::Dapper::Deploy>.
905  
906 $d->deploy('s3', dry_run => 1);
907  
908 =cut
909  
910 0 ▶sub deploy {
Subroutine deploy: not called CC=1 SCAR=6.9
Pod documented
911 0 my ($self, $target, %options) = @_;
912 0 return App::Dapper::Deploy::run($self, $target, %options);
913 }
914  
915 =head2 mcp
916  
917 Run a Model Context Protocol server on standard input and output, so that AI
918 assistants can read, write, build and check the site. See
919 L<App::Dapper::MCP>.
920  
921 =cut
922  
923 0 ▶sub mcp {
Subroutine mcp: not called CC=1 SCAR=6.9
Pod documented
924 0 my ($self) = @_;
925 0 App::Dapper::MCP::serve($self);
926 }
927  
928 =head2 serve
929  
930 Serve the site locally. Pass in the port number. The port number will be used
931 to serve the site contents from the output directory like this:
932 http://localhost:<port>. Here is an example, using the default port 8000:
933  
934 use App::Dapper;
935  
936 my $d = App::Dapper->new();
937 $d->serve("8000");
938  
939 The following is equivalent:
940  
941 $d->serve();
942  
943 This method only serves what is already in the output directory (as read from
944 the project file, if there is one); it does not build the site. The C<dapper
945 serve> command builds and watches the site in a separate process while it
946 serves.
947  
948 =cut
949  
950 0 ▶sub serve {
Subroutine serve: not called CC=3 SCAR=24.8
Pod documented
951 0 my($self, $port) = @_;
952  
953 0 require Net::HTTPServer;
954  
955 0 ▶ $port = $DEFAULT_PORT unless $port;
Branch: 1 branch
BranchTrueFalseTotal
unless $port 0 0 0
956  
957 0 $self->read_project(1);
958  
959 0 print "Starting webserver at http://localhost:$port\n";
960  
961 my $s = Net::HTTPServer->new(
962 port=>$port,
963 docroot=>$self->{output},
964 0 type=>"forking",
965 log=>"STDOUT",
966 );
967 0 ▶ $s->Start() or die "error: could not start the webserver on port $port\n";
Branch: 1 branch
BranchTrueFalseTotal
$s->Start or die "error: could not start the webserver on port $port\n" 0 0 0
968 0 $s->Process();
969 }
970  
971 =head2 watch
972  
973 Build the site, then rebuild it whenever a file in the project changes, is
974 added, or is removed. The output directory and dot files are not watched.
975 Errors in a rebuild are reported without stopping the watch. Pass the
976 polling interval in seconds (default: 1).
977  
978 $d->watch();
979  
980 =cut
981  
982 0 ▶sub watch {
Subroutine watch: not called CC=5 SCAR=34.0
Pod documented
983 0 my ($self, $interval) = @_;
984  
985 0 ▶ $interval = 1 unless $interval;
Branch: 1 branch
BranchTrueFalseTotal
unless $interval 0 0 0
986  
987 0 my $previous;
988 0 while (1) {
989 0 my $current = $self->snapshot();
990 0 ▶ if (not defined $previous or not _same_snapshot($previous, $current)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $previous and _same_snapshot($previous, $current)) 0 0 0
Condition0%0/3
defined $previous and _same_snapshot($previous, $current)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $previous and _same_snapshot($previous, $current)
defined $previous _same_snapshot($previous, $current)
Truth table0%0/3
A and B
A= defined $previousB= _same_snapshot($previous, $current)
ABresult
0X0
100
111
991 0 ▶ eval { $self->build(); 1 } or warn $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->build; 1 } } or warn $@ 0 0 0
992 0 $previous = $self->snapshot();
993 }
994 0 sleep $interval;
995 }
996 }
997  
998 =head2 snapshot
999  
1000 Returns a hash reference of every watched file in the project mapped to its
1001 modification time and size. Used by L</watch> to detect changes.
1002  
1003 =cut
1004  
1005 0 ▶sub snapshot {
Subroutine snapshot: not called CC=1 SCAR=6.9
Pod documented
1006 0 my ($self) = @_;
1007  
1008 0 my $output = abs_path($self->{output});
1009 0 my %files;
1010  
1011 File::Find::find({
1012 no_chdir => 1,
1013 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
1014 0 my $path = $File::Find::name;
1015 0 my $name = basename($path);
1016  
1017 0 ▶ if (-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) 0 0 0
1018 0 ▶ return if $path eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "." 0 0 0
1019 0 my $abs = abs_path($path);
1020 0 ▶ $File::Find::prune = 1
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^\./ or defined $output and defined $abs and $abs eq $output 0 0 0
Condition0%0/9
$name =~ /^\./ or defined $output and defined $abs and $abs eq $output
l!l&&r!l&&!r
000
defined $output and defined $abs and $abs eq $output
!ll&&!rl&&r
000
defined $output and defined $abs
!ll&&!rl&&r
000
MC/DC0%0/4
$name =~ /^\./ or defined $output and defined $abs and $abs eq $output
$name =~ /^\./ defined $output defined $abs $abs eq $output
Truth table0%0/5
A or B and C and D
A= $name =~ /^\./B= defined $outputC= defined $absD= $abs eq $output
ABCDresult
1XXX1
01111
00XX0
010X0
01100
1021 if $name =~ /^\./ or (defined $output and defined $abs and $abs eq $output);
1022 0 return;
1023 }
1024  
1025 0 ▶ return if $name =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^\./ 0 0 0
1026 0 my @stat = stat $path;
1027 0 ▶ $files{$path} = "$stat[9]:$stat[7]" if @stat;
Branch: 1 branch
BranchTrueFalseTotal
if @stat 0 0 0
1028 },
1029 0 }, '.');
1030  
1031 0 return \%files;
1032 }
1033  
1034 0 ▶sub _same_snapshot {
Subroutine _same_snapshot: not called CC=5 SCAR=34.0
1035 0 my ($old, $new) = @_;
1036  
1037 0 ▶ return 0 unless keys %$old == keys %$new;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$old == keys %$new 0 0 0
1038 0 for my $key (keys %$old) {
1039 0 ▶ return 0 unless defined $new->{$key} and $new->{$key} eq $old->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new->{$key} and $new->{$key} eq $old->{$key} 0 0 0
Condition0%0/3
defined $new->{$key} and $new->{$key} eq $old->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $new->{$key} and $new->{$key} eq $old->{$key}
defined $new->{$key} $new->{$key} eq $old->{$key}
Truth table0%0/3
A and B
A= defined $new->{$key}B= $new->{$key} eq $old->{$key}
ABresult
0X0
100
111
1040 }
1041 0 return 1;
1042 }
1043  
1044 # sub reset_site - Start a fresh site hash from the defaults.
1045  
1046 26 ▶sub reset_site {
Subroutine reset_site: called CC=1 SCAR=2.6
Pod undocumented
1047 26 my ($self) = @_;
1048  
1049 26 $self->{site} = App::Dapper::Defaults::get_defaults();
1050 26 $self->{site}->{time} = App::Dapper::Utils::parse_date(
1051 DateTime->now( time_zone => DateTime::TimeZone->new( name => 'local' ) ));
1052 0 $self->{site}->{pages} = [];
1053 0 $self->{site}->{categories} = {};
1054 0 $self->{site}->{tags} = {};
1055 0 $self->{layout_content} = {};
1056 0 $self->{static_files} = [];
1057 0 $self->{id} = 0;
1058 }
1059  
1060 # sub read_project - Read the project file and resolve the source, output and
1061 # layout directories. Precedence: values passed to new() (the command line),
1062 # then the project file, then the defaults. Pass a true value to allow the
1063 # project file to be missing.
1064  
1065 0 ▶sub read_project {
Subroutine read_project: not called CC=11 SCAR=48.8
Pod undocumented
1066 0 my ($self, $optional) = @_;
1067  
1068 0 $self->reset_site();
1069 0 $self->{configured} = {};
1070  
1071 0 ▶ if (-f $self->{config}) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $self->{'config'}) { } 0 0 0
elsif (not $optional) { } 0 0 0
1072 0 my $config = eval { LoadFile($self->{config}) };
1073 0 ▶ die "error: could not parse \"$self->{config}\": $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1074  
1075 0 ▶ $config = {} unless defined $config;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config 0 0 0
1076 0 ▶ die "error: \"$self->{config}\" must contain key: value pairs\n" unless ref $config eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config eq "HASH" 0 0 0
1077  
1078 # Settings in the project file override the defaults
1079 0 @{$self->{site}}{keys %{$config}} = values %$config;
1080 0 $self->{configured} = { map { $_ => 1 } keys %$config };
1081 }
1082 elsif (not $optional) {
1083 0 die "error: could not find the project file \"$self->{config}\". Run \"dapper init\" to create a new project.\n";
1084 }
1085  
1086 0 for my $key (qw/ source output layout /) {
1087 0 ▶ my $value = $self->{explicit}->{$key} ? $self->{$key} : $self->{site}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'explicit'}{$key} ? : 0 0 0
1088 0 ▶ die "error: \"$key\" must be defined in project file\n" unless defined $value and length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 0 0 0
Condition0%0/3
defined $value and length $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table0%0/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
1089 0 $value = canonpath($value);
1090 0 $self->{$key} = $value;
1091 0 $self->{site}->{$key} = $value;
1092 }
1093  
1094 0 $self->{site}->{pages} = [];
1095 0 $self->{site}->{categories} = {};
1096 0 $self->{site}->{tags} = {};
1097  
1098 0 $self->{data_dir} = canonpath($self->{site}->{data_dir});
1099 0 ▶ $self->{site}->{data} = -d $self->{data_dir} ? read_data($self->{data_dir}) : {};
Branch: 1 branch
BranchTrueFalseTotal
-d $self->{'data_dir'} ? : 0 0 0
1100 }
1101  
1102 # sub read_data(dir) - Read every YAML (.yml, .yaml) and JSON (.json) file in a
1103 # directory into a hash keyed by file name without the extension, so that
1104 # _data/authors.yml is available as site.data.authors. Directories become
1105 # nested hashes: _data/menus/main.yml is site.data.menus.main.
1106  
1107 0 ▶sub read_data {
Subroutine read_data: not called CC=7 SCAR=40.3
Pod undocumented
1108 0 my ($dir) = @_;
1109  
1110 0 my %data;
1111  
1112 0 my @entries = grep { !/^\./ } App::Dapper::Utils::read_dir($dir);
1113  
1114 0 for my $entry (@entries) {
1115 0 my $path = "$dir/$entry";
1116 0 ▶ if (-d $path) {
Branch: 3 branches
BranchTrueFalseTotal
if (-d $path) { } 0 0 0
elsif ($entry =~ /^(.+)\.(?:ya?ml)$/i) { } 0 0 0
elsif ($entry =~ /^(.+)\.json$/i) { } 0 0 0
1117 0 $data{$entry} = read_data($path);
1118 }
1119 elsif ($entry =~ /^(.+)\.(?:ya?ml)$/i) {
1120 0 my $value = eval { LoadFile($path) };
1121 0 ▶ die "error: could not parse the data file \"$path\": $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1122 0 $data{$1} = $value;
1123 }
1124 elsif ($entry =~ /^(.+)\.json$/i) {
1125 0 my $value = eval { JSON->new->decode(App::Dapper::Utils::read_file($path)) };
1126 0 ▶ die "error: could not parse the data file \"$path\": $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1127 0 $data{$1} = $value;
1128 }
1129 }
1130  
1131 0 return \%data;
1132 }
1133  
1134 # sub read_templates - Read the content of the layouts in the layout directory
1135 # and expand layouts that name a parent layout in their front matter.
1136  
1137 0 ▶sub read_templates {
Subroutine read_templates: not called CC=5 SCAR=34.0
Pod undocumented
1138 0 my ($self) = @_;
1139  
1140 0 my %raw;
1141  
1142 0 ▶ die "error: could not open layout directory \"$self->{layout}\"\n" unless -d $self->{layout};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->{"layout"} 0 0 0
1143 0 my @files = grep { !/^\./ } App::Dapper::Utils::read_dir($self->{layout});
1144  
1145 0 for my $file (@files) {
1146 0 my $path = "$self->{layout}/$file";
1147 0 ▶ next unless -f $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path 0 0 0
1148 0 $raw{App::Dapper::Utils::filter_stem($file)} = App::Dapper::Utils::read_file($path);
1149 }
1150  
1151 0 $self->{layout_content} = {};
1152 0 for my $key (keys %raw) {
1153 0 $self->{layout_content}->{$key} = $self->expand_layout($key, \%raw, {});
1154 }
1155 }
1156  
1157 # sub expand_layout - Return the content of a layout with its chain of parent
1158 # layouts applied, so that a layout may be wrapped by another layout, which
1159 # may in turn be wrapped by another.
1160  
1161 0 ▶sub expand_layout {
Subroutine expand_layout: not called CC=5 SCAR=34.0
Pod undocumented
1162 0 my ($self, $name, $raw, $seen) = @_;
1163  
1164 0 ▶ die "error: layout \"$name\" is part of a loop of layouts\n" if $seen->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $seen->{$name} 0 0 0
1165  
1166 my ($frontmatter, $content) =
1167 0 App::Dapper::Utils::split_frontmatter($raw->{$name}, "$self->{layout}/$name");
1168  
1169 0 ▶ return $raw->{$name} unless defined $frontmatter;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frontmatter 0 0 0
1170  
1171 0 my $parent = $frontmatter->{layout};
1172 0 ▶ return $content unless defined $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parent 0 0 0
1173  
1174 0 ▶ if (not exists $raw->{$parent}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $raw->{$parent}) 0 0 0
1175 0 warn "warning: layout \"$name\" names the layout \"$parent\", which does not exist\n";
1176 0 return $content;
1177 }
1178  
1179 0 my $master = $self->expand_layout($parent, $raw, { %$seen, $name => 1 });
1180 0 $master =~ s/\[%[-+=~]?\s*page\.content\s*[-+=~]?%\]/$content/g;
1181  
1182 0 return $master;
1183 }
1184  
1185 # sub walk - Walk the source directory in sorted order, passing each file and
1186 # its output path to build_inventory. Dot files and directories are skipped.
1187  
1188 0 ▶sub walk {
Subroutine walk: not called CC=7 SCAR=40.3
Pod undocumented
1189 0 my ($self, $source_dir, $output_dir, $schema) = @_;
1190  
1191 0 ▶ die "error: could not open source directory \"$source_dir\"\n" unless -d $source_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $source_dir 0 0 0
1192 0 my @entries = grep { !/^\./ } App::Dapper::Utils::read_dir($source_dir);
1193  
1194 # A _schema.yml applies to this directory and those below it, unless they
1195 # have a schema of their own
1196 0 my $schema_file = "$source_dir/_schema.yml";
1197 0 ▶ if (-f $schema_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $schema_file) 0 0 0
1198 0 ▶ $self->attempt($schema_file, sub { $schema = App::Dapper::Schema::load($schema_file) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
1199 }
1200  
1201 0 for my $entry (@entries) {
1202 0 ▶ next if $entry eq '_schema.yml';
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq "_schema.yml" 0 0 0
1203  
1204 0 my $source = "$source_dir/$entry";
1205 0 my $output = "$output_dir/$entry";
1206  
1207 0 ▶ if (-d $source) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $source) { } 0 0 0
elsif (-f $source) { } 0 0 0
1208 0 $self->walk($source, $output, $schema);
1209 }
1210 elsif (-f $source) {
1211 0 ▶ $self->attempt($source, sub { $self->build_inventory($source, $output, $schema) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
1212 }
1213 }
1214 }
1215  
1216 # sub build_inventory - Read one source file and add it to the site hash as a
1217 # page, working out its date, slug, URL and output file. A file without front
1218 # matter is recorded to be copied as it is instead.
1219  
1220 0 ▶sub build_inventory {
Subroutine build_inventory: not called CC=38 SCAR=73.0
Pod undocumented
1221 0 my ($self, $source_file_name, $destination_file_name, $schema) = @_;
1222  
1223 # Files without front matter (images, PDFs, etc.) are copied as they are
1224 0 ▶ if (not App::Dapper::Utils::has_frontmatter($source_file_name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (App::Dapper::Utils::has_frontmatter($source_file_name)) 0 0 0
1225 0 push @{$self->{static_files}}, { source => $source_file_name, output => $destination_file_name };
1226 0 return;
1227 }
1228  
1229 0 $self->_log("Processing $source_file_name");
1230  
1231 0 my $source_content = App::Dapper::Utils::read_file($source_file_name);
1232  
1233 0 my ($frontmatter, $content) =
1234 App::Dapper::Utils::split_frontmatter($source_content, $source_file_name);
1235 0 ▶ die "error: \"$source_file_name\" has front matter that is never closed with a line containing ---\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frontmatter 0 0 0
1236 unless defined $frontmatter;
1237  
1238 # A page that does not match its schema is reported and goes no further
1239 0 ▶ if ($schema) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema) 0 0 0
1240 0 my @messages = App::Dapper::Schema::validate($schema, $frontmatter);
1241 0 for my $message (@messages) {
1242 0 ▶ if ($self->{collect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'collect'}) { } 0 0 0
1243 0 $self->problem('error', $source_file_name, "$message (see $schema->{file})");
1244 }
1245 else {
1246 0 push @{$self->{schema_errors}}, "$source_file_name: $message";
1247 }
1248 }
1249 0 ▶ return if @messages;
Branch: 1 branch
BranchTrueFalseTotal
if @messages 0 0 0
1250 }
1251  
1252 0 my %page = %$frontmatter;
1253 0 $page{content} = $content;
1254 0 $page{source} = $source_file_name;
1255  
1256 0 ▶ my @categories = ref $page{categories} eq 'ARRAY' ? @{$page{categories}}
Branch: 1 branch
BranchTrueFalseTotal
defined $page{'categories'} ? : 0 0 0
1257 : defined $page{categories} ? ($page{categories})
1258 0 ▶ : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $page{'categories'} eq 'ARRAY' ? : 0 0 0
1259 0 ▶ @categories = grep { defined and length } @categories;
Condition0%0/3
defined $_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
1260  
1261 0 $page{slug} = App::Dapper::Utils::slugify($page{title});
1262 0 ▶ if (not defined $page{slug} or not length $page{slug}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page{"slug"} and length $page{"slug"}) 0 0 0
Condition0%0/3
defined $page{"slug"} and length $page{"slug"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $page{"slug"} and length $page{"slug"}
defined $page{"slug"} length $page{"slug"}
Truth table0%0/3
A and B
A= defined $page{"slug"}B= length $page{"slug"}
ABresult
0X0
100
111
1263 0 $page{slug} = App::Dapper::Utils::slugify(basename(App::Dapper::Utils::filter_stem($source_file_name)));
1264 }
1265  
1266 my $date = App::Dapper::Utils::parse_date(
1267 0 ▶ defined $page{date} ? $page{date} : App::Dapper::Utils::get_modified_time($source_file_name),
Branch: 1 branch
BranchTrueFalseTotal
defined $page{'date'} ? : 0 0 0
1268 $source_file_name);
1269  
1270 # A date written without a time zone is in the page's time zone: its
1271 # "timezone" setting, the site's, or the local time zone
1272 0 ▶ my $zone = $page{timezone} || $self->{site}->{timezone} || 'local';
Condition0%0/5
$page{'timezone'} || $self->{'site'}{'timezone'} || 'local'
l!l
00
$page{"timezone"} or $self->{"site"}{"timezone"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$page{'timezone'} || $self->{'site'}{'timezone'} || 'local'
$page{"timezone"} $self->{"site"}{"timezone"}
Truth table0%0/3
A or B
A= $page{"timezone"}B= $self->{"site"}{"timezone"}
ABresult
1X1
011
000
1273 0 ▶ my $timezone = ref $zone ? $zone : eval { DateTime::TimeZone->new( name => $zone ) }
Branch: 2 branches
BranchTrueFalseTotal
my $timezone = ref $zone ? $zone : eval { do { "DateTime::TimeZone"->new("name", $zone) } } or die qq[error: unknown time zone "$zone" in "$source_file_name"\n] 0 0 0
ref $zone ? : 0 0 0
1274 or die "error: unknown time zone \"$zone\" in \"$source_file_name\"\n";
1275 0 ▶ $date->set_time_zone($timezone) if $date->time_zone->is_floating;
Branch: 1 branch
BranchTrueFalseTotal
if $date->time_zone->is_floating 0 0 0
1276 0 $page{timezone} = $timezone;
1277  
1278 0 $page{date} = $date;
1279 0 $page{year} = $date->year();
1280 0 $page{month} = $date->month();
1281 0 $page{day} = $date->day();
1282 0 $page{hour} = $date->hour();
1283 0 $page{minute} = $date->minute();
1284 0 $page{second} = $date->second();
1285  
1286 # Insert zero to beginning if year, month, day,
1287 # hour, minute or second is less than 10
1288 $page{$_} < 10 and $page{$_} = '0' . $page{$_}
1289 0 ▶ for ('year', 'month', 'day', 'hour', 'minute', 'second');
Branch: 1 branch
BranchTrueFalseTotal
$page{$_} < 10 and $page{$_} = '0' . $page{$_} 0 0 0
1290  
1291 0 $page{id} = ++$self->{id};
1292  
1293 0 ▶ if (not defined $page{extension}) { $page{extension} = $self->{site}->{extension}; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page{"extension"}) 0 0 0
1294 0 ▶ $page{extension} = '' unless defined $page{extension};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $page{"extension"} 0 0 0
1295  
1296 0 $page{source_file_extension} = App::Dapper::Utils::filter_extension($source_file_name);
1297 0 ▶ $page{source_file_extension} = '' unless defined $page{source_file_extension};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $page{"source_file_extension"} 0 0 0
1298  
1299 0 ▶ if (@categories or defined $page{urlpattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@categories or defined $page{'urlpattern'}) { } 0 0 0
Condition0%0/3
@categories or defined $page{"urlpattern"}
l!l&&r!l&&!r
000
MC/DC0%0/2
@categories or defined $page{"urlpattern"}
@categories defined $page{"urlpattern"}
Truth table0%0/3
A or B
A= @categoriesB= defined $page{"urlpattern"}
ABresult
1X1
011
000
1300 # The URL decides where the page is written
1301 0 ▶ my $pattern = defined $page{urlpattern} ? $page{urlpattern} : $self->{site}->{urlpattern};
Branch: 1 branch
BranchTrueFalseTotal
defined $page{'urlpattern'} ? : 0 0 0
1302 0 $page{url} = expand_urlpattern($pattern, { %page, category => $categories[0] });
1303  
1304 0 my $filename = $page{url};
1305 0 ▶ $filename .= "index$page{extension}" if $filename =~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
if $filename =~ m[/$] 0 0 0
1306 0 $page{filename} = canonpath(catfile($self->{output}, $filename));
1307 }
1308 else {
1309 # The path of the source file decides where the page is written
1310 0 $page{filename} = canonpath(App::Dapper::Utils::filter_stem($destination_file_name) . $page{extension});
1311  
1312 0 my $url = File::Spec->abs2rel($page{filename}, $self->{output});
1313 0 $url = join('/', File::Spec->splitdir($url));
1314 0 ▶ $url =~ s{(^|/)index\Q$page{extension}\E$}{$1} if length $page{extension};
Branch: 1 branch
BranchTrueFalseTotal
if length $page{"extension"} 0 0 0
1315 0 $page{url} = "/$url";
1316 }
1317  
1318 0 my ($volume, $dirname, $file) = File::Spec->splitpath( $page{filename} );
1319 0 $page{dirname} = $dirname;
1320  
1321 0 ▶ if ($page{source_file_extension} eq ".md") {
Branch: 1 branch
BranchTrueFalseTotal
if ($page{"source_file_extension"} eq ".md") 0 0 0
1322 0 $page{markdown} = $page{content};
1323 0 $page{content} = App::Dapper::Markdown::to_html($page{content});
1324  
1325 # Save first paragraph of content as excerpt
1326 0 ▶ $page{excerpt} = $1 if $page{content} =~ /(<p>.*?<\/p>)/s;
Branch: 1 branch
BranchTrueFalseTotal
if $page{"content"} =~ m[(<p>.*?</p>)]s 0 0 0
1327 }
1328  
1329 # Remove leading spaces and newline
1330 0 $page{content} =~ s/^[ \n]*//;
1331  
1332 0 for my $category (@categories) {
1333 0 ▶ my $list = $self->{site}->{categories}->{$category} ||= [];
Condition0%0/2
$self->{"site"}{"categories"}{$category} ||= []
l!l
00
MC/DC0%0/1
$self->{"site"}{"categories"}{$category} ||= []
$self->{"site"}{"categories"}{$category}
Truth table0%0/2
A
A= $self->{"site"}{"categories"}{$category}
Aresult
11
00
1334 0 push @$list, \%page;
1335  
1336 # Keep the array sorted
1337 0 @$list = sort { $a->{date} <=> $b->{date} } @$list;
1338 }
1339  
1340 0 ▶ my @tags = ref $page{tags} eq 'ARRAY' ? @{$page{tags}}
Branch: 1 branch
BranchTrueFalseTotal
defined $page{'tags'} ? : 0 0 0
1341 : defined $page{tags} ? ($page{tags})
1342 0 ▶ : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $page{'tags'} eq 'ARRAY' ? : 0 0 0
1343 0 ▶ for my $tag (grep { defined and !ref and length } @tags) {
Condition0%0/6
defined $_ && !ref($_) && length $_
!ll&&!rl&&r
000
defined $_ and not ref $_
!ll&&!rl&&r
000
MC/DC0%0/3
defined $_ && !ref($_) && length $_
defined $_ not ref $_ length $_
Truth table0%0/4
A and not(B) && C
A= defined $_B= not ref $_C= length $_
ABCresult
0XX0
10X0
1100
1111
1344 0 ▶ my $list = $self->{site}->{tags}->{$tag} ||= [];
Condition0%0/2
$self->{"site"}{"tags"}{$tag} ||= []
l!l
00
MC/DC0%0/1
$self->{"site"}{"tags"}{$tag} ||= []
$self->{"site"}{"tags"}{$tag}
Truth table0%0/2
A
A= $self->{"site"}{"tags"}{$tag}
Aresult
11
00
1345 0 push @$list, \%page;
1346 0 @$list = sort { $a->{date} <=> $b->{date} } @$list;
1347 }
1348  
1349 0 push @{$self->{site}->{pages}}, \%page;
1350 }
1351  
1352 =head2 expand_urlpattern
1353  
1354 Takes a URL pattern (e.g. C</:category/:year/:month/:slug/>) and a hash of page
1355 values, and returns the URL with each C<:name> token replaced. The tokens are
1356 C<:category>, C<:tag>, C<:year>, C<:month>, C<:day>, C<:hour>, C<:minute>,
1357 C<:second> and C<:slug>. Tokens with no
1358 value are removed along with their slash.
1359  
1360 =cut
1361  
1362 0 ▶sub expand_urlpattern {
Subroutine expand_urlpattern: not called CC=5 SCAR=34.0
Pod documented
1363 0 my ($pattern, $page) = @_;
1364  
1365 0 ▶ my $url = defined $pattern ? $pattern : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $pattern ? : 0 0 0
1366 0 for my $token (qw/ category tag year month day hour minute second slug /) {
1367 0 my $value = $page->{$token};
1368 0 ▶ $value = '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
1369 0 $url =~ s/:$token\b/$value/g;
1370 }
1371  
1372 0 $url =~ s{/{2,}}{/}g;
1373 0 ▶ $url = "/$url" unless $url =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^/] 0 0 0
1374  
1375 0 return $url;
1376 }
1377  
1378 # sub copy(sourcedir, outputdir) - Copy the files and directories in <sourcedir>
1379 # to <outputdir>, except those whose names match a pattern in
1380 # $self->{site}->{ignore}. The source, output and layout directories and the
1381 # project file are never copied.
1382  
1383 0 ▶sub copy {
Subroutine copy: not called CC=8 SCAR=42.8
Pod undocumented
1384 0 my ($self, $dir, $output) = @_;
1385  
1386 0 my @skip = map { abs_path($_) } grep { -e $_ }
1387 0 ($self->{source}, $self->{output}, $self->{layout}, $self->{config}, $self->{data_dir});
1388 0 my %skip = map { $_ => 1 } grep { defined } @skip;
1389  
1390 0 ENTRY: for my $file (App::Dapper::Utils::read_dir($dir)) {
1391  
1392 0 ▶ for my $i (@{ $self->{site}->{ignore} || [] }) {
Condition0%0/2
$self->{'site'}{'ignore'} || []
l!l
00
MC/DC0%0/1
$self->{'site'}{'ignore'} || []
$self->{'site'}{'ignore'}
Truth table0%0/2
A
A= $self->{'site'}{'ignore'}
Aresult
11
00
1393 0 ▶ next ENTRY if ($file =~ m/$i/);
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /$i/ 0 0 0
1394 }
1395  
1396 0 ▶ my $path = $dir eq '.' ? $file : "$dir/$file";
Branch: 1 branch
BranchTrueFalseTotal
$dir eq '.' ? : 0 0 0
1397 0 my $abs = abs_path($path);
1398 0 ▶ next if defined $abs and $skip{$abs};
Branch: 1 branch
BranchTrueFalseTotal
if defined $abs and $skip{$abs} 0 0 0
Condition0%0/3
defined $abs and $skip{$abs}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $abs and $skip{$abs}
defined $abs $skip{$abs}
Truth table0%0/3
A and B
A= defined $absB= $skip{$abs}
ABresult
0X0
100
111
1399  
1400 0 $self->record_written(App::Dapper::Utils::copy_recursive($path, catfile($output, $file), $self->{output}));
1401 0 $self->_log("Copied $path");
1402 }
1403 }
1404  
1405 # sub _log - Print a progress message unless quiet.
1406  
1407 0 ▶sub _log {
Subroutine _log: not called CC=2 SCAR=17.9
1408 0 my ($self, $message) = @_;
1409  
1410 0 ▶ print "$message\n" unless $self->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"quiet"} 0 0 0
1411 }
1412  
1413 =head1 AUTHOR
1414  
1415 Mark Benson, C<< <mark at jayber.org> >>
1416  
1417 =head1 BUGS
1418  
1419 Please report any bugs or feature requests at
1420 L<https://github.com/markdbenson/dapper/issues>.
1421  
1422 =head1 SUPPORT
1423  
1424 You can find documentation for this module with the perldoc command.
1425  
1426 perldoc App::Dapper
1427  
1428 You can also look for information at:
1429  
1430 =over 4
1431  
1432 =item * GitHub: source code and issue tracker
1433  
1434 L<https://github.com/markdbenson/dapper>
1435  
1436 =item * MetaCPAN
1437  
1438 L<https://metacpan.org/dist/App-Dapper>
1439  
1440 =back
1441  
1442 =head1 LICENSE AND COPYRIGHT
1443  
1444 The MIT License (MIT)
1445  
1446 Copyright (c) 2002-2026 Mark Benson
1447  
1448 Permission is hereby granted, free of charge, to any person obtaining a copy
1449 of this software and associated documentation files (the "Software"), to deal
1450 in the Software without restriction, including without limitation the rights
1451 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1452 copies of the Software, and to permit persons to whom the Software is
1453 furnished to do so, subject to the following conditions:
1454  
1455 The above copyright notice and this permission notice shall be included in all
1456 copies or substantial portions of the Software.
1457  
1458 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1459 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1460 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1461 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1462 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1463 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1464 SOFTWARE.
1465  
1466 =cut
1467  
1468 32 1; # End of App::Dapper
« lib/App/Dapper/Agents.pm ↑ Summary lib/App/Dapper/Deploy.pm »