PlainText.pm

filter: Statementstmt 73.9% 184 / 249 Branchbran 59.1% 84 / 142 Conditioncond 58.3% 14 / 24 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 87.1% 27 / 31 Podpod 3.8% 1 / 26 totaltotal 64.6% 315 / 487 CC 80 SCAR 24.0
CC
80
Cov
68%
pod2text
56.0
textblock
16.6
item
13.1
CRAP 11.0
« Select.pm ↑ Summary InputObjects.pm »
1 # Pod::PlainText -- Convert POD data to formatted ASCII text.
2 # $Id: Text.pm,v 2.1 1999/09/20 11:53:33 eagle Exp $
3 #
4 # Copyright 1999-2000 by Russ Allbery <rra@stanford.edu>
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the same terms as Perl itself.
8 #
9 # This module is intended to be a replacement for Pod::Text, and attempts to
10 # match its output except for some specific circumstances where other
11 # decisions seemed to produce better output. It uses Pod::Parser and is
12 # designed to be very easy to subclass.
13  
14 ############################################################################
15 # Modules and declarations
16 ############################################################################
17  
18 package Pod::PlainText;
19 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 12 require 5.005;
22  
23 12 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 12 ▶use Pod::Select ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 12 ▶use vars qw(@ISA %ESCAPES $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # We inherit from Pod::Select instead of Pod::Parser so that we can be used
29 # by Pod::Usage.
30 12 @ISA = qw(Pod::Select);
31  
32 12 $VERSION = '2.07';
33  
34 12 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=10.5
35 12 ▶ if ($] < 5.006) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.006") 0 12 12
36 0 require Symbol;
37 0 import Symbol;
38 }
39 }
40  
41 ############################################################################
42 # Table of supported E<> escapes
43 ############################################################################
44  
45 # This table is taken near verbatim from Pod::PlainText in Pod::Parser,
46 # which got it near verbatim from the original Pod::Text. It is therefore
47 # credited to Tom Christiansen, and I'm glad I didn't have to write it. :)
48 12 %ESCAPES = (
49 'amp' => '&', # ampersand
50 'lt' => '<', # left chevron, less-than
51 'gt' => '>', # right chevron, greater-than
52 'quot' => '"', # double quote
53  
54 "Aacute" => "\xC1", # capital A, acute accent
55 "aacute" => "\xE1", # small a, acute accent
56 "Acirc" => "\xC2", # capital A, circumflex accent
57 "acirc" => "\xE2", # small a, circumflex accent
58 "AElig" => "\xC6", # capital AE diphthong (ligature)
59 "aelig" => "\xE6", # small ae diphthong (ligature)
60 "Agrave" => "\xC0", # capital A, grave accent
61 "agrave" => "\xE0", # small a, grave accent
62 "Aring" => "\xC5", # capital A, ring
63 "aring" => "\xE5", # small a, ring
64 "Atilde" => "\xC3", # capital A, tilde
65 "atilde" => "\xE3", # small a, tilde
66 "Auml" => "\xC4", # capital A, dieresis or umlaut mark
67 "auml" => "\xE4", # small a, dieresis or umlaut mark
68 "Ccedil" => "\xC7", # capital C, cedilla
69 "ccedil" => "\xE7", # small c, cedilla
70 "Eacute" => "\xC9", # capital E, acute accent
71 "eacute" => "\xE9", # small e, acute accent
72 "Ecirc" => "\xCA", # capital E, circumflex accent
73 "ecirc" => "\xEA", # small e, circumflex accent
74 "Egrave" => "\xC8", # capital E, grave accent
75 "egrave" => "\xE8", # small e, grave accent
76 "ETH" => "\xD0", # capital Eth, Icelandic
77 "eth" => "\xF0", # small eth, Icelandic
78 "Euml" => "\xCB", # capital E, dieresis or umlaut mark
79 "euml" => "\xEB", # small e, dieresis or umlaut mark
80 "Iacute" => "\xCD", # capital I, acute accent
81 "iacute" => "\xED", # small i, acute accent
82 "Icirc" => "\xCE", # capital I, circumflex accent
83 "icirc" => "\xEE", # small i, circumflex accent
84 "Igrave" => "\xCD", # capital I, grave accent
85 "igrave" => "\xED", # small i, grave accent
86 "Iuml" => "\xCF", # capital I, dieresis or umlaut mark
87 "iuml" => "\xEF", # small i, dieresis or umlaut mark
88 "Ntilde" => "\xD1", # capital N, tilde
89 "ntilde" => "\xF1", # small n, tilde
90 "Oacute" => "\xD3", # capital O, acute accent
91 "oacute" => "\xF3", # small o, acute accent
92 "Ocirc" => "\xD4", # capital O, circumflex accent
93 "ocirc" => "\xF4", # small o, circumflex accent
94 "Ograve" => "\xD2", # capital O, grave accent
95 "ograve" => "\xF2", # small o, grave accent
96 "Oslash" => "\xD8", # capital O, slash
97 "oslash" => "\xF8", # small o, slash
98 "Otilde" => "\xD5", # capital O, tilde
99 "otilde" => "\xF5", # small o, tilde
100 "Ouml" => "\xD6", # capital O, dieresis or umlaut mark
101 "ouml" => "\xF6", # small o, dieresis or umlaut mark
102 "szlig" => "\xDF", # small sharp s, German (sz ligature)
103 "THORN" => "\xDE", # capital THORN, Icelandic
104 "thorn" => "\xFE", # small thorn, Icelandic
105 "Uacute" => "\xDA", # capital U, acute accent
106 "uacute" => "\xFA", # small u, acute accent
107 "Ucirc" => "\xDB", # capital U, circumflex accent
108 "ucirc" => "\xFB", # small u, circumflex accent
109 "Ugrave" => "\xD9", # capital U, grave accent
110 "ugrave" => "\xF9", # small u, grave accent
111 "Uuml" => "\xDC", # capital U, dieresis or umlaut mark
112 "uuml" => "\xFC", # small u, dieresis or umlaut mark
113 "Yacute" => "\xDD", # capital Y, acute accent
114 "yacute" => "\xFD", # small y, acute accent
115 "yuml" => "\xFF", # small y, dieresis or umlaut mark
116  
117 "lchevron" => "\xAB", # left chevron (double less than)
118 "rchevron" => "\xBB", # right chevron (double greater than)
119 );
120  
121  
122 ############################################################################
123 # Initialization
124 ############################################################################
125  
126 # Initialize the object. Must be sure to call our parent initializer.
127 12 ▶sub initialize {
Subroutine initialize: called CC=6 SCAR=19.0
Pod undocumented
128 12 my $self = shift;
129  
130 12 ▶ $$self{alt} = 0 unless defined $$self{alt};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"alt"} 12 0 12
131 12 ▶ $$self{indent} = 4 unless defined $$self{indent};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"indent"} 12 0 12
132 12 ▶ $$self{loose} = 0 unless defined $$self{loose};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"loose"} 12 0 12
133 12 ▶ $$self{sentence} = 0 unless defined $$self{sentence};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"sentence"} 12 0 12
134 12 ▶ $$self{width} = 76 unless defined $$self{width};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"width"} 12 0 12
135  
136 12 $$self{INDENTS} = []; # Stack of indentations.
137 12 $$self{MARGIN} = $$self{indent}; # Current left margin in spaces.
138  
139 12 return $self->SUPER::initialize;
140 }
141  
142  
143 ############################################################################
144 # Core overrides
145 ############################################################################
146  
147 # Called for each command paragraph. Gets the command, the associated
148 # paragraph, the line number, and a Pod::Paragraph object. Just dispatches
149 # the command to a method named the same as the command. =cut is handled
150 # internally by Pod::Parser.
151 61 ▶sub command {
Subroutine command: called CC=6 SCAR=18.1
Pod undocumented
152 61 my $self = shift;
153 61 my $command = shift;
154 61 ▶ return if $command eq 'pod';
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "pod" 9 52 61
155 52 ▶ return if ($$self{EXCLUDE} && $command ne 'end');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"EXCLUDE"} and $command ne "end" 0 52 52
Condition66%2/3
$self->{"EXCLUDE"} and $command ne "end"
!ll&&!rl&&r
5110
MC/DC0%0/2
$self->{"EXCLUDE"} and $command ne "end"
$self->{"EXCLUDE"} $command ne "end"
Truth table66%2/3
A and B
A= $self->{"EXCLUDE"}B= $command ne "end"
ABresult
0X0
100
111
156 52 ▶ if (defined $$self{ITEM}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"ITEM"}) 2 50 52
157 2 $self->item ("\n");
158 2 local $_ = "\n";
159 2 ▶ $self->output($_) if($command eq 'back');
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "back" 0 2 2
160 }
161 52 $command = 'cmd_' . $command;
162 52 return $self->$command (@_);
163 }
164  
165 # Called for a verbatim paragraph. Gets the paragraph, the line number, and
166 # a Pod::Paragraph object. Just output it verbatim, but with tabs converted
167 # to spaces.
168 6 ▶sub verbatim {
Subroutine verbatim: called CC=4 SCAR=13.9
Pod undocumented
169 6 my $self = shift;
170 6 ▶ return if $$self{EXCLUDE};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"EXCLUDE"} 0 6 6
171 6 ▶ $self->item if defined $$self{ITEM};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ITEM"} 1 5 6
172 6 local $_ = shift;
173 6 ▶ return if /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 3 3 6
174 3 s/^(\s*\S+)/(' ' x $$self{MARGIN}) . $1/gme;
175 3 return $self->output($_);
176 }
177  
178 # Called for a regular text block. Gets the paragraph, the line number, and
179 # a Pod::Paragraph object. Perform interpolation and output the results.
180 81 ▶sub textblock {
Subroutine textblock: called CC=8 SCAR=28.1
Pod undocumented
181 81 my $self = shift;
182 81 ▶ return if $$self{EXCLUDE};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"EXCLUDE"} 3 78 81
183 78 ▶ if($$self{VERBATIM}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"VERBATIM"}) 4 74 78
184 4 $self->output($_[0]);
185 4 return;
186 }
187 74 local $_ = shift;
188 74 my $line = shift;
189  
190 # Perform a little magic to collapse multiple L<> references. This is
191 # here mostly for backwards-compatibility. We'll just rewrite the whole
192 # thing into actual text at this part, bypassing the whole internal
193 # sequence parsing thing.
194 74 s{
195 (
196 L< # A link of the form L</something>.
197 /
198 (
199 [:\w]+ # The item has to be a simple word...
200 (\(\))? # ...or simple function.
201 )
202 >
203 (
204 ,?\s+(and\s+)? # Allow lots of them, conjuncted.
205 L<
206 /
207 (
208 [:\w]+
209 (\(\))?
210 )
211 >
212 )+
213 )
214 } {
215 0 local $_ = $1;
216 0 s%L</([^>]+)>%$1%g;
217 0 my @items = split /(?:,?\s+(?:and\s+)?)/;
218 0 my $string = "the ";
219 0 my $i;
220 0 ▶ for ($i = 0; $i < @items; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @items) 0 0 0
221 0 $string .= $items[$i];
222 0 ▶ $string .= ", " if @items > 2 && $i != $#items;
Branch: 1 branch
BranchTrueFalseTotal
if @items > 2 and $i != $#items 0 0 0
Condition0%0/3
@items > 2 and $i != $#items
!ll&&!rl&&r
000
MC/DC0%0/2
@items > 2 and $i != $#items
@items > 2 $i != $#items
Truth table0%0/3
A and B
A= @items > 2B= $i != $#items
ABresult
0X0
100
111
223 0 ▶ $string .= " and " if ($i == $#items - 1);
Branch: 1 branch
BranchTrueFalseTotal
if $i == $#items - 1 0 0 0
224 }
225 0 $string .= " entries elsewhere in this document";
226 0 $string;
227 }gex;
228  
229 # Now actually interpolate and output the paragraph.
230 74 $_ = $self->interpolate ($_, $line);
231 74 s/\s*$/\n/s;
232 74 ▶ if (defined $$self{ITEM}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'ITEM'}) { } 13 61 74
233 13 $self->item ($_ . "\n");
234 } else {
235 61 $self->output ($self->reformat ($_ . "\n"));
236 }
237 }
238  
239 # Called for an interior sequence. Gets the command, argument, and a
240 # Pod::InteriorSequence object and is expected to return the resulting text.
241 # Calls code, bold, italic, file, and link to handle those types of
242 # sequences, and handles S<>, E<>, X<>, and Z<> directly.
243 107 ▶sub interior_sequence {
Subroutine interior_sequence: called CC=12 SCAR=25.6
Pod undocumented
244 107 my $self = shift;
245 107 my $command = shift;
246 107 local $_ = shift;
247 107 ▶ return '' if ($command eq 'X' || $command eq 'Z');
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "X" or $command eq "Z" 0 107 107
Condition33%1/3
$command eq "X" or $command eq "Z"
l!l&&r!l&&!r
00107
MC/DC0%0/2
$command eq "X" or $command eq "Z"
$command eq "X" $command eq "Z"
Truth table33%1/3
A or B
A= $command eq "X"B= $command eq "Z"
ABresult
1X1
011
000
248  
249 # Expand escapes into the actual character now, carping if invalid.
250 107 ▶ if ($command eq 'E') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "E") 10 97 107
251 10 ▶ return $ESCAPES{$_} if defined $ESCAPES{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $Pod::PlainText::ESCAPES{$_} 10 0 10
252 0 carp "Unknown escape: E<$_>";
253 0 return "E<$_>";
254 }
255  
256 # For all the other sequences, empty content produces no output.
257 97 ▶ return if $_ eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" 1 96 97
258  
259 # For S<>, compress all internal whitespace and then map spaces to \01.
260 # When we output the text, we'll map this back.
261 96 ▶ if ($command eq 'S') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "S") 3 93 96
262 3 s/\s{2,}/ /g;
263 3 tr/ /\01/;
264 3 return $_;
265 }
266  
267 # Anything else needs to get dispatched to another method.
268 93 ▶ if ($command eq 'B') { return $self->seq_b ($_) }
Branch: 5 branches
BranchTrueFalseTotal
if ($command eq 'B') { } 28 65 93
elsif ($command eq 'C') { } 26 39 65
elsif ($command eq 'F') { } 2 37 39
elsif ($command eq 'I') { } 14 23 37
elsif ($command eq 'L') { } 23 0 23
269 26 elsif ($command eq 'C') { return $self->seq_c ($_) }
270 2 elsif ($command eq 'F') { return $self->seq_f ($_) }
271 14 elsif ($command eq 'I') { return $self->seq_i ($_) }
272 23 elsif ($command eq 'L') { return $self->seq_l ($_) }
273 0 else { carp "Unknown sequence $command<$_>" }
274 }
275  
276 # Called for each paragraph that's actually part of the POD. We take
277 # advantage of this opportunity to untabify the input.
278 158 ▶sub preprocess_paragraph {
Subroutine preprocess_paragraph: called CC=2 SCAR=7.4
Pod undocumented
279 158 my $self = shift;
280 158 local $_ = shift;
281 0 ▶ 1 while s/^(.*?)(\t+)/$1 . ' ' x (length ($2) * 8 - length ($1) % 8)/me;
Branch: 1 branch
BranchTrueFalseTotal
s/^(.*?)(\t+)/$1 . " " x (length($2) * 8 - length($1) % 8);/me and do { '???' } 0 158 158
282 158 return $_;
283 }
284  
285  
286 ############################################################################
287 # Command paragraphs
288 ############################################################################
289  
290 # All command paragraphs take the paragraph and the line number.
291  
292 # First level heading.
293 16 ▶sub cmd_head1 {
Subroutine cmd_head1: called CC=3 SCAR=11.4
Pod undocumented
294 16 my $self = shift;
295 16 local $_ = shift;
296 16 s/\s+$//s;
297 16 $_ = $self->interpolate ($_, shift);
298 16 ▶ if ($$self{alt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'alt'}) { } 0 16 16
299 0 $self->output ("\n==== $_ ====\n\n");
300 } else {
301 16 ▶ $_ .= "\n" if $$self{loose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"loose"} 0 16 16
302 16 $self->output ($_ . "\n");
303 }
304 }
305  
306 # Second level heading.
307 0 ▶sub cmd_head2 {
Subroutine cmd_head2: not called CC=3 SCAR=24.8
Pod undocumented
308 0 my $self = shift;
309 0 local $_ = shift;
310 0 s/\s+$//s;
311 0 $_ = $self->interpolate ($_, shift);
312 0 ▶ if ($$self{alt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'alt'}) { } 0 0 0
313 0 $self->output ("\n== $_ ==\n\n");
314 } else {
315 0 ▶ $_ .= "\n" if $$self{loose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"loose"} 0 0 0
316 0 $self->output (' ' x ($$self{indent} / 2) . $_ . "\n");
317 }
318 }
319  
320 # third level heading - not strictly perlpodspec compliant
321 0 ▶sub cmd_head3 {
Subroutine cmd_head3: not called CC=3 SCAR=24.8
Pod undocumented
322 0 my $self = shift;
323 0 local $_ = shift;
324 0 s/\s+$//s;
325 0 $_ = $self->interpolate ($_, shift);
326 0 ▶ if ($$self{alt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'alt'}) { } 0 0 0
327 0 $self->output ("\n= $_ =\n");
328 } else {
329 0 ▶ $_ .= "\n" if $$self{loose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"loose"} 0 0 0
330 0 $self->output (' ' x ($$self{indent}) . $_ . "\n");
331 }
332 }
333  
334 # fourth level heading - not strictly perlpodspec compliant
335 # just like head3
336 12 *cmd_head4 = \&cmd_head3;
337  
338 # Start a list.
339 7 ▶sub cmd_over {
Subroutine cmd_over: called CC=2 SCAR=7.1
Pod undocumented
340 7 my $self = shift;
341 7 local $_ = shift;
342 0 ▶ unless (/^[-+]?\d+\s+$/) { $_ = $$self{indent} }
Branch: 1 branch
BranchTrueFalseTotal
unless (/^[-+]?\d+\s+$/) 0 7 7
343 7 push (@{ $$self{INDENTS} }, $$self{MARGIN});
344 7 $$self{MARGIN} += ($_ + 0);
345 }
346  
347 # End a list.
348 7 ▶sub cmd_back {
Subroutine cmd_back: called CC=2 SCAR=7.9
Pod undocumented
349 7 my $self = shift;
350 7 $$self{MARGIN} = pop @{ $$self{INDENTS} };
351 7 ▶ unless (defined $$self{MARGIN}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"MARGIN"}) 0 7 7
352 0 carp 'Unmatched =back';
353 0 $$self{MARGIN} = $$self{indent};
354 }
355 }
356  
357 # An individual list item.
358 16 ▶sub cmd_item {
Subroutine cmd_item: called CC=2 SCAR=7.2
Pod undocumented
359 16 my $self = shift;
360 0 ▶ if (defined $$self{ITEM}) { $self->item }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"ITEM"}) 0 16 16
361 16 local $_ = shift;
362 16 s/\s+$//s;
363 16 $$self{ITEM} = $self->interpolate ($_);
364 }
365  
366 # Begin a block for a particular translator. Setting VERBATIM triggers
367 # special handling in textblock().
368 2 ▶sub cmd_begin {
Subroutine cmd_begin: called CC=3 SCAR=11.0
Pod undocumented
369 2 my $self = shift;
370 2 local $_ = shift;
371 2 ▶ my ($kind) = /^(\S+)/ or return;
Branch: 1 branch
BranchTrueFalseTotal
my($kind) = /^(\S+)/ or return 2 0 2
372 2 ▶ if ($kind eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq 'text') { } 1 1 2
373 1 $$self{VERBATIM} = 1;
374 } else {
375 1 $$self{EXCLUDE} = 1;
376 }
377 }
378  
379 # End a block for a particular translator. We assume that all =begin/=end
380 # pairs are properly closed.
381 2 ▶sub cmd_end {
Subroutine cmd_end: called CC=1 SCAR=0.0
Pod undocumented
382 2 my $self = shift;
383 2 $$self{EXCLUDE} = 0;
384 2 $$self{VERBATIM} = 0;
385 }
386  
387 # One paragraph for a particular translator. Ignore it unless it's intended
388 # for text, in which case we treat it as a verbatim text block.
389 2 ▶sub cmd_for {
Subroutine cmd_for: called CC=2 SCAR=6.9
Pod undocumented
390 2 my $self = shift;
391 2 local $_ = shift;
392 2 my $line = shift;
393 2 ▶ return unless s/^text\b[ \t]*\r?\n?//;
Branch: 1 branch
BranchTrueFalseTotal
unless s/^text\b[ \t]*\r?\n?// 1 1 2
394 1 $self->verbatim ($_, $line);
395 }
396  
397 # just a dummy method for the time being
398 0 ▶sub cmd_encoding {
Subroutine cmd_encoding: not called CC=1 SCAR=6.9
Pod undocumented
399 0 return;
400 }
401  
402 ############################################################################
403 # Interior sequences
404 ############################################################################
405  
406 # The simple formatting ones. These are here mostly so that subclasses can
407 # override them and do more complicated things.
408 28 ▶sub seq_b { return $_[0]{alt} ? "``$_[1]''" : $_[1] }
Subroutine seq_b: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'alt'} ? : 0 28 28
409 26 ▶sub seq_c { return $_[0]{alt} ? "``$_[1]''" : "`$_[1]'" }
Subroutine seq_c: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'alt'} ? : 0 26 26
410 2 ▶sub seq_f { return $_[0]{alt} ? "\"$_[1]\"" : $_[1] }
Subroutine seq_f: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'alt'} ? : 0 2 2
411 14 ▶sub seq_i { return '*' . $_[1] . '*' }
Subroutine seq_i: called CC=1 SCAR=0.0
Pod undocumented
412  
413 # The complicated one. Handle links. Since this is plain text, we can't
414 # actually make any real links, so this is all to figure out what text we
415 # print out.
416 23 ▶sub seq_l {
Subroutine seq_l: called CC=11 SCAR=24.0
Pod undocumented
417 23 my $self = shift;
418 23 local $_ = shift;
419  
420 # Smash whitespace in case we were split across multiple lines.
421 23 s/\s+/ /g;
422  
423 # If we were given any explicit text, just output it.
424 23 ▶ if (/^([^|]+)\|/) { return $1 }
Branch: 1 branch
BranchTrueFalseTotal
if (/^([^|]+)\|/) 9 14 23
425  
426 # Okay, leading and trailing whitespace isn't important; get rid of it.
427 14 s/^\s+//;
428 14 s/\s+$//;
429  
430 # Default to using the whole content of the link entry as a section
431 # name. Note that L<manpage/> forces a manpage interpretation, as does
432 # something looking like L<manpage(section)>. The latter is an
433 # enhancement over the original Pod::Text.
434 14 my ($manpage, $section) = ('', $_);
435 14 ▶ if (/^(?:https?|ftp|news):/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/^(?:https?|ftp|news):/) { } 1 13 14
elsif (/^"\s*(.*?)\s*"$/) { } 1 12 13
elsif (/^[-:.\w]+(?:\(\S+\))?$/) { } 2 10 12
elsif (m[/]) { } 10 0 10
436 # a URL
437 1 return $_;
438 } elsif (/^"\s*(.*?)\s*"$/) {
439 1 $section = '"' . $1 . '"';
440 } elsif (m/^[-:.\w]+(?:\(\S+\))?$/) {
441 2 ($manpage, $section) = ($_, '');
442 } elsif (m{/}) {
443 10 ($manpage, $section) = split (/\s*\/\s*/, $_, 2);
444 }
445  
446 13 my $text = '';
447 # Now build the actual output text.
448 13 ▶ if (!length $section) {
Branch: 2 branches
BranchTrueFalseTotal
if (not length $section) { } 2 11 13
elsif ($section =~ /^[:\w]+(?:\(\))?/) { } 8 3 11
449 2 ▶ $text = "the $manpage manpage" if length $manpage;
Branch: 1 branch
BranchTrueFalseTotal
if length $manpage 2 0 2
450 } elsif ($section =~ /^[:\w]+(?:\(\))?/) {
451 8 $text .= 'the ' . $section . ' entry';
452 8 ▶ $text .= (length $manpage) ? " in the $manpage manpage"
Branch: 1 branch
BranchTrueFalseTotal
length $manpage ? : 8 0 8
453 : ' elsewhere in this document';
454 } else {
455 3 $section =~ s/^\"\s*//;
456 3 $section =~ s/\s*\"$//;
457 3 $text .= 'the section on "' . $section . '"';
458 3 ▶ $text .= " in the $manpage manpage" if length $manpage;
Branch: 1 branch
BranchTrueFalseTotal
if length $manpage 2 1 3
459 }
460 13 return $text;
461 }
462  
463  
464 ############################################################################
465 # List handling
466 ############################################################################
467  
468 # This method is called whenever an =item command is complete (in other
469 # words, we've seen its associated paragraph or know for certain that it
470 # doesn't have one). It gets the paragraph associated with the item as an
471 # argument. If that argument is empty, just output the item tag; if it
472 # contains a newline, output the item tag followed by the newline.
473 # Otherwise, see if there's enough room for us to output the item tag in the
474 # margin of the text or if we have to put it on a separate line.
475 16 ▶sub item {
Subroutine item: called CC=12 SCAR=25.7
Pod documented
476 16 my $self = shift;
477 16 local $_ = shift;
478 16 my $tag = $$self{ITEM};
479 16 ▶ unless (defined $tag) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tag) 0 16 16
480 0 carp 'item called without tag';
481 0 return;
482 }
483 16 undef $$self{ITEM};
484 16 my $indent = $$self{INDENTS}[-1];
485 0 ▶ unless (defined $indent) { $indent = $$self{indent} }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $indent) 0 16 16
486 16 my $space = ' ' x $indent;
487 16 ▶ $space =~ s/^ /:/ if $$self{alt};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"alt"} 0 16 16
488 16 ▶ if (!$_ || /^\s+$/ || ($$self{MARGIN} - $indent < length ($tag) + 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_ or /^\s+$/ or $self->{'MARGIN'} - $indent < length($tag) + 1) { } 6 10 16
Condition100%6/6
not $_ or /^\s+$/ or $self->{"MARGIN"} - $indent < length($tag) + 1
l!l&&r!l&&!r
3310
not $_ or /^\s+$/
l!l&&r!l&&!r
1213
MC/DC100%3/3
not $_ or /^\s+$/ or $self->{"MARGIN"} - $indent < length($tag) + 1
not $_ /^\s+$/ $self->{"MARGIN"} - $indent < length($tag) + 1
Truth table100%4/4
not(A) or B or C
A= not $_B= /^\s+$/C= $self->{"MARGIN"} - $indent < length($tag) + 1
ABCresult
1XX1
01X1
0011
0000
489 6 my $margin = $$self{MARGIN};
490 6 $$self{MARGIN} = $indent;
491 6 my $output = $self->reformat ($tag);
492 6 $output =~ s/[\r\n]*$/\n/;
493 6 $self->output ($output);
494 6 $$self{MARGIN} = $margin;
495 6 ▶ $self->output ($self->reformat ($_)) if defined && /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ and /\S/ 3 3 6
Condition100%3/3
defined $_ and /\S/
!ll&&!rl&&r
123
MC/DC100%2/2
defined $_ and /\S/
defined $_ /\S/
Truth table100%3/3
A and B
A= defined $_B= /\S/
ABresult
0X0
100
111
496 } else {
497 10 $_ = $self->reformat ($_);
498 10 ▶ s/^ /:/ if ($$self{alt} && $indent > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"alt"} and $indent > 0 0 10 10
Condition33%1/3
$self->{"alt"} and $indent > 0
!ll&&!rl&&r
1000
MC/DC0%0/2
$self->{"alt"} and $indent > 0
$self->{"alt"} $indent > 0
Truth table33%1/3
A and B
A= $self->{"alt"}B= $indent > 0
ABresult
0X0
100
111
499 10 my $tagspace = ' ' x length $tag;
500 10 ▶ s/^($space)$tagspace/$1$tag/ or carp 'Bizarre space in item';
Branch: 1 branch
BranchTrueFalseTotal
s/^($space)$tagspace/$1$tag/ or carp("Bizarre space in item") 10 0 10
501 10 $self->output ($_);
502 }
503 }
504  
505  
506 ############################################################################
507 # Output formatting
508 ############################################################################
509  
510 # Wrap a line, indenting by the current left margin. We can't use
511 # Text::Wrap because it plays games with tabs. We can't use formline, even
512 # though we'd really like to, because it screws up non-printing characters.
513 # So we have to do the wrapping ourselves.
514 80 ▶sub wrap {
Subroutine wrap: called CC=4 SCAR=14.2
Pod undocumented
515 80 my $self = shift;
516 80 local $_ = shift;
517 80 my $output = '';
518 80 my $spaces = ' ' x $$self{MARGIN};
519 80 my $width = $$self{width} - $$self{MARGIN};
520 80 ▶ while (length > $width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > $width) 49 80 129
521 49 ▶ if (s/^([^\r\n]{0,$width})\s+// || s/^([^\r\n]{$width})//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^([^\r\n]{0,$width})\s+// or s/^([^\r\n]{$width})//) { } 49 0 49
Condition33%1/3
s/^([^\r\n]{0,$width})\s+// or s/^([^\r\n]{$width})//
l!l&&r!l&&!r
4900
MC/DC0%0/2
s/^([^\r\n]{0,$width})\s+// or s/^([^\r\n]{$width})//
s/^([^\r\n]{0,$width})\s+// s/^([^\r\n]{$width})//
Truth table33%1/3
A or B
A= s/^([^\r\n]{0,$width})\s+//B= s/^([^\r\n]{$width})//
ABresult
1X1
011
000
522 49 $output .= $spaces . $1 . "\n";
523 } else {
524 0 last;
525 }
526 }
527 80 $output .= $spaces . $_;
528 80 $output =~ s/\s+$/\n\n/;
529 80 return $output;
530 }
531  
532 # Reformat a paragraph of text for the current margin. Takes the text to
533 # reformat and returns the formatted text.
534 80 ▶sub reformat {
Subroutine reformat: called CC=2 SCAR=8.7
Pod undocumented
535 80 my $self = shift;
536 80 local $_ = shift;
537  
538 # If we're trying to preserve two spaces after sentences, do some
539 # munging to support that. Otherwise, smash all repeated whitespace.
540 80 ▶ if ($$self{sentence}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'sentence'}) { } 0 80 80
541 0 s/ +$//mg;
542 0 s/\.\r?\n/. \n/g;
543 0 s/[\r\n]+/ /g;
544 0 s/ +/ /g;
545 } else {
546 80 s/\s+/ /g;
547 }
548 80 return $self->wrap($_);
549 }
550  
551 # Output text to the output device.
552 103 ▶sub output { $_[1] =~ tr/\01/ /; print { $_[0]->output_handle } $_[1] }
Subroutine output: called CC=1 SCAR=0.0
Pod undocumented
553  
554  
555 ############################################################################
556 # Backwards compatibility
557 ############################################################################
558  
559 # The old Pod::Text module did everything in a pod2text() function. This
560 # tries to provide the same interface for legacy applications.
561 0 ▶sub pod2text {
Subroutine pod2text: not called CC=7 SCAR=40.3
Pod undocumented
562 0 my @args;
563  
564 # This is really ugly; I hate doing option parsing in the middle of a
565 # module. But the old Pod::Text module supported passing flags to its
566 # entry function, so handle -a and -<number>.
567 0 ▶ while ($_[0] =~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^-/) 0 0 0
568 0 my $flag = shift;
569 0 ▶ if ($flag eq '-a') { push (@args, alt => 1) }
Branch: 2 branches
BranchTrueFalseTotal
if ($flag eq '-a') { } 0 0 0
elsif ($flag =~ /^-(\d+)$/) { } 0 0 0
570 0 elsif ($flag =~ /^-(\d+)$/) { push (@args, width => $1) }
571 else {
572 0 unshift (@_, $flag);
573 0 last;
574 }
575 }
576  
577 # Now that we know what arguments we're using, create the parser.
578 0 my $parser = Pod::PlainText->new (@args);
579  
580 # If two arguments were given, the second argument is going to be a file
581 # handle. That means we want to call parse_from_filehandle(), which
582 # means we need to turn the first argument into a file handle. Magic
583 # open will handle the <&STDIN case automagically.
584 0 ▶ if (defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) { } 0 0 0
585 0 my $infh;
586 0 ▶ if ($] < 5.006) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.006") 0 0 0
587 0 $infh = gensym();
588 }
589 0 ▶ unless (open ($infh, $_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $infh, $_[0]) 0 0 0
590 0 croak ("Can't open $_[0] for reading: $!\n");
591 }
592 0 $_[0] = $infh;
593 0 return $parser->parse_from_filehandle (@_);
594 } else {
595 0 return $parser->parse_from_file (@_);
596 }
597 }
598  
599  
600 ############################################################################
601 # Module return value and documentation
602 ############################################################################
603  
604 12 1;
605 __END__
« Select.pm ↑ Summary InputObjects.pm »