File Coverage

lib/Pod/PseudoPod/Index.pm
Criterion Covered Total %
statement 57 58 98.2
branch 8 10 80.0
condition 6 11 54.5
subroutine 12 13 92.3
pod 1 7 14.2
total 84 99 84.8


line stmt bran cond sub pod time code
1             package Pod::PseudoPod::Index;
2 1     1   6341 use strict;
  1         3  
  1         39  
3 1     1   6 use Carp ();
  1         3  
  1         26  
4 1     1   5 use base qw( Pod::PseudoPod );
  1         1  
  1         386  
5              
6 1     1   10 use vars qw( $VERSION );
  1         3  
  1         647  
7             $VERSION = '0.18';
8              
9             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10              
11             sub new {
12 7     7 1 8712 my $self = shift;
13 7         15 my $index = shift;
14 7         36 my $new = $self->SUPER::new(@_);
15 7   50     64 $new->{'output_fh'} ||= *STDOUT{IO};
16 7         41 $new->accept_targets_as_text( qw(author blockquote comment caution
17             editor epigraph example figure important note production
18             programlisting screen sidebar table tip warning) );
19              
20 7         476 $new->nix_Z_codes(1);
21 7   100     39 $new->{'index'} = $index || {};
22 7         18 $new->{'scratch'} = '';
23 7         19 $new->{'Indent'} = 0;
24 7         15 $new->{'Indentstring'} = ' ';
25 7         23 return $new;
26             }
27              
28             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29              
30 7     7 0 26 sub start_X { $_[0]{'X'} = 1; }
31             sub end_X {
32 7     7 0 11 my $self = shift;
33 7         11 my $text = $self->{'scratch'};
34 7         12 $self->{'scratch'} = '';
35              
36 7   33     32 my $cross_ref = $self->{'index_file'} || $self->set_filename;
37 7         34 &_build_index($self->{'index'},$cross_ref,split(';', $text));
38 7         23 $self->{'X'} = 0;
39             }
40              
41             sub _build_index {
42 7     7   16 my ($node,$cross_ref,@elems) = @_;
43 7         16 foreach my $entry (@elems,undef) {
44 15 100       28 if (defined $entry) {
45 8 50       31 $node->{$entry} = {} unless (defined $node->{$entry});
46 8         14 $node = $node->{$entry};
47             } else {
48 7 50       34 $node->{'page'} = [] unless (defined $node->{'page'});
49 7         8 push @{$node->{'page'}}, $cross_ref;
  7         31  
50             }
51             }
52             }
53              
54 51 100   51 0 216 sub handle_text { $_[0]{'scratch'} .= $_[1] if $_[0]{'X'}; }
55              
56 0     0 0 0 sub get_index { return $_[0]{'index'} }
57              
58             sub output_text {
59 5     5 0 119 my $self = shift;
60 5         14 $self->_print_index($self->{'index'},'');
61 5         10 print {$self->{'output_fh'}} $self->{'scratch'};
  5         62  
62             }
63              
64             # recursively print out index tree structure
65             sub _print_index {
66 13     13   23 my ($self,$node,$indent) = @_;
67 13         16 foreach my $key (sort {lc($a) cmp lc($b)} keys %{$node}) {
  2         12  
  13         45  
68 15 100       30 if ($key eq 'page') {
69 7         11 $self->{'scratch'} .= ', '. join(", ", @{$node->{'page'}});
  7         40  
70             } else {
71 8         17 $self->{'scratch'} .= "\n". $indent. $key;
72 8         31 $self->_print_index($node->{$key}, $indent.' ');
73             }
74             }
75             }
76              
77             sub set_filename {
78 7     7 0 8 my $self = shift;
79 7   50     29 my $file = $self->{'source_filename'} || '';
80 7         11 $file =~ /(\w+)\.pod$/;
81 7   50     36 $self->{'index_file'} = $1 || "0";
82 7         24 return $self->{'index_file'};
83             }
84              
85             1;
86              
87              
88             __END__
89              
90             =head1 NAME
91              
92             Pod::PseudoPod::Index -- format PseudoPod index entries
93              
94             =head1 SYNOPSIS
95              
96             use Pod::PseudoPod::Index;
97              
98             my $parser = Pod::PseudoPod::Index->new();
99              
100             $parser->parse_file('path/to/file1.pod');
101             $parser->parse_file('path/to/file2.pod');
102              
103             $parser->output_text;
104              
105             =head1 DESCRIPTION
106              
107             This class is a formatter that extracts index items from PseudoPod files
108             and renders them as plain text or html.
109              
110             This is a subclass of L<Pod::PseudoPod> and inherits all its methods.
111              
112             =head1 SEE ALSO
113              
114             L<Pod::PseudoPod>
115              
116             =head1 COPYRIGHT
117              
118             Copyright (c) 2004 Allison Randal. All rights reserved.
119              
120             This library is free software; you can redistribute it and/or modify
121             it under the same terms as Perl itself. The full text of the license
122             can be found in the LICENSE file included with this module.
123              
124             This program is distributed in the hope that it will be useful, but
125             without any warranty; without even the implied warranty of
126             merchantability or fitness for a particular purpose.
127              
128             =head1 AUTHOR
129              
130             Allison Randal <allison@perl.org>
131              
132             =cut
133