Pod::PseudoPod::Index - format PseudoPod index entries


Pod-PseudoPod documentation Contained in the Pod-PseudoPod distribution.

Index


Code Index:

NAME

Top

Pod::PseudoPod::Index -- format PseudoPod index entries

SYNOPSIS

Top

  use Pod::PseudoPod::Index;

  my $parser = Pod::PseudoPod::Index->new();

  $parser->parse_file('path/to/file1.pod');
  $parser->parse_file('path/to/file2.pod');

  $parser->output_text;

DESCRIPTION

Top

This class is a formatter that extracts index items from PseudoPod files and renders them as plain text or html.

This is a subclass of Pod::PseudoPod and inherits all its methods.

SEE ALSO

Top

Pod::PseudoPod

COPYRIGHT

Top

AUTHOR

Top

Allison Randal <allison@perl.org>


Pod-PseudoPod documentation Contained in the Pod-PseudoPod distribution.

package Pod::PseudoPod::Index;
use strict;
use Carp ();
use base qw( Pod::PseudoPod );

use vars qw( $VERSION );
$VERSION = '0.11';

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

sub new {
  my $self = shift;
  my $index = shift;
  my $new = $self->SUPER::new(@_);
  $new->{'output_fh'} ||= *STDOUT{IO};
  $new->accept_targets_as_text( qw(author blockquote comment caution
      editor epigraph example figure important note production
      programlisting screen sidebar table tip warning) );

  $new->nix_Z_codes(1);
  $new->{'index'} = $index || {};
  $new->{'scratch'} = '';
  $new->{'Indent'} = 0;
  $new->{'Indentstring'} = '   ';
  return $new;
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

sub start_X { $_[0]{'X'} = 1; }
sub end_X {
  my $self = shift;
  my $text = $self->{'scratch'};
  $self->{'scratch'} = '';

  my $cross_ref = $self->{'index_file'} || $self->set_filename;
  &_build_index($self->{'index'},$cross_ref,split(';', $text));
  $self->{'X'} = 0;
}

sub _build_index {
  my ($node,$cross_ref,@elems) = @_;
  foreach my $entry (@elems,undef) {
    if (defined $entry) {
      $node->{$entry} = {} unless (defined $node->{$entry});
      $node = $node->{$entry};
    } else {
      $node->{'page'} = [] unless (defined $node->{'page'});
      push @{$node->{'page'}}, $cross_ref;
    }
  }
}

sub handle_text { $_[0]{'scratch'} .= $_[1] if $_[0]{'X'}; }

sub get_index { return $_[0]{'index'} }

sub output_text {
  my $self = shift;
  $self->_print_index($self->{'index'},'');
  print {$self->{'output_fh'}} $self->{'scratch'};
}

# recursively print out index tree structure
sub _print_index {
  my ($self,$node,$indent) = @_;
  foreach my $key (sort {lc($a) cmp lc($b)} keys %{$node}) {
    if ($key eq 'page') {
       $self->{'scratch'} .= ', '. join(", ", @{$node->{'page'}});
    } else {
       $self->{'scratch'} .= "\n". $indent. $key;
       $self->_print_index($node->{$key}, $indent.'    ');
    }
  }
}

sub set_filename {
  my $self = shift;
  my $file = $self->{'source_filename'} || '';
  $file =~ /(\w+)\.pod$/;
  $self->{'index_file'} =  $1 || "0";
  return $self->{'index_file'};
}

1;


__END__