Perl Filter and Perl API
[idzebra-moved-to-github.git] / perl / demo / pod.pm
1 #!/usr/bin/perl
2 use strict;
3 # ----------------------------------------------------------------------------
4 # A dummy example to demonstrate perl filters for zebra. This is going to
5 # extract information from the .pm perl module files.
6 # ----------------------------------------------------------------------------
7 package pod;
8
9 use IDZebra::Filter;
10 use IDZebra::Data1;
11 use Pod::Text;
12 our @ISA=qw(IDZebra::Filter);
13 1;
14
15
16 sub init {
17     # Initialization code may come here
18 }
19
20 sub process {
21     my ($self, $d1) = @_;
22
23     my $tempfile_in = "/tmp/strucc.in";
24     my $tempfile_out = "/tmp/strucc.out";
25     my $parser = Pod::Text->new (sentence => 0, width => 78);
26
27     my $r1=$d1->mk_root('pod');    
28     my $root=$d1->mk_tag($r1,'pod');
29
30     # This is dirty... Pod::Parser doesn't seems to support 
31     # parsing a string, so we have to write the whole thing out into a 
32     # temporary file
33     open (TMP, ">$tempfile_in");
34     print TMP $self->readall(10240);
35     close (TMP);
36
37     $parser->parse_from_file ($tempfile_in, $tempfile_out);
38
39     my $section;
40     my $data;
41     open (TMP, "$tempfile_out");
42     while(<TMP>) {
43         chomp;
44         if (/^([A-Z]+)\s*$/) {
45             my $ss = $1;
46             if ($section) {
47                 my $tag = $d1->mk_tag($root,$section);
48                 $d1->mk_text($tag,$data) if ($data);
49             }
50             $section = $ss;
51             $data = "";
52             next;
53         }
54         s/^\s+|\s+$//g;
55         $data.="$_\n";
56     }
57
58     if ($section) { 
59         my $tag = $d1->mk_tag($root,$section);
60         $d1->mk_text($tag,$data) if ($data);
61     }
62     close (TMP);
63     
64     return ($r1);
65 }