File Coverage

blib/lib/Pod/TOC.pm
Criterion Covered Total %
statement 58 60 96.6
branch 15 20 75.0
condition 2 3 66.6
subroutine 17 17 100.0
pod n/a
total 92 100 92.0


line stmt bran cond sub pod time code
1             package Pod::TOC;
2 2     2   1914 use strict;
  2         4  
  2         67  
3              
4 2     2   481 use parent qw( Pod::Simple );
  2         321  
  2         10  
5 2     2   64182 use vars qw( $VERSION );
  2         8  
  2         85  
6              
7 2     2   11 use warnings;
  2         5  
  2         60  
8 2     2   10 no warnings;
  2         4  
  2         191  
9              
10             $VERSION = '1.12';
11              
12 0         0 BEGIN {
13 2     2   18 my @Head_levels = 0 .. 4;
14              
15 2         5 my %flags = map { ( "head$_", $_ ) } @Head_levels;
  10         32  
16              
17 2         10 foreach my $directive ( keys %flags ) {
18 2     2   11 no strict 'refs';
  2         5  
  2         489  
19              
20 10         49 *{"_start_$directive"} = sub {
21 9     9   33 $_[0]->_set_flag( "_start_$directive" );
22 9         16 print { $_[0]->output_fh } "\t" x ( $_[0]->_get_flag - 1 )
  9         31  
23 10         48 };
24              
25 10         1007 *{"_end_$directive"} = sub {
26 9     9   33 $_[0]->_set_flag( "_end_$directive" );
27 9         14 print { $_[0]->output_fh } "\n"
  9         21  
28 10         43 };
29             }
30              
31 18     18   50 sub _is_valid_tag { exists $flags{ $_[1] } }
32 9     9   32 sub _get_tag { $flags{ $_[1] } }
33             }
34              
35             sub _handle_element {
36 42     42   75 my( $self, $element, $args ) = @_;
37              
38 42         162 my $caller_sub = ( caller(1) )[3];
39 42 50       321 return unless $caller_sub =~ s/.*_(start|end)$/_${1}_$element/;
40              
41 42         145 my $sub = $self->can( $caller_sub );
42              
43 42 100       131 $sub->( $self, $args ) if $sub;
44             }
45              
46             sub _handle_element_start {
47 21     21   9520 my $self = shift;
48 21         47 $self->_handle_element( @_ );
49             }
50              
51             sub _handle_element_end {
52 21     21   272 my $self = shift;
53 21         39 $self->_handle_element( @_ );
54             }
55              
56             sub _handle_text {
57 20 100   20   192 return unless $_[0]->_get_flag;
58              
59 9         17 print { $_[0]->output_fh } $_[1];
  9         22  
60             }
61              
62              
63             {
64             my $Flag;
65              
66 29     29   161 sub _get_flag { $Flag }
67              
68             sub _set_flag {
69 18     18   42 my( $self, $caller ) = @_;
70              
71 18 50       41 return unless $caller;
72              
73 18 100       64 my $on = $caller =~ m/^_start_/ ? 1 : 0;
74 18 100       43 my $off = $caller =~ m/^_end_/ ? 1 : 0;
75              
76 18 50 66     62 unless( $on or $off ) { return };
  0         0  
77              
78 18         74 my( $tag ) = $caller =~ m/^_.*?_(.*)/g;
79              
80 18 50       50 return unless $self->_is_valid_tag( $tag );
81              
82 18         56 $Flag = do {
83 18 100       43 if( $on ) { $self->_get_tag( $tag ) } # set the flag if we're on
  9 50       19  
84 9         23 elsif( $off ) { undef } # clear if we're off
85             };
86              
87             }
88             }
89              
90             =encoding utf8
91              
92             =head1 NAME
93              
94             Pod::TOC - Extract a table of contents from a Pod file
95              
96             =head1 SYNOPSIS
97              
98             This is a C subclass, so it can do the same things.
99              
100             use Pod::TOC;
101              
102             my $parser = Pod::TOC->new;
103              
104             my $toc;
105             open my($output_fh), ">", \$toc;
106              
107             $parser->output_fh( $output_fh );
108              
109             $parser->parse_file( $input_file );
110              
111             =head1 DESCRIPTION
112              
113             This is a C subclass to extract a table of contents
114             from a pod file. It has the same interface as C, and
115             only changes the internal bits.
116              
117             =head1 SEE ALSO
118              
119             L, L
120              
121             =head1 SOURCE AVAILABILITY
122              
123             This source is in Github:
124              
125             https://github.com/briandfoy/pod-perldoc-totoc
126              
127             =head1 AUTHOR
128              
129             brian d foy, C<< >>
130              
131             =head1 COPYRIGHT AND LICENSE
132              
133             Copyright © 2006-2015, brian d foy . All rights reserved.
134              
135             You may redistribute this under the same terms as Perl itself.
136              
137             =cut
138              
139              
140             1;