blib/lib/Pod/Manual.pm

filter: Statementstmt 69.6% 131 / 188 Branchbran 46.7% 29 / 62 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 88.0% 22 / 25 Podpod 83.3% 5 / 6 totaltotal 67.1% 192 / 286 CC 40 SCAR 33.4
CC
40
Cov
64%
save_as_pdf
132.0
add_chapter
16.0
_add_to_appendix
12.0
CRAP 28.3
↑ Summary blib/lib/Pod/Manual/PodXML2Docbook.pm »
1 package Pod::Manual;
2  
3 5 ▶use Object::InsideOut;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶no warnings qw/ uninitialized /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Pod::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Pod::Find qw/ pod_where /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use XML::XPathScript;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Pod::Manual::PodXML2Docbook;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Pod::Manual::Docbook2LaTeX;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 our $VERSION = '0.08';
19  
20 5 my @parser_of :Field;
21 5 my @dom_of :Field;
22 5 my @appendix_of :Field;
23 5 my @root_of :Field;
24 5 my @ignored_sections :Field;
25 5 my @doc_title_of :Field;
26  
27 7 ▶sub _init :Init {
Subroutine _init: called CC=4 SCAR=13.9
28 7 my $self = shift;
29 7 my $args_ref = shift;
30  
31 7 my $parser = $parser_of[ $$self ] = XML::LibXML->new;
32  
33 7 $dom_of[ $$self ] = $parser->parse_string(
34 '<book><bookinfo><title/></bookinfo></book>'
35 );
36  
37 7 $dom_of[ $$self ]->setEncoding( 'iso-8859-1' );
38  
39 7 $root_of[ $$self ] = $dom_of[ $$self ]->documentElement;
40  
41 7 $appendix_of[ $$self ] = undef;
42  
43 7 ▶ if ( my $title = $args_ref->{ title } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $title = $args_ref->{"title"}) 3 4 7
44 3 $self->_set_doc_title( $title );
45 }
46  
47 7 ▶ if ( my $x = $args_ref->{ignore_sections} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $x = $args_ref->{"ignore_sections"}) 1 6 7
48 1 ▶ push @{ $ignored_sections[ $$self ] }, ref $x ? @$x : $x ;
Branch: 1 branch
BranchTrueFalseTotal
ref $x ? : 0 1 1
49 }
50  
51 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 7 ▶sub _set_doc_title {
Subroutine _set_doc_title: called CC=2 SCAR=6.9
54 7 my( $self, $title ) = @_;
55  
56 7 $doc_title_of[ $$self ] = $title;
57 7 my $title_node = $dom_of[ $$self ]->findnodes( '/book/bookinfo/title')
58 ->[0];
59 # remove any possible title already there
60 7 $title_node->removeChild( $_ ) for $title_node->childNodes;
61  
62 7 $title_node->appendText( $title );
63  
64 7 return;
65 }
66  
67 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68  
69 3 ▶sub _find_module_pod {
Subroutine _find_module_pod: called CC=3 SCAR=11.2
70 3 my $self = shift;
71 3 my $module = shift;
72  
73 3 ▶ my $file_location = pod_where( { -inc => 1 }, $module )
Branch: 1 branch
BranchTrueFalseTotal
my $file_location = pod_where({"-inc", 1}, $module) or die "couldn't find pod for module $module\n" 3 0 3
74 or die "couldn't find pod for module $module\n";
75  
76 3 local $/ = undef;
77 3 ▶ open my $pod_fh, '<', $file_location
Branch: 1 branch
BranchTrueFalseTotal
open my $pod_fh, "<", $file_location or die "can't open pod file ${file_location}: $!" 3 0 3
78 or die "can't open pod file $file_location: $!";
79  
80 3 return <$pod_fh>;
81 }
82  
83 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
84  
85 9 ▶sub _convert_pod_to_xml {
Subroutine _convert_pod_to_xml: called CC=2 SCAR=6.9
86 9 my $self = shift;
87 9 my $pod = shift;
88  
89 9 my $parser = Pod::XML->new;
90  
91 9 my $podxml;
92 9 local *STDOUT;
93 9 open STDOUT, '>', \$podxml;
94 9 open my $input_fh, '<', \$pod;
95 9 $parser->parse_from_filehandle( $input_fh );
96  
97 9 $podxml =~ s/xmlns=".*?"//;
98 9 $podxml =~ s#]]></verbatim>\n<verbatim><!\[CDATA\[##g;
99  
100 9 ▶ my $dom = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $dom = eval { do { $parser_of[$$self]->parse_string($podxml) } } or die "error while converting raw pod to xml for '${pod}': $@" 9 0 9
101 9 $parser_of[ $$self ]->parse_string( $podxml )
102 } or die "error while converting raw pod to xml for '$pod': $@";
103  
104 9 return $dom;
105 }
106  
107 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108  
109 0 ▶sub _get_podxml {
Subroutine _get_podxml: not called CC=2 SCAR=17.9
110 0 my $self = shift;
111 0 my $pod = shift;
112  
113 0 my $pod_location = pod_where( { -inc => 1 }, $pod );
114  
115 0 my $parser = Pod::XML->new;
116  
117 0 my $podxml;
118 0 local *STDOUT;
119 0 open STDOUT, '>', \$podxml;
120 0 $parser->parse_from_file( $pod_location );
121 0 close STDOUT;
122  
123 0 $podxml =~ s/xmlns=".*?"//;
124 0 $podxml =~ s#]]></verbatim>\n<verbatim><!\[CDATA\[##g;
125  
126 0 ▶ my $dom = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $dom = eval { do { $parser_of[$$self]->parse_string($podxml) } } or die "error while converting raw pod to xml for '${pod}': $@" 0 0 0
127 0 $parser_of[ $$self ]->parse_string( $podxml )
128 } or die "error while converting raw pod to xml for '$pod': $@";
129  
130 0 return $dom;
131 }
132  
133 3 ▶sub add_chapters {
Subroutine add_chapters: called CC=3 SCAR=12.0
Pod documented
134 3 my $self = shift;
135 0 ▶ my $options = 'HASH' eq ref $_[-1] ? %{ pop @_ } : { };
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq ref $_[-1] ? : 0 3 3
136  
137 3 $self->add_chapter( $_ => $options ) for @_;
138 }
139  
140 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
141  
142 9 ▶sub add_chapter {
Subroutine add_chapter: called CC=15 SCAR=27.7
Pod documented
143 9 my $self = shift;
144 9 my $chapter = shift;
145  
146 9 ▶ my $options = 'HASH' eq ref $_[-1] ? pop @_ : { };
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq ref $_[-1] ? : 4 5 9
147  
148 9 my $podxml;
149
150 # the chapter can be passed as various things
151 9 ▶ if ( $chapter =~ /\n/ ) { # it's pure pod
Branch: 2 branches
BranchTrueFalseTotal
if ($chapter =~ /\n/) { } 5 4 9
elsif (-f $chapter) { } 1 3 4
152 5 $podxml = $self->_convert_pod_to_xml( $chapter );
153 }
154 elsif ( -f $chapter ) { # it's a file
155 1 local $/ = undef;
156 1 ▶ open my $pod_fh, '<', $chapter
Branch: 1 branch
BranchTrueFalseTotal
open my $pod_fh, "<", $chapter or die "can't open pod file ${chapter}: $!" 1 0 1
157 or die "can't open pod file $chapter: $!";
158 1 $podxml = $self->_convert_pod_to_xml( <$pod_fh> );
159 }
160 else { # it's a module name
161 3 $podxml = $self->_convert_pod_to_xml(
162 $self->_find_module_pod( $chapter )
163 );
164 }
165  
166 9 my $dom = $dom_of[ $$self ];
167  
168 9 my $docbook = XML::XPathScript->new->transform( $podxml,
169 $Pod::Manual::PodXML2Docbook::stylesheet );
170  
171 9 my $subdoc = eval {
172 9 XML::LibXML->new->parse_string( $docbook )->documentElement;
173 };
174  
175 9 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 9 9
176 0 croak "chapter couldn't be converted to docbook: $@";
177 }
178  
179 # use the title of that section if the 'doc_title' option is
180 # used, or if there are no title given yet
181 9 ▶ if ( $options->{set_title} or not defined $doc_title_of[ $$self ] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"set_title"} or not defined $doc_title_of[$$self]) 5 4 9
Condition100%3/3
$options->{"set_title"} or not defined $doc_title_of[$$self]
l!l&&r!l&&!r
144
MC/DC100%2/2
$options->{"set_title"} or not defined $doc_title_of[$$self]
$options->{"set_title"} not defined $doc_title_of[$$self]
Truth table100%3/3
A or not(B)
A= $options->{"set_title"}B= not defined $doc_title_of[$$self]
ABresult
1X1
011
000
182 5 my $title = $subdoc->findvalue( '/chapter/title/text()' );
183 5 $title =~ s/\s*-.*//; # remove desc after the '-'
184 5 ▶ $self->_set_doc_title( $title ) if $title;
Branch: 1 branch
BranchTrueFalseTotal
if $title 4 1 5
185 }
186  
187  
188 9 $dom->adoptNode( $subdoc );
189  
190 # if there is no appendix, it adds the chapter
191 # at the end of the document
192 9 $root_of[ $$self ]->insertBefore( $subdoc, $appendix_of[ $$self ] );
193  
194 9 ▶ if ( my $list = $options->{move_to_appendix} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $list = $options->{"move_to_appendix"}) 0 9 9
195 0 ▶ for my $section_title ( ref $list ? @{ $list } : $list ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $list ? : 0 0 0
196 $self->_add_to_appendix(
197 0 grep { $_->findvalue( 'title/text()' ) eq $section_title }
198 $subdoc->findnodes( 'section' )
199 );
200 }
201 }
202  
203 9 ▶ if ( $ignored_sections[ $$self ] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ignored_sections[$$self]) 1 8 9
204 1 my %to_ignore = map { $_ => 1 } @{ $ignored_sections[ $$self ] };
205 1 for my $section ( $subdoc->findnodes( 'section' ) ) {
206 8 my $title = $section->findvalue( 'title/text()' );
207 8 ▶ if ( $to_ignore{$title} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_ignore{$title}) 1 7 8
208 1 $section->parentNode->removeChild( $section );
209 }
210 }
211 }
212  
213 9 return $self;
214 }
215  
216 3 ▶sub as_dom {
Subroutine as_dom: called CC=1 SCAR=0.0
Pod undocumented
217 3 my $self = shift;
218 3 return $dom_of[ $$self ];
219 }
220  
221 5 ▶sub as_docbook {
Subroutine as_docbook: called CC=3 SCAR=13.6
Pod documented
222 5 my $self = shift;
223 0 ▶ my %option = ref $_[0] eq 'HASH' ? %{ $_[0] } : () ;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 5 5
224  
225 5 my $dom = $dom_of[ $$self ];
226  
227 5 ▶ if ( my $css = $option{ css } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $css = $option{"css"}) 0 5 5
228 # make a copy of the dom so that we're not stuck with the PI
229 0 $dom = $parser_of[ $$self ]->parse_string( $dom->toString );
230  
231 0 my $pi = $dom->createPI( 'xml-stylesheet'
232 => qq{href="$css" type="text/css"} );
233 0 $dom->insertBefore( $pi, $dom->firstChild );
234 }
235  
236 5 return $dom->toString;
237 }
238  
239 1 ▶sub as_latex {
Subroutine as_latex: called CC=2 SCAR=7.0
Pod documented
240 1 my $self = shift;
241  
242 1 my $xps = XML::XPathScript->new;
243  
244 1 my $docbook = eval { $xps->transform(
245 $self->as_docbook => $Pod::Manual::Docbook2LaTeX::stylesheet
246 ) } ;
247  
248 1 ▶ croak "couldn't convert to docbook: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
249  
250 1 return $docbook;
251 }
252  
253 0 ▶sub save_as_pdf {
Subroutine save_as_pdf: not called CC=11 SCAR=48.8
Pod documented
254 0 my $self = shift;
255 0 ▶ my $filename = shift
Branch: 1 branch
BranchTrueFalseTotal
my $filename = shift() or croak("save_as_pdf: requires a filename as an argument") 0 0 0
256 or croak 'save_as_pdf: requires a filename as an argument';
257  
258 0 ▶ $filename =~ s/\.pdf$//
Branch: 1 branch
BranchTrueFalseTotal
$filename =~ s/\.pdf$// or croak("save_as_pdf: filename '${filename}'" . q[must have suffix '.pdf']) 0 0 0
259 or croak "save_as_pdf: filename '$filename'"
260 ."must have suffix '.pdf'";
261  
262 0 my $original_dir = cwd(); # let's remember where we are
263  
264 0 ▶ if ( $filename =~ s#^(.*)/## ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ s[^(.*)/][]) 0 0 0
265 0 ▶ chdir $1 or croak "can't chdir to $1: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $1 or croak("can't chdir to $1: $!") 0 0 0
266 }
267  
268 0 my @temp_files = grep { -e } map "$filename.$_" => qw/ aux log pdf tex toc /;
269 0 ▶ if ( @temp_files ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@temp_files) 0 0 0
270 0 chdir $original_dir;
271 0 my $plural = 's' x ( @temp_files > 1 );
272 0 die "temp file$plural " . join( ' ', @temp_files )
273 . " in the way, please remove\n";
274 0 return 0;
275 }
276  
277 0 my $latex = $self->as_latex;
278  
279 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
280  
281 0 ▶ open my $latex_fh, '>', $filename.'.tex'
Branch: 1 branch
BranchTrueFalseTotal
open my $latex_fh, ">", $filename . '.tex' or croak("can't write to '$filename.tex': $!") 0 0 0
282 or croak "can't write to '$filename.tex': $!";
283 0 print {$latex_fh} $latex;
284 0 close $latex_fh;
285  
286 0 for ( 1..2 ) { # two times to populate the toc
287 0 system "pdflatex -interaction=batchmode $filename > /dev/null";
288 # and croak "problem running pdflatex: $!";
289 }
290  
291 0 for my $ext ( qw/ aux log tex toc / ) {
292 0 ▶ unlink "$filename.$ext" or croak "can't delete '$filename.$ext': $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink "$filename.$ext" or croak("can't delete '$filename.${ext}': $!") 0 0 0
293 }
294  
295 0 chdir $original_dir;
296  
297 0 return 1;
298 }
299  
300 0 ▶sub _add_to_appendix {
Subroutine _add_to_appendix: not called CC=3 SCAR=24.8
301 0 my ( $self, @nodes ) = @_;
302  
303 0 ▶ unless ( $appendix_of[ $$self ] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($appendix_of[$$self]) 0 0 0
304 # create appendix
305 0 $root_of[ $$self ]->appendChild(
306 $appendix_of[ $$self ] = $root_of[ $$self ]->new( 'appendix' )
307 );
308 0 my $label = $appendix_of[ $$self ]->new( 'label' );
309 0 $label->appendText( 'Appendix' );
310 0 $appendix_of[ $$self ]->appendChild( $label );
311 }
312  
313 0 $appendix_of[ $$self ]->appendChild( $_ ) for @nodes;
314  
315 0 return $self;
316 }
317  
318 5 1; # Magic true value required at end of module
319  
320 __END__
↑ Summary blib/lib/Pod/Manual/PodXML2Docbook.pm »