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 while ($which_head != $listdepth) {
774 if ($which_head > $listdepth) {
775 $index .= "\n" . ("\t" x $listdepth) . "<UL>\n";
777 } elsif ($which_head < $listdepth) {
779 $index .= "\n" . ("\t" x $listdepth) . "</UL>\n";
783 $index .= "\n" . ("\t" x $listdepth) . "<LI>" .
784 "<A HREF=\"#" . htmlify(0,$title) . "\">" .
785 process_text(\$title, 0) . "</A>";
789 # finish off the lists
790 while ($listdepth--) {
791 $index .= "\n" . ("\t" x $listdepth) . "</UL>\n";
794 # get rid of bogus lists
795 $index =~ s,\t*<UL>\s*</UL>\n,,g;
797 $ignore = 1; # restore old value;
803 # scan_items - scans the pod specified by $pod for =item directives. we
804 # will use this information later on in resolving C<> links.
807 my($pod, @poddata) = @_;
812 $pod .= ".html" if $pod;
814 foreach $i (0..$#poddata) {
817 # remove any formatting instructions
818 s,[A-Z]<([^<>]*)>,$1,g;
820 # figure out what kind of item it is and get the first word of
822 if (/^=item\s+(\w*)\s*.*$/s) {
823 if ($1 eq "*") { # bullet list
824 /\A=item\s+\*\s*(.*?)\s*\Z/s;
826 } elsif ($1 =~ /^[0-9]+/) { # numbered list
827 /\A=item\s+[0-9]+\.?(.*?)\s*\Z/s;
830 # /\A=item\s+(.*?)\s*\Z/s;
835 $items{$item} = "$pod" if $item;
841 # process_head - convert a pod head[1-6] tag and convert it to HTML format.
844 my($tag, $heading) = @_;
847 # figure out the level of the =head
848 $tag =~ /head([1-6])/;
851 # can't have a heading full of spaces and speechmarks and so on
852 $firstword = $heading; $firstword =~ s/\s*(\w+)\s.*/$1/;
854 print HTML "<P>\n" unless $listlevel;
855 print HTML "<HR>\n" unless $listlevel || $top;
856 print HTML "<H$level>"; # unless $listlevel;
857 #print HTML "<H$level>" unless $listlevel;
858 my $convert = $heading; process_text(\$convert, 0);
859 print HTML '<A NAME="' . htmlify(0,$heading) . "\">$convert</A>";
860 print HTML "</H$level>"; # unless $listlevel;
865 # process_item - convert a pod item tag and convert it to HTML format.
869 my($i, $quote, $name);
871 my $need_preamble = 0;
875 # lots of documents start a list without doing an =over. this is
876 # bad! but, the proper thing to do seems to be to just assume
877 # they did do an =over. so warn them once and then continue.
878 warn "$0: $podfile: unexpected =item directive in paragraph $paragraph. ignoring.\n"
880 process_over() unless $listlevel;
882 return unless $listlevel;
884 # remove formatting instructions from the text
885 1 while $text =~ s/[A-Z]<([^<>]*)>/$1/g;
888 $need_preamble = $items_seen[$listlevel]++ == 0;
890 # check if this is the first =item after an =over
892 my $need_new = $listlevel >= @listitem;
894 if ($text =~ /\A\*/) { # bullet
896 if ($need_preamble) {
897 push(@listend, "</UL>");
901 print HTML "<LI><STRONG>";
902 $text =~ /\A\*\s*(.*)\Z/s;
903 print HTML "<A NAME=\"item_" . htmlify(1,$1) . "\">" if $1 && !$items_named{$1}++;
905 #print HTML process_puretext($1, \$quote);
907 print HTML "</A>" if $1;
908 print HTML "</STRONG>";
910 } elsif ($text =~ /\A[0-9#]+/) { # numbered list
912 if ($need_preamble) {
913 push(@listend, "</OL>");
917 print HTML "<LI><STRONG>";
918 $text =~ /\A[0-9]+\.?(.*)\Z/s;
919 print HTML "<A NAME=\"item_" . htmlify(0,$1) . "\">" if $1;
921 #print HTML process_puretext($1, \$quote);
923 print HTML "</A>" if $1;
924 print HTML "</STRONG>";
926 } else { # all others
928 if ($need_preamble) {
929 push(@listend, '</DL>');
933 print HTML "<DT><STRONG>";
934 print HTML "<A NAME=\"item_" . htmlify(1,$text) . "\">"
935 if $text && !$items_named{($text =~ /(\S+)/)[0]}++;
936 # preceding craziness so that the duplicate leading bits in
937 # perlfunc work to find just the first one. otherwise
938 # open etc would have many names
940 #print HTML process_puretext($text, \$quote);
942 print HTML "</A>" if $text;
943 print HTML "</STRONG>";
952 # process_over - process a pod over tag and start a corresponding HTML
961 # process_back - process a pod back tag and convert it to HTML format.
964 warn "$0: $podfile: unexpected =back directive in paragraph $paragraph. ignoring.\n"
966 return unless $listlevel;
968 # close off the list. note, I check to see if $listend[$listlevel] is
969 # defined because an =item directive may have never appeared and thus
970 # $listend[$listlevel] may have never been initialized.
972 print HTML $listend[$listlevel] if defined $listend[$listlevel];
975 # don't need the corresponding perl code anymore
984 # process_cut - process a pod cut tag, thus stop ignoring pod directives.
991 # process_pod - process a pod pod tag, thus ignore pod directives until we see a
995 # no need to set $ignore to 0 cause the main loop did it
999 # process_for - process a =for pod tag. if it's for html, split
1000 # it out verbatim, otherwise ignore it.
1003 my($whom, $text) = @_;
1004 if ( $whom =~ /^(pod2)?html$/i) {
1010 # process_begin - process a =begin pod tag. this pushes
1011 # whom we're beginning on the begin stack. if there's a
1012 # begin stack, we only print if it us.
1015 my($whom, $text) = @_;
1017 push (@begin_stack, $whom);
1018 if ( $whom =~ /^(pod2)?html$/) {
1019 print HTML $text if $text;
1024 # process_end - process a =end pod tag. pop the
1025 # begin stack. die if we're mismatched.
1028 my($whom, $text) = @_;
1030 if ($begin_stack[-1] ne $whom ) {
1031 die "Unmatched begin/end at chunk $paragraph\n"
1037 # process_text - handles plaintext that appears in the input pod file.
1038 # there may be pod commands embedded within the text so those must be
1039 # converted to html commands.
1042 my($text, $escapeQuotes) = @_;
1043 my($result, $rest, $s1, $s2, $s3, $s4, $match, $bf);
1044 my($podcommand, $params, $tag, $quote);
1048 $quote = 0; # status of double-quote conversion
1052 if ($rest =~ /^\s+/) { # preformatted text, no pod directives
1056 1 while $line =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
1060 $rest =~ s/&/&/g;
1061 $rest =~ s/</</g;
1062 $rest =~ s/>/>/g;
1063 $rest =~ s/"/"/g;
1065 # try and create links for all occurrences of perl.* within
1066 # the preformatted text.
1070 if (defined $pages{$2}) { # is a link
1071 qq($1<A HREF="$htmlroot/$pages{$2}">$2</A>);
1072 } elsif (defined $pages{dosify($2)}) { # is a link
1073 qq($1<A HREF="$htmlroot/$pages{dosify($2)}">$2</A>);
1078 $rest =~ s/(<A HREF=)([^>:]*:)?([^>:]*)\.pod:([^>:]*:)?/$1$3.html/g;
1080 my $urls = '(' . join ('|', qw{
1093 my $gunk = '/#~:.?+=&%@!\-';
1095 my $any = "${ltrs}${gunk}${punc}";
1098 \b # start at word boundary
1100 $urls : # need resource and a colon
1101 [$any] +? # followed by on or more
1102 # of any valid character, but
1103 # be conservative and take only
1104 # what you need to....
1106 (?= # look-ahead non-consumptive assertion
1107 [$punc]* # either 0 or more puntuation
1108 [^$any] # followed by a non-url char
1110 $ # then end of the string
1112 }{<A HREF="$1">$1</A>}igox;
1114 $result = "<PRE>" # text should be as it is (verbatim)
1117 } else { # formatted text
1118 # parse through the string, stopping each time we find a
1119 # pod-escape. once the string has been throughly processed
1121 while (length $rest) {
1122 # check to see if there are any possible pod directives in
1123 # the remaining part of the text.
1124 if ($rest =~ m/[BCEIFLSZ]</) {
1125 warn "\$rest\t= $rest\n" unless
1132 $s1 = $1; # pure text
1133 $s2 = $2; # the type of pod-escape that follows
1135 $s4 = $3; # the rest of the string
1143 if ($s3 eq '<' && $s2) { # a pod-escape
1144 $result .= ($escapeQuotes ? process_puretext($s1, \$quote) : $s1);
1145 $podcommand = "$s2<";
1148 # find the matching '>'
1151 while ($match && !$bf) {
1153 if ($rest =~ /\A([^<>]*[BCEIFLSZ]<)(.*)\Z/s) {
1158 } elsif ($rest =~ /\A([^>]*>)(.*)\Z/s) {
1168 $0: $podfile: cannot find matching > for $s2 in paragraph $paragraph.
1170 $result .= substr $podcommand, 0, 2;
1171 $rest = substr($podcommand, 2) . $rest;
1175 # pull out the parameters to the pod-escape
1176 $podcommand =~ /^([BCFEILSZ]?)<(.*)>$/s;
1180 # process the text within the pod-escape so that any escapes
1181 # which must occur do.
1182 process_text(\$params, 0) unless $tag eq 'L';
1185 if (!$tag || $tag eq " ") { # <> : no tag
1186 $s1 = "<$params>";
1187 } elsif ($tag eq "L") { # L<> : link
1188 $s1 = process_L($params);
1189 } elsif ($tag eq "I" || # I<> : italicize text
1190 $tag eq "B" || # B<> : bold text
1191 $tag eq "F") { # F<> : file specification
1192 $s1 = process_BFI($tag, $params);
1193 } elsif ($tag eq "C") { # C<> : literal code
1194 $s1 = process_C($params, 1);
1195 } elsif ($tag eq "E") { # E<> : escape
1196 $s1 = process_E($params);
1197 } elsif ($tag eq "Z") { # Z<> : zero-width character
1198 $s1 = process_Z($params);
1199 } elsif ($tag eq "S") { # S<> : non-breaking space
1200 $s1 = process_S($params);
1201 } elsif ($tag eq "X") { # S<> : non-breaking space
1202 $s1 = process_X($params);
1204 warn "$0: $podfile: unhandled tag '$tag' in paragraph $paragraph\n";
1209 # for pure text we must deal with implicit links and
1210 # double-quotes among other things.
1211 $result .= ($escapeQuotes ? process_puretext("$s1$s2$s3", \$quote) : "$s1$s2$s3");
1221 $rest =~ s/&/&/g;
1222 $rest =~ s/</</g;
1223 $rest =~ s/>/>/g;
1224 $rest =~ s/"/"/g;
1229 # process_puretext - process pure text (without pod-escapes) converting
1230 # double-quotes and handling implicit C<> links.
1232 sub process_puretext {
1233 my($text, $quote) = @_;
1234 my(@words, $result, $rest, $lead, $trail);
1236 # convert double-quotes to single-quotes
1237 $text =~ s/\A([^"]*)"/$1''/s if $$quote;
1238 while ($text =~ s/\A([^"]*)["]([^"]*)["]/$1``$2''/sg) {}
1240 $$quote = ($text =~ m/"/ ? 1 : 0);
1241 $text =~ s/\A([^"]*)"/$1``/s if $$quote;
1243 # keep track of leading and trailing white-space
1244 $lead = ($text =~ /\A(\s*)/s ? $1 : "");
1245 $trail = ($text =~ /(\s*)\Z/s ? $1 : "");
1247 # collapse all white space into a single space
1249 @words = split(" ", $text);
1251 # process each word individually
1252 foreach my $word (@words) {
1253 # see if we can infer a link
1254 if ($word =~ /^\w+\(/) {
1255 # has parenthesis so should have been a C<> ref
1256 $word = process_C($word);
1257 # $word =~ /^[^()]*]\(/;
1258 # if (defined $items{$1} && $items{$1}) {
1259 # $word = "\n<CODE><A HREF=\"$htmlroot/$items{$1}#item_"
1260 # . htmlify(0,$word)
1261 # . "\">$word</A></CODE>";
1262 # } elsif (defined $items{$word} && $items{$word}) {
1263 # $word = "\n<CODE><A HREF=\"$htmlroot/$items{$word}#item_"
1264 # . htmlify(0,$word)
1265 # . "\">$word</A></CODE>";
1267 # $word = "\n<CODE><A HREF=\"#item_"
1268 # . htmlify(0,$word)
1269 # . "\">$word</A></CODE>";
1271 } elsif ($word =~ /^[\$\@%&*]+\w+$/) {
1272 # perl variables, should be a C<> ref
1273 $word = process_C($word, 1);
1274 } elsif ($word =~ m,^\w+://\w,) {
1276 $word = qq(<A HREF="$word">$word</A>);
1277 } elsif ($word =~ /[\w.-]+\@\w+\.\w/) {
1278 # looks like an e-mail address
1279 $word = qq(<A HREF="MAILTO:$word">$word</A>);
1280 } elsif ($word !~ /[a-z]/ && $word =~ /[A-Z]/) { # all uppercase?
1281 $word = html_escape($word) if $word =~ /[&<>]/;
1282 $word = "\n<FONT SIZE=-1>$word</FONT>" if $netscape;
1284 $word = html_escape($word) if $word =~ /[&<>]/;
1288 # build a new string based upon our conversion
1290 $rest = join(" ", @words);
1291 while (length($rest) > 75) {
1292 if ( $rest =~ m/^(.{0,75})\s(.*?)$/o ||
1293 $rest =~ m/^(\S*)\s(.*?)$/o) {
1298 $result .= "$rest\n";
1302 $result .= $rest if $rest;
1304 # restore the leading and trailing white-space
1305 $result = "$lead$result$trail";
1311 # pre_escape - convert & in text to $amp;
1316 $$str =~ s,&,&,g;
1320 # dosify - convert filenames to 8.3
1326 $str =~ s/(\.\w+)/substr ($1,0,4)/ge;
1327 $str =~ s/(\w+)/substr ($1,0,8)/ge;
1333 # process_L - convert a pod L<> directive to a corresponding HTML link.
1334 # most of the links made are inferred rather than known about directly
1335 # (i.e it's not known whether the =head\d section exists in the target file,
1336 # or whether a .pod file exists in the case of split files). however, the
1337 # guessing usually works.
1339 # Unlike the other directives, this should be called with an unprocessed
1340 # string, else tags in the link won't be matched.
1344 my($s1, $s2, $linktext, $page, $page83, $section, $link); # work strings
1346 $str =~ s/\n/ /g; # undo word-wrapped tags
1349 # LREF: a la HREF L<show this text|man/section>
1350 $linktext = $1 if s:^([^|]+)\|::;
1352 # a :: acts like a /
1355 # make sure sections start with a /
1357 s,^,/,g if (!m,/, && / /);
1359 # check if there's a section specified
1360 if (m,^(.*?)/"?(.*?)"?$,) { # yes
1361 ($page, $section) = ($1, $2);
1363 ($page, $section) = ($str, "");
1366 # check if we know that this is a section in this page
1367 if (!defined $pages{$page} && defined $sections{$page}) {
1373 $page83=dosify($page);
1374 $page=$page83 if (defined $pages{$page83});
1376 $link = "#" . htmlify(0,$section);
1377 $linktext = $section unless defined($linktext);
1378 } elsif (!defined $pages{$page}) {
1379 warn "$0: $podfile: cannot resolve L<$str> in paragraph $paragraph: no such page '$page'\n";
1381 $linktext = $page unless defined($linktext);
1383 $linktext = ($section ? "$section" : "the $page manpage") unless defined($linktext);
1384 $section = htmlify(0,$section) if $section ne "";
1386 # if there is a directory by the name of the page, then assume that an
1387 # appropriate section will exist in the subdirectory
1388 if ($section ne "" && $pages{$page} =~ /([^:]*[^(\.pod|\.pm)]):/) {
1389 $link = "$htmlroot/$1/$section.html";
1391 # since there is no directory by the name of the page, the section will
1392 # have to exist within a .html of the same name. thus, make sure there
1393 # is a .pod or .pm that might become that .html
1395 $section = "#$section";
1396 # check if there is a .pod with the page name
1397 if ($pages{$page} =~ /([^:]*)\.pod:/) {
1398 $link = "$htmlroot/$1.html$section";
1399 } elsif ($pages{$page} =~ /([^:]*)\.pm:/) {
1400 $link = "$htmlroot/$1.html$section";
1402 warn "$0: $podfile: cannot resolve L$str in paragraph $paragraph: ".
1403 "no .pod or .pm found\n";
1405 $linktext = $section unless defined($linktext);
1410 process_text(\$linktext, 0);
1412 $s1 = "<A HREF=\"$link\">$linktext</A>";
1414 $s1 = "<EM>$linktext</EM>";
1420 # process_BFI - process any of the B<>, F<>, or I<> pod-escapes and
1421 # convert them to corresponding HTML directives.
1424 my($tag, $str) = @_;
1425 my($s1); # work string
1426 my(%repltext) = ( 'B' => 'STRONG',
1430 # extract the modified text and convert to HTML
1431 $s1 = "<$repltext{$tag}>$str</$repltext{$tag}>";
1436 # process_C - process the C<> pod-escape.
1439 my($str, $doref) = @_;
1443 $s1 =~ s/\([^()]*\)//g; # delete parentheses
1445 $s1 =~ s/\W//g; # delete bogus characters
1447 # if there was a pod file that we found earlier with an appropriate
1448 # =item directive, then create a link to that page.
1449 if ($doref && defined $items{$s1}) {
1450 $s1 = ($items{$s1} ?
1451 "<A HREF=\"$htmlroot/$items{$s1}#item_" . htmlify(0,$s2) . "\">$str</A>" :
1452 "<A HREF=\"#item_" . htmlify(0,$s2) . "\">$str</A>");
1453 $s1 =~ s,(perl\w+/(\S+)\.html)#item_\2\b,$1,;
1454 confess "s1 has space: $s1" if $s1 =~ /HREF="[^"]*\s[^"]*"/;
1456 $s1 = "<CODE>$str</CODE>";
1457 # warn "$0: $podfile: cannot resolve C<$str> in paragraph $paragraph\n" if $verbose
1465 # process_E - process the E<> pod directive which seems to escape a character.
1471 s,([^/].*),\&$1\;,g;
1478 # process_Z - process the Z<> pod directive which really just amounts to
1479 # ignoring it. this allows someone to start a paragraph with an =
1484 # there is no equivalent in HTML for this so just ignore it.
1490 # process_S - process the S<> pod directive which means to convert all
1491 # spaces in the string to non-breaking spaces (in HTML-eze).
1496 # convert all spaces in the text to non-breaking spaces in HTML.
1497 $str =~ s/ / /g;
1502 # process_X - this is supposed to make an index entry. we'll just
1511 # finish_list - finish off any pending HTML lists. this should be called
1512 # after the entire pod file has been read and converted.
1515 while ($listlevel >= 0) {
1516 print HTML "</DL>\n";
1522 # htmlify - converts a pod section specification to a suitable section
1523 # specification for HTML. if first arg is 1, only takes 1st word.
1526 my($compact, $heading) = @_;
1529 $heading =~ /^(\w+)/;
1533 # $heading = lc($heading);
1534 $heading =~ s/[^\w\s]/_/g;
1535 $heading =~ s/(\s+)/ /g;
1536 $heading =~ s/^\s*(.*?)\s*$/$1/s;
1537 $heading =~ s/ /_/g;
1538 $heading =~ s/\A(.{32}).*\Z/$1/s;
1539 $heading =~ s/\s+\Z//;
1540 $heading =~ s/_{2,}/_/g;