DocKnot/Spin/Text.pm

filter: Statementstmt 88.5% 496 / 560 Branchbran 76.3% 174 / 228 Conditioncond 71.9% 113 / 157 MC/DCmcdc 47.2% 43 / 91 Subroutinesub 95.9% 71 / 74 Podpod 16.6% 4 / 24 totaltotal 79.4% 901 / 1134 CC 171 SCAR 38.3
CC
171
Cov
83%
_convert_document
152.2
_parse_subheaders
15.7
start
15.6
CRAP 45.9
« DocKnot/Spin.pm ↑ Summary DocKnot/Spin/Pointer.pm »
1 # Convert some particular text formats into HTML.
2 #
3 # This program is an ad hoc set of heuristics and tricks, attempting to
4 # convert a few text file formats that I commonly use into reasonable HTML.
5 # General text to XHTML conversions is impossible due to the wildly differing
6 # formats used by people when writing text, so this module doesn't try to
7 # solve the general problem. It's good enough to turn the FAQs I maintain
8 # into HTML documents, which is all that I need of it.
9 #
10 # SPDX-License-Identifier: MIT
11  
12 ##############################################################################
13 # Modules and declarations
14 ##############################################################################
15  
16 package App::DocKnot::Spin::Text v8.0.1;
17  
18 3 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use warnings FATAL => 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 ▶use vars qw($INDENT @INDENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 3 ▶use App::DocKnot;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use App::DocKnot::Util qw(print_fh);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Replace with the month names you want to use, if you don't want English.
30 3 our @MONTHS = qw(January February March April May June July August September
31 October November December);
32  
33 ##############################################################################
34 # Utility functions
35 ##############################################################################
36  
37 # Turns section numbers at the beginning of lines in a paragraph into links.
38 #
39 # $text - Text to format
40 #
41 # Returns: Text formatted as links to section numbers given by the numbers at
42 # the start of each line.
43 4 ▶sub _format_contents {
Subroutine _format_contents: called CC=1 SCAR=0.0
44 4 my ($text) = @_;
45 4 $text =~ s{
46 ^
47 (\s* ([\d.]+) [.\)] \s+ )
48 (.*?)
49 ([ \t]*\n)
50 }{$1<a href="#S$2">$3</a>$4}xmsg;
51 4 return $text;
52 }
53  
54 # Turns *some text* into <strong>some text</strong>, while trying to be
55 # careful to avoid other uses of wildcards.
56 #
57 # $string - Text to format
58 #
59 # Returns: Text with bold replaced with HTML markup.
60 235 ▶sub _format_bold {
Subroutine _format_bold: called CC=1 SCAR=0.0
61 235 my ($text) = @_;
62 235 $text =~ s{
63 (^|\s) [*] ( \w .*? \S ) [*] ([,.!?;\s])
64 }{$1<strong>$2</strong>$3}xmsg;
65 235 return $text;
66 }
67  
68 # Format a link. All whitespace in the link is treated as insignficant.
69 #
70 # $link - Link to format
71 #
72 # Returns: Link formatted as an HTML link, with the link anchor being the same
73 # as the link with any mailto: or news: removed.
74 11 ▶sub _format_url {
Subroutine _format_url: called CC=1 SCAR=0.0
75 11 my ($link) = @_;
76 11 my $text = $link;
77 11 $link = _smash(_unescape($link));
78 11 $text =~ s{ \A (?: mailto | news ): }{}xms;
79 11 return '&lt;<a href="' . $link . '">' . $text . '</a>&gt;';
80 }
81  
82 # Looks for URLs in <> or <URL:...> form and wraps a link around it. Assumes
83 # that < and > have already been escaped.
84 #
85 # $text - Text to format
86 #
87 # Returns: Text with any embedded links turned into proper HTML links.
88 227 ▶sub _format_urls {
Subroutine _format_urls: called CC=1 SCAR=0.0
89 227 my ($text) = @_;
90 227 $text =~ s{
91 &lt; (?:URL:)? ([a-z]{2,}:.+?) &gt;
92 }{
93 11 _format_url($1)
94 }xmsge;
95 227 return $text;
96 }
97  
98 # Remove an initial bullet from a paragraph, replacing it with a space.
99 #
100 # $string - Input string
101 #
102 # Returns: String with the bullet replaced with spaces.
103 74 ▶sub _remove_bullet {
Subroutine _remove_bullet: called CC=1 SCAR=0.0
104 74 my ($string) = @_;
105 74 $string =~ s{ \A (\s*) [-*o] (\s) }{$1 $2}xms;
106 74 return $string;
107 }
108  
109 # Removes an initial number on a paragraph, replacing it with spaces.
110 #
111 # $string - Input string
112 #
113 # Returns: String with the number replaced with spaces.
114 34 ▶sub _remove_number {
Subroutine _remove_number: called CC=1 SCAR=0.0
115 34 my ($string) = @_;
116 34 $string =~ s{
117 \A (\s*) (\d\d?[.\)]) (\s)
118 }{
119 34 $1 . q{ } x length($2) . $3
120 }xmse;
121 34 return $string;
122 }
123  
124 # Remove a constant prefix at the beginning of each line of a paragraph.
125 #
126 # $string - Input string
127 #
128 # Returns: String with the prefix removed from each line.
129 2 ▶sub _remove_prefix {
Subroutine _remove_prefix: called CC=1 SCAR=0.0
130 2 my ($string, $prefix) = @_;
131 2 $string =~ s{
132 ( (?:\A|\n) \s* ) ( \Q$prefix\E \s+ )
133 }{
134 18 $1 . q{ } x length($2)
135 }xmsge;
136 2 return $string;
137 }
138  
139 # Remove ASCII underlining from a section heading.
140 #
141 # $string - Input string
142 #
143 # Returns: String with the underlining removed.
144 19 ▶sub _remove_rule {
Subroutine _remove_rule: called CC=1 SCAR=0.0
145 19 my ($string) = @_;
146 19 $string =~ s{ \A [-=~]+ \n }{}xms;
147 19 return $string;
148 }
149  
150 # Remove all whitespace in a string.
151 #
152 # $string - Input string
153 #
154 # Returns: String with all whitespace removed.
155 11 ▶sub _smash {
Subroutine _smash: called CC=1 SCAR=0.0
156 11 my ($string) = @_;
157 11 $string =~ s{ \s }{}xmsg;
158 11 return $string;
159 }
160  
161 # Unescape &, <, and > characters.
162 #
163 # $text - Text to remove HTML escapes from.
164 #
165 # Returns: Text with HTML escapes changed back to their regular characters.
166 254 ▶sub _unescape {
Subroutine _unescape: called CC=1 SCAR=0.0
167 254 my ($text) = @_;
168 254 $text =~ s{ &gt; }{>}xmsg;
169 254 $text =~ s{ &lt; }{<}xmsg;
170 254 $text =~ s{ &amp; }{&}xmsg;
171 254 return $text;
172 }
173  
174 # Escapes &, <, and > characters found in a string.
175 268 ▶sub escape { local $_ = shift; s/&/&amp;/g; s/</&lt;/g; s/>/&gt;/g; $_ }
Subroutine escape: called CC=1 SCAR=0.0
Pod undocumented
176  
177 # Returns the length of the indentation of a line or paragraph.
178 599 ▶sub indent { $_[0] =~ /^(\s*)/; length $1 }
Subroutine indent: called CC=1 SCAR=0.0
Pod undocumented
179  
180 # Returns the number of lines in a paragraph, not counting trailing blanks.
181 31 ▶sub lines { local $_ = shift; s/\s+$/\n/; tr/\n// }
Subroutine lines: called CC=1 SCAR=0.0
Pod undocumented
182  
183 # Returns a nicely formatted "Last modified" string from an RCS/CVS Id.
184 1 ▶sub modified_id {
Subroutine modified_id: called CC=2 SCAR=7.0
Pod undocumented
185 1 my $id = shift;
186 1 my ($version, $date) = (split (' ', $id))[2,3];
187 1 my ($year, $month, $day) = split (m%[/-]%, $date);
188 1 $day =~ s/^0//;
189 1 ▶ my $revision = ($version =~ /\./) ? " (revision $version)" : '';
Branch: 1 branch
BranchTrueFalseTotal
$version =~ /\./u ? : 1 0 1
190 1 'Last modified '. $MONTHS[$month - 1] . ' ' . $day . ', ' . $year
191 . $revision;
192 }
193  
194 # The same, but from a UNIX timestamp.
195 3 ▶sub modified_timestamp {
Subroutine modified_timestamp: called CC=1 SCAR=0.0
Pod undocumented
196 3 my $timestamp = shift;
197 3 my ($year, $month, $day) = (localtime $timestamp)[5, 4, 3];
198 3 $year += 1900;
199 3 'Last modified ' . $MONTHS[$month] . ' ' . $day . ', ' . $year;
200 }
201  
202 # Strip a number of characters of indentation from a line that's given by the
203 # second argument, returning the result. Used to strip leading indentation
204 # off of <pre> text so that it isn't indented excessively just because in the
205 # text version it had to be indented relative to the surrounding text.
206 20 ▶sub strip_indent {
Subroutine strip_indent: called CC=3 SCAR=11.0
Pod undocumented
207 20 local $_ = shift;
208 20 my $indent = shift;
209 20 ▶ if (defined $indent && $indent > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $indent and $indent > 0) 12 8 20
Condition66%2/3
defined $indent and $indent > 0
!ll&&!rl&&r
0812
MC/DC50%1/2
defined $indent and $indent > 0
defined $indent $indent > 0
Truth table66%2/3
A and B
A= defined $indentB= $indent > 0
ABresult
0X0
100
111
210 12 s/^ {$indent}//gm;
211 }
212 20 $_;
213 }
214  
215 # Replace tabs with spaces.
216 253 ▶sub untabify {
Subroutine untabify: called CC=2 SCAR=7.6
Pod undocumented
217 253 local $_ = shift;
218 0 ▶ 1 while s/^(.*?)(\t+)/' ' x (length ($2) * 8 - length ($1) % 8)/me;
Branch: 1 branch
BranchTrueFalseTotal
s/^(.*?)(\t+)/" " x (length($2) * 8 - length($1) % 8);/emu and do { '???' } 0 253 253
219 253 $_;
220 }
221  
222 # Remove whitespace at the beginning and end of a string.
223 8 ▶sub whitechomp { local $_ = shift; s/^\s+//; s/\s+$//; $_ }
Subroutine whitechomp: called CC=1 SCAR=0.0
Pod undocumented
224  
225 ##############################################################################
226 # Classification functions
227 ##############################################################################
228  
229 # Whether a paragram is composed entirely of bullet items. Take some care to
230 # avoid returning true for paragraphs that consist of a single bullet entry,
231 # since we want to handle those separately to wrap them in paragraph tags.
232 #
233 # $paragraph - Paragraph to classify
234 #
235 # Returns: True if so, false otherwise
236 224 ▶sub _is_allbullet {
Subroutine _is_allbullet: called CC=6 SCAR=18.0
237 224 my ($paragraph) = @_;
238 224 my @lines = split(m{ \n }xms, $paragraph);
239 224 ▶ return if $lines[0] !~ m{ \A (\s* [-*o] \s) \S }xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $lines[0] =~ / \A (\s* [-*o] \s) \S /msux 178 46 224
240 46 my $bullet = $1;
241 46 my $space = $bullet;
242 46 $space =~ s { [-*o] }{ }xms;
243 46 my $bullets = 0;
244 46 for my $line (@lines) {
245 138 ▶ next if $line !~ m{ \S }xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ / \S /msux 0 138 138
246 138 ▶ return if $line !~ m{ \A (?: \Q$bullet\E | \Q$space\E ) \S }xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ / \A (?: \Q$bullet\E | \Q$space\E ) \S /msux 0 138 138
247 138 ▶ if ($line =~ m{ \A \Q$bullet\E }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ / \A \Q$bullet\E /msux) 74 64 138
248 74 $bullets++;
249 }
250 }
251 46 return $bullets > 1;
252 }
253  
254 # Whether every line of a paragraph is a numbered item with a simple number.
255 #
256 # $paragraph - Paragraph to classify
257 #
258 # Returns: True if so, false otherwise
259 222 ▶sub _is_allnumbered {
Subroutine _is_allnumbered: called CC=1 SCAR=0.0
260 222 my ($paragraph) = @_;
261 222 return $paragraph =~ m{ \A (\s* \d\d?[.\)] [ ] \N* \n){2,} \s* \z }xms;
262 }
263  
264 # Whether a line is all capital letters.
265 #
266 # $line - Line to classify
267 #
268 # Returns: True if so, false otherwise
269 3 ▶sub _is_allcaps {
Subroutine _is_allcaps: called CC=1 SCAR=0.0
270 3 my ($line) = @_;
271 3 return $line !~ m{ [^[:upper:]\d\s\"\(\),:.!/?-] }xms;
272 }
273  
274 # Whether a paragraph is broken into a series of short lines or a series of
275 # lines without internal space. The last line of the paragraph doesn't matter
276 # for this determination.
277 #
278 # $paragraph - Paragraph to classify
279 #
280 # Returns: True if so, false otherwise
281 224 ▶sub _is_broken {
Subroutine _is_broken: called CC=4 SCAR=13.9
282 224 my ($paragraph) = @_;
283 224 $paragraph =~ s{ \s* \z }{\n}xms;
284 224 my @lines = split(m{ \n }xms, $paragraph);
285 224 ▶ return if @lines == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @lines == 1 43 181 224
286 181 pop(@lines);
287 181 ▶ return 1 if grep { length($_) < 40 } @lines;
Branch: 1 branch
BranchTrueFalseTotal
if grep {length $_ < 40;} @lines 7 174 181
288 174 my $short = grep { length($_) < 60 } @lines;
289 174 ▶ return 1 if $short >= int(@lines / 2) + 1;
Branch: 1 branch
BranchTrueFalseTotal
if $short >= int(@lines / 2) + 1 0 174 174
290 174 return $paragraph =~ m{ \A (?: \s* \S+ [ \t]* \n )+ \z }xms;
291 }
292  
293 # Whether a paragraph is a bullet item.
294 #
295 # $paragraph - Paragraph to classify
296 #
297 # Returns: True if so, false otherwise
298 252 ▶sub _is_bullet {
Subroutine _is_bullet: called CC=1 SCAR=0.0
299 252 my ($paragraph) = @_;
300 252 return $paragraph =~ m{ \A \s* [-o*] \s }xms;
301 }
302  
303 # Whether a line is centered (in 74 columns). Also require at least 10 spaces
304 # of whitespace so that we don't catch accidentally centered paragraph lines
305 # by mistake.
306 #
307 # $line - Line to classify
308 #
309 # Returns: True if so, false otherwise
310 12 ▶sub _is_centered {
Subroutine _is_centered: called CC=3 SCAR=11.0
311 12 my ($line) = @_;
312 12 ▶ return if $line !~ m{ \A (\s+) (.+) }xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ / \A (\s+) (.+) /msux 5 7 12
313 7 my ($space, $text) = ($1, $2);
314 7 ▶ return if abs(74 - length($text) - length($space) * 2) >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if abs 74 - length($text) - length($space) * 2 >= 2 0 7 7
315 7 return length(untabify($space)) >= 8;
316 }
317  
318 # Whether a paragraph is a content listing.
319 #
320 # $paragraph - Paragraph to classify
321 #
322 # Returns: True if so, false otherwise
323 38 ▶sub _is_contents {
Subroutine _is_contents: called CC=1 SCAR=0.0
324 38 my ($paragraph) = @_;
325 38 return $paragraph =~ m{ \A (?: \s* [\d.]+[.\)] [ \t] \N* \n)+ \s* \z }xms;
326 }
327  
328 # Whether a paragraph looks like a title and a description. Allows for
329 # multiple titles.
330 #
331 # $paragraph - Paragraph to classify
332 #
333 # Returns: True if so, false otherwise
334 142 ▶sub _is_description {
Subroutine _is_description: called CC=2 SCAR=6.9
335 142 my ($paragraph) = @_;
336 142 ▶ return if $paragraph !~ m{
Branch: 1 branch
BranchTrueFalseTotal
unless $paragraph =~ / \A (\s*) \S \N* \n # title (1 is indent) (?: \1 \S \N* \n)* # possibly more than one (\s+) \S \N* \n # first line of description (2 is indent) (?: \2 \S \N* \n)* # subsequent lines \s* \z /msux 118 24 142
337 \A
338 (\s*) \S \N* \n # title (1 is indent)
339 (?: \1 \S \N* \n)* # possibly more than one
340 (\s+) \S \N* \n # first line of description (2 is indent)
341 (?: \2 \S \N* \n)* # subsequent lines
342 \s* \z
343 }xms;
344 24 return length($1) < length($2);
345 }
346  
347 # Whether a line is a digest divider.
348 #
349 # $line - Line to classify
350 #
351 # Returns: True if so, false otherwise
352 263 ▶sub _is_divider {
Subroutine _is_divider: called CC=1 SCAR=0.0
353 263 my ($line) = @_;
354 263 return $line =~ m{ \A -{30} \s* \z }xms;
355 }
356  
357 # Whether a line is an RFC 2822 header.
358 #
359 # $line - Line to classify
360 #
361 # Returns: True if so, false otherwise
362 5 ▶sub _is_header {
Subroutine _is_header: called CC=2 SCAR=6.9
363 5 my ($line) = @_;
364 5 ▶ return if $line =~ m{ \A [\w-]+: \s+ \N }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ / \A [\w-]+: \s+ \N /msux 3 2 5
365 }
366  
367 # Whether a paragraph is a heading. This is all about heuristics and guesses,
368 # and there are a number of other things we could confuse for headings, so we
369 # have to be careful.
370 #
371 # If it's a single line and outdented from the baseline, it's probably a
372 # heading.
373 #
374 # If it's at the baseline, check to see if it looks like a heading and either
375 # it's in all caps or there is a rule underneath it. If we haven't seen a
376 # baseline, be more accepting about headers.
377 #
378 # If we're inside a contents block, be even more careful and disallow numbered
379 # things that look like a heading unless they're outdented.
380 #
381 # Unlike most of the classification functions, this is a regular method, since
382 # it needs access to the parsing state.
383 #
384 # $paragraph - Paragraph to classify
385 #
386 # Returns: True if a heading, false otherwise
387 243 ▶sub _is_heading {
Subroutine _is_heading: called CC=14 SCAR=26.5
388 243 my ($self, $paragraph) = @_;
389 243 $paragraph = _unescape($paragraph);
390 243 my $indent = indent($paragraph);
391 243 my $nobase = !defined($self->{baseline});
392 243 ▶ my $outdented = defined($self->{baseline}) && $indent < $self->{baseline};
Condition100%3/3
defined $self->{'baseline'} && $indent < $self->{'baseline'}
!ll&&!rl&&r
551817
MC/DC100%2/2
defined $self->{'baseline'} && $indent < $self->{'baseline'}
defined $self->{'baseline'} $indent < $self->{'baseline'}
Truth table100%3/3
A && B
A= defined $self->{'baseline'}B= $indent < $self->{'baseline'}
ABresult
0X0
100
111
393  
394 # Numbered lines inside the contents section are definitely not headings.
395 243 my $numbered = $paragraph =~ m{ \A [\d.]+[.\)] \s }xms;
396 243 ▶ return if !$outdented && $self->{contents} && $numbered;
Branch: 1 branch
BranchTrueFalseTotal
if not $outdented and $self->{"contents"} and $numbered 4 239 243
Condition83%5/6
not $outdented and $self->{"contents"} and $numbered
!ll&&!rl&&r
23904
not $outdented and $self->{"contents"}
!ll&&!rl&&r
72324
MC/DC66%2/3
not $outdented and $self->{"contents"} and $numbered
not $outdented $self->{"contents"} $numbered
Truth table75%3/4
not(A) and B and C
A= not $outdentedB= $self->{"contents"}C= $numbered
ABCresult
0XX0
10X0
1100
1111
397  
398 # Outdented single lines are headings as long as they're either short or
399 # contain at least two words.
400 239 ▶ if ($outdented && lines($paragraph) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outdented and lines($paragraph) == 1) 7 232 239
Condition66%2/3
$outdented and lines($paragraph) == 1
!ll&&!rl&&r
23207
MC/DC50%1/2
$outdented and lines($paragraph) == 1
$outdented lines($paragraph) == 1
Truth table66%2/3
A and B
A= $outdentedB= lines($paragraph) == 1
ABresult
0X0
100
111
401 7 ▶ return 1 if $paragraph =~ m{ \S \s \S }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $paragraph =~ / \S \s \S /msux 2 5 7
402 5 ▶ return 1 if length($paragraph) < 30;
Branch: 1 branch
BranchTrueFalseTotal
if length $paragraph < 30 5 0 5
403 }
404  
405 # Indented lines are never headings.
406 232 ▶ return if defined($INDENT) && $indent > $INDENT;
Branch: 1 branch
BranchTrueFalseTotal
if defined $App::DocKnot::Spin::Text::INDENT and $indent > $App::DocKnot::Spin::Text::INDENT 29 203 232
Condition100%3/3
defined $App::DocKnot::Spin::Text::INDENT and $indent > $App::DocKnot::Spin::Text::INDENT
!ll&&!rl&&r
2517829
MC/DC100%2/2
defined $App::DocKnot::Spin::Text::INDENT and $indent > $App::DocKnot::Spin::Text::INDENT
defined $App::DocKnot::Spin::Text::INDENT $indent > $App::DocKnot::Spin::Text::INDENT
Truth table100%3/3
A and B
A= defined $App::DocKnot::Spin::Text::INDENTB= $indent > $App::DocKnot::Spin::Text::INDENT
ABresult
0X0
100
111
407  
408 # Lines of at most 31 characters ending in a word character or closing
409 # quote or paren are headings if they're underlined.
410 203 ▶ return 1 if $paragraph =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if $paragraph =~ m[ \A \s* [ \w\"\(\),:./&-]{0,30} [\w\"\)] \s* \n [-=~]+ \s* \z ]msux 0 203 203
411 \A \s*
412 [ \w\"\(\),:./&-]{0,30} [\w\"\)] \s* \n
413 [-=~]+ \s*
414 \z
415 }xms;
416  
417 # All-uppercase lines of at most 31 characters ending in an uppercase
418 # character, digit, or closing quote or paren are headings.
419 203 ▶ return 1 if $paragraph =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if $paragraph =~ m[ \A \s* [ [:upper:]\d\"\(\),:./&-]{0,30} [[:upper:]\d\"\)] \s* \n \z ]msux 10 193 203
420 \A \s*
421 [ [:upper:]\d\"\(\),:./&-]{0,30} [[:upper:]\d\"\)]
422 \s* \n
423 \z
424 }xms;
425  
426 # If there is no baseline, assume single lines of at most 34 characters
427 # with no unexpected characters are headings.
428 193 ▶ return $nobase && $paragraph =~ m{
Condition100%3/3
$nobase && $paragraph =~ m[ \A \s* [ \w\"\(\),:./&-]{0,33} [\w\"\)] \s* \n \z ]msux
!ll&&!rl&&r
143482
MC/DC100%2/2
$nobase && $paragraph =~ m[ \A \s* [ \w\"\(\),:./&-]{0,33} [\w\"\)] \s* \n \z ]msux
$nobase $paragraph =~ m[ \A \s* [ \w\"\(\),:./&-]{0,33} [\w\"\)] \s* \n \z ]msux
Truth table100%3/3
A && B
A= $nobaseB= $paragraph =~ m[ \A \s* [ \w\"\(\),:./&-]{0,33} [\w\"\)] \s* \n \z ]msux
ABresult
0X0
100
111
429 \A \s*
430 [ \w\"\(\),:./&-]{0,33} [\w\"\)]
431 \s* \n
432 \z
433 }xms;
434 }
435  
436 # Whether a line is an RCS/CVS Id string that has been expanded.
437 #
438 # $line - Line to classify
439 #
440 # Returns: True if so, false otherise
441 6 ▶sub _is_id {
Subroutine _is_id: called CC=1 SCAR=0.0
442 6 my ($line) = @_;
443 6 return $line =~ m{ \A \s* [\$]Id: \N+ [\$] \s* \z }xms;
444 }
445  
446 # Whether a paragraph should be a literal paragraph, decided based on whether
447 # it has internal whitespace.
448 #
449 # $paragraph - Paragraph to classify
450 #
451 # Returns: True if so, false otherwise
452 246 ▶sub _is_literal {
Subroutine _is_literal: called CC=1 SCAR=0.0
453 246 my ($paragraph) = @_;
454 246 return $paragraph =~ m{
455 \A [ \t]*
456 \S \N*
457 (?: [^.?!\"\)\]:*_\n] [ ] [ ] | [ ] [ ] [ ] | \t )
458 \S
459 }xms;
460 }
461  
462 # Whether a paragarph is part of a numbered list.
463 #
464 # $paragraph - Paragraph to classify
465 #
466 # Returns: The number if the paragraph is a numbered list element
467 # undef otherwise
468 176 ▶sub _is_numbered {
Subroutine _is_numbered: called CC=2 SCAR=6.9
469 176 my ($paragraph) = @_;
470 176 ▶ if ($paragraph =~ m{ \A \s* (\d\d?) [.\)] \s }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paragraph =~ / \A \s* (\d\d?) [.\)] \s /msux) { } 34 142 176
471 34 return $1;
472 } else {
473 142 return undef;
474 }
475 }
476  
477 # Whether a paragraph has inconsistent indentation.
478 #
479 # $paragraph - Paragraph to classify
480 #
481 # Returns: True if so, false otherwise
482 162 ▶sub _is_offset {
Subroutine _is_offset: called CC=1 SCAR=0.4
483 162 my ($paragraph) = @_;
484  
485 # Strip off a leading bullet or number and consider it whitespace in
486 # making this check.
487 0 $paragraph =~ s{ \A (\s* (?: \d\d? ) [.\)] \s) }{ q{ } x length($1) }xmse;
488 0 $paragraph =~ s{ \A (\s* [-*o] \s) }{ q{ } x length($1) }xmse;
489  
490 # Now, return true if the indentation isn't consistent.
491 162 return $paragraph !~ m{ \A (\s*) \S \N* \n (\1 \S \N* \n)* \s* \z }xms;
492 }
493  
494 # Whether a paragraph is quoted. Requires the paragraph be at least two
495 # lines, since otherwise we cannot detect a common prefix.
496 #
497 # $paragraph - Paragraph to classify
498 #
499 # Returns: The quote character if it is quoted
500 # undef otherwise
501 178 ▶sub _is_quoted {
Subroutine _is_quoted: called CC=2 SCAR=6.9
502 178 my ($paragraph) = @_;
503 178 ▶ return if $paragraph !~ m{
Branch: 1 branch
BranchTrueFalseTotal
unless $paragraph =~ / \A \s* ([^\w\s\"\']) \s* \N* \n (?: \s* \1 \s* \N* \n )+ \z /msux 175 3 178
504 \A \s*
505 ([^\w\s\"\']) \s* \N* \n
506 (?: \s* \1 \s* \N* \n )+
507 \z
508 }xms;
509 3 return $1;
510 }
511  
512 # Whether a line or paragraph is a rule.
513 #
514 # $paragraph - Paragraph or line to classify
515 #
516 # Returns: True if so, false otherwise
517 274 ▶sub _is_rule {
Subroutine _is_rule: called CC=1 SCAR=0.0
518 274 my ($paragraph) = @_;
519 274 return $paragraph =~ m{ \A \s* [-=] [-=\s]* \z }xms;
520 }
521  
522 # Whether a paragraph is a simple indented URL (already converted to a real
523 # link, so call after urlify).
524 #
525 # $paragraph - Paragraph to classify
526 #
527 # Returns: True if so, false otherwise
528 24 ▶sub _is_url {
Subroutine _is_url: called CC=1 SCAR=0.0
529 24 my ($paragraph) = @_;
530 24 return $paragraph =~ m{
531 \A \s*
532 &lt; <a [ ] href.+> \S+ </a> &gt;
533 \s* \z
534 }xms;
535 }
536  
537 # Whether a paragraph ends with a sentence. As a special case, a URL counts
538 # as a sentence so that we don't wrap <pre> around URLs.
539 #
540 # $paragraph - Paragraph to classify
541 #
542 # Returns: True if so, false otherwise
543 24 ▶sub _is_sentence {
Subroutine _is_sentence: called CC=4 SCAR=14.1
544 24 my ($paragraph) = @_;
545 24 ▶ return 1 if $paragraph =~ m{ \S [.?!] [\)\]\"]? \s* \z }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $paragraph =~ / \S [.?!] [\)\]\"]? \s* \z /msux 0 24 24
546 24 ▶ return 1 if $paragraph =~ m{ ^ \s* \w \N* \s \S+: \s* \z }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $paragraph =~ / ^ \s* \w \N* \s \S+: \s* \z /msux 0 24 24
547 24 ▶ return 1 if _is_url($paragraph);
Branch: 1 branch
BranchTrueFalseTotal
if _is_url($paragraph) 9 15 24
548 15 return 0;
549 }
550  
551 # Whether a paragraph is the start of a signature block, defined to be a
552 # paragraph whose first line is exactly "-- ".
553 #
554 # $paragraph - Paragraph to classify
555 #
556 # Returns: True if so, false otherwise
557 263 ▶sub _is_signature {
Subroutine _is_signature: called CC=1 SCAR=0.0
558 263 my ($paragraph) = @_;
559 263 return $paragraph =~ m{ \A -- [ ] \n }xms;
560 }
561  
562 ##############################################################################
563 # Input and output
564 ##############################################################################
565  
566 # Put a line back in the input buffer because we aren't going to consume it.
567 # This works in conjunction with _next_line and _next_paragraph.
568 #
569 # $line - Line to buffer
570 332 ▶sub _buffer_line {
Subroutine _buffer_line: called CC=2 SCAR=7.6
571 332 my ($self, $line) = @_;
572 332 ▶ if (defined($self->{buffer})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'buffer'}) { } 0 332 332
573 0 $self->{buffer} .= $line;
574 } else {
575 332 $self->{buffer} = $line;
576 }
577 }
578  
579 # Output some text, adding any preserved whitespace after any closing tags.
580 #
581 # @data - HTML to output
582 501 ▶sub _output {
Subroutine _output: called CC=2 SCAR=6.9
583 501 my ($self, @data) = @_;
584 501 ▶ if ($self->{whitespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"whitespace"}) 274 227 501
585 274 $data[0] =~ s{
586 \A (\s* (?: </(?!body)[^>]+> \s*)* )
587 }{$1$self->{whitespace}}xms;
588 274 $self->{whitespace} = q{};
589 }
590 501 print_fh($self->{out_fh}, $self->{output}, @data);
591 501 return;
592 }
593  
594 # Read in the next line supporting buffering.
595 #
596 # Returns: The next line, or undef at end of file
597 382 ▶sub _next_line {
Subroutine _next_line: called CC=2 SCAR=6.9
598 382 my ($self) = @_;
599 382 my $line;
600 382 ▶ if (defined($self->{buffer})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'buffer'}) { } 320 62 382
601 320 $line = $self->{buffer};
602 320 $self->{buffer} = undef;
603 } else {
604 62 $line = readline($self->{in_fh});
605 }
606 382 return $line;
607 }
608  
609 # Read a paragraph, including all of its trailing blank lines. By default,
610 # lines with nothing but whitespace are paragraph dividers.
611 #
612 # $require_blank - If true, only completely blank lines are dividers
613 286 ▶sub _next_paragraph {
Subroutine _next_paragraph: called CC=7 SCAR=19.7
614 286 my ($self, $require_blank) = @_;
615 286 my $paragraph = $self->_next_line();
616 286 my $in_fh = $self->{in_fh};
617 286 ▶ my $nonblank_line = $require_blank ? qr{ [^\n] }xms : qr{ \S }xms;
Branch: 1 branch
BranchTrueFalseTotal
$require_blank ? : 0 286 286
618  
619 286 my $line;
620 286 ▶ while (defined($line = <$in_fh>) && $line =~ $nonblank_line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $in_fh) and $line =~ /$nonblank_line/u) 643 286 929
Condition100%3/3
defined($line = readline $in_fh) and $line =~ /$nonblank_line/u
!ll&&!rl&&r
12274643
MC/DC100%2/2
defined($line = readline $in_fh) and $line =~ /$nonblank_line/u
defined($line = readline $in_fh) $line =~ /$nonblank_line/u
Truth table100%3/3
A and B
A= defined($line = readline $in_fh)B= $line =~ /$nonblank_line/u
ABresult
0X0
100
111
621 643 $paragraph .= $line;
622 }
623 286 ▶ if (defined($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 274 12 286
624 274 $paragraph .= $line;
625 }
626 286 ▶ while (defined($line = <$in_fh>) && $line =~ m{ \A \s* \z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $in_fh) and $line =~ / \A \s* \z /msux) 0 286 286
Condition66%2/3
defined($line = readline $in_fh) and $line =~ / \A \s* \z /msux
!ll&&!rl&&r
122740
MC/DC0%0/2
defined($line = readline $in_fh) and $line =~ / \A \s* \z /msux
defined($line = readline $in_fh) $line =~ / \A \s* \z /msux
Truth table66%2/3
A and B
A= defined($line = readline $in_fh)B= $line =~ / \A \s* \z /msux
ABresult
0X0
100
111
627 0 $paragraph .= $line;
628 }
629 286 $self->_buffer_line($line);
630 286 return $paragraph;
631 }
632  
633 # Read from the input file descriptor, skipping blank lines.
634 7 ▶sub _skip_blank_lines {
Subroutine _skip_blank_lines: called CC=3 SCAR=11.0
635 7 my ($self) = @_;
636 7 my $line;
637 7 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
defined $line and not $line =~ / \S /msux and do { do { $line = $self->_next_line } } 7 7 14
Condition66%2/3
defined $line and not $line =~ / \S /msux
!ll&&!rl&&r
077
MC/DC50%1/2
defined $line and not $line =~ / \S /msux
defined $line not $line =~ / \S /msux
Truth table66%2/3
A and not(B)
A= defined $lineB= not $line =~ / \S /msux
ABresult
0X0
100
111
638 14 $line = $self->_next_line();
639 } while (defined($line) && $line !~ m{ \S }xms);
640 7 $self->_buffer_line($line);
641 }
642  
643 # Read from the input file descriptor, skipping blank lines and rules.
644 17 ▶sub _skip_blank_lines_and_rules {
Subroutine _skip_blank_lines_and_rules: called CC=4 SCAR=13.9
645 17 my ($self) = @_;
646 17 my $line;
647 17 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
defined $line and !($line =~ / \S /msux) || _is_rule($line) and do { do { $line = $self->_next_line } } 6 17 23
Condition83%5/6
defined $line and !($line =~ / \S /msux) || _is_rule($line)
!ll&&!rl&&r
0176
not $line =~ / \S /msux or _is_rule($line)
l!l&&r!l&&!r
5117
MC/DC66%2/3
defined $line and !($line =~ / \S /msux) || _is_rule($line)
defined $line not $line =~ / \S /msux _is_rule($line)
Truth table75%3/4
A and not(B) or C
A= defined $lineB= not $line =~ / \S /msuxC= _is_rule($line)
ABCresult
0XX0
1000
11X1
1011
648 23 $line = $self->_next_line();
649 } while (defined($line) && ($line !~ m{ \S }xms || _is_rule($line)));
650 17 $self->_buffer_line($line);
651 }
652  
653 ##############################################################################
654 # HTML constructors
655 ##############################################################################
656  
657 # Output the header of the HTML document. We claim "transitional" XHTML 1.0
658 # compliance; we can't claim strict solely because we use the value attribute
659 # in <li> in the absence of widespread implementation of CSS Level 2. Assume
660 # English output.
661 #
662 # $header_ref - Additional information from the headers of the text document
663 6 ▶sub _output_header {
Subroutine _output_header: called CC=8 SCAR=21.4
664 6 my ($self, $header_ref) = @_;
665 $self->_output(
666 '<?xml version="1.0" encoding="utf-8"?>', "\n",
667 '<!DOCTYPE html', "\n",
668 ' PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"', "\n",
669 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">', "\n",
670 "\n",
671 '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">',
672 "\n",
673 '<head>', "\n",
674 6 ▶ q{ }, title($self->{title} // $header_ref->{title} // q{}), "\n",
Condition60%3/5
($self->{"title"} // $header_ref->{"title"}) // ""
l!l
60
$self->{"title"} // $header_ref->{"title"}
l!l&&r!l&&!r
150
MC/DC0%0/2
($self->{"title"} // $header_ref->{"title"}) // ""
$self->{"title"} $header_ref->{"title"}
Truth table66%2/3
A // B
A= $self->{"title"}B= $header_ref->{"title"}
ABresult
1X1
011
000
675 );
676 6 ▶ if ($self->{style}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"style"}) 6 0 6
677 6 $self->_output(q{ }, style($self->{style}), "\n");
678 }
679 $self->_output(
680 6 q{ },
681 '<meta http-equiv="content-type" content="text/html; charset=utf-8"',
682 " />\n",
683 );
684 6 ▶ if ($self->{sitemap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sitemap"}) 2 4 6
685 2 ▶ if (defined($self->{output}) && defined($self->{out_path})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"output"} and defined $self->{"out_path"}) 2 0 2
Condition33%1/3
defined $self->{"output"} and defined $self->{"out_path"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self->{"output"} and defined $self->{"out_path"}
defined $self->{"output"} defined $self->{"out_path"}
Truth table33%1/3
A and B
A= defined $self->{"output"}B= defined $self->{"out_path"}
ABresult
0X0
100
111
686 2 my $page = $self->{out_path}->relative($self->{output});
687 2 $self->_output($self->{sitemap}->links($page));
688 }
689 }
690 6 $self->_output("</head>\n\n");
691 6 ▶ if ($header_ref->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_ref->{"id"}) 1 5 6
692 1 $self->_output(comment($header_ref->{id}), "\n");
693 }
694  
695 # Add a generator comment.
696 6 my $date = strftime('%Y-%m-%d %T -0000', gmtime());
697 6 my $version = $App::DocKnot::VERSION;
698 6 $self->_output(comment("Converted by DocKnot $version on $date"), "\n\n");
699 }
700  
701 # An XML comment.
702 7 ▶sub comment {
Subroutine comment: called CC=1 SCAR=0.0
Pod undocumented
703 7 my @data = @_;
704 7 my $data = join ('', @data);
705 7 '<!-- ' . $data . ' -->';
706 }
707  
708 # A link to a CSS style sheet.
709 6 ▶sub style {
Subroutine style: called CC=1 SCAR=0.0
Pod documented
710 6 my $style = shift;
711 6 qq(<link rel="stylesheet" href="$style" type="text/css" />);
712 }
713  
714 # Wrap a container around data, keeping the tags on the same line.
715 78 ▶sub container {
Subroutine container: called CC=1 SCAR=0.0
Pod undocumented
716 78 my ($tag, @data) = @_;
717 78 my $data = join ('', @data);
718 78 $data = '<' . $tag . '>' . $data;
719 78 $tag =~ s/ .*//;
720 78 $data =~ s%(\s*)$%</$tag>$1%;
721 78 $data;
722 }
723  
724 # Output a list item. Takes the indentation, the item, and an optional third
725 # argument, which if specified is the number to use for the item (using the
726 # value attribute, which for some reason is deprecated under HTML 4.0 without
727 # any viable alternative for what I use it for).
728 108 ▶sub li {
Subroutine li: called CC=5 SCAR=17.8
Pod undocumented
729 108 my ($indent, $data, $value) = @_;
730 108 ▶ $indent = 0 unless defined $indent;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $indent 0 108 108
731 108 my $output = '';
732 108 ▶ if (@INDENT && $INDENT[0][0] eq 'li') {
Branch: 1 branch
BranchTrueFalseTotal
if (@App::DocKnot::Spin::Text::INDENT and $App::DocKnot::Spin::Text::INDENT[0][0] eq "li") 0 108 108
Condition33%1/3
@App::DocKnot::Spin::Text::INDENT and $App::DocKnot::Spin::Text::INDENT[0][0] eq "li"
!ll&&!rl&&r
01080
MC/DC0%0/2
@App::DocKnot::Spin::Text::INDENT and $App::DocKnot::Spin::Text::INDENT[0][0] eq "li"
@App::DocKnot::Spin::Text::INDENT $App::DocKnot::Spin::Text::INDENT[0][0] eq "li"
Truth table33%1/3
A and B
A= @App::DocKnot::Spin::Text::INDENTB= $App::DocKnot::Spin::Text::INDENT[0][0] eq "li"
ABresult
0X0
100
111
733 0 $output .= "</li>\n";
734 0 shift @INDENT;
735 }
736 108 unshift (@INDENT, [ 'li', $indent ]);
737 108 ▶ my $tag = defined $value ? qq(<li value="$value">\n) : "<li>\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 34 74 108
738 108 $output . $tag . $data;
739 }
740  
741 # Wrap a container around data, preserving trailing blank lines outside and
742 # putting the tags on lines of their own.
743 205 ▶sub paragraph {
Subroutine paragraph: called CC=2 SCAR=7.0
Pod undocumented
744 205 my ($tag, @data) = @_;
745 205 my $data = join ('', @data);
746 205 ▶ $data .= "\n" unless ($data =~ /\n$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $data =~ /\n$/u 0 205 205
747 205 '<' . $tag . ">\n" . $data . '</' . $tag . ">\n";
748 }
749  
750 # Multiparagraph structure is maintained based on indentation level. The
751 # global variable @INDENT holds a stack of pairs of block elements and their
752 # corresponding indentation levels. The possible structure elements are dl,
753 # dd, ul, ol, li, and blockquote.
754 #
755 # This function is used to start or end block structure elements. It closes
756 # any pending open structure elements with an indent level greater than the
757 # indentation level given, and then closes any open structure elements with an
758 # indentation level equal to the one given if a new structure element is given
759 # and it is different than the open one. Then, if a structure element is
760 # given, open a new block structure element with that indentation.
761 #
762 # One can pass attributes in for the opening tag; anything after a space will
763 # be stripped out for determining the close tag.
764 175 ▶sub start {
Subroutine start: called CC=15 SCAR=27.5
Pod undocumented
765 175 my ($indent, $tag, $data) = @_;
766 175 ▶ $indent = 0 unless defined $indent;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $indent 19 156 175
767 175 ▶ my $e = $tag || '';
Condition100%2/2
$tag || ''
l!l
11956
MC/DC100%1/1
$tag || ''
$tag
Truth table100%2/2
A
A= $tag
Aresult
11
00
768 175 $e =~ s/ .*//;
769 175 ▶ $data = '' unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 164 11 175
770 175 my $output = '';
771 175 ▶ while (@INDENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (@App::DocKnot::Spin::Text::INDENT) 227 81 308
772 227 ▶ last if ($INDENT[0][1] < $indent);
Branch: 1 branch
BranchTrueFalseTotal
if $App::DocKnot::Spin::Text::INDENT[0][1] < $indent 0 227 227
773 227 ▶ last if ($tag && $INDENT[0][1] == $indent && $INDENT[0][0] eq $tag);
Branch: 1 branch
BranchTrueFalseTotal
if $tag and $App::DocKnot::Spin::Text::INDENT[0][1] == $indent and $App::DocKnot::Spin::Text::INDENT[0][0] eq $tag 94 133 227
Condition83%5/6
$tag and $App::DocKnot::Spin::Text::INDENT[0][1] == $indent and $App::DocKnot::Spin::Text::INDENT[0][0] eq $tag
!ll&&!rl&&r
399494
$tag and $App::DocKnot::Spin::Text::INDENT[0][1] == $indent
!ll&&!rl&&r
390188
MC/DC66%2/3
$tag and $App::DocKnot::Spin::Text::INDENT[0][1] == $indent and $App::DocKnot::Spin::Text::INDENT[0][0] eq $tag
$tag $App::DocKnot::Spin::Text::INDENT[0][1] == $indent $App::DocKnot::Spin::Text::INDENT[0][0] eq $tag
Truth table75%3/4
A and B and C
A= $tagB= $App::DocKnot::Spin::Text::INDENT[0][1] == $indentC= $App::DocKnot::Spin::Text::INDENT[0][0] eq $tag
ABCresult
0XX0
10X0
1100
1111
774 133 ▶ last if ($INDENT[0][1] == $indent && !$tag && $INDENT[0][0] ne 'dl');
Branch: 1 branch
BranchTrueFalseTotal
if $App::DocKnot::Spin::Text::INDENT[0][1] == $indent and not $tag and $App::DocKnot::Spin::Text::INDENT[0][0] ne "dl" 0 133 133
Condition50%3/6
$App::DocKnot::Spin::Text::INDENT[0][1] == $indent and not $tag and $App::DocKnot::Spin::Text::INDENT[0][0] ne "dl"
!ll&&!rl&&r
13300
$App::DocKnot::Spin::Text::INDENT[0][1] == $indent and not $tag
!ll&&!rl&&r
39940
MC/DC0%0/3
$App::DocKnot::Spin::Text::INDENT[0][1] == $indent and not $tag and $App::DocKnot::Spin::Text::INDENT[0][0] ne "dl"
$App::DocKnot::Spin::Text::INDENT[0][1] == $indent not $tag $App::DocKnot::Spin::Text::INDENT[0][0] ne "dl"
Truth table50%2/4
A and not(B) and C
A= $App::DocKnot::Spin::Text::INDENT[0][1] == $indentB= not $tagC= $App::DocKnot::Spin::Text::INDENT[0][0] ne "dl"
ABCresult
0XX0
10X0
1100
1111
775 133 $output .= "</$INDENT[0][0]>\n";
776 133 shift @INDENT;
777 }
778 175 ▶ return $output unless $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag 56 119 175
779 119 ▶ if (!@INDENT || $indent > $INDENT[0][1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @App::DocKnot::Spin::Text::INDENT or $indent > $App::DocKnot::Spin::Text::INDENT[0][1]) 25 94 119
Condition66%2/3
not @App::DocKnot::Spin::Text::INDENT or $indent > $App::DocKnot::Spin::Text::INDENT[0][1]
l!l&&r!l&&!r
25094
MC/DC50%1/2
not @App::DocKnot::Spin::Text::INDENT or $indent > $App::DocKnot::Spin::Text::INDENT[0][1]
not @App::DocKnot::Spin::Text::INDENT $indent > $App::DocKnot::Spin::Text::INDENT[0][1]
Truth table66%2/3
not(A) or B
A= not @App::DocKnot::Spin::Text::INDENTB= $indent > $App::DocKnot::Spin::Text::INDENT[0][1]
ABresult
1X1
011
000
780 25 $output .= "<$tag>\n";
781 25 unshift (@INDENT, [ $tag, $indent ]);
782 }
783 119 $output . $data;
784 }
785  
786 # Handle titles, which should have newlines turned into spaces and leading and
787 # trailing whitespace zapped.
788 6 ▶sub title {
Subroutine title: called CC=1 SCAR=0.0
Pod documented
789 6 local $_ = shift;
790 6 s/\s*\n\s*/ /g;
791 6 s/^\s+//;
792 6 s/\s+$//;
793 6 '<title>' . $_ . '</title>';
794 }
795  
796 # Various containers.
797 0 ▶sub blockquote { paragraph ('blockquote', @_) }
Subroutine blockquote: not called CC=1 SCAR=6.9
Pod undocumented
798 0 ▶sub dt { container ('dt', @_) }
Subroutine dt: not called CC=1 SCAR=6.9
Pod undocumented
799 6 ▶sub h1 { container ('h1', @_) }
Subroutine h1: called CC=1 SCAR=0.0
Pod undocumented
800 36 ▶sub h2 { container ('h2', @_) }
Subroutine h2: called CC=1 SCAR=0.0
Pod undocumented
801 0 ▶sub h3 { container ('h3', @_) }
Subroutine h3: not called CC=1 SCAR=6.9
Pod undocumented
802 205 ▶sub p { paragraph ('p', @_) }
Subroutine p: called CC=1 SCAR=0.0
Pod undocumented
803 20 ▶sub pre { container ('pre', @_) }
Subroutine pre: called CC=1 SCAR=0.0
Pod undocumented
804  
805 ##############################################################################
806 # Header parsing
807 ##############################################################################
808  
809 # Parse a block of RFC 2822 headers.
810 #
811 # Returns: Hash of lower-cased header names to contents, or the empty hash if
812 # no headers were seen
813 6 ▶sub _parse_rfc2822_headers {
Subroutine _parse_rfc2822_headers: called CC=5 SCAR=16.9
814 6 my ($self) = @_;
815 6 my %header;
816  
817 6 my $line = $self->_next_line();
818 6 ▶ while (defined($line) && $line =~ m{ \A ([\w-]+): \s+ (.*) }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line and $line =~ / \A ([\w-]+): \s+ (.*) /msux) 27 6 33
Condition66%2/3
defined $line and $line =~ / \A ([\w-]+): \s+ (.*) /msux
!ll&&!rl&&r
0627
MC/DC50%1/2
defined $line and $line =~ / \A ([\w-]+): \s+ (.*) /msux
defined $line $line =~ / \A ([\w-]+): \s+ (.*) /msux
Truth table66%2/3
A and B
A= defined $lineB= $line =~ / \A ([\w-]+): \s+ (.*) /msux
ABresult
0X0
100
111
819 27 my ($header, $content) = ($1, $2);
820  
821 # Deal with continuation lines.
822 27 $line = $self->_next_line();
823 27 ▶ while (defined($line) && $line =~ m{ \A \s+ \S }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line and $line =~ / \A \s+ \S /msux) 0 27 27
Condition33%1/3
defined $line and $line =~ / \A \s+ \S /msux
!ll&&!rl&&r
0270
MC/DC0%0/2
defined $line and $line =~ / \A \s+ \S /msux
defined $line $line =~ / \A \s+ \S /msux
Truth table33%1/3
A and B
A= defined $lineB= $line =~ / \A \s+ \S /msux
ABresult
0X0
100
111
824 0 $content .= $line;
825 0 $line = $self->_next_line();
826 }
827  
828 # Save the header contents.
829 27 chomp($content);
830 27 $header{lc($header)} = $content;
831 }
832 6 $self->_buffer_line($line);
833  
834 6 return \%header;
835 }
836  
837 # Check to see if the header looks like that of a FAQ. If so, parse it.
838 #
839 # $header_ref - Hash into which to store the parse results.
840 3 ▶sub _handle_faq_headers {
Subroutine _handle_faq_headers: called CC=4 SCAR=14.5
841 3 my ($self, $header_ref) = @_;
842 3 my $line = $self->_next_line();
843  
844 # Skip over a leading "From " line from an mbox file.
845 3 ▶ if (defined($line) && $line !~ m{ \A From [ ] }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line and not $line =~ / \A From [ ] /msux) 3 0 3
Condition33%1/3
defined $line and not $line =~ / \A From [ ] /msux
!ll&&!rl&&r
003
MC/DC0%0/2
defined $line and not $line =~ / \A From [ ] /msux
defined $line not $line =~ / \A From [ ] /msux
Truth table33%1/3
A and not(B)
A= defined $lineB= not $line =~ / \A From [ ] /msux
ABresult
0X0
100
111
846 3 $self->_buffer_line($line);
847 }
848  
849 # Parse the top-level headers, if any, followed by the FAQ headers,
850 # skipping blank lines after each header section.
851 3 my $top_ref = $self->_parse_rfc2822_headers();
852 3 $self->_skip_blank_lines();
853 3 my $sub_ref = $self->_parse_rfc2822_headers();
854 3 $self->_skip_blank_lines();
855  
856 # Store the information we care about from the headers.
857 3 $header_ref->{author} = $top_ref->{from};
858 3 $header_ref->{original} = $sub_ref->{'original-author'};
859 3 ▶ $header_ref->{title} = $sub_ref->{'html-title'} // $top_ref->{subject};
Condition33%1/3
$sub_ref->{"html-title"} // $top_ref->{"subject"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$sub_ref->{"html-title"} // $top_ref->{"subject"}
$sub_ref->{"html-title"} $top_ref->{"subject"}
Truth table33%1/3
A // B
A= $sub_ref->{"html-title"}B= $top_ref->{"subject"}
ABresult
1X1
011
000
860 3 return;
861 }
862  
863 # Parse the headers of a text document.
864 #
865 # Returns: Hash of data from headers with the following keys:
866 # author - Author of document
867 # id - RCS Id string
868 # heading - Main document heading
869 # original - Original author of document
870 # title - Document title
871 6 ▶sub _parse_headers {
Subroutine _parse_headers: called CC=9 SCAR=22.1
872 6 my ($self) = @_;
873 6 my %header;
874  
875 # Check for a leading RCS/CVS version identifier. For FAQs that I'm
876 # posting to Usenet using postfaq, this will always be the first line of
877 # the file stored on disk.
878 6 my $line = $self->_next_line();
879 6 ▶ if (_is_id($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_id($line)) 1 5 6
880 1 chomp($line);
881 1 $header{id} = $line;
882 1 $self->_skip_blank_lines();
883 1 $line = $self->_next_line();
884 }
885  
886 # Check for the type of document. First, see if it looks like a FAQ with
887 # news/mail headers, and if so read those headers and the subheaders.
888 # Otherwise, skip over leading blank lines and rules.
889 6 $self->_buffer_line($line);
890 6 ▶ if (!$self->{title} && (_is_header($line) || $line =~ m{ \A From }xms)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"title"} and _is_header($line) || $line =~ / \A From /msux) 3 3 6
Condition83%5/6
not $self->{"title"} and _is_header($line) || $line =~ / \A From /msux
!ll&&!rl&&r
123
_is_header($line) or $line =~ / \A From /msux
l!l&&r!l&&!r
032
MC/DC66%2/3
not $self->{"title"} and _is_header($line) || $line =~ / \A From /msux
not $self->{"title"} _is_header($line) $line =~ / \A From /msux
Truth table75%3/4
not(A) and B or C
A= not $self->{"title"}B= _is_header($line)C= $line =~ / \A From /msux
ABCresult
0XX0
1000
11X1
1011
891 3 $self->_handle_faq_headers(\%header);
892 }
893 6 $self->_skip_blank_lines_and_rules();
894  
895 # See if we have a centered title at the top of the document. If so,
896 # we'll make that the document title unless we also saw a Subject header
897 # or a constructor argument. Titles shouldn't be in all caps, though.
898 6 $line = $self->_next_line();
899 6 ▶ if (_is_centered($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_centered($line)) { } 5 1 6
900 5 $header{heading} = whitechomp($line);
901 5 ▶ if (!defined($header{title})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $header{"title"}) 3 2 5
902 3 $header{title} = $header{heading};
903 3 ▶ if (_is_allcaps($header{title})) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_allcaps($header{"title"})) 0 3 3
904 0 $header{title} =~ s{ \b ([A-Z]+) \b }{\L\u$1}xmsg;
905 }
906 }
907 5 $self->_skip_blank_lines_and_rules();
908 } else {
909 1 $self->_buffer_line($line);
910 1 ▶ $header{heading} = $header{title} // $self->{title};
Condition33%1/3
$header{"title"} // $self->{"title"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$header{"title"} // $self->{"title"}
$header{"title"} $self->{"title"}
Truth table33%1/3
A // B
A= $header{"title"}B= $self->{"title"}
ABresult
1X1
011
000
911 }
912  
913 # Return the parsed header.
914 6 return \%header;
915 }
916  
917 # Parse the subheaders of a text document and generate the subheaders for the
918 # output document. The author information from the headers will be included,
919 # as will the last modified date if configured. Existing subheadings that
920 # look like they're just Revision or Date strings will be replaced by a
921 # nicely-formatted string.
922 #
923 # $header_ref - Main headers of the text document
924 #
925 # Returns: List of lists of subheaders to put at the top of the output
926 # document
927 6 ▶sub _parse_subheaders {
Subroutine _parse_subheaders: called CC=15 SCAR=27.5
928 6 my ($self, $header_ref) = @_;
929 6 my (@subheaders, $modified);
930  
931 # Generate a last modified date if we have an RCS/CVS Id string or if a
932 # last modified subheader from the file modification time was requested.
933 # We'll set $modified back to undef if we push it into the subheaders at
934 # any point; otherwise, we'll add it at the end.
935 6 ▶ if ($header_ref->{id}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($header_ref->{'id'}) { } 1 5 6
elsif ($self->{'modified'} and defined $self->{'in_path'}) { } 3 4 7
936 1 $modified = modified_id($header_ref->{id});
937 ▶ } elsif ($self->{modified} && defined($self->{in_path})) {
Condition66%2/3
$self->{"modified"} and defined $self->{"in_path"}
!ll&&!rl&&r
203
MC/DC50%1/2
$self->{"modified"} and defined $self->{"in_path"}
$self->{"modified"} defined $self->{"in_path"}
Truth table66%2/3
A and B
A= $self->{"modified"}B= defined $self->{"in_path"}
ABresult
0X0
100
111
938 3 $modified = modified_timestamp($self->{in_path}->stat()->[9]);
939 }
940  
941 # Parse subheaders. The first must be centered; after that, assume
942 # everything is a subheading until a blank line.
943 6 my $line;
944 6 ▶ while (defined($line = $self->_next_line())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = $self->_next_line)) 10 0 10
945 10 ▶ next if _is_rule($line);
Branch: 1 branch
BranchTrueFalseTotal
if _is_rule($line) 1 9 10
946 9 ▶ last if $line =~ m{ \A \s* \z }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ / \A \s* \z /msux 2 7 9
947  
948 # For cases other than a rule or blank line, we have to either be in a
949 # subheading or the line must be centered.
950 7 ▶ last if !(@subheaders || _is_centered($line));
Branch: 1 branch
BranchTrueFalseTotal
unless @subheaders or _is_centered($line) 4 3 7
Condition100%3/3
@subheaders or _is_centered($line)
l!l&&r!l&&!r
124
MC/DC100%2/2
@subheaders or _is_centered($line)
@subheaders _is_centered($line)
Truth table100%3/3
A or B
A= @subheadersB= _is_centered($line)
ABresult
1X1
011
000
951  
952 # A subheading to add. Replace Revision and Date keywords with our
953 # modified timestamp if we have one.
954 3 ▶ if ($modified && $line =~ m{ [\$] (?: Revision | Date ) }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modified and $line =~ / [\$] (?: Revision | Date ) /msux) { } 0 3 3
Condition66%2/3
$modified and $line =~ / [\$] (?: Revision | Date ) /msux
!ll&&!rl&&r
210
MC/DC0%0/2
$modified and $line =~ / [\$] (?: Revision | Date ) /msux
$modified $line =~ / [\$] (?: Revision | Date ) /msux
Truth table66%2/3
A and B
A= $modifiedB= $line =~ / [\$] (?: Revision | Date ) /msux
ABresult
0X0
100
111
955 0 push(@subheaders, $modified);
956 0 $modified = undef;
957 } else {
958 3 push(@subheaders, _format_urls(escape(whitechomp($line))));
959 }
960 }
961 6 $self->_buffer_line($line);
962 6 $self->_skip_blank_lines_and_rules();
963  
964 # If there is no subheading, but we have an author from the file headings,
965 # create a subheading with that information.
966 6 ▶ if (!@subheaders && $header_ref->{author}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @subheaders and $header_ref->{"author"}) 2 4 6
Condition100%3/3
not @subheaders and $header_ref->{"author"}
!ll&&!rl&&r
222
MC/DC100%2/2
not @subheaders and $header_ref->{"author"}
not @subheaders $header_ref->{"author"}
Truth table100%3/3
not(A) and B
A= not @subheadersB= $header_ref->{"author"}
ABresult
0X0
100
111
967 2 push(@subheaders, escape($header_ref->{author}));
968 2 ▶ if ($header_ref->{original}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_ref->{"original"}) 0 2 2
969 push(
970 @subheaders,
971 0 '(originally by ' . escape($header_ref->{original}) . ')',
972 )
973 }
974 }
975  
976 # If we have modification information and haven't output it yet, add that
977 # to the subheading.
978 6 ▶ if (defined($modified)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $modified) 4 2 6
979 4 push(@subheaders, $modified);
980 }
981  
982 # Return what we have.
983 6 return @subheaders;
984 }
985  
986 ##############################################################################
987 # Document conversion
988 ##############################################################################
989  
990 # Convert a document from text to HTML.
991 #
992 # $in_fh - Input file handle
993 # $in_path - Input path
994 # $out_fh - Output file handle
995 # $out_path - Output path
996 6 ▶sub _convert_document {
Subroutine _convert_document: called CC=66 SCAR=50.3
997 6 my ($self, $in_fh, $in_path, $out_fh, $out_path) = @_;
998  
999 # Initialize object state for a new document.
1000 #<<<
1001 6 $self->{baseline} = undef; # Baseline indentation of text
1002 6 $self->{buffer} = undef; # Buffered input line not yet converted
1003 6 $self->{contents} = 0; # Whether inside a contents section
1004 6 $self->{in_fh} = $in_fh; # Input file handle
1005 6 $self->{in_path} = $in_path; # Path to input file
1006 6 $self->{h2} = undef; # Indentation level for h2 headings
1007 6 $self->{out_fh} = $out_fh; # Output file handle
1008 6 $self->{out_path} = $out_path; # Path to the output file
1009 6 $self->{pre} = 0; # Whether inside a preformatted block
1010 6 $self->{whitespace} = q{}; # Pending whitespace
1011 #>>>
1012  
1013 # Parse the document headers.
1014 6 my $header_ref = $self->_parse_headers();
1015  
1016 # Generate the header of the HTML file.
1017 6 $self->_output_header($header_ref);
1018  
1019 # Open the body of the document, print the navigation links if possible,
1020 # and print out the heading if we found one.
1021 6 $self->_output("<body>\n\n");
1022 6 ▶ if ($self->{sitemap} && defined($self->{output}) && defined($out_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sitemap"} and defined $self->{"output"} and defined $out_path) 2 4 6
Condition66%4/6
$self->{"sitemap"} and defined $self->{"output"} and defined $out_path
!ll&&!rl&&r
402
$self->{"sitemap"} and defined $self->{"output"}
!ll&&!rl&&r
402
MC/DC33%1/3
$self->{"sitemap"} and defined $self->{"output"} and defined $out_path
$self->{"sitemap"} defined $self->{"output"} defined $out_path
Truth table50%2/4
A and B and C
A= $self->{"sitemap"}B= defined $self->{"output"}C= defined $out_path
ABCresult
0XX0
10X0
1100
1111
1023 2 my $page = $out_path->relative($self->{output});
1024 2 my @navbar = $self->{sitemap}->navbar($page);
1025 2 ▶ if (@navbar) {
Branch: 1 branch
BranchTrueFalseTotal
if (@navbar) 2 0 2
1026 2 $self->_output(@navbar, "\n");
1027 }
1028 }
1029 6 ▶ if ($header_ref->{heading}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_ref->{"heading"}) 6 0 6
1030 6 $self->_output(h1($header_ref->{heading}), "\n");
1031 }
1032  
1033 # Parse and output the subheaders, if any.
1034 6 my @subheaders = $self->_parse_subheaders($header_ref);
1035 6 ▶ if (@subheaders) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subheaders) 5 1 6
1036 5 $self->_output(qq(<p class="subheading">\n));
1037 5 $self->_output(q{ }, join("<br />\n ", @subheaders), "\n</p>\n\n");
1038 }
1039  
1040 # Scan the actual body of the text. We don't use paragraph mode, since it
1041 # doesn't work with blank lines that contain whitespace; instead, we
1042 # cobble together our own paragraph mode that does. Note that $_ already
1043 # has a non-blank line of input coming into this loop.
1044 6 my $space;
1045 6 ▶ while (defined($_ = $self->_next_paragraph())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $self->_next_paragraph)) 263 6 269
1046 263 ▶ last if _is_signature($_);
Branch: 1 branch
BranchTrueFalseTotal
if _is_signature($_) 0 263 263
1047  
1048 # If we just hit a digest divider, the next thing will likely be a
1049 # Subject: line that we want to turn into a section header. Digest
1050 # section titles are always level 2 headers currently.
1051 263 ▶ if (_is_divider $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_divider($_)) 17 246 263
1052 17 $self->{pre} = 0;
1053 17 $self->_output(start(-1));
1054 17 undef $INDENT;
1055 17 ($self->{whitespace}) = /\n(\s*)$/;
1056 17 $_ = $self->_next_paragraph();
1057 17 s/\n(\s*)$/\n/;
1058 17 $space = $1;
1059 17 ▶ if (s/^Subject:\s+//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^Subject:\s+//u) 17 0 17
1060 17 $self->{contents} = /\bcontents\b/i;
1061 17 $_ = escape $_;
1062 17 ▶ if (/^([\d.]+)[.\)]\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^([\d.]+)[.\)]\s/u) { } 16 1 17
1063 16 $self->_output(
1064 h2(container(qq(a name="S$1" id="S$1"), $_))
1065 );
1066 } else {
1067 1 $self->_output(h2($_));
1068 }
1069 17 next;
1070 }
1071 }
1072  
1073 # Treat lines of dash-type characters as rules.
1074 246 ▶ if (_is_rule $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_rule($_)) 0 246 246
1075 0 $self->{pre} = 0;
1076 0 ($space) = /\n(\s*)$/;
1077 0 $self->_output(start(-1), "<hr />\n");
1078 0 undef $INDENT;
1079 next
1080 0 }
1081  
1082 # Everything else needs to have special characters escaped. We don't
1083 # do this earlier because if we want to allow < and > in rules, the
1084 # escaping would make our lives miserable.
1085 246 $_ = escape $_;
1086  
1087 # Do this before untabification and stashing of trailing whitespace,
1088 # but after escaping. Check to see if this paragraph looks like
1089 # literal text. If so, we wrap it in <pre> and output it as is. As a
1090 # special exception to our normal paragraph handling, this paragraph
1091 # doesn't end until we find a literal blank line; this hack lets full
1092 # diffs be included in a FAQ without confusing the parser.
1093 246 ▶ if (_is_literal $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_literal($_)) 0 246 246
1094 0 ▶ if (/\n[ \t]+$/) { $_ .= $self->_next_paragraph(1) }
Branch: 1 branch
BranchTrueFalseTotal
if (/\n[ \t]+$/u) 0 0 0
1095 0 $self->_output(pre(strip_indent($_, $INDENT)));
1096 0 s/\n(\n\s*)$/\n/;
1097 0 $space = $1;
1098 0 $self->{pre} = 1;
1099 0 next;
1100 }
1101  
1102 # Not literal text, so untabify it and stash whitespace.
1103 246 $_ = untabify $_;
1104 246 s/\n(\s*)$/\n/;
1105 246 $space = $1;
1106 246 my $indent = indent $_;
1107  
1108 # If the paragraph has inconsistent indentation, or is indented
1109 # relative to the baseline *and* the last paragraph we emitted was
1110 # enclosed in <pre>, assume that this paragraph belongs in <pre> as
1111 # well.
1112 246 ▶ if ($self->{pre}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pre"}) 20 226 246
1113 20 ▶ if (_is_offset ($_) || (defined $INDENT && $indent > $INDENT)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_offset($_) or defined $App::DocKnot::Spin::Text::INDENT and $indent > $App::DocKnot::Spin::Text::INDENT) { } 3 17 20
Condition66%4/6
_is_offset($_) or defined $App::DocKnot::Spin::Text::INDENT and $indent > $App::DocKnot::Spin::Text::INDENT
l!l&&r!l&&!r
0317
defined $App::DocKnot::Spin::Text::INDENT and $indent > $App::DocKnot::Spin::Text::INDENT
!ll&&!rl&&r
0173
MC/DC33%1/3
_is_offset($_) or defined $App::DocKnot::Spin::Text::INDENT and $indent > $App::DocKnot::Spin::Text::INDENT
_is_offset($_) defined $App::DocKnot::Spin::Text::INDENT $indent > $App::DocKnot::Spin::Text::INDENT
Truth table50%2/4
A or B and C
A= _is_offset($_)B= defined $App::DocKnot::Spin::Text::INDENTC= $indent > $App::DocKnot::Spin::Text::INDENT
ABCresult
1XX1
0111
00X0
0100
1114 3 $self->_output(pre(strip_indent($_, $INDENT)));
1115 3 next;
1116 } else {
1117 17 $self->{pre} = 0;
1118 }
1119 }
1120  
1121 # Check for a heading. We distinguish between level 2 headings and
1122 # level 3 headings as follows: The first heading we encounter is
1123 # assumed to be a level 2 heading, and any further headers at that
1124 # same indentation level are also level 2 headings. If we detect any
1125 # other headings at a greater indent, they're marked as level 3.
1126 243 ▶ if ($self->_is_heading ($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_heading($_)) 19 224 243
1127 19 s/^\s+//;
1128 19 $self->{contents} = /\bcontents\b/i;
1129 19 my $h;
1130 19 ▶ if (defined $self->{h2}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'h2'}) { } 15 4 19
1131 15 ▶ if ($indent <= $self->{h2}) { $h = \&h2 }
Branch: 1 branch
BranchTrueFalseTotal
if ($indent <= $self->{'h2'}) { } 15 0 15
1132 0 else { $h = \&h3 }
1133 } else {
1134 4 $self->{h2} = $indent;
1135 4 $h = \&h2;
1136 }
1137 19 $_ = _remove_rule($_);
1138 19 ▶ if (/^([\d.]+)[.\)]\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^([\d.]+)[.\)]\s/u) { } 0 19 19
1139 0 my $anchor = qq(a name="S$1" id="S$1");
1140 0 $self->_output(start(), $h->(container($anchor, $_)));
1141 } else {
1142 19 $self->_output(start(), $h->($_));
1143 }
1144 19 $INDENT = $self->{baseline};
1145 19 next;
1146 }
1147  
1148 # A sudden change to an indentation of 0 when that's less than our
1149 # indentation baseline is also a sign of literal text.
1150 224 ▶ if ($INDENT && $indent == 0 && $INDENT > 0 && defined($self->{baseline})
Branch: 1 branch
BranchTrueFalseTotal
if ($App::DocKnot::Spin::Text::INDENT and $indent == 0 and $App::DocKnot::Spin::Text::INDENT > 0 and defined $self->{"baseline"} and $self->{"baseline"} > 0) 0 224 224
Condition83%10/12
$App::DocKnot::Spin::Text::INDENT and $indent == 0 and $App::DocKnot::Spin::Text::INDENT > 0 and defined $self->{"baseline"} and $self->{"baseline"} > 0
!ll&&!rl&&r
191330
$App::DocKnot::Spin::Text::INDENT and $indent == 0 and $App::DocKnot::Spin::Text::INDENT > 0 and defined $self->{"baseline"}
!ll&&!rl&&r
190133
$App::DocKnot::Spin::Text::INDENT and $indent == 0 and $App::DocKnot::Spin::Text::INDENT > 0
!ll&&!rl&&r
190034
$App::DocKnot::Spin::Text::INDENT and $indent == 0
!ll&&!rl&&r
1088234
MC/DC0%0/5
$App::DocKnot::Spin::Text::INDENT and $indent == 0 and $App::DocKnot::Spin::Text::INDENT > 0 and defined $self->{"baseline"} and $self->{"baseline"} > 0
$App::DocKnot::Spin::Text::INDENT $indent == 0 $App::DocKnot::Spin::Text::INDENT > 0 defined $self->{"baseline"} $self->{"baseline"} > 0
Truth table66%4/6
A and B and C and D and E
A= $App::DocKnot::Spin::Text::INDENTB= $indent == 0C= $App::DocKnot::Spin::Text::INDENT > 0D= defined $self->{"baseline"}E= $self->{"baseline"} > 0
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
1151 && $self->{baseline} > 0) {
1152 0 $self->_output(pre(strip_indent($_, $INDENT)));
1153 0 $self->{pre} = 1;
1154 0 next;
1155 }
1156  
1157 # We're dealing with a normal paragraph of some sort, so go ahead and
1158 # turn URLs into links. Check whether the paragraph is broken first,
1159 # though, and stash that information, since turning URLs into links
1160 # can artificially lengthen lines.
1161 224 my $broken = _is_broken $_;
1162 224 $_ = _format_urls($_);
1163  
1164 # Check to see if we're in a contents section and this paragraph looks
1165 # like a table of contents. If so, turn all of the section headings
1166 # into links.
1167 224 ▶ if ($self->{contents} && _is_contents($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"contents"} and _is_contents($_)) 4 220 224
Condition66%2/3
$self->{"contents"} and _is_contents($_)
!ll&&!rl&&r
22004
MC/DC50%1/2
$self->{"contents"} and _is_contents($_)
$self->{"contents"} _is_contents($_)
Truth table66%2/3
A and B
A= $self->{"contents"}B= _is_contents($_)
ABresult
0X0
100
111
1168 4 $_ = _format_contents($_)
1169 }
1170  
1171 # Check for paragraphs that are entirely bulletted lines, and turn
1172 # them into unordered lists without <p> tags.
1173 224 ▶ if (_is_allbullet $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_allbullet($_)) 2 222 224
1174 2 my $last;
1175 2 my @lines = split (/\n/, $_);
1176 2 for (@lines) {
1177 30 ▶ next unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/u 0 30 30
1178 30 ▶ if (_is_bullet $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_bullet($_)) { } 30 0 30
1179 30 ▶ if (defined $last) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last) 28 2 30
1180 28 $self->_output(start($INDENT, 'ul'));
1181 28 $self->_output(li($INDENT, _format_bold($last)));
1182 }
1183 30 $last = _remove_bullet($_);
1184 30 $INDENT = indent $last;
1185 } else {
1186 0 $last .= "\n$_";
1187 }
1188 }
1189 2 ▶ if (defined $last) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last) 2 0 2
1190 2 $self->_output(start($INDENT, 'ul'));
1191 2 $self->_output(li($INDENT, _format_bold($last)));
1192 }
1193 2 next;
1194 }
1195  
1196 # Check for paragraphs that are entirely numbered lines, and turn them
1197 # into ordered lists without <p> tags.
1198 222 ▶ if (_is_allnumbered $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_allnumbered($_)) 0 222 222
1199 0 my @lines = split (/\n/, $_);
1200 0 for (@lines) {
1201 0 ▶ next unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/u 0 0 0
1202 0 my ($number) = /^(\d+)/;
1203 0 $_ = _remove_number($_);
1204 0 $INDENT = indent $_;
1205 0 $self->_output(start($INDENT, 'ol'));
1206 0 $self->_output(li($INDENT, _format_bold($_), $number));
1207 }
1208 0 next;
1209 }
1210  
1211 # Check for bulletted paragraphs and turn them into lists.
1212 222 ▶ if (_is_bullet $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_bullet($_)) 44 178 222
1213 44 $_ = _remove_bullet($_);
1214 44 $INDENT = indent $_;
1215 44 $self->_output(start($INDENT, 'ul'));
1216 44 $self->_output(li($INDENT, p(_format_bold($_))));
1217 44 next;
1218 }
1219  
1220 # Check for paragraphs quoted with some character and turn them into
1221 # blockquotes provided they don't have inconsistent indentation.
1222 178 my $quote = _is_quoted ($_);
1223 178 ▶ if ($quote && !$broken) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quote and not $broken) 2 176 178
Condition100%3/3
$quote and not $broken
!ll&&!rl&&r
17512
MC/DC100%2/2
$quote and not $broken
$quote not $broken
Truth table100%3/3
A and not(B)
A= $quoteB= not $broken
ABresult
0X0
100
111
1224 2 $_ = _remove_prefix($_, $quote);
1225 2 $INDENT = indent $_;
1226 2 $self->_output(start($INDENT, 'blockquote', p(_format_bold($_))));
1227 2 next;
1228 }
1229  
1230 # Check for numbered paragraphs and turn them into lists.
1231 176 my $number = _is_numbered ($_);
1232 176 ▶ if (defined $number) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $number) 34 142 176
1233 34 my $contents = _is_contents ($_);
1234 34 $_ = _remove_number($_);
1235 34 $INDENT = indent $_;
1236 34 ▶ s%(\n\s*\S)%<br />$1%g if ($broken || $contents);
Branch: 1 branch
BranchTrueFalseTotal
if $broken or $contents 4 30 34
Condition100%3/3
$broken or $contents
l!l&&r!l&&!r
3130
MC/DC100%2/2
$broken or $contents
$broken $contents
Truth table100%3/3
A or B
A= $brokenB= $contents
ABresult
1X1
011
000
1237 34 $self->_output(start($INDENT, 'ol'));
1238 34 $self->_output(li($INDENT, p(_format_bold($_)), $number));
1239 34 next;
1240 }
1241  
1242 # Check for things that look like description lists and handle them.
1243 # Note that we don't allow indented description lists, because they're
1244 # usually something we actually want to make <pre>. This is another
1245 # fairly fragile heuristic.
1246 142 ▶ if (_is_description ($_) && defined $INDENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_description($_) and defined $App::DocKnot::Spin::Text::INDENT) 0 142 142
Condition33%1/3
_is_description($_) and defined $App::DocKnot::Spin::Text::INDENT
!ll&&!rl&&r
14200
MC/DC0%0/2
_is_description($_) and defined $App::DocKnot::Spin::Text::INDENT
_is_description($_) defined $App::DocKnot::Spin::Text::INDENT
Truth table33%1/3
A and B
A= _is_description($_)B= defined $App::DocKnot::Spin::Text::INDENT
ABresult
0X0
100
111
1247 0 my (@title, $body);
1248 0 ($title[0], $body) = split ("\n", $_, 2);
1249 0 my ($space) = ($title[0] =~ /^(\s*)/);
1250 0 ▶ while ($body =~ /^$space\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /^$space\S/u) 0 0 0
1251 0 my $title;
1252 0 ($title, $body) = split ("\n", $body, 2);
1253 0 push (@title, $title);
1254 }
1255 0 ▶ if ($indent == $INDENT || indent ($body) == $INDENT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent == $App::DocKnot::Spin::Text::INDENT or indent($body) == $App::DocKnot::Spin::Text::INDENT) 0 0 0
Condition0%0/3
$indent == $App::DocKnot::Spin::Text::INDENT or indent($body) == $App::DocKnot::Spin::Text::INDENT
l!l&&r!l&&!r
000
MC/DC0%0/2
$indent == $App::DocKnot::Spin::Text::INDENT or indent($body) == $App::DocKnot::Spin::Text::INDENT
$indent == $App::DocKnot::Spin::Text::INDENT indent($body) == $App::DocKnot::Spin::Text::INDENT
Truth table0%0/3
A or B
A= $indent == $App::DocKnot::Spin::Text::INDENTB= indent($body) == $App::DocKnot::Spin::Text::INDENT
ABresult
1X1
011
000
1256 0 @title = map { _format_bold($_) } @title;
1257 0 my $title = join ("<br />\n", @title) . "\n";
1258 0 $INDENT = indent $body;
1259 0 ▶ $body =~ s%(\n\s*\S)%<br />$1%g if _is_broken $body;
Branch: 1 branch
BranchTrueFalseTotal
if _is_broken($body) 0 0 0
1260 0 $self->_output(start($indent, 'dl', dt($title)));
1261 0 $self->_output(start($INDENT, 'dd', p(_format_bold($body))));
1262 0 next;
1263 }
1264 }
1265  
1266 # If the paragraph has inconsistent indentation, we should output it
1267 # in <pre>.
1268 142 ▶ if (_is_offset $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_offset($_)) 0 142 142
1269 0 $self->_output(pre(strip_indent($_, $INDENT)));
1270 0 $self->{pre} = 1;
1271 0 next;
1272 }
1273  
1274 # A sudden indentation change also means the paragraph should be
1275 # blockquoted. We render broken blockquoted text in <pre>, which may
1276 # not be what's wanted for things like quotes of poetry... this is
1277 # probably worth looking at in more detail.
1278 142 ▶ if (defined $INDENT && $indent > $INDENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $App::DocKnot::Spin::Text::INDENT and $indent > $App::DocKnot::Spin::Text::INDENT) 26 116 142
Condition100%3/3
defined $App::DocKnot::Spin::Text::INDENT and $indent > $App::DocKnot::Spin::Text::INDENT
!ll&&!rl&&r
189826
MC/DC100%2/2
defined $App::DocKnot::Spin::Text::INDENT and $indent > $App::DocKnot::Spin::Text::INDENT
defined $App::DocKnot::Spin::Text::INDENT $indent > $App::DocKnot::Spin::Text::INDENT
Truth table100%3/3
A and B
A= defined $App::DocKnot::Spin::Text::INDENTB= $indent > $App::DocKnot::Spin::Text::INDENT
ABresult
0X0
100
111
1279 26 ▶ if ($broken || (lines ($_) == 1 && !_is_sentence $_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($broken or lines($_) == 1 and not _is_sentence($_)) { } 17 9 26
Condition83%5/6
$broken or lines($_) == 1 and not _is_sentence($_)
l!l&&r!l&&!r
2159
lines($_) == 1 and not _is_sentence($_)
!ll&&!rl&&r
0915
MC/DC66%2/3
$broken or lines($_) == 1 and not _is_sentence($_)
$broken lines($_) == 1 not _is_sentence($_)
Truth table75%3/4
A or B and not(C)
A= $brokenB= lines($_) == 1C= not _is_sentence($_)
ABCresult
1XX1
0111
00X0
0100
1280 17 $self->_output(pre(strip_indent($_, $INDENT)));
1281 17 $self->{pre} = 1;
1282 } else {
1283 9 $INDENT = $indent;
1284 9 my $paragraph = p(_format_bold($_));
1285 9 $self->_output(start($INDENT, 'blockquote', $paragraph));
1286 }
1287 26 next;
1288 }
1289  
1290 # Close multiparagraph structure if we've outdented again.
1291 116 ▶ if ($INDENT && $indent < $INDENT) { $self->_output(start($indent)) }
Branch: 1 branch
BranchTrueFalseTotal
if ($App::DocKnot::Spin::Text::INDENT and $indent < $App::DocKnot::Spin::Text::INDENT) 14 102 116
Condition100%3/3
$App::DocKnot::Spin::Text::INDENT and $indent < $App::DocKnot::Spin::Text::INDENT
!ll&&!rl&&r
772514
MC/DC100%2/2
$App::DocKnot::Spin::Text::INDENT and $indent < $App::DocKnot::Spin::Text::INDENT
$App::DocKnot::Spin::Text::INDENT $indent < $App::DocKnot::Spin::Text::INDENT
Truth table100%3/3
A and B
A= $App::DocKnot::Spin::Text::INDENTB= $indent < $App::DocKnot::Spin::Text::INDENT
ABresult
0X0
100
111
1292  
1293 # Looks like a normal paragraph. Establish our indentation baseline
1294 # if we haven't already.
1295 116 ▶ if (!defined $self->{baseline} && !$INDENT) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"baseline"} or $App::DocKnot::Spin::Text::INDENT) 4 112 116
Condition100%3/3
defined $self->{"baseline"} or $App::DocKnot::Spin::Text::INDENT
l!l&&r!l&&!r
11024
MC/DC100%2/2
defined $self->{"baseline"} or $App::DocKnot::Spin::Text::INDENT
defined $self->{"baseline"} $App::DocKnot::Spin::Text::INDENT
Truth table100%3/3
A or B
A= defined $self->{"baseline"}B= $App::DocKnot::Spin::Text::INDENT
ABresult
1X1
011
000
1296 4 $self->{baseline} = $indent;
1297 }
1298 116 $INDENT = $indent;
1299 116 ▶ s%(\n\s*\S)%<br />$1%g if $broken;
Branch: 1 branch
BranchTrueFalseTotal
if $broken 0 116 116
1300 116 $self->_output(p(_format_bold($_)));
1301  
1302 } continue {
1303 263 $self->{whitespace} = $space;
1304 }
1305  
1306 # All done. Print out our closing tags.
1307 6 $self->_output(start(-1));
1308 6 ▶ if ($self->{sitemap} && defined($self->{output}) && defined($out_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sitemap"} and defined $self->{"output"} and defined $out_path) 2 4 6
Condition66%4/6
$self->{"sitemap"} and defined $self->{"output"} and defined $out_path
!ll&&!rl&&r
402
$self->{"sitemap"} and defined $self->{"output"}
!ll&&!rl&&r
402
MC/DC33%1/3
$self->{"sitemap"} and defined $self->{"output"} and defined $out_path
$self->{"sitemap"} defined $self->{"output"} defined $out_path
Truth table50%2/4
A and B and C
A= $self->{"sitemap"}B= defined $self->{"output"}C= defined $out_path
ABCresult
0XX0
10X0
1100
1111
1309 2 my $page = $out_path->relative($self->{output});
1310 2 my @navbar = $self->{sitemap}->navbar($page);
1311 2 ▶ if (@navbar) {
Branch: 1 branch
BranchTrueFalseTotal
if (@navbar) 2 0 2
1312 2 $self->_output("\n", @navbar);
1313 }
1314 }
1315 6 $self->_output("\n</body>\n</html>\n");
1316 }
1317  
1318 ##############################################################################
1319 # Public interface
1320 ##############################################################################
1321  
1322 # Create a new text to HTML converter.
1323 #
1324 # $args_ref - Anonymous hash of arguments with the following keys:
1325 # output - Root of the output tree (for sitemap information)
1326 # modified - Whether to get last-modified date from source file
1327 # sitemap - App::DocKnot::Spin::Sitemap object
1328 # style - URL to the style sheet
1329 # title - Document title
1330 #
1331 # Returns: Newly created object
1332 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
1333 6 my ($class, $args_ref) = @_;
1334  
1335 # Create and return the object.
1336 my $self = {
1337 output => $args_ref->{output},
1338 modified => $args_ref->{modified},
1339 sitemap => $args_ref->{sitemap},
1340 style => $args_ref->{style},
1341 title => $args_ref->{title},
1342 6 };
1343 6 bless($self, $class);
1344 6 return $self;
1345 }
1346  
1347 # Convert text to HTML.
1348 #
1349 # $input - Input file (if not given, assumes standard input)
1350 # $output - Output file (if not given, assumes standard output)
1351 6 ▶sub spin_text_file {
Subroutine spin_text_file: called CC=3 SCAR=11.0
Pod documented
1352 6 my ($self, $input, $output) = @_;
1353 6 my ($in_fh, $out_fh);
1354  
1355 # Figure out what file we're going to be processing. We can function as a
1356 # filter if so desired.
1357 6 ▶ if (defined($input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input) { } 6 0 6
1358 6 $input = path($input)->realpath();
1359 6 $in_fh = $input->openr_utf8();
1360 } else {
1361 0 open($in_fh, '<&:raw:encoding(utf-8)', 'STDIN');
1362 }
1363  
1364 # Open the output file.
1365 6 ▶ if (defined($output)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output) { } 5 1 6
1366 5 $output = path($output)->absolute();
1367 5 $out_fh = $output->openw_utf8();
1368 } else {
1369 1 open($out_fh, '>&:raw:encoding(utf-8)', 'STDOUT');
1370 }
1371  
1372 # Do the work.
1373 6 $self->_convert_document($in_fh, $input, $out_fh, $output);
1374  
1375 # Close input and output.
1376 6 close($in_fh);
1377 6 close($out_fh);
1378 }
1379  
1380 ##############################################################################
1381 # Module return value and documentation
1382 ##############################################################################
1383  
1384 3 1;
1385 __END__
« DocKnot/Spin.pm ↑ Summary DocKnot/Spin/Pointer.pm »