5 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
8 @EXPORT = qw(pod2html htmlify);
9 @EXPORT_OK = qw(anchorify);
18 use locale; # make \w work right in non-ASCII lands
22 Pod::Html - module to convert pod files to HTML
31 Converts files from pod format (see L<perlpod>) to HTML format. It
32 can automatically generate indexes and cross-references, and it keeps
33 a cache of things it knows how to cross-reference.
37 Pod::Html takes the following arguments:
43 --backlink="Back to Top"
45 Adds "Back to Top" links in front of every C<head1> heading (except for
46 the first). By default, no backlinks are generated.
52 Creates the item and directory caches in the given directory.
58 Specify the URL of a cascading style sheet. Also disables all HTML/CSS
59 C<style> attributes that are output by default (to avoid conflicts).
65 Flushes the item and directory caches.
72 Creates header and footer blocks containing the text of the C<NAME>
73 section. By default, no headers are generated.
79 Displays the usage message.
85 Sets the directory in which the resulting HTML file is placed. This
86 is used to generate relative links to other files. Not passing this
87 causes all links to be absolute, since this is the value that tells
88 Pod::Html the root of the documentation tree.
94 Sets the base URL for the HTML files. When cross-references are made,
95 the HTML root is prepended to the URL.
102 Generate an index at the top of the HTML file. This is the default
109 Specify the pod file to convert. Input is taken from STDIN if no
114 --libpods=name:...:name
116 List of page names (eg, "perlfunc") which contain linkable C<=item>s.
123 B<Deprecated>, has no effect. For backwards compatibility only.
129 Specify the HTML file to create. Output goes to STDOUT if no outfile
134 --podpath=name:...:name
136 Specify which subdirectories of the podroot contain pod files whose
137 HTML converted forms can be linked to in cross references.
143 Specify the base directory for finding library pods.
150 Don't display I<mostly harmless> warning messages. These messages
151 will be displayed by default. But this is not the same as C<verbose>
159 Recurse into subdirectories specified in podpath (default behaviour).
165 Specify the title of the resulting HTML file.
172 Display progress messages. By default, they won't be displayed.
179 "--podpath=lib:ext:pod:vms",
180 "--podroot=/usr/src/perl",
181 "--htmlroot=/perl/nmanual",
182 "--libpods=perlfunc:perlguts:perlvar:perlrun:perlop",
185 "--outfile=/perl/nmanual/foo.html");
189 Uses C<$Config{pod2html}> to setup default options.
193 Tom Christiansen, E<lt>tchrist@perl.comE<gt>.
201 This program is distributed under the Artistic License.
205 my $cachedir = "."; # The directory to which item and directory
206 # caches will be written.
207 my $cache_ext = $^O eq 'VMS' ? ".tmp" : ".x~~";
208 my $dircache = "pod2htmd$cache_ext";
209 my $itemcache = "pod2htmi$cache_ext";
211 my @begin_stack = (); # begin/end stack
213 my @libpods = (); # files to search for links from C<> directives
214 my $htmlroot = "/"; # http-server base directory from which all
215 # relative paths in $podpath stem.
216 my $htmldir = ""; # The directory to which the html pages
217 # will (eventually) be written.
218 my $htmlfile = ""; # write to stdout by default
219 my $htmlfileurl = "" ; # The url that other files would use to
220 # refer to this file. This is only used
221 # to make relative urls that point to
223 my $podfile = ""; # read from stdin by default
224 my @podpath = (); # list of directories containing library pods.
225 my $podroot = File::Spec->curdir; # filesystem base directory from which all
226 # relative paths in $podpath stem.
227 my $css = ''; # Cascading style sheet
228 my $recurse = 1; # recurse on subdirectories in $podpath.
229 my $quiet = 0; # not quiet by default
230 my $verbose = 0; # not verbose by default
231 my $doindex = 1; # non-zero if we should generate an index
232 my $backlink = ''; # text for "back to top" links
233 my $listlevel = 0; # current list depth
234 my @listend = (); # the text to use to end the list.
235 my $after_lpar = 0; # set to true after a par in an =item
236 my $ignore = 1; # whether or not to format text. we don't
237 # format text until we hit our first pod
240 my %items_named = (); # for the multiples of the same item in perlfunc
242 my $title; # title to give the pod(s)
243 my $header = 0; # produce block header/footer
244 my $top = 1; # true if we are at the top of the doc. used
245 # to prevent the first <hr /> directive.
246 my $paragraph; # which paragraph we're processing (used
247 # for error messages)
248 my $ptQuote = 0; # status of double-quote conversion
249 my %pages = (); # associative array used to find the location
250 # of pages referenced by L<> links.
251 my %sections = (); # sections within this page
252 my %items = (); # associative array used to find the location
253 # of =item directives referenced by C<> links
254 my %local_items = (); # local items - avoid destruction of %items
255 my $Is83; # is dos with short filenames (8.3)
258 $dircache = "pod2htmd$cache_ext";
259 $itemcache = "pod2htmi$cache_ext";
261 @begin_stack = (); # begin/end stack
263 @libpods = (); # files to search for links from C<> directives
264 $htmlroot = "/"; # http-server base directory from which all
265 # relative paths in $podpath stem.
266 $htmldir = ""; # The directory to which the html pages
267 # will (eventually) be written.
268 $htmlfile = ""; # write to stdout by default
269 $podfile = ""; # read from stdin by default
270 @podpath = (); # list of directories containing library pods.
271 $podroot = File::Spec->curdir; # filesystem base directory from which all
272 # relative paths in $podpath stem.
273 $css = ''; # Cascading style sheet
274 $recurse = 1; # recurse on subdirectories in $podpath.
275 $quiet = 0; # not quiet by default
276 $verbose = 0; # not verbose by default
277 $doindex = 1; # non-zero if we should generate an index
278 $backlink = ''; # text for "back to top" links
279 $listlevel = 0; # current list depth
280 @listend = (); # the text to use to end the list.
281 $after_lpar = 0; # set to true after a par in an =item
282 $ignore = 1; # whether or not to format text. we don't
283 # format text until we hit our first pod
288 $header = 0; # produce block header/footer
289 $title = ''; # title to give the pod(s)
290 $top = 1; # true if we are at the top of the doc. used
291 # to prevent the first <hr /> directive.
292 $paragraph = ''; # which paragraph we're processing (used
293 # for error messages)
294 %sections = (); # sections within this page
296 # These are not reinitialised here but are kept as a cache.
297 # See get_cache and related cache management code.
298 #%pages = (); # associative array used to find the location
299 # of pages referenced by L<> links.
300 #%items = (); # associative array used to find the location
301 # of =item directives referenced by C<> links
307 # clean_data: global clean-up of pod data
311 for my $i ( 0..$#{$dataref} ) {
312 ${$dataref}[$i] =~ s/\s+\Z//;
314 # have a look for all-space lines
315 if( ${$dataref}[$i] =~ /^\s+$/m and $dataref->[$i] !~ /^\s/ ){
316 my @chunks = split( /^\s+$/m, ${$dataref}[$i] );
317 splice( @$dataref, $i, 1, @chunks );
330 $Is83 = 0 if (defined (&Dos::UseLFN) && Dos::UseLFN());
332 # cache of %pages and %items from last time we ran pod2html
334 #undef $opt_help if defined $opt_help;
336 # parse the command-line parameters
337 parse_command_line();
339 # escape the backlink argument (same goes for title but is done later...)
340 $backlink = html_escape($backlink) if defined $backlink;
342 # set some variables to their default values if necessary
344 unless (@ARGV && $ARGV[0]) {
345 $podfile = "-" unless $podfile; # stdin
346 open(POD, "<$podfile")
347 || die "$0: cannot open $podfile file for input: $!\n";
349 $podfile = $ARGV[0]; # XXX: might be more filenames
352 $htmlfile = "-" unless $htmlfile; # stdout
353 $htmlroot = "" if $htmlroot eq "/"; # so we don't get a //
354 $htmldir =~ s#/\z## ; # so we don't get a //
356 && defined( $htmldir )
358 && substr( $htmlfile, 0, length( $htmldir ) ) eq $htmldir
361 # Set the 'base' url for this file, so that we can use it
362 # as the location from which to calculate relative links
363 # to other files. If this is '', then absolute links will
364 # be used throughout.
365 $htmlfileurl= "$htmldir/" . substr( $htmlfile, length( $htmldir ) + 1);
368 # read the pod a paragraph at a time
369 warn "Scanning for sections in input file(s)\n" if $verbose;
378 @poddata = map { s/\r\n/\n/g;
380 map { "$_\n\n" } split /\n\n/ :
383 @poddata = map { s/\r/\n/g;
385 map { "$_\n\n" } split /\n\n/ :
392 clean_data( \@poddata );
394 # scan the pod for =head[1-6] directives and build an index
395 my $index = scan_headings(\%sections, @poddata);
398 warn "No headings in $podfile\n" if $verbose;
401 # open the output file
402 open(HTML, ">$htmlfile")
403 || die "$0: cannot open $htmlfile file for output: $!\n";
405 # put a title in the HTML file if one wasn't specified
408 for (my $i = 0; $i < @poddata; $i++) {
409 if ($poddata[$i] =~ /^=head1\s*NAME\b/m) {
410 for my $para ( @poddata[$i, $i+1] ) {
412 if ($title) = $para =~ /(\S+\s+-+.*\S)/s;
419 if (!$title and $podfile =~ /\.pod\z/) {
420 # probably a split pod so take first =head[12] as title
421 for (my $i = 0; $i < @poddata; $i++) {
422 last if ($title) = $poddata[$i] =~ /^=head[12]\s*(.*)/;
424 warn "adopted '$title' as title for $podfile\n"
425 if $verbose and $title;
428 $title =~ s/\s*\(.*\)//;
430 warn "$0: no title for $podfile.\n" unless $quiet;
431 $podfile =~ /^(.*)(\.[^.\/]+)?\z/s;
432 $title = ($podfile eq "-" ? 'No Title' : $1);
433 warn "using $title" if $verbose;
435 $title = html_escape($title);
438 my $bodystyle = ' style="background-color: white"';
439 my $tdstyle = ' style="background-color: #cccccc"';
442 $csslink = qq(\n<link rel="stylesheet" href="$css" type="text/css" />);
443 $csslink =~ s,\\,/,g;
444 $csslink =~ s,(/.):,$1|,;
449 my $block = $header ? <<END_OF_BLOCK : '';
450 <table border="0" width="100%" cellspacing="0" cellpadding="3">
451 <tr><td class="block"$tdstyle valign="middle">
452 <big><strong><span class="block"> $title</span></strong></big>
457 print HTML <<END_OF_HEAD;
458 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
459 <html xmlns="http://www.w3.org/1999/xhtml">
461 <title>$title</title>$csslink
462 <link rev="made" href="mailto:$Config{perladmin}" />
469 # load/reload/validate/cache %pages and %items
470 get_cache($dircache, $itemcache, \@podpath, $podroot, $recurse);
472 # scan the pod for =item directives
473 scan_items( \%local_items, "", @poddata);
475 # put an index at the top of the file. note, if $doindex is 0 we
476 # still generate an index, but surround it with an html comment.
477 # that way some other program can extract it if desired.
479 print HTML "<p><a name=\"__index__\"></a></p>\n";
480 print HTML "<!-- INDEX BEGIN -->\n";
481 print HTML "<!--\n" unless $doindex;
483 print HTML "-->\n" unless $doindex;
484 print HTML "<!-- INDEX END -->\n\n";
485 print HTML "<hr />\n" if $doindex and $index;
487 # now convert this file
488 my $after_item; # set to true after an =item
490 warn "Converting input file $podfile\n" if $verbose;
491 foreach my $i (0..$#poddata){
492 $ptQuote = 0; # status of quote conversion
496 if (/^(=.*)/s) { # is it a pod directive?
501 if (/^=begin\s+(\S+)\s*(.*)/si) {# =begin
502 process_begin($1, $2);
503 } elsif (/^=end\s+(\S+)\s*(.*)/si) {# =end
505 } elsif (/^=cut/) { # =cut
507 } elsif (/^=pod/) { # =pod
510 next if @begin_stack && $begin_stack[-1] ne 'html';
512 if (/^=(head[1-6])\s+(.*\S)/s) { # =head[1-6] heading
513 process_head( $1, $2, $doindex && $index );
514 } elsif (/^=item\s*(.*\S)?/sm) { # =item text
515 $need_dd = process_item( $1 );
517 } elsif (/^=over\s*(.*)/) { # =over N
519 } elsif (/^=back/) { # =back
521 } elsif (/^=for\s+(\S+)\s*(.*)/si) {# =for
525 warn "$0: $podfile: unknown pod directive '$1' in "
526 . "paragraph $paragraph. ignoring.\n";
533 next if @begin_stack && $begin_stack[-1] ne 'html';
534 print HTML and next if @begin_stack && $begin_stack[-1] eq 'html';
535 print HTML "<dd>\n" if $need_dd;
537 if( $text =~ /\A\s+/ ){
538 process_pre( \$text );
539 print HTML "<pre>\n$text</pre>\n";
542 process_text( \$text );
544 # experimental: check for a paragraph where all lines
545 # have some ...\t...\t...\n pattern
547 my @lines = split( "\n", $text );
550 foreach my $line ( @lines ){
551 if( $line =~ /\S/ && $line !~ /\t/ ){
557 $text =~ s/\t+/<td>/g;
558 $text =~ s/^/<tr><td>/gm;
559 $text = '<table cellspacing="0" cellpadding="0">' .
564 ## end of experimental
567 print HTML "$text\n";
570 print HTML "<p>$text</p>\n";
573 print HTML "</dd>\n" if $need_dd;
578 # finish off any pending directives
582 print HTML "<p><a href=\"#__index__\"><small>$backlink</small></a></p>\n"
583 if $doindex and $index and $backlink;
585 print HTML <<END_OF_TAIL;
592 # close the html file
595 warn "Finished\n" if $verbose;
598 ##############################################################################
600 my $usage; # see below
603 warn "$0: $podfile: @_\n" if @_;
607 $usage =<<END_OF_USAGE;
608 Usage: $0 --help --htmlroot=<name> --infile=<name> --outfile=<name>
609 --podpath=<name>:...:<name> --podroot=<name>
610 --libpods=<name>:...:<name> --recurse --verbose --index
611 --netscape --norecurse --noindex --cachedir=<name>
613 --backlink - set text for "back to top" links (default: none).
614 --cachedir - directory for the item and directory cache files.
615 --css - stylesheet URL
616 --flush - flushes the item and directory caches.
617 --[no]header - produce block header/footer (default is no headers).
618 --help - prints this message.
619 --htmldir - directory for resulting HTML files.
620 --htmlroot - http-server base directory from which all relative paths
621 in podpath stem (default is /).
622 --[no]index - generate an index at the top of the resulting html
624 --infile - filename for the pod to convert (input taken from stdin
626 --libpods - colon-separated list of pages to search for =item pod
627 directives in as targets of C<> and implicit links (empty
628 by default). note, these are not filenames, but rather
629 page names like those that appear in L<> links.
630 --outfile - filename for the resulting html file (output sent to
632 --podpath - colon-separated list of directories containing library
633 pods (empty by default).
634 --podroot - filesystem base directory from which all relative paths
635 in podpath stem (default is .).
636 --[no]quiet - supress some benign warning messages (default is off).
637 --[no]recurse - recurse on those subdirectories listed in podpath
639 --title - title that will appear in resulting html file.
640 --[no]verbose - self-explanatory (off by default).
641 --[no]netscape - deprecated, has no effect. for backwards compatibility only.
645 sub parse_command_line {
646 my ($opt_backlink,$opt_cachedir,$opt_css,$opt_flush,$opt_header,$opt_help,
647 $opt_htmldir,$opt_htmlroot,$opt_index,$opt_infile,$opt_libpods,
648 $opt_netscape,$opt_outfile,$opt_podpath,$opt_podroot,$opt_quiet,
649 $opt_recurse,$opt_title,$opt_verbose);
651 unshift @ARGV, split ' ', $Config{pod2html} if $Config{pod2html};
652 my $result = GetOptions(
653 'backlink=s' => \$opt_backlink,
654 'cachedir=s' => \$opt_cachedir,
655 'css=s' => \$opt_css,
656 'flush' => \$opt_flush,
657 'header!' => \$opt_header,
658 'help' => \$opt_help,
659 'htmldir=s' => \$opt_htmldir,
660 'htmlroot=s' => \$opt_htmlroot,
661 'index!' => \$opt_index,
662 'infile=s' => \$opt_infile,
663 'libpods=s' => \$opt_libpods,
664 'netscape!' => \$opt_netscape,
665 'outfile=s' => \$opt_outfile,
666 'podpath=s' => \$opt_podpath,
667 'podroot=s' => \$opt_podroot,
668 'quiet!' => \$opt_quiet,
669 'recurse!' => \$opt_recurse,
670 'title=s' => \$opt_title,
671 'verbose!' => \$opt_verbose,
673 usage("-", "invalid parameters") if not $result;
675 usage("-") if defined $opt_help; # see if the user asked for help
676 $opt_help = ""; # just to make -w shut-up.
678 @podpath = split(":", $opt_podpath) if defined $opt_podpath;
679 @libpods = split(":", $opt_libpods) if defined $opt_libpods;
681 $backlink = $opt_backlink if defined $opt_backlink;
682 $cachedir = $opt_cachedir if defined $opt_cachedir;
683 $css = $opt_css if defined $opt_css;
684 $header = $opt_header if defined $opt_header;
685 $htmldir = $opt_htmldir if defined $opt_htmldir;
686 $htmlroot = $opt_htmlroot if defined $opt_htmlroot;
687 $doindex = $opt_index if defined $opt_index;
688 $podfile = $opt_infile if defined $opt_infile;
689 $htmlfile = $opt_outfile if defined $opt_outfile;
690 $podroot = $opt_podroot if defined $opt_podroot;
691 $quiet = $opt_quiet if defined $opt_quiet;
692 $recurse = $opt_recurse if defined $opt_recurse;
693 $title = $opt_title if defined $opt_title;
694 $verbose = $opt_verbose if defined $opt_verbose;
696 warn "Flushing item and directory caches\n"
697 if $opt_verbose && defined $opt_flush;
698 $dircache = "$cachedir/pod2htmd$cache_ext";
699 $itemcache = "$cachedir/pod2htmi$cache_ext";
700 if (defined $opt_flush) {
701 1 while unlink($dircache, $itemcache);
709 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
710 my @cache_key_args = @_;
712 # A first-level cache:
713 # Don't bother reading the cache files if they still apply
714 # and haven't changed since we last read them.
716 my $this_cache_key = cache_key(@cache_key_args);
718 return if $saved_cache_key and $this_cache_key eq $saved_cache_key;
720 # load the cache of %pages and %items if possible. $tests will be
721 # non-zero if successful.
723 if (-f $dircache && -f $itemcache) {
724 warn "scanning for item cache\n" if $verbose;
725 $tests = load_cache($dircache, $itemcache, $podpath, $podroot);
728 # if we didn't succeed in loading the cache then we must (re)build
731 warn "scanning directories in pod-path\n" if $verbose;
732 scan_podpath($podroot, $recurse, 0);
734 $saved_cache_key = cache_key(@cache_key_args);
738 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
739 return join('!', $dircache, $itemcache, $recurse,
740 @$podpath, $podroot, stat($dircache), stat($itemcache));
744 # load_cache - tries to find if the caches stored in $dircache and $itemcache
745 # are valid caches of %pages and %items. if they are valid then it loads
746 # them and returns a non-zero value.
749 my($dircache, $itemcache, $podpath, $podroot) = @_;
755 open(CACHE, "<$itemcache") ||
756 die "$0: error opening $itemcache for reading: $!\n";
759 # is it the same podpath?
762 $tests++ if (join(":", @$podpath) eq $_);
764 # is it the same podroot?
767 $tests++ if ($podroot eq $_);
769 # load the cache if its good
775 warn "loading item cache\n" if $verbose;
782 warn "scanning for directory cache\n" if $verbose;
783 open(CACHE, "<$dircache") ||
784 die "$0: error opening $dircache for reading: $!\n";
788 # is it the same podpath?
791 $tests++ if (join(":", @$podpath) eq $_);
793 # is it the same podroot?
796 $tests++ if ($podroot eq $_);
798 # load the cache if its good
804 warn "loading directory cache\n" if $verbose;
816 # scan_podpath - scans the directories specified in @podpath for directories,
817 # .pod files, and .pm files. it also scans the pod files specified in
818 # @libpods for =item directives.
821 my($podroot, $recurse, $append) = @_;
823 my($libpod, $dirname, $pod, @files, @poddata);
830 # scan each directory listed in @podpath
833 || die "$0: error changing to directory $podroot: $!\n";
834 foreach $dir (@podpath) {
835 scan_dir($dir, $recurse);
838 # scan the pods listed in @libpods for =item directives
839 foreach $libpod (@libpods) {
840 # if the page isn't defined then we won't know where to find it
842 next unless defined $pages{$libpod} && $pages{$libpod};
844 # if there is a directory then use the .pod and .pm files within it.
845 # NOTE: Only finds the first so-named directory in the tree.
846 # if ($pages{$libpod} =~ /([^:]*[^(\.pod|\.pm)]):/) {
847 if ($pages{$libpod} =~ /([^:]*(?<!\.pod)(?<!\.pm)):/) {
848 # find all the .pod and .pm files within the directory
850 opendir(DIR, $dirname) ||
851 die "$0: error opening directory $dirname: $!\n";
852 @files = grep(/(\.pod|\.pm)\z/ && ! -d $_, readdir(DIR));
855 # scan each .pod and .pm file for =item directives
856 foreach $pod (@files) {
857 open(POD, "<$dirname/$pod") ||
858 die "$0: error opening $dirname/$pod for input: $!\n";
861 clean_data( \@poddata );
863 scan_items( \%items, "$dirname/$pod", @poddata);
866 # use the names of files as =item directives too.
867 ### Don't think this should be done this way - confuses issues.(WL)
868 ### foreach $pod (@files) {
869 ### $pod =~ /^(.*)(\.pod|\.pm)$/;
870 ### $items{$1} = "$dirname/$1.html" if $1;
872 } elsif ($pages{$libpod} =~ /([^:]*\.pod):/ ||
873 $pages{$libpod} =~ /([^:]*\.pm):/) {
874 # scan the .pod or .pm file for =item directives
876 open(POD, "<$pod") ||
877 die "$0: error opening $pod for input: $!\n";
880 clean_data( \@poddata );
882 scan_items( \%items, "$pod", @poddata);
884 warn "$0: shouldn't be here (line ".__LINE__."\n";
887 @poddata = (); # clean-up a bit
890 || die "$0: error changing to directory $pwd: $!\n";
892 # cache the item list for later use
893 warn "caching items for later use\n" if $verbose;
894 open(CACHE, ">$itemcache") ||
895 die "$0: error open $itemcache for writing: $!\n";
897 print CACHE join(":", @podpath) . "\n$podroot\n";
898 foreach my $key (keys %items) {
899 print CACHE "$key $items{$key}\n";
904 # cache the directory list for later use
905 warn "caching directories for later use\n" if $verbose;
906 open(CACHE, ">$dircache") ||
907 die "$0: error open $dircache for writing: $!\n";
909 print CACHE join(":", @podpath) . "\n$podroot\n";
910 foreach my $key (keys %pages) {
911 print CACHE "$key $pages{$key}\n";
918 # scan_dir - scans the directory specified in $dir for subdirectories, .pod
919 # files, and .pm files. notes those that it finds. this information will
920 # be used later in order to figure out where the pages specified in L<>
921 # links are on the filesystem.
924 my($dir, $recurse) = @_;
925 my($t, @subdirs, @pods, $pod, $dirname, @dirs);
931 opendir(DIR, $dir) ||
932 die "$0: error opening directory $dir: $!\n";
933 while (defined($_ = readdir(DIR))) {
934 if (-d "$dir/$_" && $_ ne "." && $_ ne "..") { # directory
935 $pages{$_} = "" unless defined $pages{$_};
936 $pages{$_} .= "$dir/$_:";
938 } elsif (/\.pod\z/) { # .pod
940 $pages{$_} = "" unless defined $pages{$_};
941 $pages{$_} .= "$dir/$_.pod:";
942 push(@pods, "$dir/$_.pod");
943 } elsif (/\.html\z/) { # .html
945 $pages{$_} = "" unless defined $pages{$_};
946 $pages{$_} .= "$dir/$_.pod:";
947 } elsif (/\.pm\z/) { # .pm
949 $pages{$_} = "" unless defined $pages{$_};
950 $pages{$_} .= "$dir/$_.pm:";
951 push(@pods, "$dir/$_.pm");
956 # recurse on the subdirectories if necessary
958 foreach my $subdir (@subdirs) {
959 scan_dir("$dir/$subdir", $recurse);
965 # scan_headings - scan a pod file for head[1-6] tags, note the tags, and
969 my($sections, @data) = @_;
970 my($tag, $which_head, $otitle, $listdepth, $index);
972 # here we need local $ignore = 0;
973 # unfortunately, we can't have it, because $ignore is lexical
979 # scan for =head directives, note their name, and build an index
980 # pointing to each of them.
981 foreach my $line (@data) {
982 if ($line =~ /^=(head)([1-6])\s+(.*)/) {
983 ($tag, $which_head, $otitle) = ($1,$2,$3);
985 my $title = depod( $otitle );
986 my $name = anchorify( $title );
987 $$sections{$name} = 1;
988 $title = process_text( \$otitle );
990 while ($which_head != $listdepth) {
991 if ($which_head > $listdepth) {
992 $index .= "\n" . ("\t" x $listdepth) . "<ul>\n";
994 } elsif ($which_head < $listdepth) {
996 $index .= "\n" . ("\t" x $listdepth) . "</ul>\n";
1000 $index .= "\n" . ("\t" x $listdepth) . "<li>" .
1001 "<a href=\"#" . $name . "\">" .
1002 $title . "</a></li>";
1006 # finish off the lists
1007 while ($listdepth--) {
1008 $index .= "\n" . ("\t" x $listdepth) . "</ul>\n";
1011 # get rid of bogus lists
1012 $index =~ s,\t*<ul>\s*</ul>\n,,g;
1014 $ignore = 1; # restore old value;
1020 # scan_items - scans the pod specified by $pod for =item directives. we
1021 # will use this information later on in resolving C<> links.
1024 my( $itemref, $pod, @poddata ) = @_;
1028 $pod =~ s/\.pod\z//;
1029 $pod .= ".html" if $pod;
1031 foreach $i (0..$#poddata) {
1032 my $txt = depod( $poddata[$i] );
1034 # figure out what kind of item it is.
1035 # Build string for referencing this item.
1036 if ( $txt =~ /\A=item\s+\*\s*(.*)\Z/s ) { # bullet
1039 } elsif( $txt =~ /\A=item\s+(?>\d+\.?)\s*(.*)\Z/s ) { # numbered list
1041 } elsif( $txt =~ /\A=item\s+(.*)\Z/s ) { # plain item
1046 my $fid = fragment_id( $item );
1047 $$itemref{$fid} = "$pod" if $fid;
1052 # process_head - convert a pod head[1-6] tag and convert it to HTML format.
1055 my($tag, $heading, $hasindex) = @_;
1057 # figure out the level of the =head
1058 $tag =~ /head([1-6])/;
1062 warn "$0: $podfile: unterminated list at =head in paragraph $paragraph. ignoring.\n";
1063 while( $listlevel ){
1069 if( $level == 1 && ! $top ){
1070 print HTML "<a href=\"#__index__\"><small>$backlink</small></a>\n"
1071 if $hasindex and $backlink;
1072 print HTML "</p>\n<hr />\n"
1074 print HTML "</p>\n";
1077 my $name = anchorify( depod( $heading ) );
1078 my $convert = process_text( \$heading );
1079 print HTML "<h$level><a name=\"$name\">$convert</a></h$level>\n";
1084 # emit_item_tag - print an =item's text
1085 # Note: The global $EmittedItem is used for inhibiting self-references.
1089 sub emit_item_tag($$$){
1090 my( $otext, $text, $compact ) = @_;
1091 my $item = fragment_id( $text );
1093 $EmittedItem = $item;
1094 ### print STDERR "emit_item_tag=$item ($text)\n";
1096 print HTML '<strong>';
1097 if ($items_named{$item}++) {
1098 print HTML process_text( \$otext );
1100 my $name = 'item_' . $item;
1101 $name = anchorify($name);
1102 print HTML qq{<a name="$name">}, process_text( \$otext ), '</a>';
1104 print HTML "</strong><br />\n";
1105 undef( $EmittedItem );
1110 if( $items_seen[$listlevel]++ == 0 ){
1111 push( @listend, "</$tag>" );
1112 print HTML "<$tag>\n";
1114 my $emitted = $tag eq 'dl' ? 'dt' : 'li';
1115 print HTML "<$emitted>";
1120 # process_item - convert a pod item tag and convert it to HTML format.
1124 my $need_dd = 0; # set to 1 if we need a <dd></dd> after an item
1126 # lots of documents start a list without doing an =over. this is
1127 # bad! but, the proper thing to do seems to be to just assume
1128 # they did do an =over. so warn them once and then continue.
1129 if( $listlevel == 0 ){
1130 warn "$0: $podfile: unexpected =item directive in paragraph $paragraph. ignoring.\n";
1134 # formatting: insert a paragraph if preceding item has >1 paragraph
1136 print HTML "<p></p>\n";
1140 # remove formatting instructions from the text
1141 my $text = depod( $otext );
1143 my $emitted; # the tag actually emitted, used for closing
1145 # all the list variants:
1146 if( $text =~ /\A\*/ ){ # bullet
1147 $emitted = emit_li( 'ul' );
1148 if ($text =~ /\A\*\s+(.+)\Z/s ) { # with additional text
1150 $otext =~ s/\A\*\s+//;
1151 emit_item_tag( $otext, $tag, 1 );
1154 } elsif( $text =~ /\A\d+/ ){ # numbered list
1155 $emitted = emit_li( 'ol' );
1156 if ($text =~ /\A(?>\d+\.?)\s*(.+)\Z/s ) { # with additional text
1158 $otext =~ s/\A\d+\.?\s*//;
1159 emit_item_tag( $otext, $tag, 1 );
1162 } else { # definition list
1163 $emitted = emit_li( 'dl' );
1164 if ($text =~ /\A(.+)\Z/s ){ # should have text
1165 emit_item_tag( $otext, $text, 1 );
1169 print HTML "</$emitted>" if $emitted;
1175 # process_over - process a pod over tag and start a corresponding HTML list.
1180 push( @items_seen, 0 );
1185 # process_back - process a pod back tag and convert it to HTML format.
1188 if( $listlevel == 0 ){
1189 warn "$0: $podfile: unexpected =back directive in paragraph $paragraph. ignoring.\n";
1193 # close off the list. note, I check to see if $listend[$listlevel] is
1194 # defined because an =item directive may have never appeared and thus
1195 # $listend[$listlevel] may have never been initialized.
1197 if( defined $listend[$listlevel] ){
1198 print HTML '<p></p>' if $after_lpar;
1199 print HTML $listend[$listlevel];
1205 # clean up item count
1210 # process_cut - process a pod cut tag, thus start ignoring pod directives.
1217 # process_pod - process a pod tag, thus stop ignoring pod directives
1218 # until we see a corresponding cut.
1221 # no need to set $ignore to 0 cause the main loop did it
1225 # process_for - process a =for pod tag. if it's for html, spit
1226 # it out verbatim, if illustration, center it, otherwise ignore it.
1229 my($whom, $text) = @_;
1230 if ( $whom =~ /^(pod2)?html$/i) {
1232 } elsif ($whom =~ /^illustration$/i) {
1233 1 while chomp $text;
1234 for my $ext (qw[.png .gif .jpeg .jpg .tga .pcl .bmp]) {
1235 $text .= $ext, last if -r "$text$ext";
1237 print HTML qq{<p align="center"><img src="$text" alt="$text illustration" /></p>};
1242 # process_begin - process a =begin pod tag. this pushes
1243 # whom we're beginning on the begin stack. if there's a
1244 # begin stack, we only print if it us.
1247 my($whom, $text) = @_;
1249 push (@begin_stack, $whom);
1250 if ( $whom =~ /^(pod2)?html$/) {
1251 print HTML $text if $text;
1256 # process_end - process a =end pod tag. pop the
1257 # begin stack. die if we're mismatched.
1260 my($whom, $text) = @_;
1262 if ($begin_stack[-1] ne $whom ) {
1263 die "Unmatched begin/end at chunk $paragraph\n"
1265 pop( @begin_stack );
1269 # process_pre - indented paragraph, made into <pre></pre>
1278 # insert spaces in place of tabs
1281 1 while $line =~ s/(\t+)/' ' x ((length($1) * 8) - $-[0] % 8)/e;
1285 # convert some special chars to HTML escapes
1286 $rest = html_escape($rest);
1288 # try and create links for all occurrences of perl.* within
1289 # the preformatted text.
1293 if ( defined $pages{$2} ){ # is a link
1294 qq($1<a href="$htmlroot/$pages{$2}">$2</a>);
1295 } elsif (defined $pages{dosify($2)}) { # is a link
1296 qq($1<a href="$htmlroot/$pages{dosify($2)}">$2</a>);
1302 (<a\ href="?) ([^>:]*:)? ([^>:]*) \.pod: ([^>:]*:)?
1305 if ( $htmlfileurl ne '' ){
1306 # Here, we take advantage of the knowledge
1307 # that $htmlfileurl ne '' implies $htmlroot eq ''.
1308 # Since $htmlroot eq '', we need to prepend $htmldir
1309 # on the fron of the link to get the absolute path
1310 # of the link's target. We check for a leading '/'
1311 # to avoid corrupting links that are #, file:, etc.
1313 $old_url = "$htmldir$old_url" if $old_url =~ m{^\/};
1314 $url = relativize_url( "$old_url.html", $htmlfileurl );
1321 # Look for embedded URLs and make them into links. We don't
1322 # relativize them since they are best left as the author intended.
1324 my $urls = '(' . join ('|', qw{
1337 my $gunk = '/#~:.?+=&%@!\-';
1338 my $punc = '.:!?\-;';
1339 my $any = "${ltrs}${gunk}${punc}";
1342 \b # start at word boundary
1344 $urls : # need resource and a colon
1345 (?!:) # Ignore File::, among others.
1346 [$any] +? # followed by one or more of any valid
1347 # character, but be conservative and
1348 # take only what you need to....
1351 " > # maybe pre-quoted '<a href="...">'
1353 [$punc]* # 0 or more punctuation
1355 [^$any] # by a non-url char
1357 $ # end of the string
1360 $ # then end of the string
1362 }{<a href="$1">$1</a>}igox;
1364 # text should be as it is (verbatim)
1370 # pure text processing
1372 # pure_text/inIS_text: differ with respect to automatic C<> recognition.
1373 # we don't want this to happen within IS
1377 process_puretext( $text, \$ptQuote, 1 );
1382 process_puretext( $text, \$ptQuote, 0 );
1386 # process_puretext - process pure text (without pod-escapes) converting
1387 # double-quotes and handling implicit C<> links.
1389 sub process_puretext {
1390 my($text, $quote, $notinIS) = @_;
1392 ## Guessing at func() or [$@%&]*var references in plain text is destined
1393 ## to produce some strange looking ref's. uncomment to disable:
1396 my(@words, $lead, $trail);
1398 # convert double-quotes to single-quotes
1399 if( $$quote && $text =~ s/"/''/s ){
1402 while ($text =~ s/"([^"]*)"/``$1''/sg) {};
1403 $$quote = 1 if $text =~ s/"/``/s;
1405 # keep track of leading and trailing white-space
1406 $lead = ($text =~ s/\A(\s+)//s ? $1 : "");
1407 $trail = ($text =~ s/(\s+)\Z//s ? $1 : "");
1409 # split at space/non-space boundaries
1410 @words = split( /(?<=\s)(?=\S)|(?<=\S)(?=\s)/, $text );
1412 # process each word individually
1413 foreach my $word (@words) {
1415 next if $word =~ /^\s*$/;
1416 # see if we can infer a link
1417 if( $notinIS && $word =~ /^(\w+)\((.*)\)$/ ) {
1418 # has parenthesis so should have been a C<> ref
1419 ## try for a pagename (perlXXX(1))?
1420 my( $func, $args ) = ( $1, $2 );
1421 if( $args =~ /^\d+$/ ){
1422 my $url = page_sect( $word, '' );
1424 $word = "<a href=\"$url\">the $word manpage</a>";
1428 ## try function name for a link, append tt'ed argument list
1429 $word = emit_C( $func, '', "($args)");
1431 #### disabled. either all (including $\W, $\w+{.*} etc.) or nothing.
1432 ## } elsif( $notinIS && $word =~ /^[\$\@%&*]+\w+$/) {
1433 ## # perl variables, should be a C<> ref
1434 ## $word = emit_C( $word );
1436 } elsif ($word =~ m,^\w+://\w,) {
1438 # Don't relativize it: leave it as the author intended
1439 $word = qq(<a href="$word">$word</a>);
1440 } elsif ($word =~ /[\w.-]+\@[\w-]+\.\w/) {
1441 # looks like an e-mail address
1442 my ($w1, $w2, $w3) = ("", $word, "");
1443 ($w1, $w2, $w3) = ("(", $1, ")$2") if $word =~ /^\((.*?)\)(,?)/;
1444 ($w1, $w2, $w3) = ("<", $1, ">$2") if $word =~ /^<(.*?)>(,?)/;
1445 $word = qq($w1<a href="mailto:$w2">$w2</a>$w3);
1447 $word = html_escape($word) if $word =~ /["&<>]/;
1451 # put everything back together
1452 return $lead . join( '', @words ) . $trail;
1457 # process_text - handles plaintext that appears in the input pod file.
1458 # there may be pod commands embedded within the text so those must be
1459 # converted to html commands.
1462 sub process_text1($$;$$);
1463 sub pattern ($) { $_[0] ? '[^\S\n]+'.('>' x ($_[0] + 1)) : '>' }
1464 sub closing ($) { local($_) = shift; (defined && s/\s+$//) ? length : 0 }
1469 my $res = process_text1( 0, $tref );
1473 sub process_text1($$;$$){
1474 my( $lev, $rstr, $func, $closing ) = @_;
1477 unless (defined $func) {
1483 # B<text> - boldface
1484 $res = '<strong>' . process_text1( $lev, $rstr ) . '</strong>';
1486 } elsif( $func eq 'C' ){
1487 # C<code> - can be a ref or <code></code>
1488 # need to extract text
1489 my $par = go_ahead( $rstr, 'C', $closing );
1491 ## clean-up of the link target
1492 my $text = depod( $par );
1494 ### my $x = $par =~ /[BI]</ ? 'yes' : 'no' ;
1495 ### print STDERR "-->call emit_C($par) lev=$lev, par with BI=$x\n";
1497 $res = emit_C( $text, $lev > 1 || ($par =~ /[BI]</) );
1499 } elsif( $func eq 'E' ){
1500 # E<x> - convert to character
1501 $$rstr =~ s/^([^>]*)>//;
1503 $escape =~ s/^(\d+|X[\dA-F]+)$/#$1/i;
1506 } elsif( $func eq 'F' ){
1507 # F<filename> - italizice
1508 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1510 } elsif( $func eq 'I' ){
1511 # I<text> - italizice
1512 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1514 } elsif( $func eq 'L' ){
1516 ## L<text|cross-ref> => produce text, use cross-ref for linking
1517 ## L<cross-ref> => make text from cross-ref
1518 ## need to extract text
1519 my $par = go_ahead( $rstr, 'L', $closing );
1521 # some L<>'s that shouldn't be:
1522 # a) full-blown URL's are emitted as-is
1523 if( $par =~ m{^\w+://}s ){
1524 return make_URL_href( $par );
1526 # b) C<...> is stripped and treated as C<>
1527 if( $par =~ /^C<(.*)>$/ ){
1528 my $text = depod( $1 );
1529 return emit_C( $text, $lev > 1 || ($par =~ /[BI]</) );
1532 # analyze the contents
1533 $par =~ s/\n/ /g; # undo word-wrapped tags
1536 if( $par =~ s{^([^|]+)\|}{} ){
1540 # make sure sections start with a /
1543 my( $page, $section, $ident );
1545 # check for link patterns
1546 if( $par =~ m{^([^/]+?)/(?!")(.*?)$} ){ # name/ident
1547 # we've got a name/ident (no quotes)
1548 ( $page, $ident ) = ( $1, $2 );
1549 ### print STDERR "--> L<$par> to page $page, ident $ident\n";
1551 } elsif( $par =~ m{^(.*?)/"?(.*?)"?$} ){ # [name]/"section"
1552 # even though this should be a "section", we go for ident first
1553 ( $page, $ident ) = ( $1, $2 );
1554 ### print STDERR "--> L<$par> to page $page, section $section\n";
1556 } elsif( $par =~ /\s/ ){ # this must be a section with missing quotes
1557 ( $page, $section ) = ( '', $par );
1558 ### print STDERR "--> L<$par> to void page, section $section\n";
1561 ( $page, $section ) = ( $par, '' );
1562 ### print STDERR "--> L<$par> to page $par, void section\n";
1565 # now, either $section or $ident is defined. the convoluted logic
1566 # below tries to resolve L<> according to what the user specified.
1567 # failing this, we try to find the next best thing...
1568 my( $url, $ltext, $fid );
1571 if( defined $ident ){
1572 ## try to resolve $ident as an item
1573 ( $url, $fid ) = coderef( $page, $ident );
1575 if( ! defined( $linktext ) ){
1577 $linktext .= " in " if $ident && $page;
1578 $linktext .= "the $page manpage" if $page;
1580 ### print STDERR "got coderef url=$url\n";
1583 ## no luck: go for a section (auto-quoting!)
1586 ## now go for a section
1587 my $htmlsection = htmlify( $section );
1588 $url = page_sect( $page, $htmlsection );
1590 if( ! defined( $linktext ) ){
1591 $linktext = $section;
1592 $linktext .= " in " if $section && $page;
1593 $linktext .= "the $page manpage" if $page;
1595 ### print STDERR "got page/section url=$url\n";
1598 ## no luck: go for an ident
1605 ( $url, $fid ) = coderef( $page, $ident );
1607 if( ! defined( $linktext ) ){
1609 $linktext .= " in " if $ident && $page;
1610 $linktext .= "the $page manpage" if $page;
1612 ### print STDERR "got section=>coderef url=$url\n";
1616 # warning; show some text.
1617 $linktext = $opar unless defined $linktext;
1618 warn "$0: $podfile: cannot resolve L<$opar> in paragraph $paragraph.\n";
1621 # now we have a URL or just plain code
1622 $$rstr = $linktext . '>' . $$rstr;
1623 if( defined( $url ) ){
1624 $res = "<a href=\"$url\">" . process_text1( $lev, $rstr ) . '</a>';
1626 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1629 } elsif( $func eq 'S' ){
1630 # S<text> - non-breaking spaces
1631 $res = process_text1( $lev, $rstr );
1632 $res =~ s/ / /g;
1634 } elsif( $func eq 'X' ){
1636 $$rstr =~ s/^[^>]*>//;
1638 } elsif( $func eq 'Z' ){
1640 warn "$0: $podfile: invalid X<> in paragraph $paragraph.\n"
1641 unless $$rstr =~ s/^>//;
1644 my $term = pattern $closing;
1645 while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)//s ){
1646 # all others: either recurse into new function or
1647 # terminate at closing angle bracket(s)
1649 $pt .= $2 if !$3 && $lev == 1;
1650 $res .= $lev == 1 ? pure_text( $pt ) : inIS_text( $pt );
1651 return $res if !$3 && $lev > 1;
1653 $res .= process_text1( $lev, $rstr, $3, closing $4 );
1657 $res .= pure_text( $$rstr );
1659 warn "$0: $podfile: undelimited $func<> in paragraph $paragraph.\n";
1666 # go_ahead: extract text of an IS (can be nested)
1669 my( $rstr, $func, $closing ) = @_;
1671 my @closing = ($closing);
1673 s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|@{[pattern $closing[0]]})//s ){
1677 return $res unless @closing;
1679 unshift @closing, closing $4;
1683 warn "$0: $podfile: undelimited $func<> in paragraph $paragraph.\n";
1688 # emit_C - output result of C<text>
1689 # $text is the depod-ed text
1692 my( $text, $nocode, $args ) = @_;
1693 $args = '' unless defined $args;
1695 my( $url, $fid ) = coderef( undef(), $text );
1697 # need HTML-safe text
1698 my $linktext = html_escape( "$text$args" );
1700 if( defined( $url ) &&
1701 (!defined( $EmittedItem ) || $EmittedItem ne $fid ) ){
1702 $res = "<a href=\"$url\"><code>$linktext</code></a>";
1703 } elsif( 0 && $nocode ){
1706 $res = "<code>$linktext</code>";
1712 # html_escape: make text safe for HTML
1716 $rest =~ s/&/&/g;
1717 $rest =~ s/</</g;
1718 $rest =~ s/>/>/g;
1719 $rest =~ s/"/"/g;
1720 # ' is only in XHTML, not HTML4. Be conservative
1721 #$rest =~ s/'/'/g;
1727 # dosify - convert filenames to 8.3
1731 return lc($str) if $^O eq 'VMS'; # VMS just needs casing
1734 $str =~ s/(\.\w+)/substr ($1,0,4)/ge;
1735 $str =~ s/(\w+)/substr ($1,0,8)/ge;
1741 # page_sect - make a URL from the text of a L<>
1744 my( $page, $section ) = @_;
1745 my( $linktext, $page83, $link); # work strings
1747 # check if we know that this is a section in this page
1748 if (!defined $pages{$page} && defined $sections{$page}) {
1751 ### print STDERR "reset page='', section=$section\n";
1754 $page83=dosify($page);
1755 $page=$page83 if (defined $pages{$page83});
1757 $link = "#" . anchorify( $section );
1758 } elsif ( $page =~ /::/ ) {
1760 # Search page cache for an entry keyed under the html page name,
1761 # then look to see what directory that page might be in. NOTE:
1762 # this will only find one page. A better solution might be to produce
1763 # an intermediate page that is an index to all such pages.
1764 my $page_name = $page ;
1765 $page_name =~ s,^.*/,,s ;
1766 if ( defined( $pages{ $page_name } ) &&
1767 $pages{ $page_name } =~ /([^:]*$page)\.(?:pod|pm):/
1772 # NOTE: This branch assumes that all A::B pages are located in
1773 # $htmlroot/A/B.html . This is often incorrect, since they are
1774 # often in $htmlroot/lib/A/B.html or such like. Perhaps we could
1775 # analyze the contents of %pages and figure out where any
1776 # cousins of A::B are, then assume that. So, if A::B isn't found,
1777 # but A::C is found in lib/A/C.pm, then A::B is assumed to be in
1778 # lib/A/B.pm. This is also limited, but it's an improvement.
1779 # Maybe a hints file so that the links point to the correct places
1783 $link = "$htmlroot/$page.html";
1784 $link .= "#" . anchorify( $section ) if ($section);
1785 } elsif (!defined $pages{$page}) {
1788 $section = anchorify( $section ) if $section ne "";
1789 ### print STDERR "...section=$section\n";
1791 # if there is a directory by the name of the page, then assume that an
1792 # appropriate section will exist in the subdirectory
1793 # if ($section ne "" && $pages{$page} =~ /([^:]*[^(\.pod|\.pm)]):/) {
1794 if ($section ne "" && $pages{$page} =~ /([^:]*(?<!\.pod)(?<!\.pm)):/) {
1795 $link = "$htmlroot/$1/$section.html";
1796 ### print STDERR "...link=$link\n";
1798 # since there is no directory by the name of the page, the section will
1799 # have to exist within a .html of the same name. thus, make sure there
1800 # is a .pod or .pm that might become that .html
1802 $section = "#$section" if $section;
1803 ### print STDERR "...section=$section\n";
1805 # check if there is a .pod with the page name
1806 if ($pages{$page} =~ /([^:]*)\.pod:/) {
1807 $link = "$htmlroot/$1.html$section";
1808 } elsif ($pages{$page} =~ /([^:]*)\.pm:/) {
1809 $link = "$htmlroot/$1.html$section";
1817 # Here, we take advantage of the knowledge that $htmlfileurl ne ''
1818 # implies $htmlroot eq ''. This means that the link in question
1819 # needs a prefix of $htmldir if it begins with '/'. The test for
1820 # the initial '/' is done to avoid '#'-only links, and to allow
1821 # for other kinds of links, like file:, ftp:, etc.
1823 if ( $htmlfileurl ne '' ) {
1824 $link = "$htmldir$link" if $link =~ m{^/}s;
1825 $url = relativize_url( $link, $htmlfileurl );
1826 # print( " b: [$link,$htmlfileurl,$url]\n" );
1839 # relativize_url - convert an absolute URL to one relative to a base URL.
1840 # Assumes both end in a filename.
1842 sub relativize_url {
1843 my ($dest,$source) = @_ ;
1845 my ($dest_volume,$dest_directory,$dest_file) =
1846 File::Spec::Unix->splitpath( $dest ) ;
1847 $dest = File::Spec::Unix->catpath( $dest_volume, $dest_directory, '' ) ;
1849 my ($source_volume,$source_directory,$source_file) =
1850 File::Spec::Unix->splitpath( $source ) ;
1851 $source = File::Spec::Unix->catpath( $source_volume, $source_directory, '' ) ;
1854 if ( $dest ne '' ) {
1855 $rel_path = File::Spec::Unix->abs2rel( $dest, $source ) ;
1858 if ( $rel_path ne '' &&
1859 substr( $rel_path, -1 ) ne '/' &&
1860 substr( $dest_file, 0, 1 ) ne '#'
1862 $rel_path .= "/$dest_file" ;
1865 $rel_path .= "$dest_file" ;
1873 # coderef - make URL from the text of a C<>
1876 my( $page, $item ) = @_;
1879 my $fid = fragment_id( $item );
1880 if( defined( $page ) ){
1881 # we have been given a $page...
1884 # Do we take it? Item could be a section!
1885 my $base = $items{$fid} || "";
1886 $base =~ s{[^/]*/}{};
1887 if( $base ne "$page.html" ){
1888 ### print STDERR "coderef( $page, $item ): items{$fid} = $items{$fid} = $base => discard page!\n";
1893 # no page - local items precede cached items
1894 if( defined( $fid ) ){
1895 if( exists $local_items{$fid} ){
1896 $page = $local_items{$fid};
1898 $page = $items{$fid};
1903 # if there was a pod file that we found earlier with an appropriate
1904 # =item directive, then create a link to that page.
1905 if( defined $page ){
1907 if( exists $pages{$page} and $pages{$page} =~ /([^:.]*)\.[^:]*:/){
1908 $page = $1 . '.html';
1910 my $link = "$htmlroot/$page#item_" . anchorify($fid);
1912 # Here, we take advantage of the knowledge that $htmlfileurl
1913 # ne '' implies $htmlroot eq ''.
1914 if ( $htmlfileurl ne '' ) {
1915 $link = "$htmldir$link" ;
1916 $url = relativize_url( $link, $htmlfileurl ) ;
1921 $url = "#item_" . anchorify($fid);
1924 confess "url has space: $url" if $url =~ /"[^"]*\s[^"]*"/;
1926 return( $url, $fid );
1932 # Adapted from Nick Ing-Simmons' PodToHtml package.
1934 my $source_file = shift ;
1935 my $destination_file = shift;
1937 my $source = URI::file->new_abs($source_file);
1938 my $uo = URI::file->new($destination_file,$source)->abs;
1939 return $uo->rel->as_string;
1944 # finish_list - finish off any pending HTML lists. this should be called
1945 # after the entire pod file has been read and converted.
1948 while ($listlevel > 0) {
1949 print HTML "</dl>\n";
1955 # htmlify - converts a pod section specification to a suitable section
1956 # specification for HTML. Note that we keep spaces and special characters
1957 # except ", ? (Netscape problem) and the hyphen (writer's problem...).
1961 $heading =~ s/(\s+)/ /g;
1962 $heading =~ s/\s+\Z//;
1963 $heading =~ s/\A\s+//;
1964 # The hyphen is a disgrace to the English language.
1965 $heading =~ s/[-"?]//g;
1966 $heading = lc( $heading );
1971 # similar to htmlify, but turns non-alphanumerics into underscores
1975 $anchor = htmlify($anchor);
1976 $anchor =~ s/\W/_/g;
1981 # depod - convert text by eliminating all interior sequences
1982 # Note: can be called with copy or modify semantics
1989 $E2c{amp} = '&'; # in Tk's pods
1997 ${$_[0]} = depod1( \$string );
2005 my( $rstr, $func, $closing ) = @_;
2007 return $res unless defined $$rstr;
2008 if( ! defined( $func ) ){
2009 # skip to next begin of an interior sequence
2010 while( $$rstr =~ s/\A(.*?)([BCEFILSXZ])<(<+[^\S\n]+)?// ){
2011 # recurse into its text
2012 $res .= $1 . depod1( $rstr, $2, closing $3);
2015 } elsif( $func eq 'E' ){
2016 # E<x> - convert to character
2017 $$rstr =~ s/^([^>]*)>//;
2018 $res .= $E2c{$1} || "";
2019 } elsif( $func eq 'X' ){
2021 $$rstr =~ s/^[^>]*>//;
2022 } elsif( $func eq 'Z' ){
2026 # all others: either recurse into new function or
2027 # terminate at closing angle bracket
2028 my $term = pattern $closing;
2029 while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)// ){
2032 $res .= depod1( $rstr, $3, closing $4 );
2034 ## If we're here and $2 ne '>': undelimited interior sequence.
2035 ## Ignored, as this is called without proper indication of where we are.
2036 ## Rely on process_text to produce diagnostics.
2042 # fragment_id - construct a fragment identifier from:
2044 # b) contents of C<...>
2049 $text =~ s/\s+\Z//s;
2051 # a method or function?
2052 return $1 if $text =~ /(\w+)\s*\(/;
2053 return $1 if $text =~ /->\s*(\w+)\s*\(?/;
2056 return $1 if $text =~ /^([$@%*]\S+)/;
2058 # some pattern matching operator?
2059 return $1 if $text =~ m|^(\w+/).*/\w*$|;
2061 # fancy stuff... like "do { }"
2062 return $1 if $text =~ m|^(\w+)\s*{.*}$|;
2064 # honour the perlfunc manpage: func [PAR[,[ ]PAR]...]
2065 # and some funnies with ... Module ...
2066 return $1 if $text =~ m{^([a-z\d]+)(\s+[A-Z\d,/& ]+)?$};
2067 return $1 if $text =~ m{^([a-z\d]+)\s+Module(\s+[A-Z\d,/& ]+)?$};
2070 $text =~ s/\s+/_/sg;
2072 defined( $hc[ord($1)] ) ? $hc[ord($1)]
2073 : ( $hc[ord($1)] = sprintf( "%%%02X", ord($1) ) ) }gxe;
2074 $text = substr( $text, 0, 50 );
2081 # make_URL_href - generate HTML href from URL
2082 # Special treatment for CGI queries.
2084 sub make_URL_href($){
2087 s{^(http:[-\w/#~:.+=&%@!]+)(\?.*)$}{<a href="$1$2">$1</a>}i ){
2088 $url = "<a href=\"$url\">$url</a>";