Debian Codegraph @

examples/custom-pom2

raw
1 #!/usr/bin/perl -w
2 #
3 # This program demonstrates how you can easily write a custom
4 # processor for converting Pod documents to the format of your
5 # choice in the style of your choice.
6 #
7 # Written by Ron Savage <[email protected]> with some minor
8 # changes by Andy Wardley <[email protected]>.
9 #
10 # Also check out Ron's Perl pages for his own version of this
11 # script, fancy-pom2.pl, which is likely to be further advanced
12 # and more recently updated:
13 #
14 # http://savage.net.au/Perl.html#fancy-pom2.pl
15 #
16
17 use Pod::POM;
18 use File::Basename;
19
20 my $BGCOLOR = '#80C0ff';
21
22 my $program = basename($0);
23 my $format;
24
25 die usage() if grep(/^--?h(elp)?$/, @ARGV);
26
27 my $file = shift
28 || die "usage: $program podfile\n";
29
30 # create a Pod::POM parser
31 my $parser = Pod::POM->new( warn => 1 )
32 || die "$Pod::POM::ERROR\n";
33
34 # parse the file to build a POM
35 my $pom = $parser->parse_file($file)
36 || die $parser->error(), "\n";
37
38 my (@toc, @content);
39
40 # get each =head1 to make a table of content
41 for my $head1 ($pom->head1()) {
42 push(@toc, My::View->print($head1->title()) );
43 }
44
45 # generate HTML for table of contents
46 @toc = map {
47 my $name = $_;
48 $name =~ s/\W+/_/g;
49 "<tr><td align=\"center\"><a href=\"#$name\">$_</a></td></tr>\n"
50 } @toc;
51
52 # generate HTML for page content
53 @content = My::View->print($pom);
54
55 # cleanup file name to use as title
56 $file =~ s|\\|/|g;
57 $file =~ s|.*/||;
58 $file = $1 if ($file =~ /^(.+)\..+$/);
59 $file = ucfirst lc $file;
60
61 # print!
62 print <<EOF;
63 <a name="top"></a>
64
65 <div align="center">
66
67 <h1>Table of Contents</h1>
68
69 <table bgcolor="$BGCOLOR" summary="Table of Contents">
70 @toc
71 </table>
72
73 <h1>$file</h1>
74
75 <table bgcolor="$BGCOLOR" summary="Body">
76 <tr>
77 <td align="left">@content</td>
78 </tr>
79 <tr>
80 <td>&nbsp;</td>
81 </tr>
82 <tr>
83 <td><a href="#top">Top of page</a></td>
84 </tr>
85 </table>
86
87 </div>
88 EOF
89
90
91 #------------------------------------------------------------------------
92 # Here we define a custom view as a subclass of Pod::POM::View::HTML.
93 # You can add any methods here like view_head2(), view_over(), etc.,
94 # to implement different handlers for different elements in the document.
95 #------------------------------------------------------------------------
96
97 package My::View;
98
99 use parent qw( Pod::POM::View::HTML );
100
101 sub view_head1
102 {
103 my($self, $item) = @_;
104
105 # convert non-word characters in name to _
106 my $title = $item->title->present($self);
107 my $name = $title;
108 $name =~ s/\W+/_/g;
109
110 return "<h2><a name=\"$name\">$title</a></h2>\n\n"
111 . $item->content->present($self);
112 }
113
114