blib/lib/LaTeX/Pod.pm

filter: Statementstmt 92.4% 316 / 342 Branchbran 80.0% 80 / 100 Conditioncond 78.2% 18 / 23 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 86.5% 71 / 82 Podpod 100% 2 / 2 totaltotal 87.7% 494 / 563 SCAR 42.6
CC
65
Cov
89%
convert
22.1
_process_text_item
11.0
_debug
6.0
CRAP 70.6
↑ Summary Build »
1 package LaTeX::Pod;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use boolean qw(true false);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use constant do_exec => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use constant no_exec => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use LaTeX::TOM ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Params::Validate ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our ($VERSION, $DEBUG);
15  
16 2 $VERSION = '0.22';
17 2 $DEBUG = false;
18  
19 validation_options(
20 on_fail => sub
21 {
22 0 ▶ my ($error) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 0 chomp $error;
24 0 croak $error;
25 },
26 2 stack_skip => 2,
27 );
28  
29 2 my $regex_list_type = join '|', qw(description enumerate itemize);
30 2 my @text_node_types = qw(text tag);
31  
32 sub new
33 {
34 1 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.2
Pod documented
35  
36 1 ▶ my $self = bless {}, ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
37  
38 1 $self->_init_check(@_);
39 1 $self->_init(@_);
40  
41 1 return $self;
42 }
43  
44 sub convert
45 {
46 2 ▶ my $self = shift;
Subroutine convert: called CC=22 SCAR=31.0
Pod documented
47  
48 2 $self->_init_check($self->{file});
49 2 $self->_init_vars;
50  
51 2 my $nodes = $self->_init_tom;
52  
53 2 ▶ my $sort = sub { my $order = pop; sort { $order->{$a} <=> $order->{$b} } keys %{$_[0]} };
Subroutine __ANON__: called CC=1 SCAR=0.0
54  
55 2 my ($command_keyword, @queue);
56  
57 2 foreach my $i (0 .. $#$nodes) {
58 206 my $node = $nodes->[$i];
59 206 my $type = $node->getNodeType;
60  
61 206 ▶ if ($type eq 'TEXT') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'TEXT') { } 136 70 206
elsif ($type eq 'COMMENT') { } 6 64 70
elsif ($type eq 'ENVIRONMENT') { } 30 34 64
elsif ($type eq 'COMMAND') { } 34 0 34
62 136 ▶ next if $node->getNodeText !~ /\w+/
Branch: 1 branch
BranchTrueFalseTotal
if not $node->getNodeText =~ /\w+/ or $node->getNodeText =~ /^\\\w+$/m 48 88 136
Condition100%3/3
not $node->getNodeText =~ /\w+/ or $node->getNodeText =~ /^\\\w+$/m
l!l&&r!l&&!r
46288
MC/DC100%2/2
not $node->getNodeText =~ /\w+/ or $node->getNodeText =~ /^\\\w+$/m
not $node->getNodeText =~ /\w+/ $node->getNodeText =~ /^\\\w+$/m
Truth table100%3/3
not(A) or B
A= not $node->getNodeText =~ /\w+/B= $node->getNodeText =~ /^\\\w+$/m
ABresult
1X1
011
000
63 or $node->getNodeText =~ /^\\\w+$/m;
64  
65 88 ▶ if ($node->getNodeText =~ /\\item/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($node->getNodeText =~ /\\item/) { } 22 66 88
elsif (not defined $command_keyword and $i >= 1 ? !$self->_is_environment_type($node->getParent, 'verbatim') : &true()) { } 28 38 66
elsif (defined $command_keyword) { } 34 4 38
66 22 ▶ push @queue, [ sub { shift->_process_text_item(@_) }, $node ];
Subroutine __ANON__: called CC=1 SCAR=0.0
67 }
68 ▶ elsif (!defined $command_keyword && ($i >= 1
Branch: 1 branch
BranchTrueFalseTotal
$i >= 1 ? : 32 0 32
Condition100%3/3
not defined $command_keyword and $i >= 1 ? !$self->_is_environment_type($node->getParent, "verbatim") : &true()
!ll&&!rl&&r
34428
MC/DC100%2/2
not defined $command_keyword and $i >= 1 ? !$self->_is_environment_type($node->getParent, "verbatim") : &true()
not defined $command_keyword $i >= 1 ? !$self->_is_environment_type($node->getParent, "verbatim") : &true()
Truth table100%3/3
not(A) and B
A= not defined $command_keywordB= $i >= 1 ? !$self->_is_environment_type($node->getParent, "verbatim") : &true()
ABresult
0X0
100
111
69 ? !$self->_is_environment_type($node->getParent, 'verbatim') : true)
70 ) {
71 28 ▶ push @queue, [ sub { shift->_process_text(@_) }, $node ];
Subroutine __ANON__: called CC=1 SCAR=0.0
72 }
73 elsif (defined $command_keyword) {
74 34 push @queue, [ $self->{dispatch_text}->{$command_keyword}, $node ];
75 34 undef $command_keyword;
76 }
77 }
78 elsif ($type eq 'COMMENT') {
79 6 ▶ push @queue, [ sub { shift->_process_comment(@_) }, $node ];
Subroutine __ANON__: called CC=1 SCAR=0.0
80 }
81 elsif ($type eq 'ENVIRONMENT') {
82 30 my $class = $node->getEnvironmentClass;
83 30 ▶ if ($class eq 'abstract') {
Branch: 3 branches
BranchTrueFalseTotal
if ($class eq 'abstract') { } 2 28 30
elsif ($class =~ /^($regex_list_type)$/) { } 22 6 28
elsif ($class eq 'verbatim') { } 4 2 6
84 2 next;
85 }
86 elsif ($class =~ /^($regex_list_type)$/) {
87 22 ▶ push @queue, [ sub { shift->_process_start_item(@_) }, $node, $1 ];
Subroutine __ANON__: called CC=1 SCAR=0.0
88 }
89 elsif ($class eq 'verbatim') {
90 4 ▶ push @queue, [ sub { shift->_process_text_verbatim(@_) }, $node->getFirstChild ];
Subroutine __ANON__: called CC=1 SCAR=0.0
91 }
92 }
93 elsif ($type eq 'COMMAND') {
94 34 my $cmd_name = $node->getCommandName;
95 34 foreach my $keyword ($sort->(map $self->{$_}, qw(command_checks command_order))) {
96 168 ▶ if ($self->{command_checks}->{$keyword}->($cmd_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"command_checks"}{$keyword}->($cmd_name)) 34 134 168
97 34 my ($code, $exec) = @{$self->{dispatch_command}->{$keyword}};
98 34 ▶ if (defined $exec && $exec) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exec and $exec) { } 0 34 34
Condition66%2/3
defined $exec and $exec
!ll&&!rl&&r
14200
MC/DC0%0/2
defined $exec and $exec
defined $exec $exec
Truth table66%2/3
A and B
A= defined $execB= $exec
ABresult
0X0
100
111
99 0 $code->($self);
100 }
101 else {
102 34 ▶ push @queue, [ $code, $node ] if defined $exec;
Branch: 1 branch
BranchTrueFalseTotal
if defined $exec 20 14 34
103 34 $command_keyword = $keyword;
104 }
105 34 last;
106 }
107 }
108 }
109 }
110  
111 2 foreach my $dispatch (@queue) {
112 136 my ($code, $node, @args) = @$dispatch;
113 136 $code->($self, $node, @args);
114 }
115 2 $self->_process_end;
116  
117 2 return $self->_pod_finalize;
118 }
119  
120 sub _init_check
121 {
122 3 ▶ my $self = shift;
Subroutine _init_check: called CC=2 SCAR=7.2
123  
124 3 validate_pos(@_, { type => SCALAR });
125  
126 3 my ($file) = @_;
127 my $error = sub
128 {
129 3 ▶ return 'does not exist' unless -e shift;
Subroutine __ANON__: called CC=4 SCAR=14.9
Branch: 1 branch
BranchTrueFalseTotal
unless -e shift() 0 3 3
130 3 ▶ return 'is not a file' unless -f _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 0 3 3
131 3 ▶ return 'is empty' unless -s _;
Branch: 1 branch
BranchTrueFalseTotal
unless -s _ 0 3 3
132 3 return undef;
133  
134 3 }->($file);
135  
136 3 ▶ defined $error and croak "Cannot open `$file': $error";
Branch: 1 branch
BranchTrueFalseTotal
defined $error and croak("Cannot open `${file}': $error") 0 3 3
137 }
138  
139 sub _init
140 {
141 1 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
142 1 my ($file) = @_;
143  
144 1 $self->{file} = $file;
145  
146 1 my $i = 0;
147 1 %{$self->{command_order}} = map { $_ => $i++ } qw(directive title author chapter section subsection textbf textsf emph);
148  
149 1 %{$self->{command_checks}} = (
150 34 ▶ directive => sub { $_[0] =~ /^(?:documentclass|usepackage|pagestyle)$/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
151 30 ▶ title => sub { $_[0] eq 'title' },
Subroutine __ANON__: called CC=1 SCAR=0.0
152 28 ▶ author => sub { $_[0] eq 'author' },
Subroutine __ANON__: called CC=1 SCAR=0.0
153 26 ▶ chapter => sub { $_[0] eq 'chapter' },
Subroutine __ANON__: called CC=1 SCAR=0.0
154 22 ▶ section => sub { $_[0] eq 'section' },
Subroutine __ANON__: called CC=1 SCAR=0.0
155 16 ▶ subsection => sub { $_[0] =~ /^(?:sub){1,2}section$/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
156 6 ▶ textbf => sub { $_[0] eq 'textbf' },
Subroutine __ANON__: called CC=1 SCAR=0.0
157 4 ▶ textsf => sub { $_[0] eq 'textsf' },
Subroutine __ANON__: called CC=1 SCAR=0.0
158 2 ▶ emph => sub { $_[0] eq 'emph' },
Subroutine __ANON__: called CC=1 SCAR=0.0
159 1 );
160 1 %{$self->{dispatch_command}} = (
161 ▶ directive => [ sub {}, undef ],
Subroutine __ANON__: not called CC=1 SCAR=0.0
162 ▶ title => [ sub {}, undef ],
Subroutine __ANON__: not called CC=1 SCAR=0.0
163 ▶ author => [ sub {}, undef ],
Subroutine __ANON__: not called CC=1 SCAR=0.0
164 4 ▶ chapter => [ sub { shift->_process_chapter(@_) }, no_exec ],
Subroutine __ANON__: called CC=1 SCAR=0.0
165 6 ▶ section => [ sub { shift->_process_section(@_) }, no_exec ],
Subroutine __ANON__: called CC=1 SCAR=0.0
166 10 ▶ subsection => [ sub { shift->_process_subsection(@_) }, no_exec ],
Subroutine __ANON__: called CC=1 SCAR=0.0
167 ▶ textbf => [ sub {}, undef ],
Subroutine __ANON__: not called CC=1 SCAR=0.0
168 ▶ textsf => [ sub {}, undef ],
Subroutine __ANON__: not called CC=1 SCAR=0.0
169 1 ▶ emph => [ sub {}, undef ],
Subroutine __ANON__: not called CC=1 SCAR=0.0
170 );
171 1 %{$self->{dispatch_text}} = (
172 4 ▶ directive => sub { shift->_process_directive(shift, 'directive') },
Subroutine __ANON__: called CC=1 SCAR=0.0
173 2 ▶ title => sub { shift->_process_directive(shift, 'title') },
Subroutine __ANON__: called CC=1 SCAR=0.0
174 2 ▶ author => sub { shift->_process_directive(shift, 'author') },
Subroutine __ANON__: called CC=1 SCAR=0.0
175 4 ▶ chapter => sub { shift->_process_text_title(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
176 6 ▶ section => sub { shift->_process_text_title(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
177 10 ▶ subsection => sub { shift->_process_text_title(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
178 2 ▶ textbf => sub { shift->_process_tag(shift, 'textbf') },
Subroutine __ANON__: called CC=1 SCAR=0.0
179 2 ▶ textsf => sub { shift->_process_tag(shift, 'textsf') },
Subroutine __ANON__: called CC=1 SCAR=0.0
180 2 ▶ emph => sub { shift->_process_tag(shift, 'emph') },
Subroutine __ANON__: called CC=1 SCAR=0.0
181 1 );
182 }
183  
184 sub _init_vars
185 {
186 2 ▶ my $self = shift;
Subroutine _init_vars: called CC=1 SCAR=0.0
187  
188 2 delete @$self{qw(list node previous)};
189  
190 2 $self->{pod} = [];
191  
192 2 %{$self->{title_inc}} = (
193 title => 1,
194 chapter => 1,
195 );
196 }
197  
198 sub _init_tom
199 {
200 2 ▶ my $self = shift;
Subroutine _init_tom: called CC=1 SCAR=0.0
201  
202 2 my $parser = LaTeX::TOM->new(2); # silently discard warnings about unparseable LaTeX
203 2 my $document = $parser->parseFile($self->{file});
204 2 my $nodes = $document->getAllNodes;
205  
206 2 return $nodes;
207 }
208  
209 sub _process_directive
210 {
211 8 ▶ my $self = shift;
Subroutine _process_directive: called CC=3 SCAR=11.0
212 8 my ($node, $directive) = @_;
213  
214 8 ▶ return if any { $directive eq $_ } qw(directive author);
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $directive eq $_; } , "directive", "author") 6 2 8
215  
216 2 ▶ if ($directive eq 'title') {
Branch: 1 branch
BranchTrueFalseTotal
if ($directive eq "title") 2 0 2
217 2 $self->_pod_add('=head' . "$self->{title_inc}{title} " . $node->getNodeText);
218 }
219 }
220  
221 sub _process_comment
222 {
223 6 ▶ my $self = shift;
Subroutine _process_comment: called CC=1 SCAR=0.0
224 6 my ($node) = @_;
225  
226 6 $self->_process_end_item($node);
227  
228 6 $self->_unregister_previous(@text_node_types);
229  
230 6 my $text = $node->getNodeText;
231  
232 6 $self->_scrub_newlines(\$text);
233  
234 6 $text =~ s/^ \s*? \% \s*? (?=\S)//x;
235  
236 6 $self->_pod_add("=for comment $text");
237 }
238  
239 sub _process_text_title
240 {
241 20 ▶ my $self = shift;
Subroutine _process_text_title: called CC=1 SCAR=0.0
242 20 my ($node) = @_;
243  
244 20 my $text = $node->getNodeText;
245  
246 20 $self->_process_spec_chars(\$text);
247  
248 20 $self->_pod_append($text);
249 }
250  
251 sub _process_text_verbatim
252 {
253 4 ▶ my $self = shift;
Subroutine _process_text_verbatim: called CC=1 SCAR=0.0
254 4 my ($node) = @_;
255  
256 4 $self->_process_end_item($node);
257  
258 4 $self->_unregister_previous(@text_node_types);
259  
260 4 my $text = $node->getNodeText;
261  
262 4 $self->_scrub_newlines(\$text);
263 4 $self->_process_spec_chars(\$text);
264 4 $self->_prepend_spaces(\$text);
265  
266 4 $self->_pod_add($text);
267 }
268  
269 sub _process_start_item
270 {
271 22 ▶ my $self = shift;
Subroutine _process_start_item: called CC=2 SCAR=6.9
272 22 my ($node, $type) = @_;
273  
274 22 $self->_process_end_item($node);
275  
276 22 $self->_unregister_previous(@text_node_types);
277  
278 22 my $nested = $self->_list_nestedness($node);
279  
280 22 ▶ if ($nested) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nested) 8 14 22
281 8 $self->_pod_add('=back');
282 }
283  
284 22 %{$self->{list}{$nested}} = (
285 type => $type,
286 enum => 1,
287 );
288  
289 22 $self->_pod_add('=over ' . (4 + $nested));
290  
291 22 $self->_register_previous('list');
292 }
293  
294 sub _process_text_item
295 {
296 22 ▶ my $self = shift;
Subroutine _process_text_item: called CC=11 SCAR=24.0
297 22 my ($node) = @_;
298  
299 22 $self->_unregister_previous(@text_node_types);
300  
301 22 my $nested = $self->_list_nestedness($node) - 1;
302  
303 22 ▶ if ($self->_is_environment_type($node->getPreviousSibling, qr/^(?:$regex_list_type)$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_environment_type($node->getPreviousSibling, qr/^(?:$regex_list_type)$/)) 4 18 22
304 4 $self->_pod_add('=back');
305 4 $self->_pod_add('=over ' . (4 + $nested));
306 }
307  
308 22 my $text = $node->getNodeText;
309  
310 22 my $type = $self->{list}{$nested}{type};
311 22 my $enum = \$self->{list}{$nested}{enum};
312  
313 LOOP: {
314 22 local ($1, $2);
315 56 ▶ if ($text =~ /\G \s*? \\item (?:\s*?\[(.+?)\]\s+?|\s+?) (\S.+)?$/cgmx) {
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ /\G \s*? \\item (?:\s*?\[(.+?)\]\s+?|\s+?) (\S.+)?$/cgmx) { } 32 24 56
elsif ($text =~ /\G \s*? (\S.+?) \s*? (?:(?=\\item)|\z)/gsx) { } 2 22 24
316 32 my $pod = '=item ';
317 32 ▶ if ($type eq 'description') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'description') { } 2 30 32
elsif ($type eq 'enumerate') { } 14 16 30
elsif ($type eq 'itemize') { } 16 0 16
318 2 ▶ $pod .= defined $1 ? "B<$1> " : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 2 0 2
319 }
320 elsif ($type eq 'enumerate') {
321 14 ▶ $pod .= defined $1 ? "$1 " : ($$enum++ . '. ');
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 4 10 14
322 }
323 elsif ($type eq 'itemize') {
324 16 ▶ $pod .= defined $1 ? "$1 " : '* ';
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 2 14 16
325 }
326 32 ▶ $pod .= defined $2 ? $2 : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 30 2 32
327 32 $self->_process_spec_chars(\$pod);
328 32 $self->_pod_add($pod);
329 32 redo;
330 }
331 elsif ($text =~ /\G \s*? (\S.+?) \s*? (?:(?=\\item)|\z)/gsx) {
332 2 my $pod = $1;
333 2 $self->_process_spec_chars(\$pod);
334 2 $self->_pod_add($pod);
335 2 redo;
336 }
337 }
338 }
339  
340 sub _process_end_item
341 {
342 86 ▶ my $self = shift;
Subroutine _process_end_item: called CC=4 SCAR=13.9
343 86 my ($node) = @_;
344  
345 86 ▶ return unless $self->_is_set_previous('list');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_set_previous("list") 62 24 86
346  
347 24 my $parent = $node->getParent;
348  
349 24 ▶ $parent = $parent->getParent if $self->_is_environment_type($parent, 'verbatim');
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_environment_type($parent, "verbatim") 2 22 24
350  
351 24 ▶ if ($self->_is_environment_type($parent, 'document')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_environment_type($parent, "document")) 12 12 24
352 12 $self->_pod_add('=back');
353 12 $self->_unregister_previous('list', @text_node_types);
354 }
355 }
356  
357 sub _process_text
358 {
359 28 ▶ my $self = shift;
Subroutine _process_text: called CC=2 SCAR=6.9
360 28 my ($node) = @_;
361  
362 28 $self->_process_end_item($node);
363  
364 28 ▶ if ($self->_is_environment_type($node->getPreviousSibling, 'abstract')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_environment_type($node->getPreviousSibling, "abstract")) 2 26 28
365 2 $self->_unregister_previous(@text_node_types);
366 }
367  
368 28 my $text = $node->getNodeText;
369  
370 28 $self->_scrub_newlines(\$text);
371 28 $self->_process_spec_chars(\$text);
372  
373 28 $self->_text_setter($text);
374  
375 28 $self->_register_previous('text');
376 }
377  
378 sub _process_chapter
379 {
380 4 ▶ my $self = shift;
Subroutine _process_chapter: called CC=2 SCAR=7.0
381 4 my ($node) = @_;
382  
383 4 $self->_process_end_item($node);
384  
385 4 $self->_unregister_previous(@text_node_types);
386  
387 4 ▶ $self->{title_inc}{section} ||= $self->{title_inc}{chapter} + 1;
Condition66%2/3
$self->{"title_inc"}{"section"} ||= $self->{"title_inc"}{"chapter"} + 1
l!l&&r!l&&!r
220
MC/DC0%0/2
$self->{"title_inc"}{"section"} ||= $self->{"title_inc"}{"chapter"} + 1
$self->{"title_inc"}{"section"} $self->{"title_inc"}{"chapter"} + 1
Truth table66%2/3
A ||= B
A= $self->{"title_inc"}{"section"}B= $self->{"title_inc"}{"chapter"} + 1
ABresult
1X1
011
000
388  
389 4 $self->_pod_add('=head' . $self->{title_inc}{chapter} . ' ');
390 }
391  
392 sub _process_section
393 {
394 6 ▶ my $self = shift;
Subroutine _process_section: called CC=2 SCAR=7.0
395 6 my ($node) = @_;
396  
397 6 $self->_process_end_item($node);
398  
399 6 $self->_unregister_previous(@text_node_types);
400  
401 6 ▶ $self->{title_inc}{section} ||= 1;
Condition50%1/2
$self->{"title_inc"}{"section"} ||= 1
l!l
60
MC/DC0%0/1
$self->{"title_inc"}{"section"} ||= 1
$self->{"title_inc"}{"section"}
Truth table50%1/2
A
A= $self->{"title_inc"}{"section"}
Aresult
11
00
402  
403 6 $self->_pod_add('=head' . "$self->{title_inc}{section} ");
404 }
405  
406 sub _process_subsection
407 {
408 10 ▶ my $self = shift;
Subroutine _process_subsection: called CC=2 SCAR=6.9
409 10 my ($node) = @_;
410  
411 10 $self->_process_end_item($node);
412  
413 10 $self->_unregister_previous(@text_node_types);
414  
415 10 my $cmd_name = $node->getCommandName;
416  
417 10 my $nested = 0;
418 10 ▶ $nested++ while $cmd_name =~ /\Gsub/g;
Branch: 1 branch
BranchTrueFalseTotal
$cmd_name =~ /\Gsub/g and do { ++$nested } 14 10 24
419  
420 10 $self->_pod_add('=head' . ($self->{title_inc}{section} + $nested) . ' ');
421 }
422  
423 sub _process_spec_chars
424 {
425 86 ▶ my $self = shift;
Subroutine _process_spec_chars: called CC=3 SCAR=11.0
426 86 my ($text) = @_;
427  
428 86 my %umlauts = (a => 'ä',
429 A => 'Ä',
430 u => 'ü',
431 U => 'Ü',
432 o => 'ö',
433 O => 'Ö');
434  
435 86 ▶ while (my ($from, $to) = each %umlauts) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %umlauts) 516 86 602
436 516 $$text =~ s/\\\"$from/$to/g;
437 }
438  
439 86 foreach my $escape ('#', qw($ % & _ { })) {
440 602 $$text =~ s/\\\Q$escape\E/$escape/g;
441 }
442  
443 86 $$text =~ s/\\ldots/.../g;
444  
445 86 $$text =~ s/\\verb\*?(.)(.+?)\1/C<$2>/g;
446 86 $$text =~ s/(?:\\\\|\\newline)/\n/g;
447 }
448  
449 sub _process_tag
450 {
451 6 ▶ my $self = shift;
Subroutine _process_tag: called CC=2 SCAR=7.1
452 6 my ($node, $tag) = @_;
453  
454 6 $self->_process_end_item($node);
455  
456 6 ▶ if ($self->_is_environment_type($node->getPreviousSibling, 'abstract')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_environment_type($node->getPreviousSibling, "abstract")) 0 6 6
457 0 $self->_unregister_previous(@text_node_types);
458 }
459  
460 6 my $text = $node->getNodeText;
461  
462 6 my %tags = (textbf => 'B',
463 textsf => 'C',
464 emph => 'I');
465  
466 6 $self->_text_setter("$tags{$tag}<$text>");
467  
468 6 $self->_register_previous('tag');
469 }
470  
471 sub _process_end
472 {
473 2 ▶ my $self = shift;
Subroutine _process_end: called CC=2 SCAR=7.0
474  
475 2 ▶ if ($self->_is_set_previous('list')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_set_previous("list")) 2 0 2
476 2 $self->_pod_add('=back');
477 2 $self->_unregister_previous('list');
478 }
479 }
480  
481 sub _is_environment_type
482 {
483 220 ▶ my $self = shift;
Subroutine _is_environment_type: called CC=4 SCAR=13.9
484 220 my ($node, $type) = @_;
485  
486 220 ▶ $type = qr/^$type$/ unless ref $type eq 'REGEXP';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $type eq "REGEXP" 220 0 220
487  
488 220 ▶ return ($node
Condition100%6/6
$node && $node->getNodeType eq 'ENVIRONMENT' && $node->getEnvironmentClass =~ /$type/
!ll&&!rl&&r
4611064
$node and $node->getNodeType eq "ENVIRONMENT"
!ll&&!rl&&r
2620174
MC/DC100%3/3
$node && $node->getNodeType eq 'ENVIRONMENT' && $node->getEnvironmentClass =~ /$type/
$node $node->getNodeType eq "ENVIRONMENT" $node->getEnvironmentClass =~ /$type/
Truth table100%4/4
A and B && C
A= $nodeB= $node->getNodeType eq "ENVIRONMENT"C= $node->getEnvironmentClass =~ /$type/
ABCresult
0XX0
10X0
1100
1111
489 && $node->getNodeType eq 'ENVIRONMENT'
490 && $node->getEnvironmentClass =~ $type);
491 }
492  
493 sub _list_nestedness
494 {
495 44 ▶ my $self = shift;
Subroutine _list_nestedness: called CC=2 SCAR=6.9
496 44 my ($node) = @_;
497  
498 44 my $nested = 0;
499  
500 44 ▶ for (my $parent = $node->getParent;
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_environment_type($parent, qr/^(?:$regex_list_type)$/)) 40 44 84
501 $self->_is_environment_type($parent, qr/^(?:$regex_list_type)$/);
502 $parent = $parent->getParent
503 ) {
504 40 $nested++;
505 }
506  
507 44 return $nested;
508 }
509  
510 sub _prepend_spaces
511 {
512 4 ▶ my $self = shift;
Subroutine _prepend_spaces: called CC=2 SCAR=7.9
513 4 my ($text) = @_;
514  
515 4 ▶ unless (length $$text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $$text) 0 4 4
516 0 $$text =~ s/^/ /;
517 0 return;
518 }
519  
520 4 $$text =~ s/^/ /gm;
521 }
522  
523 sub _text_setter
524 {
525 34 ▶ my $self = shift;
Subroutine _text_setter: called CC=2 SCAR=6.9
526 34 my ($text) = @_;
527  
528 34 ▶ my $append = any { $self->_is_set_previous($_) } ('list', @text_node_types);
Subroutine __ANON__: called CC=1 SCAR=0.0
529 34 ▶ my $setter = $append ? '_pod_append' : '_pod_add';
Branch: 1 branch
BranchTrueFalseTotal
$append ? : 10 24 34
530  
531 34 $self->$setter($text);
532 }
533  
534 sub _pod_add
535 {
536 144 ▶ my $self = shift;
Subroutine _pod_add: called CC=3 SCAR=11.0
537 144 my ($pod) = @_;
538  
539 144 ▶ if (@{$self->{pod}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"pod"};}) 142 2 144
540 142 $self->{pod}->[-1] =~ s/[\ \t]+$//gm;
541 }
542  
543 144 push @{$self->{pod}}, $pod;
544  
545 144 ▶ $self->_debug($pod) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 144 144
546 }
547  
548 sub _pod_append
549 {
550 30 ▶ my $self = shift;
Subroutine _pod_append: called CC=2 SCAR=7.0
551 30 my ($pod) = @_;
552  
553 30 $self->{pod}->[-1] .= $pod;
554  
555 30 ▶ $self->_debug($pod) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 30 30
556 }
557  
558 sub _debug
559 {
560 0 ▶ my $self = shift;
Subroutine _debug: not called CC=2 SCAR=17.9
561 0 my ($pod) = @_;
562  
563 0 my $re = qr/^.+::(.+)$/;
564  
565 0 ▶ my $frame = (caller(2))[3] =~ /^.+::_text_setter$/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(caller 2)[3] =~ /^.+::_text_setter$/ ? : 0 0 0
566  
567 0 my ($sub) = (caller(2 + $frame))[3] =~ $re;
568 0 my $line = (caller(1 + $frame))[2];
569 0 my ($setter) = (caller(1 + $frame))[3] =~ $re;
570  
571 0 my $index = @{$self->{pod}} - 1;
572  
573 0 printf STDERR ("%-12s(%-25s:%03d):[%3d]\t'%s'\n", $setter, $sub, $line, $index, $pod);
574 }
575  
576 sub _scrub_newlines
577 {
578 38 ▶ my $self = shift;
Subroutine _scrub_newlines: called CC=1 SCAR=0.0
579 38 my ($text) = @_;
580  
581 38 $$text =~ s/^\n+//;
582 38 $$text =~ s/\n+$//;
583 }
584  
585 sub _pod_get
586 {
587 3 ▶ my $self = shift;
Subroutine _pod_get: called CC=1 SCAR=0.0
588  
589 3 return $self->{pod};
590 }
591  
592 sub _pod_finalize
593 {
594 2 ▶ my $self = shift;
Subroutine _pod_finalize: called CC=1 SCAR=0.0
595  
596 2 $self->_pod_add("=cut\n");
597  
598 2 return join "\n\n", @{$self->_pod_get};
599 }
600  
601 sub _register_node
602 {
603 0 ▶ my $self = shift;
Subroutine _register_node: not called CC=1 SCAR=6.9
604 0 my ($item) = @_;
605  
606 0 $self->{node}{$item} = true;
607 }
608  
609 sub _is_set_node
610 {
611 0 ▶ my $self = shift;
Subroutine _is_set_node: not called CC=2 SCAR=17.9
612 0 my ($item) = @_;
613  
614 0 ▶ return $self->{node}{$item} ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'node'}{$item} ? : 0 0 0
615 }
616  
617 sub _unregister_node
618 {
619 0 ▶ my $self = shift;
Subroutine _unregister_node: not called CC=1 SCAR=6.9
620 0 my ($item) = @_;
621  
622 0 delete $self->{node}{$item};
623 }
624  
625 sub _register_previous
626 {
627 56 ▶ my $self = shift;
Subroutine _register_previous: called CC=1 SCAR=0.0
628 56 my ($item) = @_;
629  
630 56 $self->{previous}{$item} = true;
631 }
632  
633 sub _is_set_previous
634 {
635 176 ▶ my $self = shift;
Subroutine _is_set_previous: called CC=3 SCAR=11.0
636 176 my @items = @_;
637  
638 176 my $ok = eval true; # eval in order to avoid fatal errors on some older perls
639  
640 176 foreach my $item (@items) {
641 176 ▶ $ok &= $self->{previous}{$item} ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'previous'}{$item} ? : 36 140 176
642 }
643  
644 176 return $ok;
645 }
646  
647 sub _unregister_previous
648 {
649 90 ▶ my $self = shift;
Subroutine _unregister_previous: called CC=2 SCAR=6.9
650 90 my @items = @_;
651  
652 90 foreach my $item (@items) {
653 190 delete $self->{previous}{$item};
654 }
655 }
656  
657 =head1 NAME
658  
659 LaTeX::Pod - Transform LaTeX source files to POD (Plain old documentation)
660  
661 =head1 SYNOPSIS
662  
663 use LaTeX::Pod;
664  
665 $parser = LaTeX::Pod->new('/path/to/source');
666 print $parser->convert;
667  
668 =head1 DESCRIPTION
669  
670 C<LaTeX::Pod> converts LaTeX sources to Perl's POD (Plain old documentation).
671 Currently only a subset of the available LaTeX language is supported;
672 see L<SUPPORTED LANGUAGE SUBSET> for further details.
673  
674 =head1 CONSTRUCTOR
675  
676 =head2 new
677  
678 The constructor requires that the path to the LaTeX source is defined:
679  
680 $parser = LaTeX::Pod->new('/path/to/source');
681  
682 Returns the parser object.
683  
684 =head1 METHODS
685  
686 =head2 convert
687  
688 There is one public I<method> available, namely C<convert()>:
689  
690 $pod = $parser->convert;
691  
692 Returns the computed POD as a string.
693  
694 =head1 SUPPORTED LANGUAGE SUBSET
695  
696 LaTeX currently supported:
697  
698 =over 4
699  
700 =item * abstracts
701  
702 =item * chapters
703  
704 =item * sections/subsections/subsubsections
705  
706 =item * description, enumerate and itemize lists
707  
708 =item * verbatim blocks (and indentation)
709  
710 =item * plain text
711  
712 =item * bold/italic/code font tags
713  
714 =item * umlauts
715  
716 =item * newlines
717  
718 =item * comments
719  
720 =back
721  
722 =head1 IMPLEMENTATION DETAILS
723  
724 The current implementation is based upon L<LaTeX::TOM> (the framework being
725 used for parsing the LaTeX source) and its clear distinction between various
726 types of nodes. As an example, a C<\chapter> command has a separate text
727 associated with it as its content. C<LaTeX::Pod> uses a "look-behind" mechanism
728 for commands and their corresponding texts since they currently cannot be easily
729 detected without such a mechanism.
730  
731 Thus C<LaTeX::Pod> was designed with the intention to be I<context-sensitive>
732 aware. This is also being aimed at by eventually registering which type of node
733 has been seen before the current one -- useful when constructing logical paragraphs
734 made out of two or more nodes. C<LaTeX::Pod> then finally unregisters the type
735 of node seen when it is no longer required. In addition, a dispatch queue is built
736 internally which is executed after all nodes have been processed.
737  
738 Considering that the POD format has a limited subset of directives, the complexity
739 of keeping track of node occurences appears to be bearable. Leading and trailing
740 newlines will be removed from the node's text extracted where needed; furthermore,
741 trailing spaces and tabs will also be purged from each line of POD resulting.
742  
743 =head1 BUGS & CAVEATS
744  
745 It is highly recommended to ensure that the structure of the LaTeX input file
746 follows the format specification strictly or the parser may B<not> succeed.
747  
748 =head1 SEE ALSO
749  
750 L<LaTeX::TOM>
751  
752 =head1 AUTHOR
753  
754 Steven Schubiger <schubiger@cpan.org>
755  
756 =head1 LICENSE
757  
758 This program is free software; you may redistribute it and/or
759 modify it under the same terms as Perl itself.
760  
761 See L<http://dev.perl.org/licenses/>
762  
763 =cut
↑ Summary Build »