DocKnot/Generate.pm

filter: Statementstmt 97.0% 196 / 202 Branchbran 84.3% 27 / 32 Conditioncond 78.5% 11 / 14 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 25 / 25 Podpod 100% 4 / 4 totaltotal 93.6% 267 / 285 CC 27 SCAR 13.7
CC
27
Cov
94%
_wrap_paragraph
10.0
__ANON__
5.0
__ANON__
4.0
CRAP 3.9
« DocKnot/Spin/Versions.pm ↑ Summary DocKnot/Config.pm »
1 # Generate human-readable documentation from package metadata.
2 #
3 # This is the implementation of the docknot generate command, which uses
4 # templates to support generation of various documentation files based on
5 # package metadata.
6 #
7 # SPDX-License-Identifier: MIT
8  
9 ##############################################################################
10 # Modules and declarations
11 ##############################################################################
12  
13 package App::DocKnot::Generate v8.0.1;
14  
15 4 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use parent qw(App::DocKnot);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use App::DocKnot::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Text::Wrap qw(wrap);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # Default output files for specific templates.
27 4 my %DEFAULT_OUTPUT = (
28 'readme' => 'README',
29 'readme-md' => 'README.md',
30 );
31  
32 ##############################################################################
33 # Generator functions
34 ##############################################################################
35  
36 # The internal helper object methods in this section are generators. They
37 # return code references intended to be passed into Template Toolkit as code
38 # references so that they can be called inside templates, incorporating data
39 # from the App::DocKnot configuration or the package metadata.
40  
41 # Returns code to center a line in $self->{width} characters given the text of
42 # the line. The returned code will take a line of text and return that line
43 # with leading whitespace added as required.
44 #
45 # Returns: Code reference to a closure that uses $self->{width} for width
46 38 ▶sub _code_for_center {
Subroutine _code_for_center: called CC=1 SCAR=0.1
47 38 my ($self) = @_;
48 42 ▶ my $center = sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
49 42 my ($text) = @_;
50 42 my $space = $self->{width} - length($text);
51 42 ▶ if ($space <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($space <= 0) { } 0 42 42
52 0 return $text;
53 } else {
54 42 return q{ } x int($space / 2) . $text;
55 }
56 38 };
57 38 return $center;
58 }
59  
60 # Returns code that formats the copyright notices for the package. The
61 # resulting code reference takes two parameter, the indentation level and an
62 # optional prefix for each line, and wraps the copyright notices accordingly.
63 # They will be wrapped with a four-space outdent and kept within
64 # $self->{width} columns.
65 #
66 # $copyrights_ref - A reference to a list of anonymous hashes, each with keys:
67 # holder - The copyright holder for that copyright
68 # years - The years of that copyright
69 #
70 # Returns: Code reference to a closure taking an indent level and an optional
71 # prefix and returning the formatted copyright notice
72 38 ▶sub _code_for_copyright {
Subroutine _code_for_copyright: called CC=1 SCAR=0.0
73 38 my ($self, $copyrights_ref) = @_;
74 14 ▶ my $copyright = sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
75 14 my ($indent, $lead) = @_;
76 14 ▶ my $prefix = ($lead // q{}) . q{ } x $indent;
Condition50%1/2
$lead // ""
l!l
014
MC/DC0%0/1
$lead // ""
$lead
Truth table50%1/2
A
A= $lead
Aresult
11
00
77 14 my $notice;
78 14 for my $copyright ($copyrights_ref->@*) {
79 25 my $holder = $copyright->{holder};
80 25 my $years = $copyright->{years};
81  
82 # Build the initial notice with the word copyright and the years.
83 25 my $text = 'Copyright ' . $copyright->{years};
84 25 local $Text::Wrap::columns = $self->{width} + 1;
85 25 local $Text::Wrap::unexpand = 0;
86 25 $text = wrap($prefix, $prefix . q{ } x 4, $text);
87  
88 # See if the holder fits on the last line. If so, add it there;
89 # otherwise, add another line.
90 25 my $last_length;
91 25 ▶ if (rindex($text, "\n") == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (rindex($text, "\n") == -1) { } 25 0 25
92 25 $last_length = length($text);
93 } else {
94 0 $last_length = length($text) - rindex($text, "\n");
95 }
96 25 ▶ if ($last_length + length($holder) < $self->{width}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_length + length($holder) < $self->{'width'}) { } 15 10 25
97 15 $text .= " $holder";
98 } else {
99 10 $text .= "\n" . $prefix . q{ } x 4 . $holder;
100 }
101 25 $notice .= $text . "\n";
102 }
103 14 chomp($notice);
104 14 return $notice;
105 38 };
106 38 return $copyright;
107 }
108  
109 # Returns code to indent each line of a paragraph by a given number of spaces.
110 # This is constructed as a method returning a closure so that its behavior can
111 # be influenced by App::DocKnot configuration in the future, but it currently
112 # doesn't use any configuration. It takes the indentation and an optional
113 # prefix to put at the start of each line.
114 #
115 # Returns: Code reference to a closure
116 38 ▶sub _code_for_indent {
Subroutine _code_for_indent: called CC=1 SCAR=0.0
117 38 my ($self) = @_;
118 132 ▶ my $indent = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
119 132 my ($text, $space, $lead) = @_;
120 132 ▶ $lead //= q{};
Condition100%2/2
$lead //= ""
l!l
15117
MC/DC100%1/1
$lead //= ""
$lead
Truth table100%2/2
A
A= $lead
Aresult
11
00
121 132 my @text = split(m{\n}xms, $text);
122 132 return join("\n", map { $lead . q{ } x $space . $_ } @text);
123 38 };
124 38 return $indent;
125 }
126  
127 # Returns code that converts metadata text (which is assumed to be in
128 # Markdown) to text. This is not a complete Markdown formatter. It only
129 # supports the bits of markup that I've had some reason to use.
130 #
131 # This is constructed as a method returning a closure so that its behavior can
132 # be influenced by App::DocKnot configuration in the future, but it currently
133 # doesn't use any configuration.
134 #
135 # Returns: Code reference to a closure that takes a block of text and returns
136 # the converted text
137 38 ▶sub _code_for_to_text {
Subroutine _code_for_to_text: called CC=1 SCAR=0.0
138 38 my ($self) = @_;
139 82 ▶ my $to_text = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
140 82 my ($text) = @_;
141  
142 # Remove triple backticks but escape all backticks inside them.
143 82 $text =~ s{ ``` \w* (\s .*?) ``` }{
144 32 my $text = $1;
145 32 $text =~ s{ [\`] }{``}xmsg;
146 32 $text;
147 }xmsge;
148  
149 # Remove backticks, but don't look at things starting with doubled
150 # backticks.
151 82 $text =~ s{ (?<! \` ) ` ([^\`]+) ` }{$1}xmsg;
152  
153 # Undo backtick escaping.
154 82 $text =~ s{ `` }{\`}xmsg;
155  
156 # Rewrite quoted paragraphs to have four spaces of additional
157 # indentation.
158 82 $text =~ s{
159 \n \n # start of paragraph
160 ( # start of the text
161 (> \s+) # quote mark on first line
162 \S [^\n]* \n # first line
163 (?: # all subsequent lines
164 \2 \S [^\n]* \n # start with the same prefix
165 )* # any number of subsequent lines
166 ) # end of the text
167 }{
168 1 my ($text, $prefix) = ($1, $2);
169 1 $text =~ s{ ^ \Q$prefix\E }{ }xmsg;
170 1 "\n\n" . $text;
171 }xmsge;
172  
173 # For each paragraph, remove URLs from all links, replacing them with
174 # numeric references, and accumulate the mapping of numbers to URLs in
175 # %urls. Then, add to the end of the paragraph the references and
176 # URLs.
177 82 my $ref = 1;
178 82 my @paragraphs = split(m{ \n\n }xms, $text);
179 82 for my $para (@paragraphs) {
180 378 my %urls;
181 378 my $regex = qr{ \[([^\]]+)\] [(] (\S+) [)] }xms;
182 378 ▶ while ($para =~ s{$regex}{$1 [$ref]}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($para =~ s/$regex/$1 [$ref]/msux) 17 378 395
183 17 $urls{$ref} = $2;
184 17 $ref++;
185 }
186 378 ▶ if (%urls) {
Branch: 1 branch
BranchTrueFalseTotal
if (%urls) 15 363 378
187 15 my @refs = map { "[$_] $urls{$_}" } sort { $a <=> $b }
188 keys(%urls);
189 15 $para .= "\n\n" . join("\n", q{}, @refs, q{});
190 }
191 }
192  
193 # Rejoin the paragraphs and return the result.
194 82 return join("\n\n", @paragraphs);
195 38 };
196 38 return $to_text;
197 }
198  
199 # Returns code that converts metadata text (which is assumed to be in
200 # Markdown) to thread. This is not a complete Markdown formatter. It only
201 # supports the bits of markup that I've had some reason to use.
202 #
203 # This is constructed as a method returning a closure so that its behavior can
204 # be influenced by App::DocKnot configuration in the future, but it currently
205 # doesn't use any configuration.
206 #
207 # Returns: Code reference to a closure that takes a block of text and returns
208 # the converted thread
209 38 ▶sub _code_for_to_thread {
Subroutine _code_for_to_thread: called CC=1 SCAR=0.0
210 38 my ($self) = @_;
211 53 ▶ my $to_thread = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
212 53 my ($text) = @_;
213  
214 # Escape all backslashes.
215 53 $text =~ s{ \\ }{\\\\}xmsg;
216  
217 # Rewrite triple backticks to \pre blocks and escape backticks inside
218 # them so that they're not turned into \code blocks.
219 53 $text =~ s{ ``` \w* (\s .*?) ``` }{
220 0 my $text = $1;
221 0 $text =~ s{ [\`] }{``}xmsg;
222 0 '\pre[' . $1 . ']';
223 }xmsge;
224  
225 # Rewrite backticks to \code blocks.
226 53 $text =~ s{ ` ([^\`]+) ` }{\\code[$1]}xmsg;
227  
228 # Undo backtick escaping.
229 53 $text =~ s{ `` }{\`}xmsg;
230  
231 # Rewrite all Markdown links into thread syntax.
232 53 $text =~ s{ \[ ([^\]]+) \] [(] (\S+) [)] }{\\link[$2][$1]}xmsg;
233  
234 # Rewrite long bullets. This is quite tricky since we have to grab
235 # every line from the first bulleted one to the point where the
236 # indentation stops.
237 53 $text =~ s{
238 ( # capture whole contents
239 ^ (\s*) # indent before bullet
240 [*] (\s+) # bullet and following indent
241 [^\n]+ \n # rest of line
242 (?: \s* \n )* # optional blank lines
243 (\2 [ ] \3) # matching indent
244 [^\n]+ \n # rest of line
245 (?: # one or more of
246 \4 # matching indent
247 [^\n]+ \n # rest of line
248 | # or
249 \s* \n # blank lines
250 )+ # end of indented block
251 ) # full bullet with leading bullet
252 }{
253 3 my $text = $1;
254 3 $text =~ s{ [*] }{ }xms;
255 3 "\\bullet[\n\n" . $text . "\n]\n";
256 }xmsge;
257  
258 # Do the same thing, but with numbered lists. This doesn't handle
259 # numbers larger than 9 currently, since that requires massaging the
260 # spacing.
261 53 $text =~ s{
262 ( # capture whole contents
263 ^ (\s*) # indent before number
264 \d [.] (\s+) # number and following indent
265 [^\n]+ \n # rest of line
266 (?: \s* \n )* # optional blank lines
267 (\2 [ ][ ] \3) # matching indent
268 [^\n]+ \n # rest of line
269 (?: # one or more of
270 \4 # matching indent
271 [^\n]+ \n # rest of line
272 | # or
273 \s* \n # blank lines
274 )+ # end of indented block
275 ) # full bullet with leading bullet
276 }{
277 3 my $text = $1;
278 3 $text =~ s{ \A (\s*) \d [.] }{$1 }xms;
279 3 "\\number[\n\n" . $text . "\n]\n\n";
280 }xmsge;
281  
282 # Rewrite compact bulleted lists.
283 53 $text =~ s{ \n ( (?: \s* [*] \s+ [^\n]+ \s* \n ){2,} ) }{
284 6 my $list = $1;
285 6 $list =~ s{ \n [*] \s+ ([^\n]+) }{\n\\bullet(packed)[$1]}xmsg;
286 6 "\n" . $list;
287 }xmsge;
288  
289 # Done. Return the results.
290 53 return $text;
291 38 };
292 38 return $to_thread;
293 }
294  
295 ##############################################################################
296 # Helper methods
297 ##############################################################################
298  
299 # Word-wrap a paragraph of text. This is a helper function for _wrap, mostly
300 # so that it can be invoked recursively to wrap bulleted paragraphs.
301 #
302 # If the paragraph looks like regular text, which means indented by two or
303 # four spaces and consistently on each line, remove the indentation and then
304 # add it back in while wrapping the text.
305 #
306 # $para - A paragraph of text to wrap
307 # $options_ref - Options to controll the wrapping
308 # ignore_indent - Ignore indentation when choosing whether to wrap
309 #
310 # Returns: The wrapped paragraph
311 2486 ▶sub _wrap_paragraph {
Subroutine _wrap_paragraph: called CC=10 SCAR=23.0
312 2486 my ($self, $para, $options_ref) = @_;
313 2486 ▶ $options_ref //= {};
Condition100%2/2
$options_ref //= {}
l!l
1192367
MC/DC100%1/1
$options_ref //= {}
$options_ref
Truth table100%2/2
A
A= $options_ref
Aresult
11
00
314 2486 my ($indent) = ($para =~ m{ \A ([ ]*) \S }xms);
315  
316 # If the indent is longer than five characters and the ignore indent
317 # option is not set, leave it alone. Allow an indent of five characters
318 # since it may be a continuation of a numbered list entry.
319 2486 ▶ if (length($indent) > 5 && !$options_ref->{ignore_indent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $indent > 5 and not $options_ref->{"ignore_indent"}) 168 2318 2486
Condition100%3/3
length $indent > 5 and not $options_ref->{"ignore_indent"}
!ll&&!rl&&r
23153168
MC/DC100%2/2
length $indent > 5 and not $options_ref->{"ignore_indent"}
length $indent > 5 not $options_ref->{"ignore_indent"}
Truth table100%3/3
A and not(B)
A= length $indent > 5B= not $options_ref->{"ignore_indent"}
ABresult
0X0
100
111
320 168 return $para;
321 }
322  
323 # If this looks like thread commands or URLs, leave it alone.
324 2318 ▶ if ($para =~ m{ \A \s* (?: \\ | \[\d+\] ) }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($para =~ / \A \s* (?: \\ | \[\d+\] ) /msux) 304 2014 2318
325 304 return $para;
326 }
327  
328 # If this starts with a bullet, strip the bullet off, wrap the paragraph,
329 # and then add it back in.
330 2014 ▶ if ($para =~ s{ \A (\s*) [*] (\s+) }{$1 $2}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($para =~ s/ \A (\s*) [*] (\s+) /$1 $2/msux) 82 1932 2014
331 82 my $offset = length($1);
332 82 $para = $self->_wrap_paragraph($para, { ignore_indent => 1 });
333 82 substr($para, $offset, 1, q{*});
334 82 return $para;
335 }
336  
337 # If this starts with a number, strip the number off, wrap the paragraph,
338 # and then add it back in.
339 1932 ▶ if ($para =~ s{\A (\s*) (\d+[.]) (\s+)}{$1 . q{ } x length($2) . $3}xmse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($para =~ s/\A (\s*) (\d+[.]) (\s+)/$1 . " " x length($2) . $3;/emsux) 23 1909 1932
340 23 my $offset = length($1);
341 23 my $number = $2;
342 23 $para = $self->_wrap_paragraph($para, { ignore_indent => 1 });
343 23 substr($para, $offset, length($number), $number);
344 23 return $para;
345 }
346  
347 # If this looks like a Markdown block quote, strip trailing whitespace,
348 # remove the leading indentation marks, wrap the paragraph, and then put
349 # them back.
350 ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
351 1909 ▶ if ($para =~ m{ \A (\s*) > \s }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($para =~ / \A (\s*) > \s /msux) 14 1895 1909
352 14 $para =~ s{ [ ]+ \n }{\n}xmsg;
353 14 $para =~ s{ ^ (\s*) > (\s) }{$1 $2}xmsg;
354 14 my $offset = length($1);
355 14 $para = $self->_wrap_paragraph($para, { ignore_indent => 1 });
356 14 $para =~ s{ ^ (\s{$offset}) \s }{$1>}xmsg;
357 14 return $para;
358 }
359 ## use critic
360  
361 # If this looks like a bunch of short lines, leave it alone.
362 1895 ▶ if ($para =~ m{ \A (?: \Q$indent\E [^\n]{1,45} \n ){3,} }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($para =~ / \A (?: \Q$indent\E [^\n]{1,45} \n ){3,} /msux) 58 1837 1895
363 58 return $para;
364 }
365  
366 # If this paragraph is not consistently indented, leave it alone.
367 1837 ▶ if ($para !~ m{ \A (?: \Q$indent\E \S[^\n]+ \n )+ \z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($para =~ / \A (?: \Q$indent\E \S[^\n]+ \n )+ \z /msux) 146 1691 1837
368 146 return $para;
369 }
370  
371 # Strip the indent from each line.
372 1691 $para =~ s{ (?: \A | (?<=\n) ) \Q$indent\E }{}xmsg;
373  
374 # Remove any existing newlines, preserving two spaces after periods.
375 1691 $para =~ s{ [.] ([)\"]?) \n (\S) }{.$1 $2}xmsg;
376 1691 $para =~ s{ \n(\S) }{ $1}xmsg;
377  
378 # Force locally correct configuration of Text::Wrap.
379 1691 local $Text::Wrap::break = qr{\s+}xms;
380 1691 local $Text::Wrap::columns = $self->{width} + 1;
381 1691 local $Text::Wrap::huge = 'overflow';
382 1691 local $Text::Wrap::unexpand = 0;
383  
384 # Do the wrapping. This modifies @paragraphs in place.
385 1691 $para = wrap($indent, $indent, $para);
386  
387 # Strip any trailing whitespace, since some gets left behind after periods
388 # by Text::Wrap.
389 1691 $para =~ s{ [ ]+ \n }{\n}xmsg;
390  
391 # All done.
392 1691 return $para;
393 }
394  
395 # Word-wrap a block of text. This requires some annoying heuristics, but the
396 # alternative is to try to get the template to always produce correctly
397 # wrapped results, which is far harder.
398 #
399 # $text - The text to wrap
400 #
401 # Returns: The wrapped text
402 38 ▶sub _wrap {
Subroutine _wrap: called CC=2 SCAR=6.9
403 38 my ($self, $text) = @_;
404  
405 # First, break the text up into paragraphs. (This will also turn more
406 # than two consecutive newlines into just two newlines.)
407 38 my @paragraphs = split(m{ \n(?:[ ]*\n)+ }xms, $text);
408  
409 # Add back the trailing newlines at the end of each paragraph.
410 38 @paragraphs = map { $_ . "\n" } @paragraphs;
411  
412 # Wrap all of the paragraphs. This modifies @paragraphs in place.
413 38 for my $paragraph (@paragraphs) {
414 2367 $paragraph = $self->_wrap_paragraph($paragraph);
415 }
416  
417 # Glue the paragraphs back together and return the result. Because the
418 # last newline won't get stripped by the split above, we have to strip an
419 # extra newline from the end of the file.
420 38 my $result = join("\n", @paragraphs);
421 38 $result =~ s{ \n+ \z }{\n}xms;
422 38 return $result;
423 }
424  
425 ##############################################################################
426 # Public interface
427 ##############################################################################
428  
429 # Create a new App::DocKnot::Generate object, which will be used for
430 # subsequent calls.
431 #
432 # $args - Anonymous hash of arguments with the following keys:
433 # metadata - Path to the directory containing package metadata
434 # width - Line length at which to wrap output files
435 #
436 # Returns: Newly created object
437 # Throws: Text exceptions on invalid metadata directory path
438 12 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
439 12 my ($class, $args_ref) = @_;
440  
441 # Create the config reader.
442 12 my %config_args;
443 12 ▶ if ($args_ref->{metadata}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args_ref->{"metadata"}) 11 1 12
444 11 $config_args{metadata} = $args_ref->{metadata};
445 }
446 12 my $config = App::DocKnot::Config->new(\%config_args);
447  
448 # Create and return the object.
449 my $self = {
450 config => $config,
451 12 ▶ width => $args_ref->{width} // 74,
Condition50%1/2
$args_ref->{"width"} // 74
l!l
012
MC/DC0%0/1
$args_ref->{"width"} // 74
$args_ref->{"width"}
Truth table50%1/2
A
A= $args_ref->{"width"}
Aresult
11
00
452 };
453 12 bless($self, $class);
454 12 return $self;
455 }
456  
457 # Generate a documentation file from the package metadata.
458 #
459 # $template - Name of the documentation template (using Template Toolkit)
460 #
461 # Returns: The generated documentation as a string
462 # Throws: autodie exception on failure to read metadata or write the output
463 # Text exception on Template Toolkit failures
464 # Text exception on inconsistencies in the package data
465 38 ▶sub generate {
Subroutine generate: called CC=3 SCAR=11.0
Pod documented
466 38 my ($self, $template) = @_;
467  
468 # Load the package metadata.
469 38 my $data_ref = $self->{config}->config();
470  
471 # Create the variable information for the template. Start with all
472 # metadata as loaded above.
473 38 my %vars = %{$data_ref};
474  
475 # Add code references for our defined helper functions.
476 38 $vars{center} = $self->_code_for_center;
477 38 $vars{copyright} = $self->_code_for_copyright($data_ref->{copyrights});
478 38 $vars{indent} = $self->_code_for_indent;
479 38 $vars{to_text} = $self->_code_for_to_text;
480 38 $vars{to_thread} = $self->_code_for_to_thread;
481  
482 # Run Template Toolkit processing.
483 38 $template = $self->appdata_path('templates', "${template}.tmpl");
484 38 ▶ my $tt = Template->new({ ABSOLUTE => 1, ENCODING => 'utf8' })
Branch: 1 branch
BranchTrueFalseTotal
my $tt = "Template"->new({"ABSOLUTE", 1, "ENCODING", "utf8"}) or croak("Template"->error) 38 0 38
485 or croak(Template->error());
486 38 my $result;
487 38 ▶ $tt->process($template, \%vars, \$result) or croak($tt->error);
Branch: 1 branch
BranchTrueFalseTotal
$tt->process($template, \%vars, \$result) or croak($tt->error) 38 0 38
488  
489 # Word-wrap the results to our width and return them.
490 38 return $self->_wrap($result);
491 }
492  
493 # Generate all package documentation from the package metadata. Only
494 # generates the output for templates with a default output file.
495 #
496 # Returns: undef
497 # Throws: autodie exception on failure to read metadata or write the output
498 # Text exception on Template Toolkit failures
499 # Text exception on inconsistencies in the package data
500 1 ▶sub generate_all {
Subroutine generate_all: called CC=2 SCAR=6.9
Pod documented
501 1 my ($self) = @_;
502 1 for my $template (keys(%DEFAULT_OUTPUT)) {
503 2 $self->generate_output($template);
504 }
505 1 return;
506 }
507  
508 # Generate a documentation file from the package metadata.
509 #
510 # $template - Name of the documentation template
511 # $output - Output file name (undef to use the default)
512 #
513 # Returns: undef
514 # Throws: autodie exception on failure to read metadata or write the output
515 # Text exception on Template Toolkit failures
516 # Text exception on inconsistencies in the package data
517 5 ▶sub generate_output {
Subroutine generate_output: called CC=3 SCAR=11.4
Pod documented
518 5 my ($self, $template, $output) = @_;
519 5 ▶ $output //= $DEFAULT_OUTPUT{$template};
Condition66%2/3
$output //= $DEFAULT_OUTPUT{$template}
l!l&&r!l&&!r
230
MC/DC0%0/2
$output //= $DEFAULT_OUTPUT{$template}
$output $DEFAULT_OUTPUT{$template}
Truth table66%2/3
A //= B
A= $outputB= $DEFAULT_OUTPUT{$template}
ABresult
1X1
011
000
520  
521 # If the template doesn't have a default output file, $output is required.
522 5 ▶ if (!defined($output)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $output) 0 5 5
523 0 croak('missing required output argument');
524 }
525  
526 # Generate the output.
527 5 my $data = $self->generate($template);
528 5 path($output)->spew_utf8($data);
529 5 return;
530 }
531  
532 ##############################################################################
533 # Module return value and documentation
534 ##############################################################################
535  
536 4 1;
537 __END__
« DocKnot/Spin/Versions.pm ↑ Summary DocKnot/Config.pm »