lib/App/Dapper/MCP.pm

filter: Statementstmt 16.5% 55 / 333 Branchbran 0.0% 0 / 200 Conditioncond 0.0% 0 / 64 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 41.4% 17 / 41 Podpod 100% 20 / 20 totaltotal 13.2% 92 / 695 CC 126 SCAR 47.9
CC
126
Cov
9%
handle
380.0
source_path
210.0
write_layout
210.0
CRAP 119.9
« lib/App/Dapper/Images.pm ↑ Summary lib/App/Dapper/Schema.pm »
1 package App::Dapper::MCP;
2  
3 =head1 NAME
4  
5 App::Dapper::MCP - Let AI assistants work on a Dapper site over MCP.
6  
7 =head1 DESCRIPTION
8  
9 C<dapper mcp> runs a Model Context Protocol server on standard input and
10 output, so that an AI assistant (Claude, or any other MCP client) can read,
11 write, build and check a site through Dapper, with Dapper's rules as
12 guardrails: pages are only written inside the source directory, and a page
13 that doesn't match its C<_schema.yml> is refused rather than written.
14  
15 To add it to Claude Code:
16  
17 $ claude mcp add dapper -- dapper -C /path/to/site mcp
18  
19 Other clients take a command and arguments in their configuration:
20  
21 { "command": "dapper", "args": ["-C", "/path/to/site", "mcp"] }
22  
23 =head2 Prompts and resources
24  
25 The server also offers prompts for common jobs (C<new_site>, C<write_post>,
26 C<audit_site>) and resources an assistant can read: C<dapper://guide> (the
27 same guide as C<AGENTS.md>), C<dapper://config> and C<dapper://schemas>.
28  
29 =head2 Tools
30  
31 =over 4
32  
33 =item B<site_info>
34  
35 The site's settings, directories, categories, tags and schemas.
36  
37 =item B<list_pages>
38  
39 Every page (optionally only those in a C<category> or with a C<tag>), with
40 its source file, URL, title, date, categories and tags.
41  
42 =item B<read_page>
43  
44 A page's front matter and body, given its source file.
45  
46 =item B<write_page>
47  
48 Create or replace a page from front matter and a body. The page is checked
49 against its schema first and is not written if it doesn't match.
50  
51 =item B<rename_page>
52  
53 Move or rename a page; if its URL changes, a redirect from the old URL is
54 added to C<_data/redirects.yml>.
55  
56 =item B<read_layout>, B<write_layout>
57  
58 Read a layout, or create or replace one (it is parsed first and not saved if
59 it has an error).
60  
61 =item B<build>
62  
63 Build the site.
64  
65 =item B<check>
66  
67 Find problems without touching the output directory (see
68 L<App::Dapper::Check>).
69  
70 =back
71  
72 =cut
73  
74 190 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 190 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 190 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 190 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
78  
79 190 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
80 190 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 190 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 190 ▶use File::Spec::Functions qw/ canonpath catfile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 190 ▶use File::Basename qw/ dirname /;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 190 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
85 190 ▶use File::Path qw/ make_path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 190 ▶use Template::Alloy;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 190 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
88 190 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 190 ▶use App::Dapper::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 190 ▶use App::Dapper::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 190 ▶use App::Dapper::Agents;
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 190 my $PROTOCOL_VERSION = '2025-06-18';
95  
96 190 my @TOOLS = (
97 {
98 name => 'site_info',
99 description => 'Describe the Dapper site: its settings, directories, number of pages, categories, tags, and the front matter schemas pages must follow.',
100 inputSchema => { type => 'object', properties => {} },
101 },
102 {
103 name => 'list_pages',
104 description => 'List the pages of the site with their source file, URL, title, date, categories and tags. Optionally only pages in a category or with a tag.',
105 inputSchema => {
106 type => 'object',
107 properties => {
108 category => { type => 'string', description => 'Only pages in this category' },
109 tag => { type => 'string', description => 'Only pages with this tag' },
110 },
111 },
112 },
113 {
114 name => 'read_page',
115 description => 'Read a page: its front matter (as an object) and its body (Markdown or HTML).',
116 inputSchema => {
117 type => 'object',
118 properties => {
119 source => { type => 'string', description => 'The source file, e.g. blog/my-post.md (relative to the source directory)' },
120 },
121 required => ['source'],
122 },
123 },
124 {
125 name => 'write_page',
126 description => 'Create or replace a page in the source directory. The front matter is checked against the nearest _schema.yml first; if it does not match, nothing is written and the problems are returned. Run check or build afterwards.',
127 inputSchema => {
128 type => 'object',
129 properties => {
130 source => { type => 'string', description => 'The source file to write, e.g. blog/my-post.md (relative to the source directory)' },
131 front_matter => { type => 'object', description => 'The page settings, e.g. {"title": "...", "date": "2026-01-01T09:00:00", "categories": ["blog"]}' },
132 body => { type => 'string', description => 'The page content, in Markdown for .md files' },
133 overwrite => { type => 'boolean', description => 'Replace the file if it already exists (default false)' },
134 },
135 required => ['source', 'front_matter', 'body'],
136 },
137 },
138 {
139 name => 'rename_page',
140 description => 'Move or rename a page. If its URL changes, a redirect from the old URL is added to _data/redirects.yml so links keep working. Refused if the page would not match the schema of its new folder.',
141 inputSchema => {
142 type => 'object',
143 properties => {
144 source => { type => 'string', description => 'The page to move, e.g. blog/old-name.md' },
145 new_source => { type => 'string', description => 'Where to move it, e.g. blog/new-name.md' },
146 },
147 required => ['source', 'new_source'],
148 },
149 },
150 {
151 name => 'read_layout',
152 description => 'Read a layout (a template in the layout directory), e.g. "post".',
153 inputSchema => {
154 type => 'object',
155 properties => { name => { type => 'string', description => 'The layout name, without .html' } },
156 required => ['name'],
157 },
158 },
159 {
160 name => 'write_layout',
161 description => 'Create or replace a layout. The template is parsed first and not saved if it has a syntax error. Layouts use Template Toolkit syntax: [% page.content %], [% page.title %], [% site.name %].',
162 inputSchema => {
163 type => 'object',
164 properties => {
165 name => { type => 'string', description => 'The layout name, without .html (letters, digits, - and _)' },
166 content => { type => 'string', description => 'The whole layout, optionally starting with front matter (layout: to wrap it in another layout, description: to explain it)' },
167 overwrite => { type => 'boolean', description => 'Replace an existing layout (default false)' },
168 },
169 required => ['name', 'content'],
170 },
171 },
172 {
173 name => 'build',
174 description => 'Build the site into the output directory. Drafts (draft: true) and pages dated in the future are left out unless drafts is true.',
175 inputSchema => { type => 'object', properties => { drafts => { type => 'boolean', description => 'Include drafts and pages dated in the future (default: false)' } } },
176 },
177 {
178 name => 'check',
179 description => 'Check the site for problems (front matter, schemas, layouts, templates, broken links, accessibility, SEO) without touching the output directory. The report lists the drafts and scheduled pages left out (in held); set drafts to true to check them too.',
180 inputSchema => { type => 'object', properties => { drafts => { type => 'boolean', description => 'Include drafts and pages dated in the future (default: false)' } } },
181 },
182 );
183  
184 190 my @PROMPTS = (
185 {
186 name => 'new_site',
187 description => 'Turn this project into a new site from a description of what it is for.',
188 arguments => [ { name => 'description', description => 'What the site is for and what it should contain', required => JSON::true } ],
189 },
190 {
191 name => 'write_post',
192 description => 'Write a new post or page that matches the site\'s schemas.',
193 arguments => [
194 { name => 'topic', description => 'What the post is about', required => JSON::true },
195 { name => 'category', description => 'The category to put it in (optional)', required => JSON::false },
196 ],
197 },
198 {
199 name => 'audit_site',
200 description => 'Check the whole site for problems and fix them.',
201 arguments => [],
202 },
203 );
204  
205 190 my @RESOURCES = (
206 { uri => 'dapper://guide', name => 'Guide to this site', description => 'How this Dapper project works: commands, folders, layouts, schemas and rules (the same as AGENTS.md).', mimeType => 'text/markdown' },
207 { uri => 'dapper://config', name => 'Project file', description => 'The site\'s settings (_config.yml).', mimeType => 'text/yaml' },
208 { uri => 'dapper://schemas', name => 'Schemas', description => 'The front matter rules for each folder, as JSON.', mimeType => 'application/json' },
209 );
210  
211 =head2 serve
212  
213 Takes a Dapper object, and answers MCP requests from standard input on
214 standard output until standard input closes.
215  
216 =cut
217  
218 0 ▶sub serve {
Subroutine serve: not called CC=8 SCAR=42.8
Pod documented
219 0 my ($dapper) = @_;
220  
221 # Responses go to the real standard output; anything else that would be
222 # printed while a tool runs (such as Pagefind's output) goes to standard
223 # error, so that it can't corrupt the conversation
224 0 ▶ open(my $out, '>&', \*STDOUT) or die "error: could not open standard output: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">&", \*STDOUT or die "error: could not open standard output: $!\n" 0 0 0
225 0 binmode($out, ':raw');
226 0 $out->autoflush(1);
227 0 ▶ open(STDOUT, '>&', \*STDERR) or die "error: could not redirect standard output: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", \*STDERR or die "error: could not redirect standard output: $!\n" 0 0 0
228 0 binmode(STDIN, ':raw');
229  
230 0 my $json = JSON->new->utf8->canonical;
231  
232 0 ▶ while (my $line = <STDIN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline STDIN)) 0 0 0
233 0 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 0 0 0
234  
235 0 my $request = eval { $json->allow_nonref->decode($line) };
236 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
237 0 print $out $json->encode({ jsonrpc => '2.0', id => undef,
238 error => { code => -32700, message => 'Parse error' } }), "\n";
239 0 next;
240 }
241 # Valid JSON that is not a request object
242 0 ▶ if (ref $request ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $request ne "HASH") 0 0 0
243 0 print $out $json->encode({ jsonrpc => '2.0', id => undef,
244 error => { code => -32600, message => 'Invalid Request' } }), "\n";
245 0 next;
246 }
247  
248 0 my $response = handle($dapper, $request);
249 0 ▶ print $out $json->encode($response), "\n" if $response;
Branch: 1 branch
BranchTrueFalseTotal
if $response 0 0 0
250 }
251 }
252  
253 =head2 handle
254  
255 Takes a Dapper object and a JSON-RPC request, and returns the response (or
256 nothing, for notifications).
257  
258 =cut
259  
260 0 ▶sub handle {
Subroutine handle: not called CC=19 SCAR=59.4
Pod documented
261 0 my ($dapper, $request) = @_;
262  
263 0 my $id = $request->{id};
264 0 ▶ my $method = defined $request->{method} ? $request->{method} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $request->{'method'} ? : 0 0 0
265 0 ▶ my $params = ref $request->{params} eq 'HASH' ? $request->{params} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $request->{'params'} eq 'HASH' ? : 0 0 0
266  
267 0 ▶ return if not defined $id; # a notification, such as notifications/initialized
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
268  
269 0 my $result;
270 0 ▶ if ($method eq 'initialize') {
Branch: 8 branches
BranchTrueFalseTotal
if ($method eq 'initialize') { } 0 0 0
elsif ($method eq 'ping') { } 0 0 0
elsif ($method eq 'tools/list') { } 0 0 0
elsif ($method eq 'prompts/list') { } 0 0 0
elsif ($method eq 'prompts/get') { } 0 0 0
elsif ($method eq 'resources/list') { } 0 0 0
elsif ($method eq 'resources/read') { } 0 0 0
elsif ($method eq 'tools/call') { } 0 0 0
271 0 my $requested = $params->{protocolVersion};
272 0 ▶ $result = {
Branch: 1 branch
BranchTrueFalseTotal
defined $requested ? : 0 0 0
273 protocolVersion => defined $requested ? $requested : $PROTOCOL_VERSION,
274 capabilities => { tools => {}, prompts => {}, resources => {} },
275 serverInfo => { name => 'dapper', version => "$App::Dapper::VERSION" },
276 instructions => 'Dapper builds a static website from Markdown and HTML files with YAML front matter. '
277 . 'Use site_info first to learn the site\'s schemas and categories, write_page to create pages '
278 . '(it refuses pages that do not match their schema), then check to find problems and build to publish.',
279 };
280 }
281 elsif ($method eq 'ping') {
282 0 $result = {};
283 }
284 elsif ($method eq 'tools/list') {
285 0 $result = { tools => \@TOOLS };
286 }
287 elsif ($method eq 'prompts/list') {
288 0 $result = { prompts => \@PROMPTS };
289 }
290 elsif ($method eq 'prompts/get') {
291 0 ▶ my $prompt = get_prompt($dapper, $params->{name}, ref $params->{arguments} eq 'HASH' ? $params->{arguments} : {});
Branch: 1 branch
BranchTrueFalseTotal
ref $params->{'arguments'} eq 'HASH' ? : 0 0 0
292 0 ▶ return { jsonrpc => '2.0', id => $id, error => { code => -32602, message => "There is no prompt called \"" . (defined $params->{name} ? $params->{name} : '') . "\"" } }
Branch: 2 branches
BranchTrueFalseTotal
unless $prompt 0 0 0
defined $params->{'name'} ? : 0 0 0
293 unless $prompt;
294 0 $result = $prompt;
295 }
296 elsif ($method eq 'resources/list') {
297 0 $result = { resources => \@RESOURCES };
298 }
299 elsif ($method eq 'resources/read') {
300 0 my $contents = read_resource($dapper, $params->{uri});
301 0 ▶ return { jsonrpc => '2.0', id => $id, error => { code => -32002, message => "Resource not found: " . (defined $params->{uri} ? $params->{uri} : '') } }
Branch: 2 branches
BranchTrueFalseTotal
unless $contents 0 0 0
defined $params->{'uri'} ? : 0 0 0
302 unless $contents;
303 0 $result = { contents => [ $contents ] };
304 }
305 elsif ($method eq 'tools/call') {
306 0 ▶ $result = call_tool($dapper, $params->{name}, ref $params->{arguments} eq 'HASH' ? $params->{arguments} : {});
Branch: 1 branch
BranchTrueFalseTotal
ref $params->{'arguments'} eq 'HASH' ? : 0 0 0
307 }
308 else {
309 0 return { jsonrpc => '2.0', id => $id, error => { code => -32601, message => "Method not found: $method" } };
310 }
311  
312 0 return { jsonrpc => '2.0', id => $id, result => $result };
313 }
314  
315 =head2 get_prompt
316  
317 Takes a Dapper object, a prompt name and its arguments, and returns the
318 prompt's messages, or nothing if there is no such prompt.
319  
320 =cut
321  
322 0 ▶sub get_prompt {
Subroutine get_prompt: not called CC=9 SCAR=45.0
Pod documented
323 0 my ($dapper, $name, $args) = @_;
324  
325 0 ▶ return unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
326  
327 0 my $steps = "Work through the dapper MCP tools: start with site_info to learn the schemas, "
328 . "categories and layouts; use write_page for every page (it refuses pages that don't match "
329 . "their schema, so fix what it reports); run check until it reports no errors; then build.";
330  
331 0 my $text;
332 0 ▶ if ($name eq 'new_site') {
Branch: 3 branches
BranchTrueFalseTotal
if ($name eq 'new_site') { } 0 0 0
elsif ($name eq 'write_post') { } 0 0 0
elsif ($name eq 'audit_site') { } 0 0 0
333 0 ▶ my $description = defined $args->{description} ? $args->{description} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'description'} ? : 0 0 0
334 0 $text = "Turn this Dapper project into this site:\n\n$description\n\n"
335 . "Write the home page and the other pages the site needs, with a title and a description on each, "
336 . "and replace any placeholder text from the starter template. Keep to the existing layouts and schemas. "
337 . "Read the dapper://guide resource for this project's conventions. $steps";
338 }
339 elsif ($name eq 'write_post') {
340 0 ▶ my $topic = defined $args->{topic} ? $args->{topic} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'topic'} ? : 0 0 0
341 0 ▶ my $category = defined $args->{category} && length $args->{category} ? " in the \"$args->{category}\" category" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'category'} && length $args->{'category'} ? : 0 0 0
Condition0%0/3
defined $args->{"category"} and length $args->{"category"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args->{"category"} and length $args->{"category"}
defined $args->{"category"} length $args->{"category"}
Truth table0%0/3
A and B
A= defined $args->{"category"}B= length $args->{"category"}
ABresult
0X0
100
111
342 0 $text = "Write a new post$category about:\n\n$topic\n\n"
343 . "Use today's date, a clear title, a description of about 150 characters, and tags that are already "
344 . "in use where they fit. Write it in Markdown, with a strong first paragraph (it becomes the excerpt). $steps";
345 }
346 elsif ($name eq 'audit_site') {
347 0 $text = "Audit this Dapper site. Run check and fix every error it reports. Then look for pages without a "
348 . "description, placeholder text, outdated dates, and broken or vague links, and fix what you can with "
349 . "write_page. Finish with check and build, and summarize what you changed. $steps";
350 }
351 else {
352 0 return;
353 }
354  
355 return {
356 0 description => (grep { $_->{name} eq $name } @PROMPTS)[0]->{description},
357 0 messages => [ { role => 'user', content => { type => 'text', text => $text } } ],
358 };
359 }
360  
361 =head2 read_resource
362  
363 Takes a Dapper object and a resource URI, and returns its contents, or
364 nothing if there is no such resource.
365  
366 =cut
367  
368 0 ▶sub read_resource {
Subroutine read_resource: not called CC=6 SCAR=37.4
Pod documented
369 0 my ($dapper, $uri) = @_;
370  
371 0 ▶ return unless defined $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri 0 0 0
372  
373 0 ▶ if ($uri eq 'dapper://guide') {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri eq "dapper://guide") 0 0 0
374 0 return { uri => $uri, mimeType => 'text/markdown', text => App::Dapper::Agents::guide($dapper) };
375 }
376 0 ▶ if ($uri eq 'dapper://config') {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri eq "dapper://config") 0 0 0
377 0 ▶ my $text = -f $dapper->{config} ? App::Dapper::Utils::read_file($dapper->{config}) : '';
Branch: 1 branch
BranchTrueFalseTotal
-f $dapper->{'config'} ? : 0 0 0
378 0 return { uri => $uri, mimeType => 'text/yaml', text => $text };
379 }
380 0 ▶ if ($uri eq 'dapper://schemas') {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri eq "dapper://schemas") 0 0 0
381 0 my $info = site_info($dapper);
382 0 return { uri => $uri, mimeType => 'application/json', text => JSON->new->canonical->pretty->encode($info->{schemas}) };
383 }
384  
385 0 return;
386 }
387  
388 =head2 call_tool
389  
390 Takes a Dapper object, a tool name and its arguments, runs the tool, and
391 returns an MCP tool result.
392  
393 =cut
394  
395 0 ▶sub call_tool {
Subroutine call_tool: not called CC=5 SCAR=34.0
Pod documented
396 0 my ($dapper, $name, $args) = @_;
397  
398 0 my %tools = (
399 site_info => \&site_info,
400 list_pages => \&list_pages,
401 read_page => \&read_page,
402 write_page => \&write_page,
403 rename_page => \&rename_page,
404 read_layout => \&read_layout,
405 write_layout => \&write_layout,
406 build => \&build,
407 check => \&check,
408 );
409  
410 0 ▶ my $tool = defined $name ? $tools{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 0 0
411 0 ▶ return tool_result({ error => "There is no tool called \"" . (defined $name ? $name : '') . "\"." }, 1) unless $tool;
Branch: 2 branches
BranchTrueFalseTotal
unless $tool 0 0 0
defined $name ? : 0 0 0
412  
413 0 my ($data, $is_error);
414 0 ▶ eval { ($data, $is_error) = $tool->($dapper, $args); 1 } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { () = &$tool($dapper, $args); 1 } } or do { (my $message = $@) =~ s/^error: //; $message =~ s/\s+\z//; () = ({"error", $message}, 1) } 0 0 0
415 0 (my $message = $@) =~ s/^error: //;
416 0 $message =~ s/\s+\z//;
417 0 ($data, $is_error) = ({ error => $message }, 1);
418 };
419  
420 0 return tool_result($data, $is_error);
421 }
422  
423 =head2 tool_result
424  
425 Takes a result (a hash) and whether it is an error, and returns it in the form
426 MCP expects: as JSON text, and as structured content.
427  
428 =cut
429  
430 0 ▶sub tool_result {
Subroutine tool_result: not called CC=2 SCAR=17.9
Pod documented
431 0 my ($data, $is_error) = @_;
432  
433 return {
434 0 ▶ content => [ { type => 'text', text => JSON->new->canonical->pretty->encode($data) } ],
Branch: 1 branch
BranchTrueFalseTotal
$is_error ? : 0 0 0
435 structuredContent => $data,
436 isError => $is_error ? JSON::true : JSON::false,
437 };
438 }
439  
440 # Read the project and source files without writing anything
441 0 ▶sub _load {
Subroutine _load: not called CC=1 SCAR=6.9
442 0 my ($dapper) = @_;
443  
444 0 local $dapper->{quiet} = 1;
445 0 $dapper->{problems} = [];
446 0 $dapper->{written} = {};
447 0 $dapper->{page_owner} = {};
448 0 $dapper->parse();
449 0 $dapper->transform();
450 }
451  
452 0 ▶sub _page_summary {
Subroutine _page_summary: not called CC=9 SCAR=45.0
453 0 my ($dapper, $page) = @_;
454  
455 0 my $source = $page->{source};
456 0 my $prefix = "$dapper->{source}/";
457 0 ▶ $source = substr($source, length $prefix) if index($source, $prefix) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($source, $prefix) == 0 0 0 0
458  
459 0 my %summary = (source => $source, url => $page->{url});
460 0 ▶ $summary{title} = $page->{title} if defined $page->{title};
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"title"} 0 0 0
461 0 ▶ $summary{date} = "$page->{date}" if defined $page->{date};
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"date"} 0 0 0
462 0 ▶ $summary{layout} = $page->{layout} if defined $page->{layout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"layout"} 0 0 0
463 0 for my $list (qw/ categories tags /) {
464 0 my $value = $page->{$list};
465 0 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
466 0 ▶ $summary{$list} = ref $value eq 'ARRAY' ? $value : [$value];
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 0 0 0
467 }
468 0 ▶ $summary{generated} = JSON::true if $page->{generated};
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"generated"} 0 0 0
469 0 return \%summary;
470 }
471  
472 =head2 site_info
473  
474 The C<site_info> tool.
475  
476 =cut
477  
478 0 ▶sub site_info {
Subroutine site_info: not called CC=6 SCAR=37.4
Pod documented
479 0 my ($dapper) = @_;
480  
481 0 _load($dapper);
482 0 my $site = $dapper->{site};
483  
484 0 my %settings;
485 0 for my $key (sort keys %$site) {
486 0 ▶ next if $key =~ /^(?:pages|categories|tags|data|time|category_pages|tag_pages)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^(?:pages|categories|tags|data|time|category_pages|tag_pages)$/ 0 0 0
487 0 my $value = $site->{$key};
488 0 ▶ next if ref $value and ref $value ne 'HASH' and ref $value ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and ref $value ne "HASH" and ref $value ne "ARRAY" 0 0 0
Condition0%0/6
ref $value and ref $value ne "HASH" and ref $value ne "ARRAY"
!ll&&!rl&&r
000
ref $value and ref $value ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
ref $value and ref $value ne "HASH" and ref $value ne "ARRAY"
ref $value ref $value ne "HASH" ref $value ne "ARRAY"
Truth table0%0/4
A and B and C
A= ref $valueB= ref $value ne "HASH"C= ref $value ne "ARRAY"
ABCresult
0XX0
10X0
1100
1111
489 0 $settings{$key} = $value;
490 }
491  
492 0 my @schemas;
493 File::Find::find({
494 no_chdir => 1,
495 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
496 0 ▶ return unless $_ =~ m{/_schema\.yml$};
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ m[/_schema\.yml$] 0 0 0
497 0 my $name = App::Dapper::Utils::name_chars($File::Find::name);
498 0 my $schema = eval { App::Dapper::Schema::load($name) };
499 0 my $dir = dirname($name);
500 push @schemas, $schema
501 ? { applies_to => $dir, file => $name, required => $schema->{required},
502 0 ▶ fields => $schema->{fields}, additional => $schema->{additional} ? JSON::true : JSON::false }
Branch: 2 branches
BranchTrueFalseTotal
$schema ? : 0 0 0
$schema->{'additional'} ? : 0 0 0
503 : { applies_to => $dir, file => $name, error => "$@" };
504 },
505 0 }, App::Dapper::Utils::name_bytes($dapper->{source}));
506  
507 return {
508 name => $site->{name},
509 url => $site->{url},
510 description => $site->{description},
511 directories => { source => $dapper->{source}, output => $dapper->{output}, layout => $dapper->{layout}, data => $dapper->{data_dir} },
512 0 pages => scalar @{$site->{pages}},
513 0 categories => { map { $_ => scalar @{$site->{categories}->{$_}} } keys %{$site->{categories}} },
514 0 tags => { map { $_ => scalar @{$site->{tags}->{$_}} } keys %{$site->{tags}} },
515 0 layouts => [ sort keys %{$dapper->{layout_content}} ],
516 0 schemas => [ sort { $a->{applies_to} cmp $b->{applies_to} } @schemas ],
517 settings => \%settings,
518 };
519 }
520  
521 =head2 list_pages
522  
523 The C<list_pages> tool.
524  
525 =cut
526  
527 0 ▶sub list_pages {
Subroutine list_pages: not called CC=5 SCAR=34.0
Pod documented
528 0 my ($dapper, $args) = @_;
529  
530 0 _load($dapper);
531 0 my $site = $dapper->{site};
532  
533 0 my @pages = @{$site->{pages}};
534 0 ▶ if (defined $args->{category}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args->{'category'}) { } 0 0 0
elsif (defined $args->{'tag'}) { } 0 0 0
535 0 ▶ @pages = reverse @{ $site->{categories}->{$args->{category}} || [] };
Condition0%0/2
$site->{'categories'}{$args->{'category'}} || []
l!l
00
MC/DC0%0/1
$site->{'categories'}{$args->{'category'}} || []
$site->{'categories'}{$args->{'category'}}
Truth table0%0/2
A
A= $site->{'categories'}{$args->{'category'}}
Aresult
11
00
536 }
537 elsif (defined $args->{tag}) {
538 0 ▶ @pages = reverse @{ $site->{tags}->{$args->{tag}} || [] };
Condition0%0/2
$site->{'tags'}{$args->{'tag'}} || []
l!l
00
MC/DC0%0/1
$site->{'tags'}{$args->{'tag'}} || []
$site->{'tags'}{$args->{'tag'}}
Truth table0%0/2
A
A= $site->{'tags'}{$args->{'tag'}}
Aresult
11
00
539 }
540  
541 0 return { pages => [ map { _page_summary($dapper, $_) } @pages ] };
542 }
543  
544 =head2 source_path
545  
546 Takes a Dapper object and a path given by a client, and returns the path of
547 the file in the source directory. Dies if the path would lead outside it.
548  
549 =cut
550  
551 0 ▶sub source_path {
Subroutine source_path: not called CC=14 SCAR=53.5
Pod documented
552 0 my ($dapper, $path) = @_;
553  
554 0 ▶ die "error: give the source file, e.g. blog/my-post.md\n" unless defined $path and length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 0 0 0
Condition0%0/3
defined $path and length $path
!ll&&!rl&&r
000
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table0%0/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
555 0 ▶ die "error: \"$path\" must be relative to the source directory\n" if File::Spec->file_name_is_absolute($path);
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($path) 0 0 0
556  
557 0 my $source = canonpath($dapper->{source});
558 0 my $relative = canonpath($path);
559 0 ▶ $relative = substr($relative, length($source) + 1) if index($relative, "$source/") == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($relative, "$source/") == 0 0 0 0
560  
561 0 my @parts = File::Spec->splitdir($relative);
562 0 ▶ die "error: \"$path\" must not contain ..\n" if grep { $_ eq '..' } @parts;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "..";} @parts 0 0 0
563 0 ▶ die "error: \"$path\" must not be a hidden file\n" if grep { /^\./ } @parts;
Branch: 1 branch
BranchTrueFalseTotal
if grep {/^\./;} @parts 0 0 0
564  
565 # A symbolic link must not lead out of the source directory
566 0 my $file = canonpath(catfile($source, $relative));
567 0 my $root = Cwd::abs_path($source);
568 0 my $dir = $file;
569 0 ▶ $dir = File::Basename::dirname($dir) until -e $dir or $dir eq '.' or $dir eq '/';
Branch: 1 branch
BranchTrueFalseTotal
(-e $dir or $dir eq "." or $dir eq "/") or do { $dir = File::Basename::dirname($dir) } 0 0 0
Condition0%0/6
-e $dir or $dir eq "." or $dir eq "/"
l!l&&r!l&&!r
000
-e $dir or $dir eq "."
l!l&&r!l&&!r
000
MC/DC0%0/3
-e $dir or $dir eq "." or $dir eq "/"
-e $dir $dir eq "." $dir eq "/"
Truth table0%0/4
A or B or C
A= -e $dirB= $dir eq "."C= $dir eq "/"
ABCresult
1XX1
01X1
0011
0000
570 0 my $real = Cwd::abs_path($dir);
571 0 ▶ die "error: \"$path\" is outside the source directory\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $root and defined $real and $real ne $root and index($real, "$root/") != 0 0 0 0
Condition0%0/9
defined $root and defined $real and $real ne $root and index($real, "$root/") != 0
!ll&&!rl&&r
000
defined $root and defined $real and $real ne $root
!ll&&!rl&&r
000
defined $root and defined $real
!ll&&!rl&&r
000
MC/DC0%0/4
defined $root and defined $real and $real ne $root and index($real, "$root/") != 0
defined $root defined $real $real ne $root index($real, "$root/") != 0
Truth table0%0/5
A and B and C and D
A= defined $rootB= defined $realC= $real ne $rootD= index($real, "$root/") != 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
572 if defined $root and defined $real and $real ne $root and index($real, "$root/") != 0;
573  
574 0 return $file;
575 }
576  
577 =head2 read_page
578  
579 The C<read_page> tool.
580  
581 =cut
582  
583 0 ▶sub read_page {
Subroutine read_page: not called CC=3 SCAR=24.8
Pod documented
584 0 my ($dapper, $args) = @_;
585  
586 0 $dapper->read_project();
587 0 my $file = source_path($dapper, $args->{source});
588 0 ▶ die "error: there is no file \"$file\"\n" unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
589  
590 0 my $text = App::Dapper::Utils::read_file($file);
591 0 my ($front_matter, $body) = App::Dapper::Utils::split_frontmatter($text, $file);
592 0 ▶ return { source => $args->{source}, file => $file, front_matter => undef, body => $text } unless defined $front_matter;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $front_matter 0 0 0
593  
594 0 $body =~ s/^(?:\r?\n)+//;
595 0 return { source => $args->{source}, file => $file, front_matter => $front_matter, body => $body };
596 }
597  
598 =head2 schema_for
599  
600 Takes a Dapper object and a file in the source directory, and returns the
601 schema that applies to it (from the nearest C<_schema.yml> at or above its
602 directory), or nothing.
603  
604 =cut
605  
606 0 ▶sub schema_for {
Subroutine schema_for: not called CC=1 SCAR=6.9
Pod documented
607 0 my ($dapper, $file) = @_;
608 0 return App::Dapper::Schema::for_file($dapper->{source}, $file);
609 }
610  
611 =head2 write_page
612  
613 The C<write_page> tool.
614  
615 =cut
616  
617 0 ▶sub write_page {
Subroutine write_page: not called CC=12 SCAR=50.5
Pod documented
618 0 my ($dapper, $args) = @_;
619  
620 0 $dapper->read_project();
621 0 my $file = source_path($dapper, $args->{source});
622  
623 0 my $front_matter = $args->{front_matter};
624 0 ▶ die "error: front_matter must be an object of page settings\n" unless ref $front_matter eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $front_matter eq "HASH" 0 0 0
625 0 ▶ my $body = defined $args->{body} ? $args->{body} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'body'} ? : 0 0 0
626 0 ▶ die "error: body must be text\n" if ref $body;
Branch: 1 branch
BranchTrueFalseTotal
if ref $body 0 0 0
627  
628 0 ▶ if (-e $file and not $args->{overwrite}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file and not $args->{"overwrite"}) 0 0 0
Condition0%0/3
-e $file and not $args->{"overwrite"}
!ll&&!rl&&r
000
MC/DC0%0/2
-e $file and not $args->{"overwrite"}
-e $file not $args->{"overwrite"}
Truth table0%0/3
A and not(B)
A= -e $fileB= not $args->{"overwrite"}
ABresult
0X0
100
111
629 0 return ({ written => JSON::false, error => "$file already exists; set overwrite to true to replace it" }, 1);
630 }
631  
632 0 ▶ if (my $schema = schema_for($dapper, $file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $schema = schema_for($dapper, $file)) 0 0 0
633 0 my @problems = App::Dapper::Schema::validate($schema, $front_matter);
634 0 ▶ if (@problems) {
Branch: 1 branch
BranchTrueFalseTotal
if (@problems) 0 0 0
635 0 return ({ written => JSON::false, schema => $schema->{file}, problems => \@problems,
636 error => "The front matter does not match $schema->{file}, so the page was not written." }, 1);
637 }
638 }
639  
640 # Empty front matter is written as two lines of dashes, which Dapper reads
641 0 ▶ my $yaml = %$front_matter ? YAML::PP->new(boolean => 'JSON::PP')->dump_string($front_matter) : "---\n";
Branch: 1 branch
BranchTrueFalseTotal
%$front_matter ? : 0 0 0
642 0 ▶ $yaml = "---\n$yaml" unless $yaml =~ /^---\n/;
Branch: 1 branch
BranchTrueFalseTotal
unless $yaml =~ /^---\n/ 0 0 0
643 0 ▶ $yaml .= "\n" unless $yaml =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $yaml =~ /\n\z/ 0 0 0
644 0 ▶ $body .= "\n" unless $body =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $body =~ /\n\z/ 0 0 0
645  
646 0 App::Dapper::Utils::write_file($file, "$yaml---\n\n$body");
647  
648 0 return { written => JSON::true, file => $file, note => 'Run check to find problems, or build to publish.' };
649 }
650  
651 =head2 rename_page
652  
653 The C<rename_page> tool.
654  
655 =cut
656  
657 0 ▶sub rename_page {
Subroutine rename_page: not called CC=11 SCAR=48.8
Pod documented
658 0 my ($dapper, $args) = @_;
659  
660 0 $dapper->read_project();
661 0 my $from = source_path($dapper, $args->{source});
662 0 my $to = source_path($dapper, $args->{new_source});
663 0 ▶ die "error: there is no file \"$from\"\n" unless -f $from;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $from 0 0 0
664 0 ▶ return ({ moved => JSON::false, error => "$to already exists" }, 1) if -e $to;
Branch: 1 branch
BranchTrueFalseTotal
if -e $to 0 0 0
665  
666 # The page must match the schema of the folder it is moving to
667 0 my $text = App::Dapper::Utils::read_file($from);
668 0 my ($front_matter) = App::Dapper::Utils::split_frontmatter($text, $from);
669 0 ▶ if (defined $front_matter and my $schema = schema_for($dapper, $to)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $front_matter and my $schema = schema_for($dapper, $to)) 0 0 0
Condition0%0/3
defined $front_matter and my $schema = schema_for($dapper, $to)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $front_matter and my $schema = schema_for($dapper, $to)
defined $front_matter my $schema = schema_for($dapper, $to)
Truth table0%0/3
A and B
A= defined $front_matterB= my $schema = schema_for($dapper, $to)
ABresult
0X0
100
111
670 0 my @problems = App::Dapper::Schema::validate($schema, $front_matter);
671 0 ▶ return ({ moved => JSON::false, schema => $schema->{file}, problems => \@problems,
Branch: 1 branch
BranchTrueFalseTotal
if @problems 0 0 0
672 error => "The page does not match $schema->{file}, so it was not moved." }, 1) if @problems;
673 }
674  
675 0 my $old_url = _url_of($dapper, $from);
676  
677 0 ▶ make_path(dirname($to)) unless -d dirname($to);
Branch: 1 branch
BranchTrueFalseTotal
unless -d dirname($to) 0 0 0
678 0 ▶ rename($from, $to) or die "error: could not move $from to $to: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
rename $from, $to or die "error: could not move $from to ${to}: $!\n" 0 0 0
679  
680 0 my $new_url = _url_of($dapper, $to);
681 0 my %result = (moved => JSON::true, from => $from, to => $to, old_url => $old_url, new_url => $new_url);
682  
683 0 ▶ if (defined $old_url and defined $new_url and $old_url ne $new_url) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_url and defined $new_url and $old_url ne $new_url) 0 0 0
Condition0%0/6
defined $old_url and defined $new_url and $old_url ne $new_url
!ll&&!rl&&r
000
defined $old_url and defined $new_url
!ll&&!rl&&r
000
MC/DC0%0/3
defined $old_url and defined $new_url and $old_url ne $new_url
defined $old_url defined $new_url $old_url ne $new_url
Truth table0%0/4
A and B and C
A= defined $old_urlB= defined $new_urlC= $old_url ne $new_url
ABCresult
0XX0
10X0
1100
1111
684 0 add_redirect($dapper, $old_url, $new_url);
685 0 $result{redirect} = "$old_url -> $new_url (in " . redirects_file($dapper) . ")";
686 }
687  
688 0 $result{note} = 'Run check to find problems, or build to publish.';
689 0 return \%result;
690 }
691  
692 0 ▶sub _url_of {
Subroutine _url_of: not called CC=2 SCAR=17.9
693 0 my ($dapper, $file) = @_;
694  
695 0 _load($dapper);
696 0 my ($page) = grep { $_->{source} eq $file } @{$dapper->{site}->{pages}};
697 0 ▶ return $page ? $page->{url} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$page ? : 0 0 0
698 }
699  
700 =head2 redirects_file
701  
702 Takes a Dapper object and returns the path of C<_data/redirects.yml>.
703  
704 =cut
705  
706 0 ▶sub redirects_file {
Subroutine redirects_file: not called CC=1 SCAR=6.9
Pod documented
707 0 my ($dapper) = @_;
708 0 return catfile($dapper->{data_dir}, 'redirects.yml');
709 }
710  
711 =head2 add_redirect
712  
713 Takes a Dapper object, an old URL and a new one, and records the redirect in
714 C<_data/redirects.yml>. Redirects that pointed to the old URL are pointed at
715 the new one, so there are no chains.
716  
717 =cut
718  
719 0 ▶sub add_redirect {
Subroutine add_redirect: not called CC=5 SCAR=34.0
Pod documented
720 0 my ($dapper, $from, $to) = @_;
721  
722 0 my $file = redirects_file($dapper);
723 0 ▶ my $redirects = -f $file ? YAML::PP->new->load_file($file) : {};
Branch: 1 branch
BranchTrueFalseTotal
-f $file ? : 0 0 0
724 0 ▶ $redirects = {} unless ref $redirects eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $redirects eq "HASH" 0 0 0
725  
726 0 for my $old (keys %$redirects) {
727 0 ▶ $redirects->{$old} = $to if $redirects->{$old} eq $from;
Branch: 1 branch
BranchTrueFalseTotal
if $redirects->{$old} eq $from 0 0 0
728 }
729 0 $redirects->{$from} = $to;
730 0 delete $redirects->{$to};
731  
732 0 my $yaml = YAML::PP->new->dump_string($redirects);
733 0 $yaml =~ s/^---\n//;
734 0 App::Dapper::Utils::write_file($file,
735 "# Redirects from old URLs to new ones (written by Dapper when pages move; edit freely)\n$yaml");
736 }
737  
738 =head2 layout_name
739  
740 Takes a layout name from a client and returns it, or dies if it isn't a
741 plain name.
742  
743 =cut
744  
745 0 ▶sub layout_name {
Subroutine layout_name: not called CC=4 SCAR=30.0
Pod documented
746 0 my ($name) = @_;
747  
748 0 ▶ die "error: give the layout's name, e.g. post\n" unless defined $name and length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 0 0
Condition0%0/3
defined $name and length $name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table0%0/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
749 0 $name =~ s/\.html?$//;
750 0 ▶ die "error: a layout name can only have letters, digits, - and _\n" unless $name =~ /^[A-Za-z0-9_-]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[A-Za-z0-9_-]+$/ 0 0 0
751 0 return $name;
752 }
753  
754 =head2 read_layout
755  
756 The C<read_layout> tool.
757  
758 =cut
759  
760 0 ▶sub read_layout {
Subroutine read_layout: not called CC=2 SCAR=17.9
Pod documented
761 0 my ($dapper, $args) = @_;
762  
763 0 $dapper->read_project();
764 0 my $name = layout_name($args->{name});
765 0 my ($file) = grep { App::Dapper::Utils::filter_stem($_) eq $name } App::Dapper::Utils::read_dir($dapper->{layout});
766 0 ▶ die "error: there is no layout called \"$name\" in $dapper->{layout}\n" unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
767  
768 0 my $path = catfile($dapper->{layout}, $file);
769 0 return { name => $name, file => $path, content => App::Dapper::Utils::read_file($path) };
770 }
771  
772 =head2 write_layout
773  
774 The C<write_layout> tool.
775  
776 =cut
777  
778 0 ▶sub write_layout {
Subroutine write_layout: not called CC=14 SCAR=53.5
Pod documented
779 0 my ($dapper, $args) = @_;
780  
781 0 $dapper->read_project();
782 0 my $name = layout_name($args->{name});
783 0 my $content = $args->{content};
784 0 ▶ die "error: content must be text\n" unless defined $content and !ref $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and not ref $content 0 0 0
Condition0%0/3
defined $content and not ref $content
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content and not ref $content
defined $content not ref $content
Truth table0%0/3
A and not(B)
A= defined $contentB= not ref $content
ABresult
0X0
100
111
785  
786 0 my ($existing) = grep { App::Dapper::Utils::filter_stem($_) eq $name } App::Dapper::Utils::read_dir($dapper->{layout});
787 0 ▶ my $path = catfile($dapper->{layout}, defined $existing ? $existing : "$name.html");
Branch: 1 branch
BranchTrueFalseTotal
defined $existing ? : 0 0 0
788 0 ▶ if (defined $existing and not $args->{overwrite}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $existing and not $args->{"overwrite"}) 0 0 0
Condition0%0/3
defined $existing and not $args->{"overwrite"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $existing and not $args->{"overwrite"}
defined $existing not $args->{"overwrite"}
Truth table0%0/3
A and not(B)
A= defined $existingB= not $args->{"overwrite"}
ABresult
0X0
100
111
789 0 return ({ written => JSON::false, error => "$path already exists; set overwrite to true to replace it" }, 1);
790 }
791  
792 # Check the template before saving it
793 0 my ($front_matter, $body) = App::Dapper::Utils::split_frontmatter($content, $path);
794 0 ▶ $body = $content unless defined $front_matter;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $front_matter 0 0 0
795 0 my $parsed = eval { Template::Alloy->new(INCLUDE_PATH => $dapper->{layout})->load_template(\$body); 1 };
796 0 ▶ if (not $parsed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed) 0 0 0
797 0 ▶ my $error = ref $@ && $@->can('as_string') ? $@->as_string : "$@";
Branch: 1 branch
BranchTrueFalseTotal
ref $@ && $@->can('as_string') ? : 0 0 0
Condition0%0/3
ref $@ and $@->can("as_string")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $@ and $@->can("as_string")
ref $@ $@->can("as_string")
Truth table0%0/3
A and B
A= ref $@B= $@->can("as_string")
ABresult
0X0
100
111
798 0 $error =~ s/\s+\z//;
799 0 return ({ written => JSON::false, error => "The layout has a template error, so it was not saved: $error" }, 1);
800 }
801 0 ▶ if (defined $front_matter and defined $front_matter->{layout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $front_matter and defined $front_matter->{"layout"}) 0 0 0
Condition0%0/3
defined $front_matter and defined $front_matter->{"layout"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $front_matter and defined $front_matter->{"layout"}
defined $front_matter defined $front_matter->{"layout"}
Truth table0%0/3
A and B
A= defined $front_matterB= defined $front_matter->{"layout"}
ABresult
0X0
100
111
802 0 my $parent = $front_matter->{layout};
803 0 my ($found) = grep { App::Dapper::Utils::filter_stem($_) eq $parent } App::Dapper::Utils::read_dir($dapper->{layout});
804 0 ▶ return ({ written => JSON::false, error => "The layout wraps itself in \"$parent\", which does not exist, so it was not saved." }, 1)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $found 0 0 0
805 unless defined $found;
806 }
807  
808 0 ▶ $content .= "\n" unless $content =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $content =~ /\n\z/ 0 0 0
809 0 App::Dapper::Utils::write_file($path, $content);
810 0 return { written => JSON::true, file => $path, note => 'Run check to find problems, or build to publish.' };
811 }
812  
813 =head2 build
814  
815 The C<build> tool.
816  
817 =cut
818  
819 0 ▶sub build {
Subroutine build: not called CC=3 SCAR=24.8
Pod documented
820 0 my ($dapper, $args) = @_;
821  
822 0 local $dapper->{quiet} = 1;
823 0 ▶ local $dapper->{drafts} = ref $args eq 'HASH' && $args->{drafts} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'HASH' && $args->{'drafts'} ? : 0 0 0
Condition0%0/3
ref $args eq "HASH" and $args->{"drafts"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $args eq "HASH" and $args->{"drafts"}
ref $args eq "HASH" $args->{"drafts"}
Truth table0%0/3
A and B
A= ref $args eq "HASH"B= $args->{"drafts"}
ABresult
0X0
100
111
824 0 $dapper->build();
825  
826 return {
827 built => JSON::true,
828 output => $dapper->{output},
829 0 pages => scalar @{$dapper->{site}->{pages}},
830 0 files => scalar keys %{$dapper->{written}},
831 };
832 }
833  
834 =head2 check
835  
836 The C<check> tool.
837  
838 =cut
839  
840 0 ▶sub check {
Subroutine check: not called CC=3 SCAR=24.8
Pod documented
841 0 my ($dapper, $args) = @_;
842  
843 0 ▶ local $dapper->{drafts} = ref $args eq 'HASH' && $args->{drafts} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'HASH' && $args->{'drafts'} ? : 0 0 0
Condition0%0/3
ref $args eq "HASH" and $args->{"drafts"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $args eq "HASH" and $args->{"drafts"}
ref $args eq "HASH" $args->{"drafts"}
Truth table0%0/3
A and B
A= ref $args eq "HASH"B= $args->{"drafts"}
ABresult
0X0
100
111
844 0 return $dapper->check();
845 }
846  
847 190 1;
« lib/App/Dapper/Images.pm ↑ Summary lib/App/Dapper/Schema.pm »