File Coverage

blib/lib/Pod/Index/Builder.pm
Criterion Covered Total %
statement 71 74 95.9
branch 11 20 55.0
condition 1 6 16.6
subroutine 15 15 100.0
pod 2 9 22.2
total 100 124 80.6


line stmt bran cond sub pod time code
1             package Pod::Index::Builder;
2              
3 1     1   3182 use 5.008;
  1         5  
  1         78  
4             $VERSION = '0.14';
5              
6 1     1   7 use strict;
  1         2  
  1         35  
7 1     1   17 use warnings;
  1         1  
  1         44  
8              
9 1     1   5 use base qw(Pod::Parser);
  1         1  
  1         109  
10 1     1   540 use Pod::Index::Entry;
  1         4  
  1         37  
11 1     1   8 use File::Spec;
  1         1  
  1         851  
12              
13             ####### Pod::Parser overriden methods
14              
15             sub begin_input {
16 1     1 0 949 my ($self) = @_;
17 1         123 $self->{pi_breadcrumbs} = [];
18             }
19              
20 4     4 0 139 sub verbatim {
21             #my ($self, $text, $line_num, $pod_para) = @_;
22             # do nothing
23             }
24              
25             sub textblock {
26 11     11 0 13 my ($self, $text, $line_num, $pod_para) = @_;
27 11         55 $self->{pi_pos} = [$pod_para->file_line, [ @{$self->{pi_breadcrumbs}} ] ];
  11         34  
28 11         584 $self->interpolate($text, $line_num);
29 11         617 return;
30             }
31              
32             sub command {
33 15     15 0 27 my ($self, $cmd, $text, $line_num, $pod_para) = @_;
34 15         22 my $breadcrumbs = $self->{pi_breadcrumbs};
35 15 100       39 if ($cmd =~ /head(\d)/) {
36 5         8 my $level = $1;
37 5 100       21 splice @$breadcrumbs, $level-1 if @$breadcrumbs >= $level;
38 5         41 $self->{pi_pos} = [$pod_para->file_line, [ @$breadcrumbs ] ];
39 5         503 my $s = $self->interpolate($text, $line_num);
40 5         15 $self->{pi_breadcrumbs}[$level - 1] = $s;
41             } else {
42 10         58 $self->{pi_pos} = [$pod_para->file_line, [ @$breadcrumbs ] ];
43 10         483 $self->interpolate($text, $line_num);
44             }
45 15         793 return;
46             }
47              
48             sub interior_sequence {
49 17     17 0 35 my ($self, $seq_command, $seq_argument, $seq_obj) = @_ ;
50 17 50       50 if ($seq_command eq 'X') {
51 17         33 $self->add_entry($seq_argument);
52 17         1143 return '';
53             }
54 0         0 return $seq_argument;
55             }
56              
57              
58             ###### new methods
59              
60 1     1 1 4 sub pod_index { shift->{pi_pod_index} }
61              
62             sub add_entry {
63 17     17 0 19 my ($self, $keyword) = @_;
64              
65 17         17 my ($filename, $line, $breadcrumbs) = @{$self->{pi_pos}};
  17         30  
66              
67 17         62 my $podname = $self->path2package($filename);
68              
69 17         24 my $context = $breadcrumbs->[-1];
70 17 100       38 $context = '' unless defined $context;
71 17         42 $context =~ s/\n.*//s;
72              
73 17         54 my $entry = Pod::Index::Entry->new(
74             keyword => $keyword,
75             filename => $filename,
76             podname => $podname,
77             line => $line,
78             context => $context,
79             );
80              
81 17         20 push @{$self->{pi_pod_index}{lc $keyword}}, $entry;
  17         73  
82             }
83              
84             sub path2package {
85 17     17 0 22 my ($self, $pathname) = @_;
86              
87 17         2338 my $relname = File::Spec->abs2rel($pathname, $self->{pi_base});
88              
89 17         162 my ($volume, $dirstring, $file) = File::Spec->splitpath($relname);
90 17         69 my @dirs = File::Spec->splitdir($dirstring);
91              
92 17 50       42 pop @dirs if ($dirs[-1] eq ''); # in case there was a trailing slash
93 17         64 $file =~ s/\.\w+$//;
94              
95 17         35 my $package = join('::',@dirs,$file);
96 17         42 return $package;
97             }
98              
99             sub print_index {
100 1     1 1 2232 my ($self, $f) = @_;
101              
102             # figure out filehandle
103 1         2 my $fh;
104 1 50 33     10 if ($f and !ref $f) {
    50          
105 0 0       0 open $fh, ">", $f or die "couldn't open $f: $!\n";
106             } elsif ($f) {
107 1         3 $fh = $f;
108             } else {
109 0   0     0 $fh ||= *STDOUT;
110             }
111              
112             # print out the index
113 1         3 my $idx = $self->pod_index;
114 1 50       9 for my $key (
  36         67  
115             sort {
116             $a cmp $b
117             or $idx->{$a}{keyword} cmp $idx->{$b}{keyword}
118             } keys %$idx
119             ) {
120 15 0       14 for my $entry (
  2         9  
121             sort {
122 15         31 $a->{podname} cmp $b->{podname}
123             or $a->{line} <=> $b->{line}
124             } @{$idx->{$key}}
125             ) {
126 17         82 print $fh join("\t", @$entry{qw(keyword podname line context)}), "\n";
127             }
128             }
129             }
130              
131             1;
132              
133             __END__