DocKnot/Spin/Thread.pm

filter: Statementstmt 95.7% 583 / 609 Branchbran 89.4% 170 / 190 Conditioncond 76.5% 49 / 64 MC/DCmcdc 63.4% 26 / 41 Subroutinesub 98.8% 82 / 83 Podpod 100% 4 / 4 totaltotal 92.2% 914 / 991 CC 124 SCAR 19.3
CC
124
Cov
93%
_parse_context
23.0
_cmd_signature
14.3
_cmd_heading
13.0
CRAP 6.9
« DocKnot/Spin/Sitemap.pm ↑ Summary DocKnot/Update.pm »
1 # Generate HTML from the macro language thread.
2 #
3 # Thread is a macro language designed for producing HTML pages. This module
4 # parses thread and generates the corresponding HTML.
5 #
6 # SPDX-License-Identifier: MIT
7  
8 ##############################################################################
9 # Modules and declarations
10 ##############################################################################
11  
12 package App::DocKnot::Spin::Thread v8.0.1;
13  
14 5 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use warnings FATAL => 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use App::DocKnot;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use App::DocKnot::Util qw(print_fh);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Git::Repository ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Image::Size qw(html_imgsize);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use Perl6::Slurp qw(slurp);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use Text::Balanced qw(extract_bracketed);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # The URL to the software page for all of my web page generation software,
29 # used to embed a link to the software that generated the page.
30 5 my $URL = 'https://www.eyrie.org/~eagle/software/docknot/';
31  
32 # The table of available commands. The columns are:
33 #
34 # 1. Number of arguments or -1 to consume as many arguments as it can find.
35 # 2. Name of the method to call with the arguments and (if wanted) format.
36 # 3. Whether to look for a format in parens before the arguments.
37 #<<<
38 5 my %COMMANDS = (
39 # name args method want_format
40 block => [1, '_cmd_block', 1],
41 bold => [1, '_cmd_bold', 1],
42 break => [0, '_cmd_break', 0],
43 bullet => [1, '_cmd_bullet', 1],
44 class => [1, '_cmd_class', 1],
45 cite => [1, '_cmd_cite', 1],
46 code => [1, '_cmd_code', 1],
47 desc => [2, '_cmd_desc', 1],
48 div => [1, '_cmd_div', 1],
49 emph => [1, '_cmd_emph', 1],
50 entity => [1, '_cmd_entity', 0],
51 h1 => [1, '_cmd_h1', 1],
52 h2 => [1, '_cmd_h2', 1],
53 h3 => [1, '_cmd_h3', 1],
54 h4 => [1, '_cmd_h4', 1],
55 h5 => [1, '_cmd_h5', 1],
56 h6 => [1, '_cmd_h6', 1],
57 heading => [2, '_cmd_heading', 0],
58 image => [2, '_cmd_image', 1],
59 include => [1, '_cmd_include', 0],
60 italic => [1, '_cmd_italic', 1],
61 link => [2, '_cmd_link', 1],
62 number => [1, '_cmd_number', 1],
63 pre => [1, '_cmd_pre', 1],
64 quote => [3, '_cmd_quote', 1],
65 release => [1, '_cmd_release', 0],
66 rss => [2, '_cmd_rss', 0],
67 rule => [0, '_cmd_rule', 0],
68 signature => [0, '_cmd_signature', 0],
69 sitemap => [0, '_cmd_sitemap', 0],
70 size => [1, '_cmd_size', 0],
71 strike => [1, '_cmd_strike', 1],
72 strong => [1, '_cmd_strong', 1],
73 sub => [1, '_cmd_sub', 1],
74 sup => [1, '_cmd_sup', 1],
75 table => [2, '_cmd_table', 1],
76 tablehead => [-1, '_cmd_tablehead', 1],
77 tablerow => [-1, '_cmd_tablerow', 1],
78 under => [1, '_cmd_under', 1],
79 version => [1, '_cmd_version', 0],
80 q{=} => [2, '_define_variable', 0],
81 q{==} => [3, '_define_macro', 0],
82 q{\\} => [0, '_literal', 0],
83 );
84 #>>>
85  
86 ##############################################################################
87 # Input and output
88 ##############################################################################
89  
90 # Determine the path to a file relative to the current file being processed.
91 # If the current file being processed is standard input, the path is relative
92 # to the current working directory.
93 #
94 # $path - File path as a string
95 #
96 # Returns: Path::Tiny object holding the absolute path
97 35 ▶sub _file_path {
Subroutine _file_path: called CC=3 SCAR=11.0
98 35 my ($self, $file) = @_;
99 35 my $input_path = $self->{input}[-1][1];
100 35 ▶ if (defined($input_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input_path) { } 34 1 35
101 34 my $path = $input_path->sibling($file);
102 34 ▶ return $path->exists() ? $path->realpath() : $path;
Branch: 1 branch
BranchTrueFalseTotal
$path->exists ? : 14 20 34
103 } else {
104 1 return path($file);
105 }
106 }
107  
108 # Read a file and check it for bad line endings.
109 #
110 # $path - Path::Tiny object
111 #
112 # Returns: Contents of the file
113 7 ▶sub _read_file {
Subroutine _read_file: called CC=3 SCAR=13.5
114 7 my ($self, $path) = @_;
115 7 my $text = $path->slurp_utf8();
116  
117 # Check for broken line endings.
118 7 ▶ if ("\n" !~ m{ \015 }xms && $text =~ m{ \015 }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (not "\n" =~ / \015 /msux and $text =~ / \015 /msux) 0 7 7
Condition33%1/3
not "\n" =~ / \015 /msux and $text =~ / \015 /msux
!ll&&!rl&&r
070
MC/DC0%0/2
not "\n" =~ / \015 /msux and $text =~ / \015 /msux
not "\n" =~ / \015 /msux $text =~ / \015 /msux
Truth table33%1/3
not(A) and B
A= not "\n" =~ / \015 /msuxB= $text =~ / \015 /msux
ABresult
0X0
100
111
119 0 my $m = 'found CR characters; are your line endings correct?';
120 0 $self->_warning($m, $path);
121 }
122  
123 # Return the contents.
124 7 return $text;
125 }
126  
127 # Sends something to the output file with special handling of whitespace for
128 # more readable HTML output.
129 #
130 # @output - Strings to output
131 380 ▶sub _output {
Subroutine _output: called CC=3 SCAR=11.0
132 380 my ($self, @output) = @_;
133 380 my $output = join(q{}, @output);
134  
135 # If we have saved whitespace, separate any closing tags at the start of
136 # the output from the rest of the output and insert that saved space
137 # between those closing tags and the rest of the output.
138 #
139 # The effect of this is to move whitespace between element bodies and
140 # their closing tags to outside of the closing tags, which makes the HTML
141 # much more readable.
142 380 ▶ if ($self->{space}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"space"}) 295 85 380
143 295 my ($prefix, $body) = $output =~ m{
144 \A
145 (\s*
146 (?: </(?!body)[^>]+> \s* )*
147 )
148 (.*)
149 }xms;
150 295 $prefix .= $self->{space};
151  
152 # Collapse multiple whitespace-only lines into a single blank line.
153 295 $prefix =~ s{ \n\s* \n\s* \n }{\n\n}xmsg;
154  
155 # Replace the output with added whitespace and clear saved whitespace.
156 295 $output = $prefix . $body;
157 295 $self->{space} = q{};
158 }
159  
160 # Remove and save any trailing whitespace.
161 380 ▶ if ($output =~ s{ \n (\s+) \z }{\n}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output =~ s/ \n (\s+) \z /\n/msux) 296 84 380
162 296 $self->{space} = $1;
163 }
164  
165 # Send the results to the output file.
166 380 print_fh($self->{out_fh}, $self->{out_path}, $output);
167 380 return;
168 }
169  
170 # Report a fatal problem with the current file and line.
171 #
172 # $problem - Error message to report
173 #
174 # Throws: Text exception with the provided error
175 0 ▶sub _fatal {
Subroutine _fatal: not called CC=2 SCAR=17.9
176 0 my ($self, $problem) = @_;
177 0 my (undef, $file, $lineno) = $self->{input}[-1]->@*;
178 0 ▶ $file //= q{-};
Condition0%0/2
$file //= "-"
l!l
00
MC/DC0%0/1
$file //= "-"
$file
Truth table0%0/2
A
A= $file
Aresult
11
00
179 0 die "$file:$lineno: $problem\n";
180 }
181  
182 # Warn about a problem with the current file and line.
183 #
184 # $problem - Warning message to report
185 # $file - Optional path where the problem was seen, otherwise the current
186 # input file is used
187 10 ▶sub _warning {
Subroutine _warning: called CC=3 SCAR=11.4
188 10 my ($self, $problem, $file) = @_;
189 10 my $lineno;
190 10 ▶ if (!defined($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $file) { } 10 0 10
191 10 (undef, $file, $lineno) = $self->{input}[-1]->@*;
192 10 ▶ $file //= q{-};
Condition50%1/2
$file //= "-"
l!l
100
MC/DC0%0/1
$file //= "-"
$file
Truth table50%1/2
A
A= $file
Aresult
11
00
193 } else {
194 0 $lineno = 0;
195 }
196 10 warn "$file:$lineno: $problem\n";
197 10 return;
198 }
199  
200 ##############################################################################
201 # Basic parsing
202 ##############################################################################
203  
204 # Escapes &, <, and > characters for HTML output.
205 #
206 # $string - Input string
207 #
208 # Returns: Escaped string
209 418 ▶sub _escape {
Subroutine _escape: called CC=1 SCAR=0.0
210 418 my ($string) = @_;
211 418 $string =~ s{ & }{&amp;}xmsg;
212 418 $string =~ s{ < }{&lt;}xmsg;
213 418 $string =~ s{ > }{&gt;}xmsg;
214 418 return $string;
215 }
216  
217 # Wrap something in paragraph markers, being careful to get newlines right.
218 # Special-case a paragraph consisting entirely of <span> by turning it into a
219 # <p> with the same class.
220 #
221 # $text - Text to wrap
222 #
223 # Returns: Text wrapped in <p> tags
224 307 ▶sub _paragraph {
Subroutine _paragraph: called CC=2 SCAR=6.9
225 307 my ($self, $text) = @_;
226  
227 # Trim leading newline and whitespace and ensure the paragraph ends with a
228 # newline.
229 307 $text =~ s{ \A \n (\s*\n)* }{}xms;
230 307 $text =~ s{ ( \S [ \t]* ) \z }{$1\n}xms;
231  
232 # If the whole paragraph is wrapped in <span>, lift its attributes into
233 # the <p> tag. Otherwise, just add the <p> tags. This unfortunately
234 # means we also won't lift <span> for any paragraph with nexted \class
235 # commands; doing that would require more HTML parsing than I want to do.
236 307 my $re = qr{
237 \A # start of paragraph
238 (\s*) # any whitespace (1)
239 <span([^>]*)> # span tag before any text with class (2)
240 (?! .* <span) # no second span tag
241 (.*) # text of the paragraph (3)
242 </span> # close span tag
243 (\s*) # any whitespace (4)
244 \z # end of paragraph without other text
245 }xms;
246 307 ▶ if ($text =~ $re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$re/u) { } 29 278 307
247 29 my ($lead, $attrs, $body, $trail) = ($1, $2, $3, $4);
248 29 return "$lead<p$attrs>$body</p>$trail";
249 } else {
250 278 $text =~ s{ \A }{<p>\n}xms;
251 278 $text =~ s{ (\n\s*) \z }{\n</p>$1}xms;
252 278 return $text;
253 }
254 }
255  
256 # Opens the border of a continued structure.
257 #
258 # spin, unlike HTML, does not require declaring structures like lists in
259 # advance of adding elements to them. You start a bullet list by simply
260 # having a bullet item, and a list is started if one is not already open.
261 # This is the method that does that: check whether the desired structure is
262 # already open and, if not, open it and add it to the state stack.
263 #
264 # $border - Name of the border state to open
265 # $start - The opening tag
266 # $end - The closing tag
267 #
268 # Returns: Output to write to start the structure
269 542 ▶sub _border_start {
Subroutine _border_start: called CC=3 SCAR=11.0
270 542 my ($self, $border, $start, $end) = @_;
271 542 my $state = $self->{state}[-1];
272 542 my $output = q{};
273  
274 # If we're at the top-level block structure or inside a structure other
275 # than ours, open the structure and add it to the state stack.
276 542 ▶ if ($state eq 'BLOCK' || $state->[0] ne $border) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "BLOCK" or $state->[0] ne $border) 26 516 542
Condition66%2/3
$state eq "BLOCK" or $state->[0] ne $border
l!l&&r!l&&!r
260516
MC/DC50%1/2
$state eq "BLOCK" or $state->[0] ne $border
$state eq "BLOCK" $state->[0] ne $border
Truth table66%2/3
A or B
A= $state eq "BLOCK"B= $state->[0] ne $border
ABresult
1X1
011
000
277 26 $output .= $start;
278 26 push($self->{state}->@*, [$border, $end]);
279 }
280  
281 542 return $output;
282 }
283  
284 # Closes the border of any currently-open continued structure. This is done,
285 # for example, when a new block structure is opened or a paragraph of regular
286 # text is seen at the same level as the structure elements.
287 #
288 # Returns: Output to write to close the structure.
289 1140 ▶sub _border_end {
Subroutine _border_end: called CC=3 SCAR=11.0
290 1140 my ($self) = @_;
291 1140 my $output = q{};
292  
293 # Find all open structures up to the first general block structure. We'll
294 # pop off the block structure so put it back when we're done.
295 1140 ▶ while (defined(my $state = pop($self->{state}->@*))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $state = pop @{$$self{"state"}})) 1166 0 1166
296 1166 ▶ last if $state eq 'BLOCK';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "BLOCK" 1140 26 1166
297 26 $output .= $state->[1];
298 }
299 1140 push($self->{state}->@*, 'BLOCK');
300  
301 1140 return $output;
302 }
303  
304 # Marks the beginning of major block structure. Within this structure,
305 # borders will only clear to the level of this structure.
306 591 ▶sub _block_start {
Subroutine _block_start: called CC=1 SCAR=0.0
307 591 my ($self) = @_;
308 591 push($self->{state}->@*, 'BLOCK');
309 591 return;
310 }
311  
312 # Clears a major block structure.
313 611 ▶sub _block_end {
Subroutine _block_end: called CC=1 SCAR=0.0
314 611 my ($self) = @_;
315 611 my $output = $self->_border_end();
316 611 pop($self->{state}->@*);
317 611 return $output;
318 }
319  
320 # Extract some number of arguments from the front of the given string.
321 #
322 # $text - Text to parse arguments from
323 # $count - How many arguments to extract, or -1 for as many as possible
324 # $want_format - If true, check for a parenthesized formatting instruction
325 # first and extract it if present
326 #
327 # Returns: List of the following strings:
328 # $format - Format or empty string, omitted if !$want_format
329 # $text - The remaining unparsed text
330 # @args - $count arguments (undef if the argument wasn't found)
331 3349 ▶sub _extract {
Subroutine _extract: called CC=9 SCAR=22.0
332 3349 my ($self, $text, $count, $want_format) = @_;
333 3349 my $format = q{};
334 3349 my @args;
335  
336 # Extract the format string if requested.
337 3349 ▶ if ($want_format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_format) 2198 1151 3349
338 2198 ▶ $format = extract_bracketed($text, '()') // q{};
Condition100%2/2
&extract_bracketed($text, "()") // ""
l!l
6321566
MC/DC100%1/1
&extract_bracketed($text, "()") // ""
&extract_bracketed($text, "()")
Truth table100%2/2
A
A= &extract_bracketed($text, "()")
Aresult
11
00
339 2198 ▶ if ($format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format) 632 1566 2198
340 632 $format = substr($format, 1, -1);
341 }
342 }
343  
344 # Extract the desired number of arguments, or all arguments present if
345 # $count was negative.
346 3349 ▶ if ($count >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count >= 0) { } 3299 50 3349
347 3299 for my $i (1 .. $count) {
348 5131 my $arg = extract_bracketed($text, '[]');
349 5131 ▶ if (defined($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) { } 5130 1 5131
350 5130 $arg = substr($arg, 1, -1);
351 } else {
352 1 $self->_warning("cannot find argument $i: $@");
353 1 $arg = q{};
354 }
355 5131 push(@args, $arg);
356 }
357 } else {
358 50 ▶ while (defined(my $arg = extract_bracketed($text, '[]'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $arg = &extract_bracketed($text, "[]"))) 104 50 154
359 104 push(@args, substr($arg, 1, -1));
360 }
361 }
362  
363 # Return the results.
364 3349 ▶ return $want_format ? ($format, $text, @args) : ($text, @args);
Branch: 1 branch
BranchTrueFalseTotal
$want_format ? : 2198 1151 3349
365 }
366  
367 # Expand a macro invocation.
368 #
369 # $definition - Definition of the macro
370 # $block - True if currently in block context
371 # @args - The arguments to the macro
372 #
373 # Returns: List with the macro expansion and the block context flag
374 810 ▶sub _macro {
Subroutine _macro: called CC=1 SCAR=0.0
375 810 my ($self, $definition, $block, @args) = @_;
376  
377 # The function that expands a macro substitution marker. If the number of
378 # the marker is higher than the number of arguments of the macro, leave it
379 # as-is. (We will have already warned about this when defining the
380 # macro.)
381 1654 ▶ my $expand = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
382 1654 my ($n) = @_;
383 1654 ▶ return ($n > scalar(@args)) ? "\\\\$n" : $args[$n - 1];
Branch: 1 branch
BranchTrueFalseTotal
$n > scalar @args ? : 1 1653 1654
384 810 };
385  
386 # Replace the substitution markers in the macro definition.
387 810 $definition =~ s{ \\(\d+) }{ $expand->($1) }xmsge;
388  
389 # Now parse the result as if it were input thread and return the results.
390 810 return $self->_parse_context($definition, $block);
391 }
392  
393 # Expand a given command into its representation. This function is mutually
394 # recursive with _parse_context and _macro.
395 #
396 # $command - Name of the command
397 # $text - Input text following the command
398 # $block - True if currently in block context (if so, and if the command
399 # doesn't generate its own container, it will need to be wrapped
400 # in <p>
401 #
402 # Returns: List with the following elements:
403 # $output - Output from expanding the command
404 # $block - Whether the output is block context
405 # $text - Remaining unparsed text
406 3365 ▶sub _expand {
Subroutine _expand: called CC=8 SCAR=20.8
407 3365 my ($self, $command, $text, $block) = @_;
408  
409 # Special handling for expanding variables. These references look like
410 # \=NAME and expand to the value of the variable "NAME".
411 3365 ▶ if ($command =~ m{ \A = \w }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ / \A = \w /msux) 7 3358 3365
412 7 my $variable = substr($command, 1);
413 7 ▶ if (exists($self->{variable}{$variable})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'variable'}{$variable}) { } 6 1 7
414 6 return ($self->{variable}{$variable}, 0, $text);
415 } else {
416 1 $self->_warning("unknown variable \\=$variable");
417 1 return (q{}, 0, $text);
418 }
419 }
420  
421 # Special handling for macros. Macros shadow commands of the same name.
422 3358 ▶ if (exists($self->{macro}{$command})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"macro"}{$command}) 810 2548 3358
423 810 my ($args, $definition) = $self->{macro}{$command}->@*;
424  
425 # Extract the macro arguments, if any were requested.
426 810 my @args;
427 810 ▶ if ($args != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args != 0) 802 8 810
428 802 ($text, @args) = $self->_extract($text, $args, 0);
429 }
430  
431 # The macro runs in a block context if we're currently in block
432 # context and there is no remaining non-whitespace text. Otherwise,
433 # use an inline context.
434 810 ▶ $block &&= $text =~ m{ \A \s* \z }xms;
Condition100%3/3
$block &&= $text =~ / \A \s* \z /msux
!ll&&!rl&&r
291377142
MC/DC100%2/2
$block &&= $text =~ / \A \s* \z /msux
$block $text =~ / \A \s* \z /msux
Truth table100%3/3
A &&= B
A= $blockB= $text =~ / \A \s* \z /msux
ABresult
0X0
100
111
435  
436 # Expand the macro.
437 810 my ($result, $blocktag) = $self->_macro($definition, $block, @args);
438  
439 # We have now double-counted all of the lines in the macro body
440 # itself, so we need to subtract the line count in the macro
441 # definition from the line number.
442 #
443 # This unfortunately means that the line number of errors that happen
444 # inside macro arguments will be somewhat off if the macro definition
445 # itself contains newlines. I don't see a way to avoid that without
446 # much more complex parsing and state tracking.
447 810 $self->{input}[-1][2] -= $definition =~ tr{\n}{};
448  
449 # Return the macro results.
450 810 return ($result, $blocktag, $text);
451 }
452  
453 # The normal command-handling case. Ensure it is a valid command.
454 2548 ▶ if (!ref($COMMANDS{$command})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $COMMANDS{$command}) 1 2547 2548
455 1 $self->_warning("unknown command or macro \\$command");
456 1 return (q{}, 1, $text);
457 }
458  
459 # Dispatch the command to its handler.
460 2547 my ($args, $handler, $want_format) = $COMMANDS{$command}->@*;
461 2547 ▶ if ($want_format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_format) { } 2198 349 2547
462 2198 my ($format, $rest, @args) = $self->_extract($text, $args, 1);
463 2198 my ($blocktag, $output) = $self->$handler($format, @args);
464 2198 return ($output, $blocktag, $rest);
465 } else {
466 349 my ($rest, @args) = $self->_extract($text, $args);
467 349 my ($blocktag, $output) = $self->$handler(@args);
468 349 return ($output, $blocktag, $rest);
469 }
470 }
471  
472 # This is the heart of the input parser. Take a string of raw input, expand
473 # the commands in it, and format the results as HTML. This function is
474 # mutually recursive with _expand and _macro.
475 #
476 # This function is responsible for maintaining the line number in the file
477 # currently being processed, for error reporting. The strategy used is to
478 # increment the line number whenever a newline is seen in processed text.
479 # This means that newlines are not seen until the text containing them is
480 # parsed, which in turn means that every argument that may contain a newline
481 # must be parsed or must update the line number.
482 #
483 # $text - Input text to parse
484 # $block - True if the parse is done in a block context
485 #
486 # Returns: List of the following values:
487 # $output - HTML output corresponding to $text
488 # $block - Whether the result is suitable for block level
489 #
490 ## no critic (Subroutines::ProhibitExcessComplexity)
491 4714 ▶sub _parse_context {
Subroutine _parse_context: called CC=23 SCAR=31.4
492 4714 my ($self, $text, $block) = @_;
493  
494 # Check if there are any commands in the input. If not, we have a
495 # paragraph of regular text.
496 4714 ▶ if (index($text, q{\\}) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($text, "\\") == -1) 2463 2251 4714
497 2463 my $output = $text;
498  
499 # Update the line number.
500 2463 $self->{input}[-1][2] += $text =~ tr{\n}{};
501  
502 # If we are at block context, we need to make the text into a block
503 # element, which means wrapping it in <p> tags. Since that is a
504 # top-level block construct, also close any open block structure.
505 2463 ▶ if ($block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block) 171 2292 2463
506 171 $output = $self->_border_end() . $self->_paragraph($output);
507 }
508  
509 # Return the result.
510 2463 return ($output, $block);
511 }
512  
513 # The output seen so far.
514 2251 my $output = q{};
515  
516 # Output required to close any open block-level constructs that we saw
517 # prior to the text we're currently parsing.
518 2251 my $border = q{};
519  
520 # Output with inline context that needs to be wrapped in <p> tags.
521 2251 my $paragraph = q{};
522  
523 # Leading whitespace that should be added to a created paragraph. This is
524 # only non-empty if $paragraph is empty.
525 2251 my $space = q{};
526  
527 # Whether we saw a construct not suitable for block level.
528 2251 my $nonblock = 0;
529  
530 # We have at least one command. Parse the text into sections of regular
531 # text and commands, expand the commands, and glue the results together as
532 # HTML.
533 #
534 # If we are at block level, we have to distinguish between plain text and
535 # inline commands, which have to be wrapped in paragraph tags, and
536 # block-level commands, which shouldn't be.
537 2251 ▶ while ($text ne q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text ne "") 4506 2251 6757
538 4506 my ($string, $command);
539  
540 # Extract text before the next command, or a command name (but none of
541 # its arguments). I think it's impossible for this regex to fail to
542 # match as long as $text is non-empty, but do error handling just in
543 # case.
544 4506 ▶ if ($text =~ s{ \A ( [^\\]+ | \\ ([\w=]+ | .) ) }{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/ \A ( [^\\]+ | \\ ([\w=]+ | .) ) //msux) { } 4506 0 4506
545 4506 ($string, $command) = ($1, $2);
546 } else {
547 0 my $context = substr($text, 0, 20);
548 0 $context =~ s{ \n .* }{}xms;
549 0 $self->_fatal(qq(unable to parse near "$context"));
550 }
551  
552 # Update the line number.
553 4506 $self->{input}[-1][2] += $string =~ tr{\n}{};
554  
555 # If this is not a command, and we're not at the block level, just add
556 # it verbatim to the output.
557 #
558 # if we are at the block level, pull off any leading space. If there
559 # is still remaining text, add it plus any accumulated whitespace to a
560 # new paragraph.
561 4506 ▶ if (index($string, q{\\}) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($string, '\\') == -1) { } 1141 3365 4506
562 1141 ▶ if ($block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block) { } 468 673 1141
563 468 ▶ if ($string =~ s{ \A (\s+) }{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/ \A (\s+) //msux) 343 125 468
564 343 $space .= $1;
565 }
566 468 ▶ if ($paragraph ne q{} || $string ne q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paragraph ne "" or $string ne "") 393 75 468
Condition100%3/3
$paragraph ne "" or $string ne ""
l!l&&r!l&&!r
3098475
MC/DC100%2/2
$paragraph ne "" or $string ne ""
$paragraph ne "" $string ne ""
Truth table100%3/3
A or B
A= $paragraph ne ""B= $string ne ""
ABresult
1X1
011
000
567 393 ▶ if ($paragraph eq q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paragraph eq "") 84 309 393
568 84 $border = $self->_border_end();
569 }
570 393 $paragraph .= $space . $string;
571 393 $space = q{};
572 }
573 } else {
574 673 $output .= $string;
575 673 $nonblock = 1;
576 }
577 }
578  
579 # Otherwise, we have a command. Expand that command, setting block
580 # context if we haven't seen any inline content so far.
581 else {
582 3365 my ($result, $blocktag);
583 3365 ▶ ($result, $blocktag, $text)
Condition100%3/3
$block && $paragraph eq ''
!ll&&!rl&&r
18074561102
MC/DC100%2/2
$block && $paragraph eq ''
$block $paragraph eq ''
Truth table100%3/3
A && B
A= $blockB= $paragraph eq ''
ABresult
0X0
100
111
584 = $self->_expand($command, $text, $block && $paragraph eq q{});
585  
586 # If the result requires block context, output any pending
587 # paragraph and then the result. Otherwise, if we are already at
588 # block context, start a new paragraph. Otherwise, just append
589 # the result to our output.
590 3365 ▶ if ($blocktag) {
Branch: 2 branches
BranchTrueFalseTotal
if ($blocktag) { } 1448 1917 3365
elsif ($block) { } 509 1408 1917
591 1448 ▶ if ($block && $paragraph ne q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block and $paragraph ne '') { } 1 1447 1448
Condition100%3/3
$block and $paragraph ne ""
!ll&&!rl&&r
39910481
MC/DC100%2/2
$block and $paragraph ne ""
$block $paragraph ne ""
Truth table100%3/3
A and B
A= $blockB= $paragraph ne ""
ABresult
0X0
100
111
592 1 $output .= $border . $self->_paragraph($paragraph);
593 1 $border = q{};
594 1 $paragraph = q{};
595 } else {
596 1447 $output .= $space;
597 }
598 1448 $output .= $result;
599 } elsif ($block) {
600 509 ▶ if ($paragraph eq q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paragraph eq "") 54 455 509
601 54 $border = $self->_border_end();
602 }
603 509 $paragraph .= $space . $result;
604 509 $nonblock = 1;
605 } else {
606 1408 $output .= $result;
607 1408 $nonblock = 1;
608 }
609 3365 $space = q{};
610 }
611  
612 # If the next bit of unparsed text starts with a newline, extract it
613 # and any following whitespace now.
614 4506 ▶ if ($text =~ s{ \A \n (\s*) }{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/ \A \n (\s*) //msux) 1212 3294 4506
615 1212 my $spaces = $1;
616  
617 # Update the line number.
618 1212 $self->{input}[-1][2] += 1 + $spaces =~ tr{\n}{};
619  
620 # Add it to our paragraph if we're accumulating one; otherwise,
621 # add it to the output, but only add the newline if we saw inline
622 # elements or there is remaining text. This suppresses some
623 # useless black lines.
624 1212 ▶ if ($paragraph ne q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paragraph ne '') { } 182 1030 1212
625 182 $paragraph .= "\n$spaces";
626 } else {
627 1030 ▶ if ($text ne q{} || $nonblock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text ne "" or $nonblock) 634 396 1030
Condition100%3/3
$text ne "" or $nonblock
l!l&&r!l&&!r
6286396
MC/DC100%2/2
$text ne "" or $nonblock
$text ne "" $nonblock
Truth table100%3/3
A or B
A= $text ne ""B= $nonblock
ABresult
1X1
011
000
628 634 $output .= "\n";
629 }
630 1030 $output .= $spaces;
631 }
632 }
633 }
634  
635 # If there is any remaining paragraph text, wrap it in tags and append it
636 # to the output. If we were at block level, our output is always suitable
637 # for block level. Otherwise, it's suitable for block level only if all
638 # of our output was from block commands.
639 2251 ▶ if ($paragraph ne q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paragraph ne "") 135 2116 2251
640 135 $output .= $border . $self->_paragraph($paragraph);
641 }
642 2251 ▶ return ($output, $block || !$nonblock);
Condition100%3/3
$block || !$nonblock
l!l&&r!l&&!r
6613991191
MC/DC100%2/2
$block || !$nonblock
$block !$nonblock
Truth table100%3/3
A || not(B)
A= $blockB= !$nonblock
ABresult
1X1
011
000
643 }
644 ## use critic
645  
646 # A wrapper around parse_context for callers who don't care about the block
647 # level of the results.
648 #
649 # $text - Input text to parse
650 # $block - True if the parse is done in a block context
651 #
652 # Returns: HTML output corresponding to $text
653 3904 ▶sub _parse {
Subroutine _parse: called CC=1 SCAR=0.0
654 3904 my ($self, $text, $block) = @_;
655 3904 my ($output) = $self->_parse_context($text, $block);
656 3904 return $output;
657 }
658  
659 # The top-level function for parsing a thread document. Be aware that the
660 # working directory from which this function is run matters a great deal,
661 # since thread may contain relative paths to files that the spinning process
662 # needs to access.
663 #
664 # $thread - Thread to spin
665 # $in_path - Input file path as a Path::Tiny object, or undef
666 # $out_fh - Output file handle to which to write the HTML
667 # $out_path - Output file path as a Path::Tiny object, or undef
668 # $input_type - Optional one-word description of input type
669 20 ▶sub _parse_document {
Subroutine _parse_document: called CC=5 SCAR=16.1
670 20 my ($self, $thread, $in_path, $out_fh, $out_path, $input_type) = @_;
671  
672 # Parse the thread into paragraphs and reverse them to form a stack.
673 20 my @input = reverse($self->_split_paragraphs($thread));
674  
675 # Initialize object state for a new document.
676 #<<<
677 20 $self->{input} = [[\@input, $in_path, 1]];
678 20 ▶ $self->{input_type} = $input_type // 'thread';
Condition100%2/2
$input_type // "thread"
l!l
218
MC/DC100%1/1
$input_type // "thread"
$input_type
Truth table100%2/2
A
A= $input_type
Aresult
11
00
679 20 $self->{macro} = {};
680 20 $self->{out_fh} = $out_fh;
681 20 $self->{out_path} = $out_path;
682 20 $self->{rss} = [];
683 20 $self->{space} = q{};
684 20 $self->{state} = ['BLOCK'];
685 20 $self->{variable} = {};
686 #>>>
687  
688 # Parse the thread file a paragraph at a time. _split_paragraphs takes
689 # care of ensuring that each paragraph contains the complete value of a
690 # command argument.
691 #
692 # The stack of parsed input is maintained in $self->{input} and the file
693 # being parsed at any given point is $self->{input}[-1]. _cmd_include
694 # will push new file information into this stack, and we pop off the top
695 # element of the stack when we exhaust its paragraphs.
696 20 ▶ while ($self->{input}->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"input"}}) 27 20 47
697 27 ▶ while (defined(my $para = pop($self->{input}[-1][0]->@*))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $para = pop @{$$self{"input"}[-1][0]})) 418 27 445
698 418 my $result = $self->_parse(_escape($para), 1);
699 418 $result =~ s{ \A (?:\s*\n)+ }{}xms;
700 418 ▶ if ($result !~ m{ \A \s* \z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result =~ / \A \s* \z /msux) 380 38 418
701 380 $self->_output($result);
702 }
703 }
704 27 pop($self->{input}->@*);
705 }
706  
707 # Close open tags and print any deferred whitespace.
708 20 print_fh($out_fh, $out_path, $self->_block_end(), $self->{space});
709 20 return;
710 }
711  
712 ##############################################################################
713 # Supporting functions
714 ##############################################################################
715  
716 # Generate the format attributes for an HTML tag.
717 #
718 # $format - Format argument to the command
719 #
720 # Returns: String suitable for interpolating into the tag, which means it
721 # starts with a space if non-empty
722 2279 ▶sub _format_attr {
Subroutine _format_attr: called CC=4 SCAR=13.9
723 2279 my ($self, $format) = @_;
724 2279 ▶ return q{} if !$format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 1647 632 2279
725  
726 # Formats starting with # become id tags. Otherwise, it is a class.
727 632 ▶ if ($format =~ s{ \A \# }{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ s/ \A \# //msux) { } 22 610 632
728 22 ▶ if ($format =~ m{ \s }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ / \s /msux) 1 21 22
729 1 $self->_warning(qq(space in anchor "#$format"));
730 }
731 22 return qq{ id="$format"};
732 } else {
733 610 return qq{ class="$format"};
734 }
735 }
736  
737 # Split a block of text apart at paired newlines so that it can be reparsed as
738 # paragraphs, but combine a paragraph with the next one if it has an
739 # unbalanced number of open brackets. Used to parse the top-level structure
740 # of a file and by containiners like \block that can contain multiple
741 # paragraphs.
742 #
743 # $text - Text to split
744 #
745 # Returns: List of paragraphs
746 157 ▶sub _split_paragraphs {
Subroutine _split_paragraphs: called CC=6 SCAR=18.0
747 157 my ($self, $text) = @_;
748 157 my @paragraphs;
749  
750 # Collapse any consecutive newlines at the start to a single newline.
751 157 $text =~ s{ \A \n (\s*\n)+ }{\n}xms;
752  
753 # Pull paragraphs off the text one by one.
754 157 ▶ while ($text ne q{} && $text =~ s{ \A ( .*? (?: \n\n+ | \s*\z ) )}{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text ne "" and $text =~ s/ \A ( .*? (?: \n\n+ | \s*\z ) )//msux) 690 157 847
Condition66%2/3
$text ne "" and $text =~ s/ \A ( .*? (?: \n\n+ | \s*\z ) )//msux
!ll&&!rl&&r
1570690
MC/DC50%1/2
$text ne "" and $text =~ s/ \A ( .*? (?: \n\n+ | \s*\z ) )//msux
$text ne "" $text =~ s/ \A ( .*? (?: \n\n+ | \s*\z ) )//msux
Truth table66%2/3
A and B
A= $text ne ""B= $text =~ s/ \A ( .*? (?: \n\n+ | \s*\z ) )//msux
ABresult
0X0
100
111
755 690 my $para = $1;
756 690 my $open_count = ($para =~ tr{\[}{});
757 690 my $close_count = ($para =~ tr{\]}{});
758 690 ▶ while ($text ne q{} && $open_count > $close_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text ne "" and $open_count > $close_count) 226 690 916
Condition100%3/3
$text ne "" and $open_count > $close_count
!ll&&!rl&&r
157533226
MC/DC100%2/2
$text ne "" and $open_count > $close_count
$text ne "" $open_count > $close_count
Truth table100%3/3
A and B
A= $text ne ""B= $open_count > $close_count
ABresult
0X0
100
111
759 226 ▶ if ($text =~ s{ \A ( .*? (?: \n\n+ | \s*\z ) )}{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/ \A ( .*? (?: \n\n+ | \s*\z ) )//msux) { } 226 0 226
760 226 my $extra = $1;
761 226 $open_count += ($extra =~ tr{\[}{});
762 226 $close_count += ($extra =~ tr{\]}{});
763 226 $para .= $extra;
764 } else {
765 # This should be impossible.
766 0 break;
767 }
768 }
769 690 push(@paragraphs, $para);
770 }
771  
772 # Return the paragraphs.
773 157 return @paragraphs;
774 }
775  
776 # A simple block element. Handles splitting the argument on paragraph
777 # boundaries and surrounding things properly with the tag.
778 #
779 # $tag - Name of the tag
780 # $border - Initial string to output before the block
781 # $format - Format string for the block
782 # $text - Contents of the block
783 #
784 # Returns: Block context, output
785 583 ▶sub _block {
Subroutine _block: called CC=3 SCAR=11.0
786 583 my ($self, $tag, $border, $format, $text) = @_;
787 583 my $output = $border . "<$tag" . $self->_format_attr($format) . '>';
788 583 $self->_block_start();
789  
790 # If the format is packed, the contents of the block should be treated as
791 # inline rather than block and not surrounded by <p>. This is how compact
792 # bullet or number lists are done. Otherwise, parse each containing
793 # paragraph separately in block context.
794 583 ▶ if ($format eq 'packed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'packed') { } 461 122 583
795 461 $output .= $self->_parse($text, 0);
796 } else {
797 122 my @paragraphs = $self->_split_paragraphs($text);
798 122 $output .= join(q{}, map { $self->_parse($_, 1) } @paragraphs);
799 }
800 583 $output .= $self->_block_end();
801  
802 # Close the tag. The tag may have contained attributes, which aren't
803 # allowed in the closing tag.
804 583 $tag =~ s{ [ ] .* }{}xms;
805 583 $output =~ s{ \s* \z }{</$tag>}xms;
806 583 ▶ if ($format ne 'packed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format ne "packed") 122 461 583
807 122 $output .= "\n";
808 }
809  
810 583 return (1, $output);
811 }
812  
813 # A simple inline element.
814 #
815 # $tag - Name of the tag
816 # $format - Format string
817 # $text - Contents of the element
818 #
819 # Returns: Inline context, output
820 723 ▶sub _inline {
Subroutine _inline: called CC=1 SCAR=0.0
821 723 my ($self, $tag, $format, $text) = @_;
822 723 my $output = "<$tag" . $self->_format_attr($format) . '>';
823 723 $output .= $self->_parse($text) . "</$tag>";
824 723 return (0, $output);
825 }
826  
827 # A heading. Handles formats of #something specially by adding an <a name>
828 # tag inside the heading tag to make it a valid target for internal links even
829 # in old browsers.
830 #
831 # $level - Level of the heading
832 # $format - Format string
833 # $text - Content of the heading
834 #
835 # Returns: Block context, output
836 85 ▶sub _heading {
Subroutine _heading: called CC=2 SCAR=6.9
837 85 my ($self, $level, $format, $text) = @_;
838 85 my $output = $self->_border_end();
839 85 $text = $self->_parse($text);
840  
841 # Special handling for anchors in the format string.
842 85 ▶ if ($format =~ m{ \A \# }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format =~ / \A \# /msux) 22 63 85
843 22 my $tag = $format;
844 22 $tag =~ s{ \A \# }{}xms;
845 22 $text = qq{<a name="$tag">$text</a>};
846 }
847  
848 # Build the output.
849 85 $output .= "<h$level" . $self->_format_attr($format) . '>' . $text;
850 85 $output =~ s{ \n \z }{}xms;
851 85 $output .= "</h$level>\n";
852 85 return (1, $output);
853 }
854  
855 # Enclose some text in another tag. If the enclosed text is entirely enclosed
856 # in <span> or <div> tags, we pull the options of the <span> or <div> out and
857 # instead apply them to the parent tag.
858 #
859 # $tag - Name of tag
860 # $text - Text to enclose
861 104 ▶sub _enclose {
Subroutine _enclose: called CC=2 SCAR=6.9
862 104 my ($self, $tag, $text) = @_;
863  
864 # Strip any attributes from the tag.
865 104 my $close_tag = $tag;
866 104 $close_tag =~ s{ [ ] .*}{}xms;
867  
868 # Handle <div> and <span> wrapping.
869 104 my $re = qr{
870 \A # start of paragraph
871 (\s*) # any whitespace (1)
872 < (span | div) # span or div tag before any text (2)
873 ([^>]*) # any class attribute for that tag (3)
874 > # close tag
875 (?! .* <\2) # no second tag
876 (.*) # text of the paragraph (4)
877 </\2> # close tag
878 (\s*) # any whitespace (5)
879 \z # end of paragraph without other text
880 }xms;
881 104 ▶ if ($text =~ $re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$re/u) { } 58 46 104
882 58 my ($lead, $class, $body, $trail) = ($1, $3, $4, $5);
883 58 return "$lead<$tag$class>$body</$close_tag>$trail";
884 } else {
885 46 return "<$tag>$text</$close_tag>";
886 }
887 }
888  
889 ##############################################################################
890 # Special commands
891 ##############################################################################
892  
893 # These methods are all used, but are indirected through the above table, so
894 # perlcritic gets confused.
895 #
896 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
897  
898 # Define a new macro. This is the command handler for \==.
899 #
900 # $name - Name of the macro
901 # $args - Number of arguments
902 # $definition - Definition of the macro
903 #
904 # Returns: Block context, empty output
905 114 ▶sub _define_macro {
Subroutine _define_macro: called CC=4 SCAR=13.9
906 114 my ($self, $name, $args, $definition) = @_;
907 114 $args = $self->_parse($args);
908  
909 # Verify the argument count and definition.
910 114 ▶ if ($args !~ m{ \A \d+ \z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args =~ / \A \d+ \z /msux) 1 113 114
911 1 $self->_warning("invalid macro argument count for \\$name");
912 1 return (1, q{});
913 }
914 113 for my $arg ($definition =~ m{ \\(\d+) }xmsg) {
915 231 ▶ if ($arg > $args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg > $args) 1 230 231
916 1 my $msg = "invalid macro placeholder \\$arg (greater than $args)";
917 1 $self->_warning($msg);
918 }
919 }
920  
921 # We don't parse the macro definition now but we need to update the line
922 # number for accurate error reporting.
923 113 $self->{input}[-1][2] += $definition =~ tr{\n}{};
924  
925 # Define the macro.
926 113 $self->{macro}{$name} = [$args, $definition];
927 113 return (1, q{});
928 }
929  
930 # Define a new variable. This is the command handler for \=.
931 #
932 # $name - Name of the variable
933 # $value - Value of the variable
934 #
935 # Returns: Block context, empty output
936 6 ▶sub _define_variable {
Subroutine _define_variable: called CC=1 SCAR=0.0
937 6 my ($self, $name, $value) = @_;
938 6 $self->{variable}{$name} = $self->_parse($value);
939 6 return (1, q{});
940 }
941  
942 # Literal backslash. This is the command handler for \\.
943 1 ▶sub _literal { return (0, q{\\}) }
Subroutine _literal: called CC=1 SCAR=0.0
944  
945 ##############################################################################
946 # Regular commands
947 ##############################################################################
948  
949 # Basic inline commands.
950 #<<<
951 86 ▶sub _cmd_break { return (0, '<br />') }
Subroutine _cmd_break: called CC=1 SCAR=0.0
952 2 ▶sub _cmd_bold { my ($self, @a) = @_; return $self->_inline('b', @a) }
Subroutine _cmd_bold: called CC=1 SCAR=0.0
953 44 ▶sub _cmd_cite { my ($self, @a) = @_; return $self->_inline('cite', @a) }
Subroutine _cmd_cite: called CC=1 SCAR=0.0
954 101 ▶sub _cmd_class { my ($self, @a) = @_; return $self->_inline('span', @a) }
Subroutine _cmd_class: called CC=1 SCAR=0.0
955 6 ▶sub _cmd_code { my ($self, @a) = @_; return $self->_inline('code', @a) }
Subroutine _cmd_code: called CC=1 SCAR=0.0
956 24 ▶sub _cmd_emph { my ($self, @a) = @_; return $self->_inline('em', @a) }
Subroutine _cmd_emph: called CC=1 SCAR=0.0
957 22 ▶sub _cmd_italic { my ($self, @a) = @_; return $self->_inline('i', @a) }
Subroutine _cmd_italic: called CC=1 SCAR=0.0
958 1 ▶sub _cmd_strike { my ($self, @a) = @_; return $self->_inline('strike', @a) }
Subroutine _cmd_strike: called CC=1 SCAR=0.0
959 254 ▶sub _cmd_strong { my ($self, @a) = @_; return $self->_inline('strong', @a) }
Subroutine _cmd_strong: called CC=1 SCAR=0.0
960 1 ▶sub _cmd_sub { my ($self, @a) = @_; return $self->_inline('sub', @a) }
Subroutine _cmd_sub: called CC=1 SCAR=0.0
961 12 ▶sub _cmd_sup { my ($self, @a) = @_; return $self->_inline('sup', @a) }
Subroutine _cmd_sup: called CC=1 SCAR=0.0
962 256 ▶sub _cmd_under { my ($self, @a) = @_; return $self->_inline('u', @a) }
Subroutine _cmd_under: called CC=1 SCAR=0.0
963 #>>>
964  
965 # The headings.
966 19 ▶sub _cmd_h1 { my ($self, @a) = @_; return $self->_heading(1, @a); }
Subroutine _cmd_h1: called CC=1 SCAR=0.0
967 62 ▶sub _cmd_h2 { my ($self, @a) = @_; return $self->_heading(2, @a); }
Subroutine _cmd_h2: called CC=1 SCAR=0.0
968 1 ▶sub _cmd_h3 { my ($self, @a) = @_; return $self->_heading(3, @a); }
Subroutine _cmd_h3: called CC=1 SCAR=0.0
969 1 ▶sub _cmd_h4 { my ($self, @a) = @_; return $self->_heading(4, @a); }
Subroutine _cmd_h4: called CC=1 SCAR=0.0
970 1 ▶sub _cmd_h5 { my ($self, @a) = @_; return $self->_heading(5, @a); }
Subroutine _cmd_h5: called CC=1 SCAR=0.0
971 1 ▶sub _cmd_h6 { my ($self, @a) = @_; return $self->_heading(6, @a); }
Subroutine _cmd_h6: called CC=1 SCAR=0.0
972  
973 # A horizontal rule.
974 1 ▶sub _cmd_rule {
Subroutine _cmd_rule: called CC=1 SCAR=0.0
975 1 my ($self) = @_;
976 1 return (1, $self->_border_end() . "<hr />\n");
977 }
978  
979 # Simple block commands.
980  
981 21 ▶sub _cmd_div {
Subroutine _cmd_div: called CC=1 SCAR=0.0
982 21 my ($self, $format, $text) = @_;
983 21 return $self->_block('div', q{}, $format, $text);
984 }
985  
986 6 ▶sub _cmd_block {
Subroutine _cmd_block: called CC=1 SCAR=0.0
987 6 my ($self, $format, $text) = @_;
988 6 return $self->_block('blockquote', q{}, $format, $text);
989 }
990  
991 54 ▶sub _cmd_bullet {
Subroutine _cmd_bullet: called CC=1 SCAR=0.0
992 54 my ($self, $format, $text) = @_;
993 54 my $border = $self->_border_start('bullet', "<ul>\n", "</ul>\n\n");
994 54 return $self->_block('li', $border, $format, $text);
995 }
996  
997 407 ▶sub _cmd_number {
Subroutine _cmd_number: called CC=1 SCAR=0.0
998 407 my ($self, $format, $text) = @_;
999 407 my $border = $self->_border_start('number', "<ol>\n", "</ol>\n\n");
1000 407 return $self->_block('li', $border, $format, $text);
1001 }
1002  
1003 # A description list entry.
1004 #
1005 # $format - Format string
1006 # $heading - Initial heading
1007 # $text - Body text
1008 81 ▶sub _cmd_desc {
Subroutine _cmd_desc: called CC=1 SCAR=0.0
1009 81 my ($self, $format, $heading, $text) = @_;
1010 81 $heading = $self->_parse($heading);
1011 81 my $format_attr = $self->_format_attr($format);
1012 81 my $border = $self->_border_start('desc', "<dl>\n", "</dl>\n\n");
1013 81 my $initial = $border . "<dt$format_attr>" . $heading . "</dt>\n";
1014 81 return $self->_block('dd', $initial, $format, $text);
1015 }
1016  
1017 # An HTML entity. Check for and handle numeric entities properly, including
1018 # special-casing [ and ] since the user may have needed to use \entity to
1019 # express text that contains literal brackets.
1020 #
1021 # $entity - Entity specification, an HTML name or a Unicode number
1022 53 ▶sub _cmd_entity {
Subroutine _cmd_entity: called CC=4 SCAR=13.9
1023 53 my ($self, $char) = @_;
1024 53 $char = $self->_parse($char);
1025 53 ▶ if ($char eq '91') {
Branch: 3 branches
BranchTrueFalseTotal
if ($char eq '91') { } 2 51 53
elsif ($char eq '93') { } 2 49 51
elsif ($char =~ / \A \d+ \z /msux) { } 1 48 49
1026 2 return (0, '[');
1027 } elsif ($char eq '93') {
1028 2 return (0, ']');
1029 } elsif ($char =~ m{ \A \d+ \z }xms) {
1030 1 return (0, "&#$char;");
1031 } else {
1032 48 return (0, "&$char;");
1033 }
1034 }
1035  
1036 # Generates the page heading at the top of the document. This is where the
1037 # XHTML declarations come from.
1038 #
1039 # $title - Page title
1040 # $style - Page style
1041 20 ▶sub _cmd_heading {
Subroutine _cmd_heading: called CC=13 SCAR=25.7
1042 20 my ($self, $title, $style) = @_;
1043 20 $title = $self->_parse($title);
1044 20 $style = $self->_parse($style);
1045  
1046 # Get the relative URL of the output page, used for sitemap information.
1047 20 my $page;
1048 20 ▶ if (defined($self->{out_path}) && defined($self->{output})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"out_path"} and defined $self->{"output"}) 12 8 20
Condition100%3/3
defined $self->{"out_path"} and defined $self->{"output"}
!ll&&!rl&&r
7112
MC/DC100%2/2
defined $self->{"out_path"} and defined $self->{"output"}
defined $self->{"out_path"} defined $self->{"output"}
Truth table100%3/3
A and B
A= defined $self->{"out_path"}B= defined $self->{"output"}
ABresult
0X0
100
111
1049 12 $page = $self->{out_path}->relative($self->{output});
1050 }
1051  
1052 # Build the page header.
1053 20 my $output = qq{<?xml version="1.0" encoding="utf-8"?>\n};
1054 20 $output .= qq{<!DOCTYPE html\n};
1055 20 $output .= qq{ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n};
1056 20 $output .= qq{ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n};
1057 20 $output .= qq{\n<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"};
1058 20 $output .= qq{ lang="en">\n};
1059 20 $output .= qq{<head>\n <title>$title</title>\n};
1060 20 $output .= q{ <meta http-equiv="Content-Type"};
1061 20 $output .= qq{ content="text/html; charset=utf-8" />\n};
1062  
1063 # Add style sheet.
1064 20 ▶ if ($style) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style) 18 2 20
1065 18 $style .= '.css';
1066 18 ▶ if ($self->{style_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"style_url"}) 14 4 18
1067 14 $style = $self->{style_url} . $style;
1068 }
1069 18 $output .= qq{ <link rel="stylesheet" href="$style"};
1070 18 $output .= qq{ type="text/css" />\n};
1071 }
1072  
1073 # Add RSS links if any.
1074 20 for my $rss ($self->{rss}->@*) {
1075 9 my ($url, $rss_title) = $rss->@*;
1076 9 $output .= q{ <link rel="alternate" type="application/rss+xml"};
1077 9 $output .= qq{ href="$url"\n};
1078 9 $output .= qq{ title="$rss_title" />\n};
1079 }
1080  
1081 # Add <link> tags based on the sitemap.
1082 20 ▶ if ($self->{sitemap} && defined($page)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sitemap"} and defined $page) 12 8 20
Condition66%2/3
$self->{"sitemap"} and defined $page
!ll&&!rl&&r
8012
MC/DC50%1/2
$self->{"sitemap"} and defined $page
$self->{"sitemap"} defined $page
Truth table66%2/3
A and B
A= $self->{"sitemap"}B= defined $page
ABresult
0X0
100
111
1083 12 my @links = $self->{sitemap}->links($page);
1084 12 ▶ if (@links) {
Branch: 1 branch
BranchTrueFalseTotal
if (@links) 6 6 12
1085 6 $output .= join(q{}, @links);
1086 }
1087 }
1088  
1089 # End of the header.
1090 20 $output .= "</head>\n\n";
1091  
1092 # Add some generator comments.
1093 20 my $date = strftime('%Y-%m-%d %T -0000', gmtime());
1094 20 my $input_path = $self->{input}[-1][1];
1095 20 ▶ my $from = defined($input_path) ? ' from ' . $input_path->basename() : q{};
Branch: 1 branch
BranchTrueFalseTotal
defined $input_path ? : 19 1 20
1096 20 my $version = $App::DocKnot::VERSION;
1097 20 $output .= "<!-- Spun$from by DocKnot $version on $date -->\n";
1098  
1099 # Add the <body> tag and the navbar (if we have a sitemap).
1100 20 $output .= "\n<body>\n";
1101 20 ▶ if ($self->{sitemap} && defined($page)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sitemap"} and defined $page) 12 8 20
Condition66%2/3
$self->{"sitemap"} and defined $page
!ll&&!rl&&r
8012
MC/DC50%1/2
$self->{"sitemap"} and defined $page
$self->{"sitemap"} defined $page
Truth table66%2/3
A and B
A= $self->{"sitemap"}B= defined $page
ABresult
0X0
100
111
1102 12 my @navbar = $self->{sitemap}->navbar($page);
1103 12 ▶ if (@navbar) {
Branch: 1 branch
BranchTrueFalseTotal
if (@navbar) 6 6 12
1104 6 $output .= join(q{}, @navbar);
1105 }
1106 }
1107  
1108 20 return (1, $output);
1109 }
1110  
1111 # Include an image. The size is added to the HTML tag automatically.
1112 #
1113 # $format - Format string
1114 # $image - Path to the image (may be relative or an absolute URL)
1115 # $alt - Alt text of image
1116 23 ▶sub _cmd_image {
Subroutine _cmd_image: called CC=2 SCAR=6.9
1117 23 my ($self, $format, $image, $text) = @_;
1118 23 $image = $self->_parse($image);
1119 23 $text = $self->_parse($text);
1120  
1121 # Determine the size attributes of the image if possible.
1122 23 my $path = $self->_file_path($image);
1123 23 ▶ my $size = $path->exists() ? q{ } . lc(html_imgsize("$path")) : q{};
Branch: 1 branch
BranchTrueFalseTotal
$path->exists ? : 4 19 23
1124  
1125 # Generate the tag.
1126 23 my $output = qq{<img src="$image" alt="$text"$size};
1127 23 $output .= $self->_format_attr($format) . ' />';
1128 23 return (1, $output);
1129 }
1130  
1131 # Include a file. Note that this includes a file after the current paragraph,
1132 # not immediately, which may be a bit surprising.
1133 7 ▶sub _cmd_include {
Subroutine _cmd_include: called CC=1 SCAR=0.0
1134 7 my ($self, $file) = @_;
1135 7 $file = $self->_file_path($self->_parse($file));
1136  
1137 # Read the thread, split it on paragraphs, and reverse it to make a stack.
1138 7 my $thread = $self->_read_file($file);
1139 7 my @paragraphs = reverse($self->_split_paragraphs($thread));
1140  
1141 # Add it to the file stack.
1142 7 push($self->{input}->@*, [\@paragraphs, $file, 1]);
1143  
1144 # Expand into empty output.
1145 7 return (1, q{});
1146 }
1147  
1148 # A link to a URL or partial URL.
1149 #
1150 # $format - Format string
1151 # $url - Target URL
1152 # $text - Anchor text
1153 724 ▶sub _cmd_link {
Subroutine _cmd_link: called CC=1 SCAR=0.0
1154 724 my ($self, $format, $url, $text) = @_;
1155 724 $url = $self->_parse($url);
1156 724 $text = $self->_parse($text);
1157 724 my $format_attr = $self->_format_attr($format);
1158 724 return (0, qq{<a href="$url"$format_attr>$text</a>});
1159 }
1160  
1161 # Preformatted text. This does not use _block because we don't want to split
1162 # the contained text into paragraphs and we want to parse it all in inline
1163 # context always.
1164 2 ▶sub _cmd_pre {
Subroutine _cmd_pre: called CC=1 SCAR=0.0
1165 2 my ($self, $format, $text) = @_;
1166 2 my $output = $self->_border_end();
1167 2 $output .= '<pre' . $self->_format_attr($format) . '>';
1168 2 $output .= $self->_parse($text);
1169 2 $output .= "</pre>\n";
1170 2 return (1, $output);
1171 }
1172  
1173 # Used for the leading quotes that I have on many of my pages. If the format
1174 # is "broken", adds line breaks at the end of each line.
1175 #
1176 # $format - Format string, used as the format for the main <p> tag inside the
1177 # <blockquote>. Values broken and short trigger special handling,
1178 # such as adding line breaks or changing the attribution class.
1179 # $quote - Text of the quote
1180 # $author - Author of the quote
1181 # $cite - Attribution of the quote
1182 8 ▶sub _cmd_quote {
Subroutine _cmd_quote: called CC=7 SCAR=19.7
1183 8 my ($self, $format, $quote, $author, $cite) = @_;
1184 8 $author = $self->_parse($author);
1185 8 $cite = $self->_parse($cite);
1186 8 my $output = $self->_border_end() . q{<blockquote class="quote">};
1187  
1188 # Parse the contents of the quote in a new block context.
1189 8 $self->_block_start();
1190 8 my @paragraphs = $self->_split_paragraphs($quote);
1191 8 $quote = join(q{}, map { $self->_parse($_, 1) } @paragraphs);
1192 8 $quote .= $self->_block_end();
1193  
1194 # Remove trailing newlines.
1195 8 $quote =~ s{ \n+ \z }{}xms;
1196  
1197 # If this is a broken quote, add line breaks to each line.
1198 8 ▶ if ($format eq 'broken') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "broken") 5 3 8
1199 5 $quote =~ s{ ( \S [ ]* ) ( \n\s* (?!</p>)\S )}{$1<br />$2}xmsg;
1200  
1201 # Remove <br /> tags for blank lines or at the start.
1202 5 $quote =~ s{ \n <br [ ] /> }{\n}xmsg;
1203 5 $quote =~ s{ <p> <br [ ] /> }{<p>}xmsg;
1204 }
1205  
1206 # If there was a format, apply it to every <p> tag in the quote.
1207 8 ▶ if ($format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format) 8 0 8
1208 8 my $format_attr = $self->_format_attr($format);
1209 8 $quote =~ s{ <p> }{<p$format_attr>}xmsg;
1210 }
1211  
1212 # Done with the quote.
1213 8 $output .= $quote;
1214  
1215 # Format the author and citation.
1216 8 ▶ if ($author) {
Branch: 1 branch
BranchTrueFalseTotal
if ($author) { } 8 0 8
1217 8 my $prefix = q{};
1218 8 ▶ if ($format eq 'broken' || $format eq 'short') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'broken' or $format eq 'short') { } 8 0 8
Condition66%2/3
$format eq "broken" or $format eq "short"
l!l&&r!l&&!r
530
MC/DC0%0/2
$format eq "broken" or $format eq "short"
$format eq "broken" $format eq "short"
Truth table66%2/3
A or B
A= $format eq "broken"B= $format eq "short"
ABresult
1X1
011
000
1219 8 $output .= qq{<p class="attribution">\n};
1220 } else {
1221 0 $output .= qq{<p class="long-attrib">\n};
1222 0 $prefix = '&mdash; ';
1223 }
1224 8 ▶ if ($cite) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cite) { } 4 4 8
1225 4 $output .= " $prefix$author,\n $cite\n";
1226 } else {
1227 4 $output .= " $prefix$author\n";
1228 }
1229 8 $output .= '</p>';
1230 } else {
1231 0 $output .= "\n";
1232 }
1233  
1234 # Close the HTML tag and return the output.
1235 8 $output .= "</blockquote>\n";
1236 8 return (1, $output);
1237 }
1238  
1239 # Given the name of a product, return the release date of the product.
1240 5 ▶sub _cmd_release {
Subroutine _cmd_release: called CC=3 SCAR=11.3
1241 5 my ($self, $package) = @_;
1242 5 $package = $self->_parse($package);
1243 5 ▶ if (!$self->{versions}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"versions"}) 1 4 5
1244 1 $self->_warning('no package release information available');
1245 1 return (0, q{});
1246 }
1247 4 my $date = $self->{versions}->release_date($package);
1248 4 ▶ if (!defined($date)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $date) 0 4 4
1249 0 $self->_warning(qq(no release date known for "$package"));
1250 0 return (0, q{});
1251 }
1252 4 return (0, $date);
1253 }
1254  
1255 # Used to save RSS feed information for the page. Doesn't output anything
1256 # directly; the RSS feed information is used later in _cmd_heading.
1257 9 ▶sub _cmd_rss {
Subroutine _cmd_rss: called CC=1 SCAR=0.0
1258 9 my ($self, $url, $title) = @_;
1259 9 $url = $self->_parse($url);
1260 9 $title = $self->_parse($title);
1261 9 push($self->{rss}->@*, [$url, $title]);
1262 9 return (1, q{});
1263 }
1264  
1265 # Used to end each page, this adds the navigation links and my standard
1266 # address block.
1267 19 ▶sub _cmd_signature {
Subroutine _cmd_signature: called CC=11 SCAR=26.6
1268 19 my ($self) = @_;
1269 19 my $input_path = $self->{input}[-1][1];
1270 19 my $output = $self->_border_end();
1271  
1272 # If we're spinning from standard input to standard output, don't add any
1273 # of the standard footer, just close the HTML tags.
1274 19 ▶ if (!defined($input_path) && !defined($self->{out_path})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $input_path or defined $self->{"out_path"}) 1 18 19
Condition66%2/3
defined $input_path or defined $self->{"out_path"}
l!l&&r!l&&!r
1801
MC/DC50%1/2
defined $input_path or defined $self->{"out_path"}
defined $input_path defined $self->{"out_path"}
Truth table66%2/3
A or B
A= defined $input_pathB= defined $self->{"out_path"}
ABresult
1X1
011
000
1275 1 $output .= "</body>\n</html>\n";
1276 1 return (1, $output);
1277 }
1278  
1279 # Add the end-of-page navbar if we have sitemap information.
1280 18 ▶ if ($self->{sitemap} && $self->{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sitemap"} and $self->{"output"}) 12 6 18
Condition66%2/3
$self->{"sitemap"} and $self->{"output"}
!ll&&!rl&&r
6012
MC/DC50%1/2
$self->{"sitemap"} and $self->{"output"}
$self->{"sitemap"} $self->{"output"}
Truth table66%2/3
A and B
A= $self->{"sitemap"}B= $self->{"output"}
ABresult
0X0
100
111
1281 12 my $page = $self->{out_path}->relative($self->{output});
1282 12 $output .= join(q{}, $self->{sitemap}->navbar($page)) . "\n";
1283 }
1284  
1285 # Figure out the modification dates. Use the Git repository if available.
1286 18 my $now = strftime('%Y-%m-%d', gmtime());
1287 18 my $modified = $now;
1288 18 ▶ if (defined($input_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input_path) 18 0 18
1289 18 $modified = strftime('%Y-%m-%d', gmtime($input_path->stat()->[9]));
1290 }
1291 18 ▶ if ($self->{repository} && $self->{source}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"repository"} and $self->{"source"}) 0 18 18
Condition33%1/3
$self->{"repository"} and $self->{"source"}
!ll&&!rl&&r
1800
MC/DC0%0/2
$self->{"repository"} and $self->{"source"}
$self->{"repository"} $self->{"source"}
Truth table33%1/3
A and B
A= $self->{"repository"}B= $self->{"source"}
ABresult
0X0
100
111
1292 0 ▶ if (path($self->{source})->subsumes($input_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (path($self->{"source"})->subsumes($input_path)) 0 0 0
1293 0 my $repository = $self->{repository};
1294 $modified = $self->{repository}->run(
1295 0 'log', '-1', '--format=%ct', "$input_path",
1296 );
1297 0 ▶ if ($modified) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modified) 0 0 0
1298 0 $modified = strftime('%Y-%m-%d', gmtime($modified));
1299 }
1300 }
1301 }
1302  
1303 # Determine which template to use and substitute in the appropriate times.
1304 18 $output .= "<address>\n";
1305 18 my $link = qq{<a href="$URL">spun</a>};
1306 18 ▶ if ($modified eq $now) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modified eq $now) { } 14 4 18
1307 14 $output .= " Last modified and\n $link $modified\n";
1308 } else {
1309 4 $output .= " Last $link\n";
1310 4 $output .= " $now from $self->{input_type} modified $modified\n";
1311 }
1312  
1313 # Close out the document.
1314 18 $output .= "</address>\n</body>\n</html>\n";
1315 18 return (1, $output);
1316 }
1317  
1318 # Insert the formatted size in bytes, kilobytes, or megabytes of some local
1319 # file. We could use Number::Format here, but what we're doing is simple
1320 # enough and doesn't seem worth the trouble of another dependency.
1321 5 ▶sub _cmd_size {
Subroutine _cmd_size: called CC=5 SCAR=16.1
1322 5 my ($self, $file) = @_;
1323 5 $file = $self->_file_path($self->_parse($file));
1324  
1325 # Get the size of the file.
1326 5 my $size;
1327 5 ▶ if ($file->exists()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->exists) 4 1 5
1328 4 $size = $file->stat()->[7];
1329 }
1330 5 ▶ if (!defined($size)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $size) 1 4 5
1331 1 $self->_warning("cannot stat file $file: $!");
1332 1 return (0, q{});
1333 }
1334  
1335 # Format the size using SI units.
1336 4 my @suffixes = qw(Ki Mi Gi Ti);
1337 4 my $suffix = q{};
1338 4 ▶ while ($size > 1024 && @suffixes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > 1024 and @suffixes) 1 4 5
Condition66%2/3
$size > 1024 and @suffixes
!ll&&!rl&&r
401
MC/DC50%1/2
$size > 1024 and @suffixes
$size > 1024 @suffixes
Truth table66%2/3
A and B
A= $size > 1024B= @suffixes
ABresult
0X0
100
111
1339 1 $size /= 1024;
1340 1 $suffix = shift(@suffixes);
1341 }
1342  
1343 # Return the result.
1344 4 return (0, sprintf('%.0f', $size) . $suffix . 'B');
1345 }
1346  
1347 # Generates a HTML version of the sitemap and outputs that.
1348 2 ▶sub _cmd_sitemap {
Subroutine _cmd_sitemap: called CC=2 SCAR=6.9
1349 2 my ($self) = @_;
1350 2 ▶ if (!$self->{sitemap}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sitemap"}) 1 1 2
1351 1 $self->_warning('no sitemap file found');
1352 1 return (1, q{});
1353 }
1354 1 my $sitemap = join(q{}, $self->{sitemap}->sitemap());
1355 1 return (1, $self->_border_end() . $sitemap);
1356 }
1357  
1358 # Start a table. Takes any additional HTML attributes to set for the table
1359 # (this is ugly, but <table> takes so many attributes for which there is no
1360 # style sheet equivalent that it's unavoidable) and the body of the table
1361 # (which should consist of \tablehead and \tablerow lines).
1362 14 ▶sub _cmd_table {
Subroutine _cmd_table: called CC=2 SCAR=7.1
1363 14 my ($self, $format, $options, $body) = @_;
1364 14 ▶ my $tag = $options ? "table $options" : 'table';
Branch: 1 branch
BranchTrueFalseTotal
$options ? : 0 14 14
1365 14 return $self->_block($tag, q{}, $format, $body);
1366 }
1367  
1368 # A heading of a table. Takes the contents of the cells in that heading.
1369 1 ▶sub _cmd_tablehead {
Subroutine _cmd_tablehead: called CC=2 SCAR=6.9
1370 1 my ($self, $format, @cells) = @_;
1371 1 my $output = ' <tr' . $self->_format_attr($format) . ">\n";
1372 1 for (@cells) {
1373 2 my $text = $self->_parse($_) . $self->_border_end();
1374 2 $output .= (q{ } x 4) . $self->_enclose('th', $text) . "\n";
1375 }
1376 1 $output .= " </tr>\n";
1377 1 return (1, $output);
1378 }
1379  
1380 # A data line of a table. Takes the contents of the cells in that row.
1381 49 ▶sub _cmd_tablerow {
Subroutine _cmd_tablerow: called CC=2 SCAR=6.9
1382 49 my ($self, $format, @cells) = @_;
1383 49 my $output = ' <tr' . $self->_format_attr($format) . ">\n";
1384 49 for (@cells) {
1385 102 my $text = $self->_parse($_) . $self->_border_end();
1386 102 $output .= (q{ } x 4) . $self->_enclose('td', $text) . "\n";
1387 }
1388 49 $output .= " </tr>\n";
1389 49 return (1, $output);
1390 }
1391  
1392 # Given the name of a package, return the version number of its latest
1393 # release.
1394 21 ▶sub _cmd_version {
Subroutine _cmd_version: called CC=3 SCAR=11.3
1395 21 my ($self, $package) = @_;
1396 21 ▶ if (!$self->{versions}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"versions"}) 1 20 21
1397 1 $self->_warning('no package version information available');
1398 1 return (0, q{});
1399 }
1400 20 my $version = $self->{versions}->version($package);
1401 20 ▶ if (!defined($version)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $version) 0 20 20
1402 0 $self->_warning(qq(no version known for "$package"));
1403 0 return (0, q{});
1404 }
1405 20 return (0, $version);
1406 }
1407  
1408 ##############################################################################
1409 # Public interface
1410 ##############################################################################
1411  
1412 # Create a new thread to HTML converter. This object can (and should) be
1413 # reused for all thread conversions done while spinning a tree of files.
1414 #
1415 # $args - Anonymous hash of arguments with the following keys:
1416 # output - Root of the output tree
1417 # sitemap - App::DocKnot::Spin::Sitemap object
1418 # source - Root of the source tree
1419 # style-url - Partial URL to style sheets
1420 # versions - App::DocKnot::Spin::Versions object
1421 #
1422 # Returns: Newly created object
1423 13 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
1424 13 my ($class, $args_ref) = @_;
1425 13 my $output;
1426 13 ▶ if (defined($args_ref->{output})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args_ref->{"output"}) 5 8 13
1427 5 $output = path($args_ref->{output});
1428 }
1429  
1430 # Add a trailing slash to the partial URL for style sheets.
1431 13 ▶ my $style_url = $args_ref->{'style-url'} // q{};
Condition100%2/2
$args_ref->{"style-url"} // ""
l!l
76
MC/DC100%1/1
$args_ref->{"style-url"} // ""
$args_ref->{"style-url"}
Truth table100%2/2
A
A= $args_ref->{"style-url"}
Aresult
11
00
1432 13 ▶ if ($style_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style_url) 7 6 13
1433 7 $style_url =~ s{ /* \z }{/}xms;
1434 }
1435  
1436 # Use a Git::Repository object to get modification timestamps if a source
1437 # tree was specified and it appears to be a git repository.
1438 13 my ($source, $repository);
1439 13 ▶ if (defined($args_ref->{source})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args_ref->{"source"}) 5 8 13
1440 5 $source = path($args_ref->{source});
1441 5 ▶ if ($source->child('.git')->is_dir()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->child(".git")->is_dir) 0 5 5
1442 0 $repository = Git::Repository->new(work_tree => "$source");
1443 }
1444 }
1445  
1446 # Create and return the object.
1447 my $self = {
1448 output => $output,
1449 repository => $repository,
1450 sitemap => $args_ref->{sitemap},
1451 source => $source,
1452 style_url => $style_url,
1453 versions => $args_ref->{versions},
1454 13 };
1455 13 bless($self, $class);
1456 13 return $self;
1457 }
1458  
1459 # Convert thread to HTML and return the output as a string. The working
1460 # directory still matters for file references in the thread.
1461 #
1462 # $thread - Thread to spin
1463 # $input - Optional input file path (for relative path and timestamps)
1464 #
1465 # Returns: Resulting HTML
1466 5 ▶sub spin_thread {
Subroutine spin_thread: called CC=1 SCAR=0.0
Pod documented
1467 5 my ($self, $thread, $input) = @_;
1468 5 my $result;
1469 5 open(my $out_fh, '>:raw:encoding(utf-8)', \$result);
1470 5 $self->_parse_document($thread, $input, $out_fh, undef);
1471 5 close($out_fh);
1472 5 return decode('utf-8', $result);
1473 }
1474  
1475 # Spin a single file of thread to HTML.
1476 #
1477 # $input - Input file (if not given, assumes standard input)
1478 # $output - Output file (if not given, assumes standard output)
1479 #
1480 # Raises: Text exception on processing error
1481 13 ▶sub spin_thread_file {
Subroutine spin_thread_file: called CC=3 SCAR=11.0
Pod documented
1482 13 my ($self, $input, $output) = @_;
1483 13 my $out_fh;
1484 my $thread;
1485  
1486 # Read the input file.
1487 13 ▶ if (defined($input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input) { } 13 0 13
1488 13 $input = path($input)->realpath();
1489 13 $thread = $input->slurp_utf8();
1490 } else {
1491 0 $thread = slurp(\*STDIN);
1492 }
1493  
1494 # Open the output file.
1495 13 ▶ if (defined($output)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output) { } 11 2 13
1496 11 $output = path($output)->absolute();
1497 11 $out_fh = $output->openw_utf8();
1498 } else {
1499 2 open($out_fh, '>&:raw:encoding(utf-8)', 'STDOUT');
1500 }
1501  
1502 # Do the work.
1503 13 $self->_parse_document($thread, $input, $out_fh, $output);
1504  
1505 # Clean up.
1506 13 close($out_fh);
1507 13 return;
1508 }
1509  
1510 # Convert thread to HTML and write it to the given output file. This is used
1511 # when the thread isn't part of the input tree but instead is intermediate
1512 # output from some other conversion process.
1513 #
1514 # $thread - Thread to spin
1515 # $input - Original input file path (for relative path and timestamps)
1516 # $input_type - One-word description of input type for the page footer
1517 # $output - Output file
1518 #
1519 # Returns: Resulting HTML
1520 2 ▶sub spin_thread_output {
Subroutine spin_thread_output: called CC=2 SCAR=7.0
Pod documented
1521 2 my ($self, $thread, $input, $input_type, $output) = @_;
1522 2 $input = path($input);
1523  
1524 # Open the output file.
1525 2 my $out_fh;
1526 2 ▶ if (defined($output)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output) { } 2 0 2
1527 2 $output = path($output)->absolute();
1528 2 $out_fh = $output->openw_utf8();
1529 } else {
1530 0 open($out_fh, '>&:raw:encoding(utf-8)', 'STDOUT');
1531 }
1532  
1533 # Do the work.
1534 2 $self->_parse_document($thread, $input, $out_fh, $output, $input_type);
1535  
1536 # Clean up and restore the working directory.
1537 2 close($out_fh);
1538 2 return;
1539 }
1540  
1541 ##############################################################################
1542 # Module return value and documentation
1543 ##############################################################################
1544  
1545 5 1;
1546 __END__
« DocKnot/Spin/Sitemap.pm ↑ Summary DocKnot/Update.pm »