4 use Getopt::Long; # package for handling command-line parameters
7 @EXPORT = qw(pod2html htmlify);
16 Pod::HTML - module to convert pod files to HTML
25 Converts files from pod format (see L<perlpod>) to HTML format. It
26 can automatically generate indexes and cross-references, and it keeps
27 a cache of things it knows how to cross-reference.
31 Pod::Html takes the following arguments:
39 Displays the usage message.
45 Sets the base URL for the HTML files. When cross-references are made,
46 the HTML root is prepended to the URL.
52 Specify the pod file to convert. Input is taken from STDIN if no
59 Specify the HTML file to create. Output goes to STDOUT if no outfile
66 Specify the base directory for finding library pods.
70 --podpath=name:...:name
72 Specify which subdirectories of the podroot contain pod files whose
73 HTML converted forms can be linked-to in cross-references.
77 --libpods=name:...:name
79 List of page names (eg, "perlfunc") which contain linkable C<=item>s.
85 Use Netscape HTML directives when applicable.
91 Do not use Netscape HTML directives (default).
97 Generate an index at the top of the HTML file (default behaviour).
103 Do not generate an index at the top of the HTML file.
110 Recurse into subdirectories specified in podpath (default behaviour).
116 Do not recurse into subdirectories specified in podpath.
122 Specify the title of the resulting HTML file.
128 Display progress messages.
135 "--podpath=lib:ext:pod:vms",
136 "--podroot=/usr/src/perl",
137 "--htmlroot=/perl/nmanual",
138 "--libpods=perlfunc:perlguts:perlvar:perlrun:perlop",
141 "--outfile=/perl/nmanual/foo.html");
145 Tom Christiansen, E<lt>tchrist@perl.comE<gt>.
149 Has trouble with C<> etc in = commands.
157 This program is distributed under the Artistic License.
161 my $dircache = "pod2html-dircache";
162 my $itemcache = "pod2html-itemcache";
164 my @begin_stack = (); # begin/end stack
166 my @libpods = (); # files to search for links from C<> directives
167 my $htmlroot = "/"; # http-server base directory from which all
168 # relative paths in $podpath stem.
169 my $htmlfile = ""; # write to stdout by default
170 my $podfile = ""; # read from stdin by default
171 my @podpath = (); # list of directories containing library pods.
172 my $podroot = "."; # filesystem base directory from which all
173 # relative paths in $podpath stem.
174 my $recurse = 1; # recurse on subdirectories in $podpath.
175 my $verbose = 0; # not verbose by default
176 my $doindex = 1; # non-zero if we should generate an index
177 my $listlevel = 0; # current list depth
178 my @listitem = (); # stack of HTML commands to use when a =item is
179 # encountered. the top of the stack is the
181 my @listdata = (); # similar to @listitem, but for the text after
183 my @listend = (); # similar to @listitem, but the text to use to
185 my $ignore = 1; # whether or not to format text. we don't
186 # format text until we hit our first pod
189 my %items_named = (); # for the multiples of the same item in perlfunc
191 my $netscape = 0; # whether or not to use netscape directives.
192 my $title; # title to give the pod(s)
193 my $top = 1; # true if we are at the top of the doc. used
194 # to prevent the first <HR> directive.
195 my $paragraph; # which paragraph we're processing (used
196 # for error messages)
197 my %pages = (); # associative array used to find the location
198 # of pages referenced by L<> links.
199 my %sections = (); # sections within this page
200 my %items = (); # associative array used to find the location
201 # of =item directives referenced by C<> links
202 my $Is83; # is dos with short filenames (8.3)
205 $dircache = "pod2html-dircache";
206 $itemcache = "pod2html-itemcache";
208 @begin_stack = (); # begin/end stack
210 @libpods = (); # files to search for links from C<> directives
211 $htmlroot = "/"; # http-server base directory from which all
212 # relative paths in $podpath stem.
213 $htmlfile = ""; # write to stdout by default
214 $podfile = ""; # read from stdin by default
215 @podpath = (); # list of directories containing library pods.
216 $podroot = "."; # filesystem base directory from which all
217 # relative paths in $podpath stem.
218 $recurse = 1; # recurse on subdirectories in $podpath.
219 $verbose = 0; # not verbose by default
220 $doindex = 1; # non-zero if we should generate an index
221 $listlevel = 0; # current list depth
222 @listitem = (); # stack of HTML commands to use when a =item is
223 # encountered. the top of the stack is the
225 @listdata = (); # similar to @listitem, but for the text after
227 @listend = (); # similar to @listitem, but the text to use to
229 $ignore = 1; # whether or not to format text. we don't
230 # format text until we hit our first pod
235 $netscape = 0; # whether or not to use netscape directives.
236 $title = ''; # title to give the pod(s)
237 $top = 1; # true if we are at the top of the doc. used
238 # to prevent the first <HR> directive.
239 $paragraph = ''; # which paragraph we're processing (used
240 # for error messages)
241 %sections = (); # sections within this page
243 # These are not reinitialised here but are kept as a cache.
244 # See get_cache and related cache management code.
245 #%pages = (); # associative array used to find the location
246 # of pages referenced by L<> links.
247 #%items = (); # associative array used to find the location
248 # of =item directives referenced by C<> links
259 $Is83 = 0 if (defined (&Dos::UseLFN) && Dos::UseLFN());
261 # cache of %pages and %items from last time we ran pod2html
263 #undef $opt_help if defined $opt_help;
265 # parse the command-line parameters
266 parse_command_line();
268 # set some variables to their default values if necessary
270 unless (@ARGV && $ARGV[0]) {
271 $podfile = "-" unless $podfile; # stdin
272 open(POD, "<$podfile")
273 || die "$0: cannot open $podfile file for input: $!\n";
275 $podfile = $ARGV[0]; # XXX: might be more filenames
278 $htmlfile = "-" unless $htmlfile; # stdout
279 $htmlroot = "" if $htmlroot eq "/"; # so we don't get a //
281 # read the pod a paragraph at a time
282 warn "Scanning for sections in input file(s)\n" if $verbose;
287 # scan the pod for =head[1-6] directives and build an index
288 my $index = scan_headings(\%sections, @poddata);
291 warn "No pod in $podfile\n" if $verbose;
295 # open the output file
296 open(HTML, ">$htmlfile")
297 || die "$0: cannot open $htmlfile file for output: $!\n";
299 # put a title in the HTML file
302 for (my $i = 0; $i < @poddata; $i++) {
303 if ($poddata[$i] =~ /^=head1\s*NAME\b/m) {
304 for my $para ( @poddata[$i, $i+1] ) {
305 last TITLE_SEARCH if ($title) = $para =~ /(\S+\s+-+\s*.*)/s;
311 if (!$title and $podfile =~ /\.pod$/) {
312 # probably a split pod so take first =head[12] as title
313 for (my $i = 0; $i < @poddata; $i++) {
314 last if ($title) = $poddata[$i] =~ /^=head[12]\s*(.*)/;
316 warn "adopted '$title' as title for $podfile\n"
317 if $verbose and $title;
320 warn "$0: no title for $podfile";
321 $podfile =~ /^(.*)(\.[^.\/]+)?$/;
322 $title = ($podfile eq "-" ? 'No Title' : $1);
323 warn "using $title" if $verbose;
325 print HTML <<END_OF_HEAD;
328 <TITLE>$title</TITLE>
335 # load/reload/validate/cache %pages and %items
336 get_cache($dircache, $itemcache, \@podpath, $podroot, $recurse);
338 # scan the pod for =item directives
339 scan_items("", \%items, @poddata);
341 # put an index at the top of the file. note, if $doindex is 0 we
342 # still generate an index, but surround it with an html comment.
343 # that way some other program can extract it if desired.
345 print HTML "<!-- INDEX BEGIN -->\n";
346 print HTML "<!--\n" unless $doindex;
348 print HTML "-->\n" unless $doindex;
349 print HTML "<!-- INDEX END -->\n\n";
350 print HTML "<HR>\n" if $doindex;
352 # now convert this file
353 warn "Converting input file\n" if $verbose;
354 foreach my $i (0..$#poddata) {
357 if (/^(=.*)/s) { # is it a pod directive?
360 if (/^=begin\s+(\S+)\s*(.*)/si) {# =begin
361 process_begin($1, $2);
362 } elsif (/^=end\s+(\S+)\s*(.*)/si) {# =end
364 } elsif (/^=cut/) { # =cut
366 } elsif (/^=pod/) { # =pod
369 next if @begin_stack && $begin_stack[-1] ne 'html';
371 if (/^=(head[1-6])\s+(.*)/s) { # =head[1-6] heading
372 process_head($1, $2);
373 } elsif (/^=item\s*(.*)/sm) { # =item text
375 } elsif (/^=over\s*(.*)/) { # =over N
377 } elsif (/^=back/) { # =back
379 } elsif (/^=for\s+(\S+)\s+(.*)/si) {# =for
383 warn "$0: $podfile: unknown pod directive '$1' in "
384 . "paragraph $paragraph. ignoring.\n";
391 next if @begin_stack && $begin_stack[-1] ne 'html';
393 process_text(\$text, 1);
394 print HTML "$text\n<P>\n\n";
398 # finish off any pending directives
400 print HTML <<END_OF_TAIL;
406 # close the html file
409 warn "Finished\n" if $verbose;
412 ##############################################################################
414 my $usage; # see below
417 warn "$0: $podfile: @_\n" if @_;
421 $usage =<<END_OF_USAGE;
422 Usage: $0 --help --htmlroot=<name> --infile=<name> --outfile=<name>
423 --podpath=<name>:...:<name> --podroot=<name>
424 --libpods=<name>:...:<name> --recurse --verbose --index
425 --netscape --norecurse --noindex
427 --flush - flushes the item and directory caches.
428 --help - prints this message.
429 --htmlroot - http-server base directory from which all relative paths
430 in podpath stem (default is /).
431 --index - generate an index at the top of the resulting html
433 --infile - filename for the pod to convert (input taken from stdin
435 --libpods - colon-separated list of pages to search for =item pod
436 directives in as targets of C<> and implicit links (empty
437 by default). note, these are not filenames, but rather
438 page names like those that appear in L<> links.
439 --netscape - will use netscape html directives when applicable.
440 --nonetscape - will not use netscape directives (default).
441 --outfile - filename for the resulting html file (output sent to
443 --podpath - colon-separated list of directories containing library
444 pods. empty by default.
445 --podroot - filesystem base directory from which all relative paths
446 in podpath stem (default is .).
447 --noindex - don't generate an index at the top of the resulting html.
448 --norecurse - don't recurse on those subdirectories listed in podpath.
449 --recurse - recurse on those subdirectories listed in podpath
451 --title - title that will appear in resulting html file.
452 --verbose - self-explanatory
456 sub parse_command_line {
457 my ($opt_flush,$opt_help,$opt_htmlroot,$opt_index,$opt_infile,$opt_libpods,$opt_netscape,$opt_outfile,$opt_podpath,$opt_podroot,$opt_norecurse,$opt_recurse,$opt_title,$opt_verbose);
458 my $result = GetOptions(
459 'flush' => \$opt_flush,
460 'help' => \$opt_help,
461 'htmlroot=s' => \$opt_htmlroot,
462 'index!' => \$opt_index,
463 'infile=s' => \$opt_infile,
464 'libpods=s' => \$opt_libpods,
465 'netscape!' => \$opt_netscape,
466 'outfile=s' => \$opt_outfile,
467 'podpath=s' => \$opt_podpath,
468 'podroot=s' => \$opt_podroot,
469 'norecurse' => \$opt_norecurse,
470 'recurse!' => \$opt_recurse,
471 'title=s' => \$opt_title,
472 'verbose' => \$opt_verbose,
474 usage("-", "invalid parameters") if not $result;
476 usage("-") if defined $opt_help; # see if the user asked for help
477 $opt_help = ""; # just to make -w shut-up.
479 $podfile = $opt_infile if defined $opt_infile;
480 $htmlfile = $opt_outfile if defined $opt_outfile;
482 @podpath = split(":", $opt_podpath) if defined $opt_podpath;
483 @libpods = split(":", $opt_libpods) if defined $opt_libpods;
485 warn "Flushing item and directory caches\n"
486 if $opt_verbose && defined $opt_flush;
487 unlink($dircache, $itemcache) if defined $opt_flush;
489 $htmlroot = $opt_htmlroot if defined $opt_htmlroot;
490 $podroot = $opt_podroot if defined $opt_podroot;
492 $doindex = $opt_index if defined $opt_index;
493 $recurse = $opt_recurse if defined $opt_recurse;
494 $title = $opt_title if defined $opt_title;
495 $verbose = defined $opt_verbose ? 1 : 0;
496 $netscape = $opt_netscape if defined $opt_netscape;
503 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
504 my @cache_key_args = @_;
506 # A first-level cache:
507 # Don't bother reading the cache files if they still apply
508 # and haven't changed since we last read them.
510 my $this_cache_key = cache_key(@cache_key_args);
512 return if $saved_cache_key and $this_cache_key eq $saved_cache_key;
514 # load the cache of %pages and %items if possible. $tests will be
515 # non-zero if successful.
517 if (-f $dircache && -f $itemcache) {
518 warn "scanning for item cache\n" if $verbose;
519 $tests = load_cache($dircache, $itemcache, $podpath, $podroot);
522 # if we didn't succeed in loading the cache then we must (re)build
525 warn "scanning directories in pod-path\n" if $verbose;
526 scan_podpath($podroot, $recurse, 0);
528 $saved_cache_key = cache_key(@cache_key_args);
532 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
533 return join('!', $dircache, $itemcache, $recurse,
534 @$podpath, $podroot, stat($dircache), stat($itemcache));
538 # load_cache - tries to find if the caches stored in $dircache and $itemcache
539 # are valid caches of %pages and %items. if they are valid then it loads
540 # them and returns a non-zero value.
544 my($dircache, $itemcache, $podpath, $podroot) = @_;
550 open(CACHE, "<$itemcache") ||
551 die "$0: error opening $itemcache for reading: $!\n";
554 # is it the same podpath?
557 $tests++ if (join(":", @$podpath) eq $_);
559 # is it the same podroot?
562 $tests++ if ($podroot eq $_);
564 # load the cache if its good
570 warn "loading item cache\n" if $verbose;
577 warn "scanning for directory cache\n" if $verbose;
578 open(CACHE, "<$dircache") ||
579 die "$0: error opening $dircache for reading: $!\n";
583 # is it the same podpath?
586 $tests++ if (join(":", @$podpath) eq $_);
588 # is it the same podroot?
591 $tests++ if ($podroot eq $_);
593 # load the cache if its good
599 warn "loading directory cache\n" if $verbose;
611 # scan_podpath - scans the directories specified in @podpath for directories,
612 # .pod files, and .pm files. it also scans the pod files specified in
613 # @libpods for =item directives.
616 my($podroot, $recurse, $append) = @_;
618 my($libpod, $dirname, $pod, @files, @poddata);
625 # scan each directory listed in @podpath
628 || die "$0: error changing to directory $podroot: $!\n";
629 foreach $dir (@podpath) {
630 scan_dir($dir, $recurse);
633 # scan the pods listed in @libpods for =item directives
634 foreach $libpod (@libpods) {
635 # if the page isn't defined then we won't know where to find it
637 next unless defined $pages{$libpod} && $pages{$libpod};
639 # if there is a directory then use the .pod and .pm files within it.
640 if ($pages{$libpod} =~ /([^:]*[^(\.pod|\.pm)]):/) {
641 # find all the .pod and .pm files within the directory
643 opendir(DIR, $dirname) ||
644 die "$0: error opening directory $dirname: $!\n";
645 @files = grep(/(\.pod|\.pm)$/ && ! -d $_, readdir(DIR));
648 # scan each .pod and .pm file for =item directives
649 foreach $pod (@files) {
650 open(POD, "<$dirname/$pod") ||
651 die "$0: error opening $dirname/$pod for input: $!\n";
655 scan_items("$dirname/$pod", @poddata);
658 # use the names of files as =item directives too.
659 foreach $pod (@files) {
660 $pod =~ /^(.*)(\.pod|\.pm)$/;
661 $items{$1} = "$dirname/$1.html" if $1;
663 } elsif ($pages{$libpod} =~ /([^:]*\.pod):/ ||
664 $pages{$libpod} =~ /([^:]*\.pm):/) {
665 # scan the .pod or .pm file for =item directives
667 open(POD, "<$pod") ||
668 die "$0: error opening $pod for input: $!\n";
672 scan_items("$pod", @poddata);
674 warn "$0: shouldn't be here (line ".__LINE__."\n";
677 @poddata = (); # clean-up a bit
680 || die "$0: error changing to directory $pwd: $!\n";
682 # cache the item list for later use
683 warn "caching items for later use\n" if $verbose;
684 open(CACHE, ">$itemcache") ||
685 die "$0: error open $itemcache for writing: $!\n";
687 print CACHE join(":", @podpath) . "\n$podroot\n";
688 foreach my $key (keys %items) {
689 print CACHE "$key $items{$key}\n";
694 # cache the directory list for later use
695 warn "caching directories for later use\n" if $verbose;
696 open(CACHE, ">$dircache") ||
697 die "$0: error open $dircache for writing: $!\n";
699 print CACHE join(":", @podpath) . "\n$podroot\n";
700 foreach my $key (keys %pages) {
701 print CACHE "$key $pages{$key}\n";
708 # scan_dir - scans the directory specified in $dir for subdirectories, .pod
709 # files, and .pm files. notes those that it finds. this information will
710 # be used later in order to figure out where the pages specified in L<>
711 # links are on the filesystem.
714 my($dir, $recurse) = @_;
715 my($t, @subdirs, @pods, $pod, $dirname, @dirs);
721 opendir(DIR, $dir) ||
722 die "$0: error opening directory $dir: $!\n";
723 while (defined($_ = readdir(DIR))) {
724 if (-d "$dir/$_" && $_ ne "." && $_ ne "..") { # directory
725 $pages{$_} = "" unless defined $pages{$_};
726 $pages{$_} .= "$dir/$_:";
728 } elsif (/\.pod$/) { # .pod
730 $pages{$_} = "" unless defined $pages{$_};
731 $pages{$_} .= "$dir/$_.pod:";
732 push(@pods, "$dir/$_.pod");
733 } elsif (/\.pm$/) { # .pm
735 $pages{$_} = "" unless defined $pages{$_};
736 $pages{$_} .= "$dir/$_.pm:";
737 push(@pods, "$dir/$_.pm");
742 # recurse on the subdirectories if necessary
744 foreach my $subdir (@subdirs) {
745 scan_dir("$dir/$subdir", $recurse);
751 # scan_headings - scan a pod file for head[1-6] tags, note the tags, and
755 my($sections, @data) = @_;
756 my($tag, $which_head, $title, $listdepth, $index);
758 # here we need local $ignore = 0;
759 # unfortunately, we can't have it, because $ignore is lexical
765 # scan for =head directives, note their name, and build an index
766 # pointing to each of them.
767 foreach my $line (@data) {
768 if ($line =~ /^=(head)([1-6])\s+(.*)/) {
769 ($tag,$which_head, $title) = ($1,$2,$3);
771 $$sections{htmlify(0,$title)} = 1;
773 if ($which_head > $listdepth) {
774 $index .= "\n" . ("\t" x $listdepth) . "<UL>\n";
775 } elsif ($which_head < $listdepth) {
777 $index .= "\n" . ("\t" x $listdepth) . "</UL>\n";
779 $listdepth = $which_head;
781 $index .= "\n" . ("\t" x $listdepth) . "<LI>" .
782 "<A HREF=\"#" . htmlify(0,$title) . "\">" .
783 process_text(\$title, 0) . "</A>";
787 # finish off the lists
788 while ($listdepth--) {
789 $index .= "\n" . ("\t" x $listdepth) . "</UL>\n";
792 # get rid of bogus lists
793 $index =~ s,\t*<UL>\s*</UL>\n,,g;
795 $ignore = 1; # restore old value;
801 # scan_items - scans the pod specified by $pod for =item directives. we
802 # will use this information later on in resolving C<> links.
805 my($pod, @poddata) = @_;
810 $pod .= ".html" if $pod;
812 foreach $i (0..$#poddata) {
815 # remove any formatting instructions
816 s,[A-Z]<([^<>]*)>,$1,g;
818 # figure out what kind of item it is and get the first word of
820 if (/^=item\s+(\w*)\s*.*$/s) {
821 if ($1 eq "*") { # bullet list
822 /\A=item\s+\*\s*(.*?)\s*\Z/s;
824 } elsif ($1 =~ /^[0-9]+/) { # numbered list
825 /\A=item\s+[0-9]+\.?(.*?)\s*\Z/s;
828 # /\A=item\s+(.*?)\s*\Z/s;
833 $items{$item} = "$pod" if $item;
839 # process_head - convert a pod head[1-6] tag and convert it to HTML format.
842 my($tag, $heading) = @_;
845 # figure out the level of the =head
846 $tag =~ /head([1-6])/;
849 # can't have a heading full of spaces and speechmarks and so on
850 $firstword = $heading; $firstword =~ s/\s*(\w+)\s.*/$1/;
852 print HTML "<P>\n" unless $listlevel;
853 print HTML "<HR>\n" unless $listlevel || $top;
854 print HTML "<H$level>"; # unless $listlevel;
855 #print HTML "<H$level>" unless $listlevel;
856 my $convert = $heading; process_text(\$convert, 0);
857 print HTML '<A NAME="' . htmlify(0,$heading) . "\">$convert</A>";
858 print HTML "</H$level>"; # unless $listlevel;
863 # process_item - convert a pod item tag and convert it to HTML format.
867 my($i, $quote, $name);
869 my $need_preamble = 0;
873 # lots of documents start a list without doing an =over. this is
874 # bad! but, the proper thing to do seems to be to just assume
875 # they did do an =over. so warn them once and then continue.
876 warn "$0: $podfile: unexpected =item directive in paragraph $paragraph. ignoring.\n"
878 process_over() unless $listlevel;
880 return unless $listlevel;
882 # remove formatting instructions from the text
883 1 while $text =~ s/[A-Z]<([^<>]*)>/$1/g;
886 $need_preamble = $items_seen[$listlevel]++ == 0;
888 # check if this is the first =item after an =over
890 my $need_new = $listlevel >= @listitem;
892 if ($text =~ /\A\*/) { # bullet
894 if ($need_preamble) {
895 push(@listend, "</UL>");
899 print HTML "<LI><STRONG>";
900 $text =~ /\A\*\s*(.*)\Z/s;
901 print HTML "<A NAME=\"item_" . htmlify(1,$1) . "\">" if $1 && !$items_named{$1}++;
903 #print HTML process_puretext($1, \$quote);
905 print HTML "</A>" if $1;
906 print HTML "</STRONG>";
908 } elsif ($text =~ /\A[0-9#]+/) { # numbered list
910 if ($need_preamble) {
911 push(@listend, "</OL>");
915 print HTML "<LI><STRONG>";
916 $text =~ /\A[0-9]+\.?(.*)\Z/s;
917 print HTML "<A NAME=\"item_" . htmlify(0,$1) . "\">" if $1;
919 #print HTML process_puretext($1, \$quote);
921 print HTML "</A>" if $1;
922 print HTML "</STRONG>";
924 } else { # all others
926 if ($need_preamble) {
927 push(@listend, '</DL>');
931 print HTML "<DT><STRONG>";
932 print HTML "<A NAME=\"item_" . htmlify(1,$text) . "\">"
933 if $text && !$items_named{($text =~ /(\S+)/)[0]}++;
934 # preceding craziness so that the duplicate leading bits in
935 # perlfunc work to find just the first one. otherwise
936 # open etc would have many names
938 #print HTML process_puretext($text, \$quote);
940 print HTML "</A>" if $text;
941 print HTML "</STRONG>";
950 # process_over - process a pod over tag and start a corresponding HTML
959 # process_back - process a pod back tag and convert it to HTML format.
962 warn "$0: $podfile: unexpected =back directive in paragraph $paragraph. ignoring.\n"
964 return unless $listlevel;
966 # close off the list. note, I check to see if $listend[$listlevel] is
967 # defined because an =item directive may have never appeared and thus
968 # $listend[$listlevel] may have never been initialized.
970 print HTML $listend[$listlevel] if defined $listend[$listlevel];
973 # don't need the corresponding perl code anymore
982 # process_cut - process a pod cut tag, thus stop ignoring pod directives.
989 # process_pod - process a pod pod tag, thus ignore pod directives until we see a
993 # no need to set $ignore to 0 cause the main loop did it
997 # process_for - process a =for pod tag. if it's for html, split
998 # it out verbatim, otherwise ignore it.
1001 my($whom, $text) = @_;
1002 if ( $whom =~ /^(pod2)?html$/i) {
1008 # process_begin - process a =begin pod tag. this pushes
1009 # whom we're beginning on the begin stack. if there's a
1010 # begin stack, we only print if it us.
1013 my($whom, $text) = @_;
1015 push (@begin_stack, $whom);
1016 if ( $whom =~ /^(pod2)?html$/) {
1017 print HTML $text if $text;
1022 # process_end - process a =end pod tag. pop the
1023 # begin stack. die if we're mismatched.
1026 my($whom, $text) = @_;
1028 if ($begin_stack[-1] ne $whom ) {
1029 die "Unmatched begin/end at chunk $paragraph\n"
1035 # process_text - handles plaintext that appears in the input pod file.
1036 # there may be pod commands embedded within the text so those must be
1037 # converted to html commands.
1040 my($text, $escapeQuotes) = @_;
1041 my($result, $rest, $s1, $s2, $s3, $s4, $match, $bf);
1042 my($podcommand, $params, $tag, $quote);
1046 $quote = 0; # status of double-quote conversion
1050 if ($rest =~ /^\s+/) { # preformatted text, no pod directives
1054 1 while $line =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
1058 $rest =~ s/&/&/g;
1059 $rest =~ s/</</g;
1060 $rest =~ s/>/>/g;
1061 $rest =~ s/"/"/g;
1063 # try and create links for all occurrences of perl.* within
1064 # the preformatted text.
1068 if (defined $pages{$2}) { # is a link
1069 qq($1<A HREF="$htmlroot/$pages{$2}">$2</A>);
1070 } elsif (defined $pages{dosify($2)}) { # is a link
1071 qq($1<A HREF="$htmlroot/$pages{dosify($2)}">$2</A>);
1076 $rest =~ s/(<A HREF=)([^>:]*:)?([^>:]*)\.pod:([^>:]*:)?/$1$3.html/g;
1078 my $urls = '(' . join ('|', qw{
1091 my $gunk = '/#~:.?+=&%@!\-';
1093 my $any = "${ltrs}${gunk}${punc}";
1096 \b # start at word boundary
1098 $urls : # need resource and a colon
1099 [$any] +? # followed by on or more
1100 # of any valid character, but
1101 # be conservative and take only
1102 # what you need to....
1104 (?= # look-ahead non-consumptive assertion
1105 [$punc]* # either 0 or more puntuation
1106 [^$any] # followed by a non-url char
1108 $ # then end of the string
1110 }{<A HREF="$1">$1</A>}igox;
1112 $result = "<PRE>" # text should be as it is (verbatim)
1115 } else { # formatted text
1116 # parse through the string, stopping each time we find a
1117 # pod-escape. once the string has been throughly processed
1119 while (length $rest) {
1120 # check to see if there are any possible pod directives in
1121 # the remaining part of the text.
1122 if ($rest =~ m/[BCEIFLSZ]</) {
1123 warn "\$rest\t= $rest\n" unless
1130 $s1 = $1; # pure text
1131 $s2 = $2; # the type of pod-escape that follows
1133 $s4 = $3; # the rest of the string
1141 if ($s3 eq '<' && $s2) { # a pod-escape
1142 $result .= ($escapeQuotes ? process_puretext($s1, \$quote) : $s1);
1143 $podcommand = "$s2<";
1146 # find the matching '>'
1149 while ($match && !$bf) {
1151 if ($rest =~ /\A([^<>]*[BCEIFLSZ]<)(.*)\Z/s) {
1156 } elsif ($rest =~ /\A([^>]*>)(.*)\Z/s) {
1166 $0: $podfile: cannot find matching > for $s2 in paragraph $paragraph.
1168 $result .= substr $podcommand, 0, 2;
1169 $rest = substr($podcommand, 2) . $rest;
1173 # pull out the parameters to the pod-escape
1174 $podcommand =~ /^([BCFEILSZ]?)<(.*)>$/s;
1178 # process the text within the pod-escape so that any escapes
1179 # which must occur do.
1180 process_text(\$params, 0) unless $tag eq 'L';
1183 if (!$tag || $tag eq " ") { # <> : no tag
1184 $s1 = "<$params>";
1185 } elsif ($tag eq "L") { # L<> : link
1186 $s1 = process_L($params);
1187 } elsif ($tag eq "I" || # I<> : italicize text
1188 $tag eq "B" || # B<> : bold text
1189 $tag eq "F") { # F<> : file specification
1190 $s1 = process_BFI($tag, $params);
1191 } elsif ($tag eq "C") { # C<> : literal code
1192 $s1 = process_C($params, 1);
1193 } elsif ($tag eq "E") { # E<> : escape
1194 $s1 = process_E($params);
1195 } elsif ($tag eq "Z") { # Z<> : zero-width character
1196 $s1 = process_Z($params);
1197 } elsif ($tag eq "S") { # S<> : non-breaking space
1198 $s1 = process_S($params);
1199 } elsif ($tag eq "X") { # S<> : non-breaking space
1200 $s1 = process_X($params);
1202 warn "$0: $podfile: unhandled tag '$tag' in paragraph $paragraph\n";
1207 # for pure text we must deal with implicit links and
1208 # double-quotes among other things.
1209 $result .= ($escapeQuotes ? process_puretext("$s1$s2$s3", \$quote) : "$s1$s2$s3");
1219 $rest =~ s/&/&/g;
1220 $rest =~ s/</</g;
1221 $rest =~ s/>/>/g;
1222 $rest =~ s/"/"/g;
1227 # process_puretext - process pure text (without pod-escapes) converting
1228 # double-quotes and handling implicit C<> links.
1230 sub process_puretext {
1231 my($text, $quote) = @_;
1232 my(@words, $result, $rest, $lead, $trail);
1234 # convert double-quotes to single-quotes
1235 $text =~ s/\A([^"]*)"/$1''/s if $$quote;
1236 while ($text =~ s/\A([^"]*)["]([^"]*)["]/$1``$2''/sg) {}
1238 $$quote = ($text =~ m/"/ ? 1 : 0);
1239 $text =~ s/\A([^"]*)"/$1``/s if $$quote;
1241 # keep track of leading and trailing white-space
1242 $lead = ($text =~ /\A(\s*)/s ? $1 : "");
1243 $trail = ($text =~ /(\s*)\Z/s ? $1 : "");
1245 # collapse all white space into a single space
1247 @words = split(" ", $text);
1249 # process each word individually
1250 foreach my $word (@words) {
1251 # see if we can infer a link
1252 if ($word =~ /^\w+\(/) {
1253 # has parenthesis so should have been a C<> ref
1254 $word = process_C($word);
1255 # $word =~ /^[^()]*]\(/;
1256 # if (defined $items{$1} && $items{$1}) {
1257 # $word = "\n<CODE><A HREF=\"$htmlroot/$items{$1}#item_"
1258 # . htmlify(0,$word)
1259 # . "\">$word</A></CODE>";
1260 # } elsif (defined $items{$word} && $items{$word}) {
1261 # $word = "\n<CODE><A HREF=\"$htmlroot/$items{$word}#item_"
1262 # . htmlify(0,$word)
1263 # . "\">$word</A></CODE>";
1265 # $word = "\n<CODE><A HREF=\"#item_"
1266 # . htmlify(0,$word)
1267 # . "\">$word</A></CODE>";
1269 } elsif ($word =~ /^[\$\@%&*]+\w+$/) {
1270 # perl variables, should be a C<> ref
1271 $word = process_C($word, 1);
1272 } elsif ($word =~ m,^\w+://\w,) {
1274 $word = qq(<A HREF="$word">$word</A>);
1275 } elsif ($word =~ /[\w.-]+\@\w+\.\w/) {
1276 # looks like an e-mail address
1277 $word = qq(<A HREF="MAILTO:$word">$word</A>);
1278 } elsif ($word !~ /[a-z]/ && $word =~ /[A-Z]/) { # all uppercase?
1279 $word = html_escape($word) if $word =~ /[&<>]/;
1280 $word = "\n<FONT SIZE=-1>$word</FONT>" if $netscape;
1282 $word = html_escape($word) if $word =~ /[&<>]/;
1286 # build a new string based upon our conversion
1288 $rest = join(" ", @words);
1289 while (length($rest) > 75) {
1290 if ( $rest =~ m/^(.{0,75})\s(.*?)$/o ||
1291 $rest =~ m/^(\S*)\s(.*?)$/o) {
1296 $result .= "$rest\n";
1300 $result .= $rest if $rest;
1302 # restore the leading and trailing white-space
1303 $result = "$lead$result$trail";
1309 # pre_escape - convert & in text to $amp;
1314 $$str =~ s,&,&,g;
1318 # dosify - convert filenames to 8.3
1324 $str =~ s/(\.\w+)/substr ($1,0,4)/ge;
1325 $str =~ s/(\w+)/substr ($1,0,8)/ge;
1331 # process_L - convert a pod L<> directive to a corresponding HTML link.
1332 # most of the links made are inferred rather than known about directly
1333 # (i.e it's not known whether the =head\d section exists in the target file,
1334 # or whether a .pod file exists in the case of split files). however, the
1335 # guessing usually works.
1337 # Unlike the other directives, this should be called with an unprocessed
1338 # string, else tags in the link won't be matched.
1342 my($s1, $s2, $linktext, $page, $page83, $section, $link); # work strings
1344 $str =~ s/\n/ /g; # undo word-wrapped tags
1347 # LREF: a la HREF L<show this text|man/section>
1348 $linktext = $1 if s:^([^|]+)\|::;
1350 # a :: acts like a /
1353 # make sure sections start with a /
1355 s,^,/,g if (!m,/, && / /);
1357 # check if there's a section specified
1358 if (m,^(.*?)/"?(.*?)"?$,) { # yes
1359 ($page, $section) = ($1, $2);
1361 ($page, $section) = ($str, "");
1364 # check if we know that this is a section in this page
1365 if (!defined $pages{$page} && defined $sections{$page}) {
1371 $page83=dosify($page);
1372 $page=$page83 if (defined $pages{$page83});
1374 $link = "#" . htmlify(0,$section);
1375 $linktext = $section unless defined($linktext);
1376 } elsif (!defined $pages{$page}) {
1377 warn "$0: $podfile: cannot resolve L<$str> in paragraph $paragraph: no such page '$page'\n";
1379 $linktext = $page unless defined($linktext);
1381 $linktext = ($section ? "$section" : "the $page manpage") unless defined($linktext);
1382 $section = htmlify(0,$section) if $section ne "";
1384 # if there is a directory by the name of the page, then assume that an
1385 # appropriate section will exist in the subdirectory
1386 if ($section ne "" && $pages{$page} =~ /([^:]*[^(\.pod|\.pm)]):/) {
1387 $link = "$htmlroot/$1/$section.html";
1389 # since there is no directory by the name of the page, the section will
1390 # have to exist within a .html of the same name. thus, make sure there
1391 # is a .pod or .pm that might become that .html
1393 $section = "#$section";
1394 # check if there is a .pod with the page name
1395 if ($pages{$page} =~ /([^:]*)\.pod:/) {
1396 $link = "$htmlroot/$1.html$section";
1397 } elsif ($pages{$page} =~ /([^:]*)\.pm:/) {
1398 $link = "$htmlroot/$1.html$section";
1400 warn "$0: $podfile: cannot resolve L$str in paragraph $paragraph: ".
1401 "no .pod or .pm found\n";
1403 $linktext = $section unless defined($linktext);
1408 process_text(\$linktext, 0);
1410 $s1 = "<A HREF=\"$link\">$linktext</A>";
1412 $s1 = "<EM>$linktext</EM>";
1418 # process_BFI - process any of the B<>, F<>, or I<> pod-escapes and
1419 # convert them to corresponding HTML directives.
1422 my($tag, $str) = @_;
1423 my($s1); # work string
1424 my(%repltext) = ( 'B' => 'STRONG',
1428 # extract the modified text and convert to HTML
1429 $s1 = "<$repltext{$tag}>$str</$repltext{$tag}>";
1434 # process_C - process the C<> pod-escape.
1437 my($str, $doref) = @_;
1441 $s1 =~ s/\([^()]*\)//g; # delete parentheses
1443 $s1 =~ s/\W//g; # delete bogus characters
1445 # if there was a pod file that we found earlier with an appropriate
1446 # =item directive, then create a link to that page.
1447 if ($doref && defined $items{$s1}) {
1448 $s1 = ($items{$s1} ?
1449 "<A HREF=\"$htmlroot/$items{$s1}#item_" . htmlify(0,$s2) . "\">$str</A>" :
1450 "<A HREF=\"#item_" . htmlify(0,$s2) . "\">$str</A>");
1451 $s1 =~ s,(perl\w+/(\S+)\.html)#item_\2\b,$1,;
1452 confess "s1 has space: $s1" if $s1 =~ /HREF="[^"]*\s[^"]*"/;
1454 $s1 = "<CODE>$str</CODE>";
1455 # warn "$0: $podfile: cannot resolve C<$str> in paragraph $paragraph\n" if $verbose
1463 # process_E - process the E<> pod directive which seems to escape a character.
1469 s,([^/].*),\&$1\;,g;
1476 # process_Z - process the Z<> pod directive which really just amounts to
1477 # ignoring it. this allows someone to start a paragraph with an =
1482 # there is no equivalent in HTML for this so just ignore it.
1488 # process_S - process the S<> pod directive which means to convert all
1489 # spaces in the string to non-breaking spaces (in HTML-eze).
1494 # convert all spaces in the text to non-breaking spaces in HTML.
1495 $str =~ s/ / /g;
1500 # process_X - this is supposed to make an index entry. we'll just
1509 # finish_list - finish off any pending HTML lists. this should be called
1510 # after the entire pod file has been read and converted.
1513 while ($listlevel >= 0) {
1514 print HTML "</DL>\n";
1520 # htmlify - converts a pod section specification to a suitable section
1521 # specification for HTML. if first arg is 1, only takes 1st word.
1524 my($compact, $heading) = @_;
1527 $heading =~ /^(\w+)/;
1531 # $heading = lc($heading);
1532 $heading =~ s/[^\w\s]/_/g;
1533 $heading =~ s/(\s+)/ /g;
1534 $heading =~ s/^\s*(.*?)\s*$/$1/s;
1535 $heading =~ s/ /_/g;
1536 $heading =~ s/\A(.{32}).*\Z/$1/s;
1537 $heading =~ s/\s+\Z//;
1538 $heading =~ s/_{2,}/_/g;