blib/lib/Pod/PXML.pm

filter: Statementstmt 17.2% 54 / 314 Branchbran 3.9% 7 / 178 Conditioncond 0.0% 0 / 58 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 36.8% 7 / 19 Podpod 28.5% 2 / 7 totaltotal 11.4% 70 / 610 CC 110 SCAR 65.8
CC
110
Cov
11%
__ANON__
1640.0
__ANON__
650.0
__ANON__
420.0
CRAP 717.8
↑ Summary
1  
2 #Time-stamp: "2004-12-29 18:34:27 AST"
3 #TODO: for xml2pod:
4 # Make utf8/Latin-1 an option (default utf8?)
5 # Make E<>ification an option (default to all)
6 # Option for whether to delete highbit things in codeblocks (default: no?)
7 #TODO: for pod2xml:
8 # Option: choice of XML encoding (Latin-1 or UTF-8)
9 # Option: whether to represent things as literals, or as numeric entities.
10 # (and whether to use decimal entities, or hex??)
11  
12 2 require 5;
13 package Pod::PXML;
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use vars qw($VERSION $XMLNS %Char2podent %Char2xmlent
Subroutine BEGIN: called CC=1 SCAR=0.0
16 $LATIN_1 $XML_VALIDATE $LINK_TEXT_INFER $FUSE_ADJACENT_PRES
17 $HIGH_BIT_OK
18 2 );
19 2 $XMLNS = 'http://www.perl.com/CPAN/authors/id/S/SB/SBURKE/pxml_0.01.dtd';
20 2 $VERSION = '0.12';
21 # I'm going to try to keep the major version numbers in the DTD and the
22 # module in synch. I dunno about the fractional part, tho.
23 2 $LATIN_1 = 1;
24 2 $XML_VALIDATE = 1;
25 2 $HIGH_BIT_OK = 0;
26  
27 2 $LINK_TEXT_INFER = 0;
28  
29 2 $FUSE_ADJACENT_PRES = 1;
30 # Whether to make " foo\n\n bar" as a single PRE,
31 # as if it were from " foo\n \n bar\n\n"
32 # TODO: set to 1
33  
34 2 ▶BEGIN { *DEBUG = sub () {0} unless defined &DEBUG }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Pod::PXML::DEBUG 2 0 2
35  
36 2 my $nil = [];
37  
38 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 # POD entities are just HTML entities plus verbar and sol
42 #------------------------------------------------------------------------
43  
44 # Fill out Char2podent, Char2xmlent.
45 {
46 2 ▶ use HTML::Entities ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2 ▶ die "\%HTML::Entities::char2entity is empty?"
Branch: 1 branch
BranchTrueFalseTotal
unless keys %HTML::Entities::char2entity 0 2 2
48 unless keys %HTML::Entities::char2entity;
49
50 2 my($c,$e);
51 2 ▶ while(($c,$e) = each(%HTML::Entities::char2entity)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %HTML::Entities::char2entity) 816 2 818
52 816 ▶ if($e =~ m{^&#(\d+);$}s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e =~ /^&#(\d+);$/s) { } 312 504 816
elsif ($e =~ /^&([^;]+);$/s) { } 504 0 504
53 312 $Char2podent{ord $c} = "E<$1>";
54 #print "num $e => E<$1>\n";
55 # &#123; => E<123>
56 # $Char2xmlent{ord $c} = $e;
57 } elsif($e =~ m{^&([^;]+);$}s) {
58 504 $Char2podent{ord $c} = "E<$1>";
59 #print "eng $e => E<$1>\n";
60 # &eacute; => E<eacute>
61 # $Char2xmlent{ord $c} = $e;
62 } else {
63 0 warn "Unknown thingy in %HTML::Entities::char2entity: $c => $e"
64 # if $^W;
65 }
66 }
67
68 # Points of difference between HTML entities and POD entities:
69
70 2 $Char2podent{ord "\xA0"} = "E<160>"; # there is no E<nbsp>
71
72 2 $Char2podent{ord "\xAB"} = "E<lchevron>";
73 2 $Char2podent{ord "\xBB"} = "E<rchevron>";
74 # Altho new POD processors also know E<laquo> and E<raquo>
75
76 # Old POD processors don't know these two -- so leave numeric
77 # $Char2podent{ord '/'} = 'E<sol>';
78 # $Char2podent{ord '|'} = 'E<verbar>';
79
80 # And a few that we have to make completely sure are present.
81 2 $Char2xmlent{ord '"'} = '&quot;' ;
82 2 $Char2xmlent{ord '<'} = '&lt;' ;
83 2 $Char2xmlent{ord '>'} = '&gt;' ;
84 2 $Char2podent{ord '<'} = 'E<lt>' ;
85 2 $Char2podent{ord '>'} = 'E<gt>' ;
86 }
87  
88 #print STDERR "Sanity: 214 is ", $Char2podent{214}, "\n";
89  
90 #------------------------------------------------------------------------
91  
92 0 ▶sub pod2xml ($) {
Subroutine pod2xml: not called CC=3 SCAR=24.8
Pod documented
93 0 require Pod::Tree;
94
95 0 my $content = $_[0];
96  
97 0 my $tree = Pod::Tree->new;
98 0 ▶ if(ref($content) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq 'SCALAR') { } 0 0 0
99 0 $tree->load_string($$content);
100 } else {
101 0 $tree->load_file($content);
102 }
103 0 ▶ unless($tree->loaded) { croak("Couldn't load pod") }
Branch: 1 branch
BranchTrueFalseTotal
unless ($tree->loaded) 0 0 0
104 0 return _pod_tree_as_xml($tree);
105 }
106  
107 #------------------------------------------------------------------------
108 # Real work:
109  
110 0 ▶sub _pod_tree_as_xml {
Subroutine _pod_tree_as_xml: not called CC=2 SCAR=17.9
111 0 my $root = $_[0]->get_root;
112 0 DEBUG > 2 and print "TREE DUMP: <<\n", $_[0]->dump, ">>\n\n";
113
114 0 ▶ return "<!-- no pod -->\n\n" unless $root;
Branch: 1 branch
BranchTrueFalseTotal
unless $root 0 0 0
115 0 my $out = '';
116  
117 0 my $trav;
118 my $x; # scratch
119 0 ▶ $trav = sub {
Subroutine __ANON__: not called CC=40 SCAR=74.0
120 0 my $it = $_[0];
121 0 my $type = $it->get_type;
122 0 my $post = '';
123 0 DEBUG and print "Hitting $type\n";
124 0 ▶ if($type eq 'root') {
Branch: 9 branches
BranchTrueFalseTotal
if ($type eq 'root') { } 0 0 0
elsif ($type eq 'for') { } 0 0 0
elsif ($type eq 'sequence') { } 0 0 0
elsif ($type eq 'list') { } 0 0 0
elsif ($type eq 'ordinary') { } 0 0 0
elsif ($type eq 'command') { } 0 0 0
elsif ($type eq 'item') { } 0 0 0
elsif ($type eq 'verbatim') { } 0 0 0
elsif ($type eq 'text') { } 0 0 0
125 0 $out .= join "\n",
126 qq{<?xml version="1.0" encoding="UTF-8" ?>},
127 qq{<!DOCTYPE pod PUBLIC "-//Sean Michael Burke//DTD PXML 0.01//EN"},
128 qq{ "$XMLNS">},
129 qq{<pod xmlns=\"$XMLNS\">},
130 "<!-- (translated from pod, by " . __PACKAGE__ . " v$VERSION) -->",
131 '',
132 '',
133 ;
134  
135 0 $post = "</pod>\n"; # harmless newline, I figure.
136
137 } elsif($type eq 'for') {
138 0 $out .= "<for target=\"" . xml_attr_escape($it->get_arg) . "\">";
139 0 $out .= xml_escape_maybe_cdata($it->get_text);
140 0 $out .= "</for>\n\n";
141 0 return;
142
143 } elsif($type eq 'sequence') {
144 0 $type = lc($it->get_letter);
145 0 DEBUG and print "Sequence type \"$type\"\n";
146 0 ▶ if($type eq 'e') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'e') { } 0 0 0
elsif ($type eq 'l') { } 0 0 0
147 # An unresolved entity.
148 0 $x = $it->get_children;
149 0 ▶ if($x and @$x ==1 and $x->[0]->get_type eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x and @$x == 1 and $x->[0]->get_type eq 'text') { } 0 0 0
Condition0%0/6
$x and @$x == 1 and $x->[0]->get_type eq "text"
!ll&&!rl&&r
000
$x and @$x == 1
!ll&&!rl&&r
000
MC/DC0%0/3
$x and @$x == 1 and $x->[0]->get_type eq "text"
$x @$x == 1 $x->[0]->get_type eq "text"
Truth table0%0/4
A and B and C
A= $xB= @$x == 1C= $x->[0]->get_type eq "text"
ABCresult
0XX0
10X0
1100
1111
150 0 $x = $x->[0]->get_text;
151 0 ▶ die "Impossible entity name \"$x\"" if $x =~ m/[ \t<>]/s;
Branch: 1 branch
BranchTrueFalseTotal
if $x =~ /[ \t<>]/s 0 0 0
152 # minimal sanity
153 0 $out .= '&' . $x . ';';
154 } else {
155 # $out .= '&WHAT;';
156 0 die "Aberrant E<..> content \"", $it->get_deep_text, "\"";
157 }
158 0 return;
159 } elsif($type eq 'l') {
160 # At time of writing, Pod::Tree is less than sterling in its
161 # treatment of L<...> sequences.
162  
163 #use Data::Dumper;
164 #print "LINK DUMP: {{\n", Dumper($it), "}}\n";
165
166 # Some special treatment...
167 0 ▶ my $target = $it->get_target || die 'targetless link?';
Condition0%0/2
$it->get_target || die('targetless link?')
l!l
00
MC/DC0%0/1
$it->get_target || die('targetless link?')
$it->get_target
Truth table0%0/2
A
A= $it->get_target
Aresult
11
00
168
169 0 my($page, $section);
170 0 $out .= "<link";
171 0 $page = xml_attr_escape( $target->get_page );
172 0 ▶ $out .= " page=\"$page\"" if length $page;
Branch: 1 branch
BranchTrueFalseTotal
if length $page 0 0 0
173 0 $section = xml_attr_escape( $target->get_section );
174 0 ▶ $out .= " section=\"$section\"" if length $section;
Branch: 1 branch
BranchTrueFalseTotal
if length $section 0 0 0
175 0 $out .= ">";
176
177 #if(!$LINK_TEXT_INFER and not(($x = $target->get_children) and @$x)) {
178 0 ▶ unless(($x = $target->get_children) and @$x) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x = $target->get_children and @$x) 0 0 0
Condition0%0/3
$x = $target->get_children and @$x
!ll&&!rl&&r
000
MC/DC0%0/2
$x = $target->get_children and @$x
$x = $target->get_children @$x
Truth table0%0/3
A and B
A= $x = $target->get_childrenB= @$x
ABresult
0X0
100
111
179 # There was no gloss (i.e., the bit after the "|").
180 0 ▶ if(! $LINK_TEXT_INFER) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $Pod::PXML::LINK_TEXT_INFER) { } 0 0 0
181 # subvert the normal processing of children of this sequence.
182 0 $out .= "</link>";
183 0 return;
184 } else {
185 # Infer the text instead.
186 0 my $ch;
187 0 ▶ if(($ch = $it->get_children) and @$ch == 1
Branch: 1 branch
BranchTrueFalseTotal
if ($ch = $it->get_children and @$ch == 1 and $ch->[0]->get_type eq "text") 0 0 0
Condition0%0/6
$ch = $it->get_children and @$ch == 1 and $ch->[0]->get_type eq "text"
!ll&&!rl&&r
000
$ch = $it->get_children and @$ch == 1
!ll&&!rl&&r
000
MC/DC0%0/3
$ch = $it->get_children and @$ch == 1 and $ch->[0]->get_type eq "text"
$ch = $it->get_children @$ch == 1 $ch->[0]->get_type eq "text"
Truth table0%0/4
A and B and C
A= $ch = $it->get_childrenB= @$ch == 1C= $ch->[0]->get_type eq "text"
ABCresult
0XX0
10X0
1100
1111
188 and $ch->[0]->get_type eq 'text'
189 ) {
190 # So this /is/ just some text bit that Pod::Tree implicated.
191  
192 # To replicate Pod::Text's inscrutible weirdness as
193 # best we can, for sake of continuity if not actual
194 # good sense or clarity.
195  
196 # The moral of the story is to always have L<text|...> !!!
197  
198 0 $x = '';
199 0 ▶ if (!length $section) {
Branch: 2 branches
BranchTrueFalseTotal
if (not length $section) { } 0 0 0
elsif ($section =~ /^[:\w]+(?:\(\))?/) { } 0 0 0
200 0 ▶ $x = "the $page manpage" if length $page;
Branch: 1 branch
BranchTrueFalseTotal
if length $page 0 0 0
201 } elsif ($section =~ m/^[:\w]+(?:\(\))?/) {
202 0 $x .= "the $section entry";
203 0 ▶ $x .= (length $page) ? " in the $page manpage"
Branch: 1 branch
BranchTrueFalseTotal
length $page ? : 0 0 0
204 : " elsewhere in this document";
205 } else {
206 0 $section =~ s/^\"\s*//;
207 0 $section =~ s/\s*\"$//;
208 0 $x .= 'the section on "' . $section . '"';
209 0 ▶ $x .= " in the $page manpage" if length $page;
Branch: 1 branch
BranchTrueFalseTotal
if length $page 0 0 0
210 }
211 0 $out .= "$x</link>";
212 0 return; # subvert the usual processing.
213 }
214 # Else it's complicated and scary. Fall thru.
215 }
216 }
217 0 $post = '</link>';
218
219 } else {
220 # Unknown sequence. Ahwell, pass thru.
221 0 $out .= "<$type>";
222 0 $post = "</$type>";
223 }
224 } elsif($type eq 'list') {
225 0 $x = xml_attr_escape($it->get_arg);
226 0 ▶ $out .= length($x) ? "<list indent=\"$x\">\n\n" : "<list>\n\n";
Branch: 1 branch
BranchTrueFalseTotal
length $x ? : 0 0 0
227  
228 # used to have:
229 # sprintf "<list type=\"%s\" indent=\"%s\">\n\n",
230 # xml_attr_escape($it->get_list_type),
231 # xml_attr_escape($it->get_arg) ;
232  
233 0 $post = "</list>\n\n";
234  
235 } elsif($type eq 'ordinary') {
236 0 $out .= "<p>";
237 0 $post = "</p>\n\n";
238  
239 } elsif($type eq 'command') {
240 0 $x = $it->get_command();
241 0 ▶ if($x =~ m/^head[1234]$/is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x =~ /^head[1234]$/is) { } 0 0 0
242 0 $x = lc($x);
243 0 $out .= "<$x>";
244 0 $post = "</$x>\n\n";
245 } else {
246 0 die "Unknown POD command \"$x\"";
247 }
248
249 } elsif($type eq 'item') {
250 # Needs special recursion!
251 0 $out .= '<item>';
252 # used to have: sprintf '<item type="%s">',
253 # xml_attr_escape($it->get_item_type);
254  
255 # Recurse for the item's children:
256 0 ▶ foreach my $c (@{ $it->get_children || $nil }) { $trav->($c) }
Condition0%0/3
$it->get_children || $nil
l!l&&r!l&&!r
000
MC/DC0%0/2
$it->get_children || $nil
$it->get_children $nil
Truth table0%0/3
A || B
A= $it->get_childrenB= $nil
ABresult
1X1
011
000
257 0 $out .= "</item>\n\n";
258  
259 # Then recurse for the bastards further down...
260
261 } elsif($type eq 'verbatim') {
262 0 ▶ ( $FUSE_ADJACENT_PRES and $out =~ s/<\/pre>\n\n$//s )
Branch: 1 branch
BranchTrueFalseTotal
$Pod::PXML::FUSE_ADJACENT_PRES and $out =~ s[</pre>\n\n$][]s or $out .= '<pre>' 0 0 0
Condition0%0/3
$Pod::PXML::FUSE_ADJACENT_PRES and $out =~ s[</pre>\n\n$][]s
!ll&&!rl&&r
000
MC/DC0%0/2
$Pod::PXML::FUSE_ADJACENT_PRES and $out =~ s[</pre>\n\n$][]s
$Pod::PXML::FUSE_ADJACENT_PRES $out =~ s[</pre>\n\n$][]s
Truth table0%0/3
A and B
A= $Pod::PXML::FUSE_ADJACENT_PRESB= $out =~ s[</pre>\n\n$][]s
ABresult
0X0
100
111
263 or $out .= "<pre>";
264 # possibly combine adjacent verbatims into a single 'pre'
265 0 $out .= xml_escape_maybe_cdata("\n" . $it->get_text . "\n");
266 0 ▶ $out =~ s/]]><!\[CDATA\[// if $out =~ m/]]>$/s;
Branch: 1 branch
BranchTrueFalseTotal
if $out =~ /]]>$/s 0 0 0
267 # combining adjacent CDATA sections is nice, and always harmless
268 0 $out .= "</pre>\n\n";
269 0 return;
270
271 } elsif($type eq 'text') {
272 0 $out .= xml_escape($it->get_text);
273 0 return;
274
275 } else {
276 0 $out .= "\n<!-- unknown podtree node type \"$type\" -->\n";
277 0 return;
278 }
279  
280 0 foreach my $c (@{ # Recurse...
281 0 ▶ (($type eq 'item') ? $it->get_siblings() : $it->get_children())
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'item' ? : 0 0 0
Condition0%0/3
($type eq 'item' ? $it->get_siblings : $it->get_children) || $nil
l!l&&r!l&&!r
000
MC/DC0%0/2
($type eq 'item' ? $it->get_siblings : $it->get_children) || $nil
($type eq 'item' ? $it->get_siblings : $it->get_children) $nil
Truth table0%0/3
A || B
A= ($type eq 'item' ? $it->get_siblings : $it->get_children)B= $nil
ABresult
1X1
011
000
282 || $nil
283 0 }) { $trav->($c) }
284  
285 0 $out .= $post;
286 0 return;
287 0 };
288 0 $trav->($root);
289 0 undef $trav; # break cyclicity
290 0 print "\n\n" if DEBUG;
291  
292 0 sanitize_newlines($out);
293  
294 0 return $out;
295 }
296  
297 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
298  
299 0 ▶sub xml_escape_maybe_cdata { # not destructive
Subroutine xml_escape_maybe_cdata: not called CC=5 SCAR=34.0
Pod undocumented
300 0 my $x;
301 0 ▶ $x = '' unless defined($x = $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined($x = $_[0]) 0 0 0
302 0 ▶ if($x =~ m/[&<>]/ and not $x =~ m/[^\x00-\x80]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x =~ /[&<>]/ and not $x =~ /[^\x00-\x80]/) { } 0 0 0
Condition0%0/3
$x =~ /[&<>]/ and not $x =~ /[^\x00-\x80]/
!ll&&!rl&&r
000
MC/DC0%0/2
$x =~ /[&<>]/ and not $x =~ /[^\x00-\x80]/
$x =~ /[&<>]/ not $x =~ /[^\x00-\x80]/
Truth table0%0/3
A and not(B)
A= $x =~ /[&<>]/B= not $x =~ /[^\x00-\x80]/
ABresult
0X0
100
111
303 # CDATA only if uses those [&<>], and does not use anything highbit.
304 0 $x =~ s/]]>/]]>]]&gt;<!\[CDATA\[/g; # "escape" any ]]'s
305 0 $x = "<![CDATA[" . $x . "]]>";
306 } else {
307 # Otherwise escape things.
308 0 $x =~ s/&/&amp;/g;
309 0 $x =~ s/</&lt;/g;
310 0 $x =~ s/>/&gt;/g;
311
312 #$x =~ s/([^\x00-\x7E])/$Char2xmlent{ord $1} or "&#".ord($1).";"/eg;
313 0 ▶ $x =~ s/([^\x00-\x7E])/"&#".ord($1).";"/eg unless $HIGH_BIT_OK;
Branch: 1 branch
BranchTrueFalseTotal
unless $Pod::PXML::HIGH_BIT_OK 0 0 0
314
315 # Why care about highbittyness? Even tho we're declaring this content
316 # to be in UTF8, might as well entitify what we can.
317 }
318 0 return $x;
319 }
320  
321 0 ▶sub xml_escape { # not destructive
Subroutine xml_escape: not called CC=5 SCAR=34.0
Pod undocumented
322 0 my $x;
323 0 ▶ return '' unless defined($x = $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined($x = $_[0]) 0 0 0
324 0 ▶ if($HIGH_BIT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::PXML::HIGH_BIT_OK) { } 0 0 0
325 0 ▶ $x =~ s/([&<>])/$Char2xmlent{ord $1} or "&#".ord($1).";"/eg;
Condition0%0/2
$Pod::PXML::Char2xmlent{ord $1} || '&#' . ord($1) . ';'
l!l
00
MC/DC0%0/1
$Pod::PXML::Char2xmlent{ord $1} || '&#' . ord($1) . ';'
$Pod::PXML::Char2xmlent{ord $1}
Truth table0%0/2
A
A= $Pod::PXML::Char2xmlent{ord $1}
Aresult
11
00
326 # Encode '&', and '<' and '>'
327 } else {
328 0 ▶ $x =~ s/([^\cm\cj\f\t !-%'-;=?-~])/$Char2xmlent{ord $1} or "&#".ord($1).";"/eg;
Condition0%0/2
$Pod::PXML::Char2xmlent{ord $1} || '&#' . ord($1) . ';'
l!l
00
MC/DC0%0/1
$Pod::PXML::Char2xmlent{ord $1} || '&#' . ord($1) . ';'
$Pod::PXML::Char2xmlent{ord $1}
Truth table0%0/2
A
A= $Pod::PXML::Char2xmlent{ord $1}
Aresult
11
00
329 # Encode control chars, high bit chars, '&', and '<' and '>'
330 }
331 0 return $x;
332 }
333  
334 0 ▶sub xml_attr_escape { # not destructive
Subroutine xml_attr_escape: not called CC=5 SCAR=34.0
Pod undocumented
335 0 my $x;
336 0 ▶ return '' unless defined($x = $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined($x = $_[0]) 0 0 0
337  
338 0 ▶ if($HIGH_BIT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::PXML::HIGH_BIT_OK) { } 0 0 0
339 0 ▶ $x =~ s/([&<>"])/$Char2xmlent{ord $1} or "&#".ord($1).";"/eg;
Condition0%0/2
$Pod::PXML::Char2xmlent{ord $1} || '&#' . ord($1) . ';'
l!l
00
MC/DC0%0/1
$Pod::PXML::Char2xmlent{ord $1} || '&#' . ord($1) . ';'
$Pod::PXML::Char2xmlent{ord $1}
Truth table0%0/2
A
A= $Pod::PXML::Char2xmlent{ord $1}
Aresult
11
00
340 # Encode '&', '"', and '<' and '>'
341 } else {
342 0 ▶ $x =~ s/([^\cm\cj\f\t !\#-\%'-;=?-~])/$Char2xmlent{ord $1} or "&#".ord($1).";"/eg;
Condition0%0/2
$Pod::PXML::Char2xmlent{ord $1} || '&#' . ord($1) . ';'
l!l
00
MC/DC0%0/1
$Pod::PXML::Char2xmlent{ord $1} || '&#' . ord($1) . ';'
$Pod::PXML::Char2xmlent{ord $1}
Truth table0%0/2
A
A= $Pod::PXML::Char2xmlent{ord $1}
Aresult
11
00
343 # Encode control chars, high bit chars, '"', '&', and '<' and '>'
344 }
345 0 return $x;
346 }
347  
348 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
349 0 ▶sub sanitize_newlines { # DESTRUCTIVE
Subroutine sanitize_newlines: not called CC=1 SCAR=6.9
Pod undocumented
350 0 if("\n" eq "\cm") {
351 $_[0] =~ s/\cm?\cj/\n/g; # turn \cj and \cm\cj into \n
352 } elsif("\n" eq "\cj") {
353 0 $_[0] =~ s/\cm\cj/\n/g; # turn \cm and \cm\cj into \n
354 } else {
355 $_[0] =~ s/(?:(?:\cm?\cj)|\cm)/\n/g;
356 # turn \cm\cj, \cj, or \cm into \n
357 }
358 0 return;
359 }
360  
361 ###########################################################################
362 ###########################################################################
363  
364 2 ▶use vars qw(%Acceptable_children);
Subroutine BEGIN: called CC=1 SCAR=0.0
365 {
366 # This just recapitulates what's in the DTD:
367 2 my $style = {map{;$_,1} qw(b i c x f s link)};
368 2 my $pstyle = {'#PCDATA',1, %$style};
369 2 my $pcdata = {'#PCDATA',1};
370 %Acceptable_children = (
371 16 'pod' => {map{;$_,1} qw(head1 head2 head3 head4 p pre list for)},
372 map(($_=>$pstyle), qw(head1 head2 head3 head4 p)),
373 'pre' => $pcdata,
374 2 'list' => {map{;$_,1} qw(item p pre list for)},
375 'item' => $pstyle,
376 'for' => $pcdata,
377 map(($_=>$pstyle), qw(link b i c f x s)),
378 );
379 }
380  
381 0 ▶sub xml2pod ($) {
Subroutine xml2pod: not called CC=2 SCAR=17.9
Pod documented
382 0 my $content = $_[0];
383 0 require XML::Parser;
384
385 0 my $out;
386 0 my($gi, %attr, $text, $cm_set); # scratch
387
388 0 my(@stack);
389 0 my @paragraph_stack;
390 # pop/pushed only by paragraph-containing elements, and link
391 0 my @for_stack; # kept by 'for' elements
392 0 my @link_stack; # kept by 'link' elements
393 my $xml = XML::Parser->new( 'Handlers' => {
394  
395 ##
396 ##
397 ## On the way in...
398
399 0 ▶ 'Start' => sub {
Subroutine __ANON__: not called CC=20 SCAR=60.4
400 0 (undef, $gi, %attr) = @_;
401 0 push @stack, $gi;
402 0 DEBUG > 1 and print ' ', join('.', @stack), "+\n";
403
404 0 ▶ if($XML_VALIDATE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Pod::PXML::XML_VALIDATE) 0 0 0
405 0 ▶ if(@stack < 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@stack < 2) { } 0 0 0
elsif (defined($cm_set = $Pod::PXML::Acceptable_children{$stack[-2]})) { } 0 0 0
406 0 ▶ unless($gi eq 'pod') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gi eq "pod") 0 0 0
407 # I think XML::Parser would catch this, but anyway.
408 0 die "Can't have a childless \"$gi\" element, in $content";
409 }
410 } elsif(defined($cm_set = $Acceptable_children{$stack[-2]})) {
411 die "Can't have a \"$gi\" in a \"$stack[-2]\", in $content (stack @stack)"
412 0 ▶ unless $cm_set->{$gi};
Branch: 1 branch
BranchTrueFalseTotal
unless $cm_set->{$gi} 0 0 0
413 } else {
414 0 die "Unknown element \"$gi\"";
415 }
416 # TODO: attribute validation!
417 }
418
419 0 ▶ if($gi =~ m/^[bicxfs]$/s) {
Branch: 8 branches
BranchTrueFalseTotal
if ($gi =~ /^[bicxfs]$/s) { } 0 0 0
elsif ($gi eq 'p' or $gi eq 'pre') { } 0 0 0
elsif ($gi eq 'for') { } 0 0 0
elsif ($gi eq 'list') { } 0 0 0
elsif ($gi eq 'item') { } 0 0 0
elsif ($gi =~ /^head[1234]$/s) { } 0 0 0
elsif ($gi eq 'link') { } 0 0 0
elsif ($gi eq 'pod') { } 0 0 0
420 0 $paragraph_stack[-1] .= "\U$gi<";
421 ▶ } elsif($gi eq 'p' or $gi eq 'pre') {
Condition0%0/3
$gi eq "p" or $gi eq "pre"
l!l&&r!l&&!r
000
MC/DC0%0/2
$gi eq "p" or $gi eq "pre"
$gi eq "p" $gi eq "pre"
Truth table0%0/3
A or B
A= $gi eq "p"B= $gi eq "pre"
ABresult
1X1
011
000
422 0 push @paragraph_stack, '';
423 } elsif($gi eq 'for') {
424 0 ▶ $text = $attr{'target'} || '????';
Condition0%0/2
$attr{'target'} || '????'
l!l
00
MC/DC0%0/1
$attr{'target'} || '????'
$attr{'target'}
Truth table0%0/2
A
A= $attr{'target'}
Aresult
11
00
425 0 push @for_stack, $text;
426 0 push @paragraph_stack, '';
427 } elsif($gi eq 'list') {
428 0 $text = $attr{'indent'};
429 0 ▶ $out .= (defined($text) && length($text))
Branch: 1 branch
BranchTrueFalseTotal
defined $text && length $text ? : 0 0 0
Condition0%0/3
defined $text and length $text
!ll&&!rl&&r
000
MC/DC0%0/2
defined $text and length $text
defined $text length $text
Truth table0%0/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
430 ? "=over $text\n\n" : "=over\n\n";
431 } elsif($gi eq 'item') {
432 0 $out .= '=item ';
433 0 push @paragraph_stack, '';
434 } elsif($gi =~ m/^head[1234]$/s) {
435 0 push @paragraph_stack, '=' . $gi . ' ';
436 } elsif($gi eq 'link') { # a hack
437 0 push @link_stack, [$attr{'page'}, $attr{'section'}];
438 0 push @paragraph_stack, '';
439 } elsif($gi eq 'pod') {
440 0 ▶ my $text = $attr{'xmlns'} || $XMLNS;
Condition0%0/3
$attr{'xmlns'} || $Pod::PXML::XMLNS
l!l&&r!l&&!r
000
MC/DC0%0/2
$attr{'xmlns'} || $Pod::PXML::XMLNS
$attr{'xmlns'} $Pod::PXML::XMLNS
Truth table0%0/3
A || B
A= $attr{'xmlns'}B= $Pod::PXML::XMLNS
ABresult
1X1
011
000
441 0 ▶ die "pod has a foreign namespace: \"$text\" instead of \"$XMLNS\""
Branch: 1 branch
BranchTrueFalseTotal
unless $text eq $Pod::PXML::XMLNS 0 0 0
442 unless $text eq $XMLNS;
443 } else {
444 0 DEBUG and print "Opening unknown element \"$gi\"\n";
445 }
446 0 return;
447 },
448  
449 ##
450 ##
451 ## And on the way out...
452  
453 0 ▶ 'End' => sub {
Subroutine __ANON__: not called CC=25 SCAR=64.8
454 0 $gi = $_[1];
455 0 DEBUG > 1 and print ' ', join('.', @stack), "-\n";
456 0 ▶ die "INSANE! Stack mismatch! $text ne $gi"
Branch: 1 branch
BranchTrueFalseTotal
unless $gi eq ($text = pop @stack) 0 0 0
457 unless $gi eq ($text = pop @stack);
458  
459 0 ▶ if($gi =~ m/^[bicxfs]$/s) {
Branch: 9 branches
BranchTrueFalseTotal
if ($gi =~ /^[bicxfs]$/s) { } 0 0 0
elsif ($gi eq 'p') { } 0 0 0
elsif ($gi eq 'pre') { } 0 0 0
elsif ($gi eq 'for') { } 0 0 0
elsif ($gi eq 'list') { } 0 0 0
elsif ($gi eq 'item') { } 0 0 0
elsif ($gi =~ /^head[1234]$/s) { } 0 0 0
elsif ($gi eq 'link') { } 0 0 0
elsif ($gi eq 'pod') { } 0 0 0
460 0 $paragraph_stack[-1] .= ">";
461 } elsif($gi eq 'p') {
462 # A paragraph must start with non-WS, non-=, and must contain
463 # no \n\n's until its very end.
464
465 0 $text = pop @paragraph_stack;
466 0 $text =~ s/^(\s)/Z<>$1/s; # make sure we're NOT indented
467 0 $text =~ s/^=/Z<>=/s; # make sure we're NOT =-initial
468 0 $text =~ s/\n+$//s; # nix terminal newlines!
469 0 $text =~ s/\n(?=\n)/\n /g; # separate double-newlines
470 0 ▶ unless(length $text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $text) 0 0 0
471 0 DEBUG and print "Odd, null p-paragraph\n";
472 0 return;
473 }
474
475 # These don't beautify /everything/ beautifiable, but they try.
476 0 ▶ while($text =~ s/([^a-zA-Z<])E<lt>/$1</g) {1}
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/([^a-zA-Z<])E<lt>/$1</g) 0 0 0
477 # Turn E<gt>'s that obviously don't need escaping, back into <'s
478 0 ▶ while($text =~ s/^([^<]*)E<gt>/$1>/) {1}
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^([^<]*)E<gt>/$1>/) 0 0 0
479 # Turn obviously harmless E<gt>'s back into ">"'s.
480
481 0 $text .= "\n\n";
482 0 $out .= $text;
483 } elsif($gi eq 'pre') {
484 # A verbatim paragraph must start with WS, and must contain
485 # no \n\n's until its very end.
486  
487 0 $text = pop @paragraph_stack;
488 0 $text =~ s/^\n+//s; # nix leading strictly-blank lines
489 0 $text =~ s/^(\S)/ \n$1/s; # make sure we ARE indented
490 # that means we don't have to make sure we don't start with a '='
491 0 $text =~ s/\n+$//s; # nix terminal newlines!
492 0 $text =~ s/\n(?=\n)/\n /g; # separate double-newlines
493 #$text =~ tr/\0-\xFF//CU if $LATIN_1; # since we can't E<..> things
494 0 ▶ unless(length $text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $text) 0 0 0
495 0 DEBUG and print "Odd, null pre-paragraph\n";
496 0 return;
497 }
498 0 $text .= "\n\n";
499 0 $out .= $text;
500  
501 } elsif($gi eq 'for') {
502 0 my $kind = pop @for_stack;
503 0 $text = "\n\n=begin $kind\n\n" . pop @paragraph_stack;
504 0 $text =~ s/\n+$//s; # nix terminal newlines!
505 0 $text =~ s/\n(?=\n)/\n /g; # separate double-newlines
506 0 $text .= "\n\n=end $kind\n\n";
507 0 $out .= $text;
508  
509 } elsif($gi eq 'list') {
510 0 $out .= "=back\n\n";
511  
512 } elsif($gi eq 'item') {
513 0 $text = pop @paragraph_stack;
514 0 $text =~ s/^\s*//s; # kill leading space
515 0 $text =~ s/\n+$//s; # nix terminal newlines!
516 0 $text =~ s/\n(?=\n)/\n /g; # separate double-newlines
517 0 $text .= "\n\n";
518  
519 # These don't beautify /everything/ beautifiable, but they try.
520 0 ▶ while($text =~ s/([^a-zA-Z<])E<lt>/$1</g) {1}
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/([^a-zA-Z<])E<lt>/$1</g) 0 0 0
521 # Turn E<gt>'s that obviously don't need escaping, back into <'s
522 0 ▶ while($text =~ s/^([^<]*)E<gt>/$1>/) {1}
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^([^<]*)E<gt>/$1>/) 0 0 0
523 # Turn obviously harmless E<gt>'s back into ">"'s.
524  
525 0 $out .= $text;
526  
527 } elsif($gi =~ m/^head[1234]$/s) {
528 0 $text = pop @paragraph_stack;
529 0 $text =~ s/^(\s)/Z<>$1/s; # make sure we're NOT (visibly) indented
530 0 $text =~ s/\n+$//s; # nix terminal newlines!
531 0 $text =~ s/\n(?=\n)/\n /g; # nix any double-newlines
532 0 $text .= "\n\n";
533  
534 # These don't beautify /everything/ beautifiable, but they try.
535 0 ▶ while($text =~ s/([^a-zA-Z<])E<lt>/$1</g) {1}
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/([^a-zA-Z<])E<lt>/$1</g) 0 0 0
536 # Turn E<gt>'s that obviously don't need escaping, back into <'s
537 0 ▶ while($text =~ s/^([^<]*)E<gt>/$1>/) {1}
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^([^<]*)E<gt>/$1>/) 0 0 0
538 # Turn obviously harmless E<gt>'s back into ">"'s.
539  
540 0 $out .= $text;
541
542 } elsif($gi eq 'link') { # a hack
543 0 $text = pop @paragraph_stack;
544 # "Text cannot contain the characters '/' and '|'"
545 0 $text =~ s/\|/E<124>/g; # AKA verbar
546 0 $text =~ s{/}{E<47>}g; # AKA sol
547 0 $text =~ s/\n(?=\n)/\n /g;
548 # nix any double-newlines, just for good measure
549 0 ▶ $text .= '|' if length $text;
Branch: 1 branch
BranchTrueFalseTotal
if length $text 0 0 0
550
551 0 my($xref, $section) = @{pop @link_stack};
552 0 ▶ $xref = '' unless defined $xref; # "" means 'in this document'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xref 0 0 0
553 0 ▶ $section = '' unless defined $section;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $section 0 0 0
554  
555 0 $xref = pod_escape($xref);
556 0 $xref =~ s{/}{E<47>}g;
557 0 ▶ $section = pod_escape("/\"$section\"") if length $section;
Branch: 1 branch
BranchTrueFalseTotal
if length $section 0 0 0
558
559 0 ▶ $section = '/"???"' unless length $xref or length $section;
Branch: 1 branch
BranchTrueFalseTotal
unless length $xref or length $section 0 0 0
Condition0%0/3
length $xref or length $section
l!l&&r!l&&!r
000
MC/DC0%0/2
length $xref or length $section
length $xref length $section
Truth table0%0/3
A or B
A= length $xrefB= length $section
ABresult
1X1
011
000
560 # signals aberrant input!
561
562 0 $paragraph_stack[-1] .= "L<$text$xref$section>";
563  
564 } elsif($gi eq 'pod') {
565 # no-op
566 } else {
567 0 DEBUG and print "Closing unknown element \"$gi\"\n";
568 }
569 0 return;
570 },
571
572 ##
573 ##
574 ## Character data! MATANGA!!!
575 0 ▶ 'Char' => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
576 0 shift;
577 0 ▶ return unless defined $_[0] and length $_[0]; # sanity
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and length $_[0] 0 0 0
Condition0%0/3
defined $_[0] and length $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] and length $_[0]
defined $_[0] length $_[0]
Truth table0%0/3
A and B
A= defined $_[0]B= length $_[0]
ABresult
0X0
100
111
578  
579 0 ▶ if(!@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @stack) { } 0 0 0
580 0 ▶ die "Non-WS text on empty stack: \"$_[0]\""
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^\s+$/s 0 0 0
581 unless $_[0] =~ m/^\s+$/s;
582 } else {
583 0 ▶ if(($Acceptable_children{$stack[-1]}
Branch: 1 branch
BranchTrueFalseTotal
if (+($Pod::PXML::Acceptable_children{$stack[-1]} || die(qq[Putting text under unknown element "$stack[-1]"]))->{'#PCDATA'}) { } 0 0 0
Condition0%0/2
$Pod::PXML::Acceptable_children{$stack[-1]} || die(qq[Putting text under unknown element "$stack[-1]"])
l!l
00
MC/DC0%0/1
$Pod::PXML::Acceptable_children{$stack[-1]} || die(qq[Putting text under unknown element "$stack[-1]"])
$Pod::PXML::Acceptable_children{$stack[-1]}
Truth table0%0/2
A
A= $Pod::PXML::Acceptable_children{$stack[-1]}
Aresult
11
00
584 || die "Putting text under unknown element \"$stack[-1]\""
585 )->{'#PCDATA'}) {
586 # This is the only case where we can add:
587 0 ▶ die "\@paragraph_stack is empty? (stack: @stack)"
Branch: 1 branch
BranchTrueFalseTotal
unless @paragraph_stack 0 0 0
588 unless @paragraph_stack;
589 0 ▶ if($stack[-1] eq 'pre') {
Branch: 1 branch
BranchTrueFalseTotal
if ($stack[-1] eq 'pre') { } 0 0 0
590 0 $paragraph_stack[-1] .= $_[0];
591 } else {
592 0 $paragraph_stack[-1] .= pod_escape($_[0]);
593 }
594 } else {
595 # doesn't allow PCDATA
596 0 ▶ die "Can't have non-WS text in a \"$stack[-1]\""
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^\s+$/s 0 0 0
597 unless $_[0] =~ m/^\s+$/s;
598 # Else it's just ignorable whitespace.
599 }
600 }
601  
602 0 return;
603 },
604
605 # 'Comment' => sub { },
606 # 'Proc' => sub { },
607 # 'Attlist' => sub { },
608 # 'Element' => sub { },
609 # 'Doctype' => sub { },
610 0 });
611  
612 # Now actually process...
613 0 $out = "";
614 0 ▶ if(ref($content) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq 'SCALAR') { } 0 0 0
615 0 $xml->parse($$content);
616 } else {
617 0 $xml->parsefile($content);
618 }
619
620 0 $out =~ s/^([^=])/=pod\n\n$1/;
621 # make sure that we start with a =-thingie, one way or another.
622
623 0 $out .= "=cut\n\n";
624  
625 0 sanitize_newlines($out);
626 0 return $out;
627 }
628  
629 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
630  
631 {
632 2 my %e = ('<' => 'E<lt>', '>' => 'E<gt>' );
633 0 ▶ sub pod_escape {
Subroutine pod_escape: not called CC=2 SCAR=17.9
Pod undocumented
634 #print STDERR "IN: <$_[0]>\n";
635 0 my $it = $_[0];
636 0 ▶ $it =~ s/([^\cm\cj\f\t !-;=?-~])/$Char2podent{ord $1} or "E<".ord($1).">"/eg;
Condition0%0/2
$Pod::PXML::Char2podent{ord $1} || 'E<' . ord($1) . '>'
l!l
00
MC/DC0%0/1
$Pod::PXML::Char2podent{ord $1} || 'E<' . ord($1) . '>'
$Pod::PXML::Char2podent{ord $1}
Truth table0%0/2
A
A= $Pod::PXML::Char2podent{ord $1}
Aresult
11
00
637 # Encode control chars, high bit chars and '<' and '>'
638 #print STDERR "OUT: <$_[0]>\n\n";
639 0 return $it;
640 }
641 }
642  
643 ###########################################################################
644 ###########################################################################
645 2 1;
646  
↑ Summary