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.
40 "--podpath=lib:ext:pod:vms",
41 "--podroot=/usr/src/perl",
42 "--htmlroot=/perl/nmanual",
43 "--libpods=perlfunc:perlguts:perlvar:perlrun:perlop",
46 "--outfile=/perl/nmanual/foo.html");
48 pod2html takes the following arguments:
54 --backlink="Back to Top"
56 Adds "Back to Top" links in front of every C<head1> heading (except for
57 the first). By default, no backlinks are generated.
63 Creates the item and directory caches in the given directory.
69 Specify the URL of a cascading style sheet. Also disables all HTML/CSS
70 C<style> attributes that are output by default (to avoid conflicts).
76 Flushes the item and directory caches.
83 Creates header and footer blocks containing the text of the C<NAME>
84 section. By default, no headers are generated.
90 Displays the usage message.
97 Include hidden directories in the search for POD's in podpath if recurse
99 The default is not to traverse any directory whose name begins with C<.>.
100 See L</"podpath"> and L</"recurse">.
102 [This option is for backward compatibility only.
103 It's hard to imagine that one would usefully create a module with a
104 name component beginning with C<.>.]
110 Sets the directory in which the resulting HTML file is placed. This
111 is used to generate relative links to other files. Not passing this
112 causes all links to be absolute, since this is the value that tells
113 Pod::Html the root of the documentation tree.
119 Sets the base URL for the HTML files. When cross-references are made,
120 the HTML root is prepended to the URL.
127 Generate an index at the top of the HTML file. This is the default
134 Specify the pod file to convert. Input is taken from STDIN if no
139 --libpods=name:...:name
141 List of page names (eg, "perlfunc") which contain linkable C<=item>s.
148 B<Deprecated>, has no effect. For backwards compatibility only.
154 Specify the HTML file to create. Output goes to STDOUT if no outfile
159 --podpath=name:...:name
161 Specify which subdirectories of the podroot contain pod files whose
162 HTML converted forms can be linked to in cross references.
168 Specify the base directory for finding library pods.
175 Don't display I<mostly harmless> warning messages. These messages
176 will be displayed by default. But this is not the same as C<verbose>
184 Recurse into subdirectories specified in podpath (default behaviour).
190 Specify the title of the resulting HTML file.
197 Display progress messages. By default, they won't be displayed.
205 Converts a pod section specification to a suitable section specification
206 for HTML. Note that we keep spaces and special characters except
207 C<", ?> (Netscape problem) and the hyphen (writer's problem...).
213 Similar to C<htmlify()>, but turns non-alphanumerics into underscores. Note
214 that C<anchorify()> is not exported by default.
218 Uses C<$Config{pod2html}> to setup default options.
222 Tom Christiansen, E<lt>tchrist@perl.comE<gt>.
230 This program is distributed under the Artistic License.
236 my($Dircache, $Itemcache);
239 my($Htmlroot, $Htmldir, $Htmlfile, $Htmlfileurl);
240 my($Podfile, @Podpath, $Podroot);
250 my($Listlevel, @Listend);
252 use vars qw($Ignore); # need to localize it later.
254 my(%Items_Named, @Items_Seen);
263 my %Pages = (); # associative array used to find the location
264 # of pages referenced by L<> links.
265 my %Items = (); # associative array used to find the location
266 # of =item directives referenced by C<> links
272 my $Curdir = File::Spec->curdir;
277 $Cachedir = "."; # The directory to which item and directory
278 # caches will be written.
280 $Dircache = "pod2htmd.tmp";
281 $Itemcache = "pod2htmi.tmp";
283 @Begin_Stack = (); # begin/end stack
285 @Libpods = (); # files to search for links from C<> directives
286 $Htmlroot = "/"; # http-server base directory from which all
287 # relative paths in $podpath stem.
288 $Htmldir = ""; # The directory to which the html pages
289 # will (eventually) be written.
290 $Htmlfile = ""; # write to stdout by default
291 $Htmlfileurl = "" ; # The url that other files would use to
292 # refer to this file. This is only used
293 # to make relative urls that point to
296 $Podfile = ""; # read from stdin by default
297 @Podpath = (); # list of directories containing library pods.
298 $Podroot = $Curdir; # filesystem base directory from which all
299 # relative paths in $podpath stem.
300 $Css = ''; # Cascading style sheet
301 $Recurse = 1; # recurse on subdirectories in $podpath.
302 $Quiet = 0; # not quiet by default
303 $Verbose = 0; # not verbose by default
304 $Doindex = 1; # non-zero if we should generate an index
305 $Backlink = ''; # text for "back to top" links
306 $Listlevel = 0; # current list depth
307 @Listend = (); # the text to use to end the list.
308 $After_Lpar = 0; # set to true after a par in an =item
309 $Ignore = 1; # whether or not to format text. we don't
310 # format text until we hit our first pod
313 @Items_Seen = (); # for multiples of the same item in perlfunc
315 $Header = 0; # produce block header/footer
316 $Title = ''; # title to give the pod(s)
317 $Top = 1; # true if we are at the top of the doc. used
318 # to prevent the first <hr /> directive.
319 $Paragraph = ''; # which paragraph we're processing (used
320 # for error messages)
321 $PTQuote = 0; # status of double-quote conversion
322 %Sections = (); # sections within this page
325 $Is83 = $^O eq 'dos'; # Is it an 8.3 filesystem?
329 # clean_data: global clean-up of pod data
333 for my $i ( 0..$#{$dataref} ) {
334 ${$dataref}[$i] =~ s/\s+\Z//;
336 # have a look for all-space lines
337 if( ${$dataref}[$i] =~ /^\s+$/m and $dataref->[$i] !~ /^\s/ ){
338 my @chunks = split( /^\s+$/m, ${$dataref}[$i] );
339 splice( @$dataref, $i, 1, @chunks );
352 $Is83 = 0 if (defined (&Dos::UseLFN) && Dos::UseLFN());
354 # cache of %Pages and %Items from last time we ran pod2html
356 #undef $opt_help if defined $opt_help;
358 # parse the command-line parameters
359 parse_command_line();
361 # escape the backlink argument (same goes for title but is done later...)
362 $Backlink = html_escape($Backlink) if defined $Backlink;
364 # set some variables to their default values if necessary
366 unless (@ARGV && $ARGV[0]) {
367 $Podfile = "-" unless $Podfile; # stdin
368 open(POD, "<$Podfile")
369 || die "$0: cannot open $Podfile file for input: $!\n";
371 $Podfile = $ARGV[0]; # XXX: might be more filenames
374 $Htmlfile = "-" unless $Htmlfile; # stdout
375 $Htmlroot = "" if $Htmlroot eq "/"; # so we don't get a //
376 $Htmldir =~ s#/\z## ; # so we don't get a //
378 && defined( $Htmldir )
380 && substr( $Htmlfile, 0, length( $Htmldir ) ) eq $Htmldir
383 # Set the 'base' url for this file, so that we can use it
384 # as the location from which to calculate relative links
385 # to other files. If this is '', then absolute links will
386 # be used throughout.
387 $Htmlfileurl= "$Htmldir/" . substr( $Htmlfile, length( $Htmldir ) + 1);
390 # read the pod a paragraph at a time
391 warn "Scanning for sections in input file(s)\n" if $Verbose;
400 @poddata = map { s/\r\n/\n/g;
402 map { "$_\n\n" } split /\n\n/ :
405 @poddata = map { s/\r/\n/g;
407 map { "$_\n\n" } split /\n\n/ :
414 clean_data( \@poddata );
416 # scan the pod for =head[1-6] directives and build an index
417 my $index = scan_headings(\%Sections, @poddata);
420 warn "No headings in $Podfile\n" if $Verbose;
423 # open the output file
424 open(HTML, ">$Htmlfile")
425 || die "$0: cannot open $Htmlfile file for output: $!\n";
427 # put a title in the HTML file if one wasn't specified
430 for (my $i = 0; $i < @poddata; $i++) {
431 if ($poddata[$i] =~ /^=head1\s*NAME\b/m) {
432 for my $para ( @poddata[$i, $i+1] ) {
434 if ($Title) = $para =~ /(\S+\s+-+.*\S)/s;
441 if (!$Title and $Podfile =~ /\.pod\z/) {
442 # probably a split pod so take first =head[12] as title
443 for (my $i = 0; $i < @poddata; $i++) {
444 last if ($Title) = $poddata[$i] =~ /^=head[12]\s*(.*)/;
446 warn "adopted '$Title' as title for $Podfile\n"
447 if $Verbose and $Title;
450 $Title =~ s/\s*\(.*\)//;
452 warn "$0: no title for $Podfile.\n" unless $Quiet;
453 $Podfile =~ /^(.*)(\.[^.\/]+)?\z/s;
454 $Title = ($Podfile eq "-" ? 'No Title' : $1);
455 warn "using $Title" if $Verbose;
457 $Title = html_escape($Title);
460 my $bodystyle = ' style="background-color: white"';
461 my $tdstyle = ' style="background-color: #cccccc"';
464 $csslink = qq(\n<link rel="stylesheet" href="$Css" type="text/css" />);
465 $csslink =~ s,\\,/,g;
466 $csslink =~ s,(/.):,$1|,;
471 my $block = $Header ? <<END_OF_BLOCK : '';
472 <table border="0" width="100%" cellspacing="0" cellpadding="3">
473 <tr><td class="block"$tdstyle valign="middle">
474 <big><strong><span class="block"> $Title</span></strong></big>
479 print HTML <<END_OF_HEAD;
480 <?xml version="1.0" ?>
481 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
482 <html xmlns="http://www.w3.org/1999/xhtml">
484 <title>$Title</title>$csslink
485 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
486 <link rev="made" href="mailto:$Config{perladmin}" />
493 # load/reload/validate/cache %Pages and %Items
494 get_cache($Dircache, $Itemcache, \@Podpath, $Podroot, $Recurse);
496 # scan the pod for =item directives
497 scan_items( \%Local_Items, "", @poddata);
499 # put an index at the top of the file. note, if $Doindex is 0 we
500 # still generate an index, but surround it with an html comment.
501 # that way some other program can extract it if desired.
503 print HTML "<p><a name=\"__index__\"></a></p>\n";
504 print HTML "<!-- INDEX BEGIN -->\n";
505 print HTML "<!--\n" unless $Doindex;
507 print HTML "-->\n" unless $Doindex;
508 print HTML "<!-- INDEX END -->\n\n";
509 print HTML "<hr />\n" if $Doindex and $index;
511 # now convert this file
512 my $after_item; # set to true after an =item
514 warn "Converting input file $Podfile\n" if $Verbose;
515 foreach my $i (0..$#poddata){
516 $PTQuote = 0; # status of quote conversion
520 if (/^(=.*)/s) { # is it a pod directive?
525 if (/^=begin\s+(\S+)\s*(.*)/si) {# =begin
526 process_begin($1, $2);
527 } elsif (/^=end\s+(\S+)\s*(.*)/si) {# =end
529 } elsif (/^=cut/) { # =cut
531 } elsif (/^=pod/) { # =pod
534 next if @Begin_Stack && $Begin_Stack[-1] ne 'html';
536 if (/^=(head[1-6])\s+(.*\S)/s) { # =head[1-6] heading
537 process_head( $1, $2, $Doindex && $index );
538 } elsif (/^=item\s*(.*\S)?/sm) { # =item text
539 $need_dd = process_item( $1 );
541 } elsif (/^=over\s*(.*)/) { # =over N
543 } elsif (/^=back/) { # =back
544 process_back($need_dd);
545 } elsif (/^=for\s+(\S+)\s*(.*)/si) {# =for
549 warn "$0: $Podfile: unknown pod directive '$1' in "
550 . "paragraph $Paragraph. ignoring.\n" unless $Quiet;
557 next if @Begin_Stack && $Begin_Stack[-1] ne 'html';
558 print HTML and next if @Begin_Stack && $Begin_Stack[-1] eq 'html';
559 print HTML "<dd>\n" if $need_dd;
561 if( $text =~ /\A\s+/ ){
562 process_pre( \$text );
563 print HTML "<pre>\n$text</pre>\n";
566 process_text( \$text );
568 # experimental: check for a paragraph where all lines
569 # have some ...\t...\t...\n pattern
571 my @lines = split( "\n", $text );
574 foreach my $line ( @lines ){
575 if( $line =~ /\S/ && $line !~ /\t/ ){
581 $text =~ s/\t+/<td>/g;
582 $text =~ s/^/<tr><td>/gm;
583 $text = '<table cellspacing="0" cellpadding="0">' .
588 ## end of experimental
593 print HTML "<p>$text</p>\n";
595 print HTML "</dd>\n" if $need_dd;
600 # finish off any pending directives
604 print HTML "<p><a href=\"#__index__\"><small>$Backlink</small></a></p>\n"
605 if $Doindex and $index and $Backlink;
607 print HTML <<END_OF_TAIL;
614 # close the html file
617 warn "Finished\n" if $Verbose;
620 ##############################################################################
624 warn "$0: $podfile: @_\n" if @_;
626 Usage: $0 --help --htmlroot=<name> --infile=<name> --outfile=<name>
627 --podpath=<name>:...:<name> --podroot=<name>
628 --libpods=<name>:...:<name> --recurse --verbose --index
629 --netscape --norecurse --noindex --cachedir=<name>
631 --backlink - set text for "back to top" links (default: none).
632 --cachedir - directory for the item and directory cache files.
633 --css - stylesheet URL
634 --flush - flushes the item and directory caches.
635 --[no]header - produce block header/footer (default is no headers).
636 --help - prints this message.
637 --hiddendirs - search hidden directories in podpath
638 --htmldir - directory for resulting HTML files.
639 --htmlroot - http-server base directory from which all relative paths
640 in podpath stem (default is /).
641 --[no]index - generate an index at the top of the resulting html
643 --infile - filename for the pod to convert (input taken from stdin
645 --libpods - colon-separated list of pages to search for =item pod
646 directives in as targets of C<> and implicit links (empty
647 by default). note, these are not filenames, but rather
648 page names like those that appear in L<> links.
649 --outfile - filename for the resulting html file (output sent to
651 --podpath - colon-separated list of directories containing library
652 pods (empty by default).
653 --podroot - filesystem base directory from which all relative paths
654 in podpath stem (default is .).
655 --[no]quiet - suppress some benign warning messages (default is off).
656 --[no]recurse - recurse on those subdirectories listed in podpath
658 --title - title that will appear in resulting html file.
659 --[no]verbose - self-explanatory (off by default).
660 --[no]netscape - deprecated, has no effect. for backwards compatibility only.
666 sub parse_command_line {
667 my ($opt_backlink,$opt_cachedir,$opt_css,$opt_flush,$opt_header,$opt_help,
668 $opt_htmldir,$opt_htmlroot,$opt_index,$opt_infile,$opt_libpods,
669 $opt_netscape,$opt_outfile,$opt_podpath,$opt_podroot,$opt_quiet,
670 $opt_recurse,$opt_title,$opt_verbose,$opt_hiddendirs);
672 unshift @ARGV, split ' ', $Config{pod2html} if $Config{pod2html};
673 my $result = GetOptions(
674 'backlink=s' => \$opt_backlink,
675 'cachedir=s' => \$opt_cachedir,
676 'css=s' => \$opt_css,
677 'flush' => \$opt_flush,
678 'header!' => \$opt_header,
679 'help' => \$opt_help,
680 'hiddendirs!'=> \$opt_hiddendirs,
681 'htmldir=s' => \$opt_htmldir,
682 'htmlroot=s' => \$opt_htmlroot,
683 'index!' => \$opt_index,
684 'infile=s' => \$opt_infile,
685 'libpods=s' => \$opt_libpods,
686 'netscape!' => \$opt_netscape,
687 'outfile=s' => \$opt_outfile,
688 'podpath=s' => \$opt_podpath,
689 'podroot=s' => \$opt_podroot,
690 'quiet!' => \$opt_quiet,
691 'recurse!' => \$opt_recurse,
692 'title=s' => \$opt_title,
693 'verbose!' => \$opt_verbose,
695 usage("-", "invalid parameters") if not $result;
697 usage("-") if defined $opt_help; # see if the user asked for help
698 $opt_help = ""; # just to make -w shut-up.
700 @Podpath = split(":", $opt_podpath) if defined $opt_podpath;
701 @Libpods = split(":", $opt_libpods) if defined $opt_libpods;
703 $Backlink = $opt_backlink if defined $opt_backlink;
704 $Cachedir = $opt_cachedir if defined $opt_cachedir;
705 $Css = $opt_css if defined $opt_css;
706 $Header = $opt_header if defined $opt_header;
707 $Htmldir = $opt_htmldir if defined $opt_htmldir;
708 $Htmlroot = $opt_htmlroot if defined $opt_htmlroot;
709 $Doindex = $opt_index if defined $opt_index;
710 $Podfile = $opt_infile if defined $opt_infile;
711 $HiddenDirs = $opt_hiddendirs if defined $opt_hiddendirs;
712 $Htmlfile = $opt_outfile if defined $opt_outfile;
713 $Podroot = $opt_podroot if defined $opt_podroot;
714 $Quiet = $opt_quiet if defined $opt_quiet;
715 $Recurse = $opt_recurse if defined $opt_recurse;
716 $Title = $opt_title if defined $opt_title;
717 $Verbose = $opt_verbose if defined $opt_verbose;
719 warn "Flushing item and directory caches\n"
720 if $opt_verbose && defined $opt_flush;
721 $Dircache = "$Cachedir/pod2htmd.tmp";
722 $Itemcache = "$Cachedir/pod2htmi.tmp";
723 if (defined $opt_flush) {
724 1 while unlink($Dircache, $Itemcache);
732 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
733 my @cache_key_args = @_;
735 # A first-level cache:
736 # Don't bother reading the cache files if they still apply
737 # and haven't changed since we last read them.
739 my $this_cache_key = cache_key(@cache_key_args);
741 return if $Saved_Cache_Key and $this_cache_key eq $Saved_Cache_Key;
743 # load the cache of %Pages and %Items if possible. $tests will be
744 # non-zero if successful.
746 if (-f $dircache && -f $itemcache) {
747 warn "scanning for item cache\n" if $Verbose;
748 $tests = load_cache($dircache, $itemcache, $podpath, $podroot);
751 # if we didn't succeed in loading the cache then we must (re)build
754 warn "scanning directories in pod-path\n" if $Verbose;
755 scan_podpath($podroot, $recurse, 0);
757 $Saved_Cache_Key = cache_key(@cache_key_args);
761 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
762 return join('!', $dircache, $itemcache, $recurse,
763 @$podpath, $podroot, stat($dircache), stat($itemcache));
767 # load_cache - tries to find if the caches stored in $dircache and $itemcache
768 # are valid caches of %Pages and %Items. if they are valid then it loads
769 # them and returns a non-zero value.
772 my($dircache, $itemcache, $podpath, $podroot) = @_;
778 open(CACHE, "<$itemcache") ||
779 die "$0: error opening $itemcache for reading: $!\n";
782 # is it the same podpath?
785 $tests++ if (join(":", @$podpath) eq $_);
787 # is it the same podroot?
790 $tests++ if ($podroot eq $_);
792 # load the cache if its good
798 warn "loading item cache\n" if $Verbose;
805 warn "scanning for directory cache\n" if $Verbose;
806 open(CACHE, "<$dircache") ||
807 die "$0: error opening $dircache for reading: $!\n";
811 # is it the same podpath?
814 $tests++ if (join(":", @$podpath) eq $_);
816 # is it the same podroot?
819 $tests++ if ($podroot eq $_);
821 # load the cache if its good
827 warn "loading directory cache\n" if $Verbose;
839 # scan_podpath - scans the directories specified in @podpath for directories,
840 # .pod files, and .pm files. it also scans the pod files specified in
841 # @Libpods for =item directives.
844 my($podroot, $recurse, $append) = @_;
846 my($libpod, $dirname, $pod, @files, @poddata);
853 # scan each directory listed in @Podpath
856 || die "$0: error changing to directory $podroot: $!\n";
857 foreach $dir (@Podpath) {
858 scan_dir($dir, $recurse);
861 # scan the pods listed in @Libpods for =item directives
862 foreach $libpod (@Libpods) {
863 # if the page isn't defined then we won't know where to find it
865 next unless defined $Pages{$libpod} && $Pages{$libpod};
867 # if there is a directory then use the .pod and .pm files within it.
868 # NOTE: Only finds the first so-named directory in the tree.
869 # if ($Pages{$libpod} =~ /([^:]*[^(\.pod|\.pm)]):/) {
870 if ($Pages{$libpod} =~ /([^:]*(?<!\.pod)(?<!\.pm)):/) {
871 # find all the .pod and .pm files within the directory
873 opendir(DIR, $dirname) ||
874 die "$0: error opening directory $dirname: $!\n";
875 @files = grep(/(\.pod|\.pm)\z/ && ! -d $_, readdir(DIR));
878 # scan each .pod and .pm file for =item directives
879 foreach $pod (@files) {
880 open(POD, "<$dirname/$pod") ||
881 die "$0: error opening $dirname/$pod for input: $!\n";
884 clean_data( \@poddata );
886 scan_items( \%Items, "$dirname/$pod", @poddata);
889 # use the names of files as =item directives too.
890 ### Don't think this should be done this way - confuses issues.(WL)
891 ### foreach $pod (@files) {
892 ### $pod =~ /^(.*)(\.pod|\.pm)$/;
893 ### $Items{$1} = "$dirname/$1.html" if $1;
895 } elsif ($Pages{$libpod} =~ /([^:]*\.pod):/ ||
896 $Pages{$libpod} =~ /([^:]*\.pm):/) {
897 # scan the .pod or .pm file for =item directives
899 open(POD, "<$pod") ||
900 die "$0: error opening $pod for input: $!\n";
903 clean_data( \@poddata );
905 scan_items( \%Items, "$pod", @poddata);
907 warn "$0: shouldn't be here (line ".__LINE__."\n" unless $Quiet;
910 @poddata = (); # clean-up a bit
913 || die "$0: error changing to directory $pwd: $!\n";
915 # cache the item list for later use
916 warn "caching items for later use\n" if $Verbose;
917 open(CACHE, ">$Itemcache") ||
918 die "$0: error open $Itemcache for writing: $!\n";
920 print CACHE join(":", @Podpath) . "\n$podroot\n";
921 foreach my $key (keys %Items) {
922 print CACHE "$key $Items{$key}\n";
927 # cache the directory list for later use
928 warn "caching directories for later use\n" if $Verbose;
929 open(CACHE, ">$Dircache") ||
930 die "$0: error open $Dircache for writing: $!\n";
932 print CACHE join(":", @Podpath) . "\n$podroot\n";
933 foreach my $key (keys %Pages) {
934 print CACHE "$key $Pages{$key}\n";
941 # scan_dir - scans the directory specified in $dir for subdirectories, .pod
942 # files, and .pm files. notes those that it finds. this information will
943 # be used later in order to figure out where the pages specified in L<>
944 # links are on the filesystem.
947 my($dir, $recurse) = @_;
948 my($t, @subdirs, @pods, $pod, $dirname, @dirs);
954 opendir(DIR, $dir) ||
955 die "$0: error opening directory $dir: $!\n";
956 while (defined($_ = readdir(DIR))) {
957 if (-d "$dir/$_" && $_ ne "." && $_ ne ".."
958 && ($HiddenDirs || !/^\./)
960 $Pages{$_} = "" unless defined $Pages{$_};
961 $Pages{$_} .= "$dir/$_:";
963 } elsif (/\.pod\z/) { # .pod
965 $Pages{$_} = "" unless defined $Pages{$_};
966 $Pages{$_} .= "$dir/$_.pod:";
967 push(@pods, "$dir/$_.pod");
968 } elsif (/\.html\z/) { # .html
970 $Pages{$_} = "" unless defined $Pages{$_};
971 $Pages{$_} .= "$dir/$_.pod:";
972 } elsif (/\.pm\z/) { # .pm
974 $Pages{$_} = "" unless defined $Pages{$_};
975 $Pages{$_} .= "$dir/$_.pm:";
976 push(@pods, "$dir/$_.pm");
977 } elsif (-T "$dir/$_") { # script(?)
979 if (open(F, "$dir/$_")) {
981 while (defined($line = <F>)) {
982 if ($line =~ /^=(?:pod|head1)/) {
983 $Pages{$_} = "" unless defined $Pages{$_};
984 $Pages{$_} .= "$dir/$_.pod:";
994 # recurse on the subdirectories if necessary
996 foreach my $subdir (@subdirs) {
997 scan_dir("$dir/$subdir", $recurse);
1003 # scan_headings - scan a pod file for head[1-6] tags, note the tags, and
1007 my($sections, @data) = @_;
1008 my($tag, $which_head, $otitle, $listdepth, $index);
1015 # scan for =head directives, note their name, and build an index
1016 # pointing to each of them.
1017 foreach my $line (@data) {
1018 if ($line =~ /^=(head)([1-6])\s+(.*)/) {
1019 ($tag, $which_head, $otitle) = ($1,$2,$3);
1021 my $title = depod( $otitle );
1022 my $name = anchorify( $title );
1023 $$sections{$name} = 1;
1024 $title = process_text( \$otitle );
1026 while ($which_head != $listdepth) {
1027 if ($which_head > $listdepth) {
1028 $index .= "\n" . ("\t" x $listdepth) . "<ul>\n";
1030 } elsif ($which_head < $listdepth) {
1032 $index .= "\n" . ("\t" x $listdepth) . "</ul>\n";
1036 $index .= "\n" . ("\t" x $listdepth) . "<li>" .
1037 "<a href=\"#" . $name . "\">" .
1038 $title . "</a></li>";
1042 # finish off the lists
1043 while ($listdepth--) {
1044 $index .= "\n" . ("\t" x $listdepth) . "</ul>\n";
1047 # get rid of bogus lists
1048 $index =~ s,\t*<ul>\s*</ul>\n,,g;
1054 # scan_items - scans the pod specified by $pod for =item directives. we
1055 # will use this information later on in resolving C<> links.
1058 my( $itemref, $pod, @poddata ) = @_;
1062 $pod =~ s/\.pod\z//;
1063 $pod .= ".html" if $pod;
1065 foreach $i (0..$#poddata) {
1066 my $txt = depod( $poddata[$i] );
1068 # figure out what kind of item it is.
1069 # Build string for referencing this item.
1070 if ( $txt =~ /\A=item\s+\*\s*(.*)\Z/s ) { # bullet
1073 } elsif( $txt =~ /\A=item\s+(?>\d+\.?)\s*(.*)\Z/s ) { # numbered list
1075 } elsif( $txt =~ /\A=item\s+(.*)\Z/s ) { # plain item
1080 my $fid = fragment_id( $item );
1081 $$itemref{$fid} = "$pod" if $fid;
1086 # process_head - convert a pod head[1-6] tag and convert it to HTML format.
1089 my($tag, $heading, $hasindex) = @_;
1091 # figure out the level of the =head
1092 $tag =~ /head([1-6])/;
1096 warn "$0: $Podfile: unterminated list at =head in paragraph $Paragraph. ignoring.\n" unless $Quiet;
1097 while( $Listlevel ){
1103 if( $level == 1 && ! $Top ){
1104 print HTML "<a href=\"#__index__\"><small>$Backlink</small></a>\n"
1105 if $hasindex and $Backlink;
1106 print HTML "</p>\n<hr />\n"
1108 print HTML "</p>\n";
1111 my $name = anchorify( depod( $heading ) );
1112 my $convert = process_text( \$heading );
1113 print HTML "<h$level><a name=\"$name\">$convert</a></h$level>\n";
1118 # emit_item_tag - print an =item's text
1119 # Note: The global $EmittedItem is used for inhibiting self-references.
1123 sub emit_item_tag($$$){
1124 my( $otext, $text, $compact ) = @_;
1125 my $item = fragment_id( $text );
1127 $EmittedItem = $item;
1128 ### print STDERR "emit_item_tag=$item ($text)\n";
1130 print HTML '<strong>';
1131 if ($Items_Named{$item}++) {
1132 print HTML process_text( \$otext );
1134 my $name = 'item_' . $item;
1135 $name = anchorify($name);
1136 print HTML qq{<a name="$name">}, process_text( \$otext ), '</a>';
1138 print HTML "</strong>\n";
1139 undef( $EmittedItem );
1144 if( $Items_Seen[$Listlevel]++ == 0 ){
1145 push( @Listend, "</$tag>" );
1146 print HTML "<$tag>\n";
1148 my $emitted = $tag eq 'dl' ? 'dt' : 'li';
1149 print HTML "<$emitted>";
1154 # process_item - convert a pod item tag and convert it to HTML format.
1158 my $need_dd = 0; # set to 1 if we need a <dd></dd> after an item
1160 # lots of documents start a list without doing an =over. this is
1161 # bad! but, the proper thing to do seems to be to just assume
1162 # they did do an =over. so warn them once and then continue.
1163 if( $Listlevel == 0 ){
1164 warn "$0: $Podfile: unexpected =item directive in paragraph $Paragraph. ignoring.\n" unless $Quiet;
1168 # formatting: insert a paragraph if preceding item has >1 paragraph
1170 print HTML $need_dd ? "</dd>\n" : "</li>\n" if $After_Lpar;
1174 # remove formatting instructions from the text
1175 my $text = depod( $otext );
1177 my $emitted; # the tag actually emitted, used for closing
1179 # all the list variants:
1180 if( $text =~ /\A\*/ ){ # bullet
1181 $emitted = emit_li( 'ul' );
1182 if ($text =~ /\A\*\s+(.+)\Z/s ) { # with additional text
1184 $otext =~ s/\A\*\s+//;
1185 emit_item_tag( $otext, $tag, 1 );
1188 } elsif( $text =~ /\A\d+/ ){ # numbered list
1189 $emitted = emit_li( 'ol' );
1190 if ($text =~ /\A(?>\d+\.?)\s*(.+)\Z/s ) { # with additional text
1192 $otext =~ s/\A\d+\.?\s*//;
1193 emit_item_tag( $otext, $tag, 1 );
1196 } else { # definition list
1197 $emitted = emit_li( 'dl' );
1198 if ($text =~ /\A(.+)\Z/s ){ # should have text
1199 emit_item_tag( $otext, $text, 1 );
1208 # process_over - process a pod over tag and start a corresponding HTML list.
1213 push( @Items_Seen, 0 );
1218 # process_back - process a pod back tag and convert it to HTML format.
1221 my $need_dd = shift;
1222 if( $Listlevel == 0 ){
1223 warn "$0: $Podfile: unexpected =back directive in paragraph $Paragraph. ignoring.\n" unless $Quiet;
1227 # close off the list. note, I check to see if $Listend[$Listlevel] is
1228 # defined because an =item directive may have never appeared and thus
1229 # $Listend[$Listlevel] may have never been initialized.
1231 if( defined $Listend[$Listlevel] ){
1232 print HTML $need_dd ? "</dd>\n" : "</li>\n" if $After_Lpar;
1233 print HTML $Listend[$Listlevel];
1239 # clean up item count
1244 # process_cut - process a pod cut tag, thus start ignoring pod directives.
1251 # process_pod - process a pod tag, thus stop ignoring pod directives
1252 # until we see a corresponding cut.
1255 # no need to set $Ignore to 0 cause the main loop did it
1259 # process_for - process a =for pod tag. if it's for html, spit
1260 # it out verbatim, if illustration, center it, otherwise ignore it.
1263 my($whom, $text) = @_;
1264 if ( $whom =~ /^(pod2)?html$/i) {
1266 } elsif ($whom =~ /^illustration$/i) {
1267 1 while chomp $text;
1268 for my $ext (qw[.png .gif .jpeg .jpg .tga .pcl .bmp]) {
1269 $text .= $ext, last if -r "$text$ext";
1271 print HTML qq{<p align="center"><img src="$text" alt="$text illustration" /></p>};
1276 # process_begin - process a =begin pod tag. this pushes
1277 # whom we're beginning on the begin stack. if there's a
1278 # begin stack, we only print if it us.
1281 my($whom, $text) = @_;
1283 push (@Begin_Stack, $whom);
1284 if ( $whom =~ /^(pod2)?html$/) {
1285 print HTML $text if $text;
1290 # process_end - process a =end pod tag. pop the
1291 # begin stack. die if we're mismatched.
1294 my($whom, $text) = @_;
1296 if ($Begin_Stack[-1] ne $whom ) {
1297 die "Unmatched begin/end at chunk $Paragraph\n"
1299 pop( @Begin_Stack );
1303 # process_pre - indented paragraph, made into <pre></pre>
1312 # insert spaces in place of tabs
1315 1 while $line =~ s/(\t+)/' ' x ((length($1) * 8) - $-[0] % 8)/e;
1319 # convert some special chars to HTML escapes
1320 $rest = html_escape($rest);
1322 # try and create links for all occurrences of perl.* within
1323 # the preformatted text.
1327 if ( defined $Pages{$2} ){ # is a link
1328 qq($1<a href="$Htmlroot/$Pages{$2}">$2</a>);
1329 } elsif (defined $Pages{dosify($2)}) { # is a link
1330 qq($1<a href="$Htmlroot/$Pages{dosify($2)}">$2</a>);
1336 (<a\ href="?) ([^>:]*:)? ([^>:]*) \.pod: ([^>:]*:)?
1339 if ( $Htmlfileurl ne '' ){
1340 # Here, we take advantage of the knowledge
1341 # that $Htmlfileurl ne '' implies $Htmlroot eq ''.
1342 # Since $Htmlroot eq '', we need to prepend $Htmldir
1343 # on the fron of the link to get the absolute path
1344 # of the link's target. We check for a leading '/'
1345 # to avoid corrupting links that are #, file:, etc.
1347 $old_url = "$Htmldir$old_url" if $old_url =~ m{^\/};
1348 $url = relativize_url( "$old_url.html", $Htmlfileurl );
1355 # Look for embedded URLs and make them into links. We don't
1356 # relativize them since they are best left as the author intended.
1358 my $urls = '(' . join ('|', qw{
1371 my $gunk = '/#~:.?+=&%@!\-';
1372 my $punc = '.:!?\-;';
1373 my $any = "${ltrs}${gunk}${punc}";
1376 \b # start at word boundary
1378 $urls : # need resource and a colon
1379 (?!:) # Ignore File::, among others.
1380 [$any] +? # followed by one or more of any valid
1381 # character, but be conservative and
1382 # take only what you need to....
1385 " > # maybe pre-quoted '<a href="...">'
1387 [$punc]* # 0 or more punctuation
1389 [^$any] # by a non-url char
1391 $ # end of the string
1394 $ # then end of the string
1396 }{<a href="$1">$1</a>}igox;
1398 # text should be as it is (verbatim)
1404 # pure text processing
1406 # pure_text/inIS_text: differ with respect to automatic C<> recognition.
1407 # we don't want this to happen within IS
1411 process_puretext( $text, \$PTQuote, 1 );
1416 process_puretext( $text, \$PTQuote, 0 );
1420 # process_puretext - process pure text (without pod-escapes) converting
1421 # double-quotes and handling implicit C<> links.
1423 sub process_puretext {
1424 my($text, $quote, $notinIS) = @_;
1426 ## Guessing at func() or [\$\@%&]*var references in plain text is destined
1427 ## to produce some strange looking ref's. uncomment to disable:
1430 my(@words, $lead, $trail);
1432 # convert double-quotes to single-quotes
1433 if( $$quote && $text =~ s/"/''/s ){
1436 while ($text =~ s/"([^"]*)"/``$1''/sg) {};
1437 $$quote = 1 if $text =~ s/"/``/s;
1439 # keep track of leading and trailing white-space
1440 $lead = ($text =~ s/\A(\s+)//s ? $1 : "");
1441 $trail = ($text =~ s/(\s+)\Z//s ? $1 : "");
1443 # split at space/non-space boundaries
1444 @words = split( /(?<=\s)(?=\S)|(?<=\S)(?=\s)/, $text );
1446 # process each word individually
1447 foreach my $word (@words) {
1449 next if $word =~ /^\s*$/;
1450 # see if we can infer a link
1451 if( $notinIS && $word =~ /^(\w+)\((.*)\)$/ ) {
1452 # has parenthesis so should have been a C<> ref
1453 ## try for a pagename (perlXXX(1))?
1454 my( $func, $args ) = ( $1, $2 );
1455 if( $args =~ /^\d+$/ ){
1456 my $url = page_sect( $word, '' );
1458 $word = "<a href=\"$url\">the $word manpage</a>";
1462 ## try function name for a link, append tt'ed argument list
1463 $word = emit_C( $func, '', "($args)");
1465 #### disabled. either all (including $\W, $\w+{.*} etc.) or nothing.
1466 ## } elsif( $notinIS && $word =~ /^[\$\@%&*]+\w+$/) {
1467 ## # perl variables, should be a C<> ref
1468 ## $word = emit_C( $word );
1470 } elsif ($word =~ m,^\w+://\w,) {
1472 # Don't relativize it: leave it as the author intended
1473 $word = qq(<a href="$word">$word</a>);
1474 } elsif ($word =~ /[\w.-]+\@[\w-]+\.\w/) {
1475 # looks like an e-mail address
1476 my ($w1, $w2, $w3) = ("", $word, "");
1477 ($w1, $w2, $w3) = ("(", $1, ")$2") if $word =~ /^\((.*?)\)(,?)/;
1478 ($w1, $w2, $w3) = ("<", $1, ">$2") if $word =~ /^<(.*?)>(,?)/;
1479 $word = qq($w1<a href="mailto:$w2">$w2</a>$w3);
1481 $word = html_escape($word) if $word =~ /["&<>]/;
1485 # put everything back together
1486 return $lead . join( '', @words ) . $trail;
1491 # process_text - handles plaintext that appears in the input pod file.
1492 # there may be pod commands embedded within the text so those must be
1493 # converted to html commands.
1496 sub process_text1($$;$$);
1497 sub pattern ($) { $_[0] ? '[^\S\n]+'.('>' x ($_[0] + 1)) : '>' }
1498 sub closing ($) { local($_) = shift; (defined && s/\s+$//) ? length : 0 }
1503 my $res = process_text1( 0, $tref );
1507 sub process_text1($$;$$){
1508 my( $lev, $rstr, $func, $closing ) = @_;
1511 unless (defined $func) {
1517 # B<text> - boldface
1518 $res = '<strong>' . process_text1( $lev, $rstr ) . '</strong>';
1520 } elsif( $func eq 'C' ){
1521 # C<code> - can be a ref or <code></code>
1522 # need to extract text
1523 my $par = go_ahead( $rstr, 'C', $closing );
1525 ## clean-up of the link target
1526 my $text = depod( $par );
1528 ### my $x = $par =~ /[BI]</ ? 'yes' : 'no' ;
1529 ### print STDERR "-->call emit_C($par) lev=$lev, par with BI=$x\n";
1531 $res = emit_C( $text, $lev > 1 || ($par =~ /[BI]</) );
1533 } elsif( $func eq 'E' ){
1534 # E<x> - convert to character
1535 $$rstr =~ s/^([^>]*)>//;
1537 $escape =~ s/^(\d+|X[\dA-F]+)$/#$1/i;
1540 } elsif( $func eq 'F' ){
1541 # F<filename> - italizice
1542 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1544 } elsif( $func eq 'I' ){
1545 # I<text> - italizice
1546 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1548 } elsif( $func eq 'L' ){
1550 ## L<text|cross-ref> => produce text, use cross-ref for linking
1551 ## L<cross-ref> => make text from cross-ref
1552 ## need to extract text
1553 my $par = go_ahead( $rstr, 'L', $closing );
1555 # some L<>'s that shouldn't be:
1556 # a) full-blown URL's are emitted as-is
1557 if( $par =~ m{^\w+://}s ){
1558 return make_URL_href( $par );
1560 # b) C<...> is stripped and treated as C<>
1561 if( $par =~ /^C<(.*)>$/ ){
1562 my $text = depod( $1 );
1563 return emit_C( $text, $lev > 1 || ($par =~ /[BI]</) );
1566 # analyze the contents
1567 $par =~ s/\n/ /g; # undo word-wrapped tags
1570 if( $par =~ s{^([^|]+)\|}{} ){
1574 # make sure sections start with a /
1577 my( $page, $section, $ident );
1579 # check for link patterns
1580 if( $par =~ m{^([^/]+?)/(?!")(.*?)$} ){ # name/ident
1581 # we've got a name/ident (no quotes)
1582 ( $page, $ident ) = ( $1, $2 );
1583 ### print STDERR "--> L<$par> to page $page, ident $ident\n";
1585 } elsif( $par =~ m{^(.*?)/"?(.*?)"?$} ){ # [name]/"section"
1586 # even though this should be a "section", we go for ident first
1587 ( $page, $ident ) = ( $1, $2 );
1588 ### print STDERR "--> L<$par> to page $page, section $section\n";
1590 } elsif( $par =~ /\s/ ){ # this must be a section with missing quotes
1591 ( $page, $section ) = ( '', $par );
1592 ### print STDERR "--> L<$par> to void page, section $section\n";
1595 ( $page, $section ) = ( $par, '' );
1596 ### print STDERR "--> L<$par> to page $par, void section\n";
1599 # now, either $section or $ident is defined. the convoluted logic
1600 # below tries to resolve L<> according to what the user specified.
1601 # failing this, we try to find the next best thing...
1602 my( $url, $ltext, $fid );
1605 if( defined $ident ){
1606 ## try to resolve $ident as an item
1607 ( $url, $fid ) = coderef( $page, $ident );
1609 if( ! defined( $linktext ) ){
1611 $linktext .= " in " if $ident && $page;
1612 $linktext .= "the $page manpage" if $page;
1614 ### print STDERR "got coderef url=$url\n";
1617 ## no luck: go for a section (auto-quoting!)
1620 ## now go for a section
1621 my $htmlsection = htmlify( $section );
1622 $url = page_sect( $page, $htmlsection );
1624 if( ! defined( $linktext ) ){
1625 $linktext = $section;
1626 $linktext .= " in " if $section && $page;
1627 $linktext .= "the $page manpage" if $page;
1629 ### print STDERR "got page/section url=$url\n";
1632 ## no luck: go for an ident
1639 ( $url, $fid ) = coderef( $page, $ident );
1641 if( ! defined( $linktext ) ){
1643 $linktext .= " in " if $ident && $page;
1644 $linktext .= "the $page manpage" if $page;
1646 ### print STDERR "got section=>coderef url=$url\n";
1650 # warning; show some text.
1651 $linktext = $opar unless defined $linktext;
1652 warn "$0: $Podfile: cannot resolve L<$opar> in paragraph $Paragraph.\n" unless $Quiet;
1655 # now we have a URL or just plain code
1656 $$rstr = $linktext . '>' . $$rstr;
1657 if( defined( $url ) ){
1658 $res = "<a href=\"$url\">" . process_text1( $lev, $rstr ) . '</a>';
1660 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1663 } elsif( $func eq 'S' ){
1664 # S<text> - non-breaking spaces
1665 $res = process_text1( $lev, $rstr );
1666 $res =~ s/ / /g;
1668 } elsif( $func eq 'X' ){
1670 $$rstr =~ s/^[^>]*>//;
1672 } elsif( $func eq 'Z' ){
1674 warn "$0: $Podfile: invalid X<> in paragraph $Paragraph.\n"
1675 unless $$rstr =~ s/^>// or $Quiet;
1678 my $term = pattern $closing;
1679 while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)//s ){
1680 # all others: either recurse into new function or
1681 # terminate at closing angle bracket(s)
1683 $pt .= $2 if !$3 && $lev == 1;
1684 $res .= $lev == 1 ? pure_text( $pt ) : inIS_text( $pt );
1685 return $res if !$3 && $lev > 1;
1687 $res .= process_text1( $lev, $rstr, $3, closing $4 );
1691 $res .= pure_text( $$rstr );
1693 warn "$0: $Podfile: undelimited $func<> in paragraph $Paragraph.\n" unless $Quiet;
1700 # go_ahead: extract text of an IS (can be nested)
1703 my( $rstr, $func, $closing ) = @_;
1705 my @closing = ($closing);
1707 s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|@{[pattern $closing[0]]})//s ){
1711 return $res unless @closing;
1713 unshift @closing, closing $4;
1717 warn "$0: $Podfile: undelimited $func<> in paragraph $Paragraph.\n" unless $Quiet;
1722 # emit_C - output result of C<text>
1723 # $text is the depod-ed text
1726 my( $text, $nocode, $args ) = @_;
1727 $args = '' unless defined $args;
1729 my( $url, $fid ) = coderef( undef(), $text );
1731 # need HTML-safe text
1732 my $linktext = html_escape( "$text$args" );
1734 if( defined( $url ) &&
1735 (!defined( $EmittedItem ) || $EmittedItem ne $fid ) ){
1736 $res = "<a href=\"$url\"><code>$linktext</code></a>";
1737 } elsif( 0 && $nocode ){
1740 $res = "<code>$linktext</code>";
1746 # html_escape: make text safe for HTML
1750 $rest =~ s/&/&/g;
1751 $rest =~ s/</</g;
1752 $rest =~ s/>/>/g;
1753 $rest =~ s/"/"/g;
1754 # ' is only in XHTML, not HTML4. Be conservative
1755 #$rest =~ s/'/'/g;
1761 # dosify - convert filenames to 8.3
1765 return lc($str) if $^O eq 'VMS'; # VMS just needs casing
1768 $str =~ s/(\.\w+)/substr ($1,0,4)/ge;
1769 $str =~ s/(\w+)/substr ($1,0,8)/ge;
1775 # page_sect - make a URL from the text of a L<>
1778 my( $page, $section ) = @_;
1779 my( $linktext, $page83, $link); # work strings
1781 # check if we know that this is a section in this page
1782 if (!defined $Pages{$page} && defined $Sections{$page}) {
1785 ### print STDERR "reset page='', section=$section\n";
1788 $page83=dosify($page);
1789 $page=$page83 if (defined $Pages{$page83});
1791 $link = "#" . anchorify( $section );
1792 } elsif ( $page =~ /::/ ) {
1794 # Search page cache for an entry keyed under the html page name,
1795 # then look to see what directory that page might be in. NOTE:
1796 # this will only find one page. A better solution might be to produce
1797 # an intermediate page that is an index to all such pages.
1798 my $page_name = $page ;
1799 $page_name =~ s,^.*/,,s ;
1800 if ( defined( $Pages{ $page_name } ) &&
1801 $Pages{ $page_name } =~ /([^:]*$page)\.(?:pod|pm):/
1806 # NOTE: This branch assumes that all A::B pages are located in
1807 # $Htmlroot/A/B.html . This is often incorrect, since they are
1808 # often in $Htmlroot/lib/A/B.html or such like. Perhaps we could
1809 # analyze the contents of %Pages and figure out where any
1810 # cousins of A::B are, then assume that. So, if A::B isn't found,
1811 # but A::C is found in lib/A/C.pm, then A::B is assumed to be in
1812 # lib/A/B.pm. This is also limited, but it's an improvement.
1813 # Maybe a hints file so that the links point to the correct places
1817 $link = "$Htmlroot/$page.html";
1818 $link .= "#" . anchorify( $section ) if ($section);
1819 } elsif (!defined $Pages{$page}) {
1822 $section = anchorify( $section ) if $section ne "";
1823 ### print STDERR "...section=$section\n";
1825 # if there is a directory by the name of the page, then assume that an
1826 # appropriate section will exist in the subdirectory
1827 # if ($section ne "" && $Pages{$page} =~ /([^:]*[^(\.pod|\.pm)]):/) {
1828 if ($section ne "" && $Pages{$page} =~ /([^:]*(?<!\.pod)(?<!\.pm)):/) {
1829 $link = "$Htmlroot/$1/$section.html";
1830 ### print STDERR "...link=$link\n";
1832 # since there is no directory by the name of the page, the section will
1833 # have to exist within a .html of the same name. thus, make sure there
1834 # is a .pod or .pm that might become that .html
1836 $section = "#$section" if $section;
1837 ### print STDERR "...section=$section\n";
1839 # check if there is a .pod with the page name.
1840 # for L<Foo>, Foo.(pod|pm) is preferred to A/Foo.(pod|pm)
1841 if ($Pages{$page} =~ /([^:]*)\.(?:pod|pm):/) {
1842 $link = "$Htmlroot/$1.html$section";
1850 # Here, we take advantage of the knowledge that $Htmlfileurl ne ''
1851 # implies $Htmlroot eq ''. This means that the link in question
1852 # needs a prefix of $Htmldir if it begins with '/'. The test for
1853 # the initial '/' is done to avoid '#'-only links, and to allow
1854 # for other kinds of links, like file:, ftp:, etc.
1856 if ( $Htmlfileurl ne '' ) {
1857 $link = "$Htmldir$link" if $link =~ m{^/}s;
1858 $url = relativize_url( $link, $Htmlfileurl );
1859 # print( " b: [$link,$Htmlfileurl,$url]\n" );
1872 # relativize_url - convert an absolute URL to one relative to a base URL.
1873 # Assumes both end in a filename.
1875 sub relativize_url {
1876 my ($dest,$source) = @_ ;
1878 my ($dest_volume,$dest_directory,$dest_file) =
1879 File::Spec::Unix->splitpath( $dest ) ;
1880 $dest = File::Spec::Unix->catpath( $dest_volume, $dest_directory, '' ) ;
1882 my ($source_volume,$source_directory,$source_file) =
1883 File::Spec::Unix->splitpath( $source ) ;
1884 $source = File::Spec::Unix->catpath( $source_volume, $source_directory, '' ) ;
1887 if ( $dest ne '' ) {
1888 $rel_path = File::Spec::Unix->abs2rel( $dest, $source ) ;
1891 if ( $rel_path ne '' &&
1892 substr( $rel_path, -1 ) ne '/' &&
1893 substr( $dest_file, 0, 1 ) ne '#'
1895 $rel_path .= "/$dest_file" ;
1898 $rel_path .= "$dest_file" ;
1906 # coderef - make URL from the text of a C<>
1909 my( $page, $item ) = @_;
1912 my $fid = fragment_id( $item );
1913 if( defined( $page ) && $page ne "" ){
1914 # we have been given a $page...
1917 # Do we take it? Item could be a section!
1918 my $base = $Items{$fid} || "";
1919 $base =~ s{[^/]*/}{};
1920 if( $base ne "$page.html" ){
1921 ### print STDERR "coderef( $page, $item ): items{$fid} = $Items{$fid} = $base => discard page!\n";
1926 # no page - local items precede cached items
1927 if( defined( $fid ) ){
1928 if( exists $Local_Items{$fid} ){
1929 $page = $Local_Items{$fid};
1931 $page = $Items{$fid};
1936 # if there was a pod file that we found earlier with an appropriate
1937 # =item directive, then create a link to that page.
1938 if( defined $page ){
1940 if( exists $Pages{$page} and $Pages{$page} =~ /([^:.]*)\.[^:]*:/){
1941 $page = $1 . '.html';
1943 my $link = "$Htmlroot/$page#item_" . anchorify($fid);
1945 # Here, we take advantage of the knowledge that $Htmlfileurl
1946 # ne '' implies $Htmlroot eq ''.
1947 if ( $Htmlfileurl ne '' ) {
1948 $link = "$Htmldir$link" ;
1949 $url = relativize_url( $link, $Htmlfileurl ) ;
1954 $url = "#item_" . anchorify($fid);
1957 confess "url has space: $url" if $url =~ /"[^"]*\s[^"]*"/;
1959 return( $url, $fid );
1965 # Adapted from Nick Ing-Simmons' PodToHtml package.
1967 my $source_file = shift ;
1968 my $destination_file = shift;
1970 my $source = URI::file->new_abs($source_file);
1971 my $uo = URI::file->new($destination_file,$source)->abs;
1972 return $uo->rel->as_string;
1977 # finish_list - finish off any pending HTML lists. this should be called
1978 # after the entire pod file has been read and converted.
1981 while ($Listlevel > 0) {
1982 print HTML "</dl>\n";
1988 # htmlify - converts a pod section specification to a suitable section
1989 # specification for HTML. Note that we keep spaces and special characters
1990 # except ", ? (Netscape problem) and the hyphen (writer's problem...).
1994 $heading =~ s/(\s+)/ /g;
1995 $heading =~ s/\s+\Z//;
1996 $heading =~ s/\A\s+//;
1997 # The hyphen is a disgrace to the English language.
1998 $heading =~ s/[-"?]//g;
1999 $heading = lc( $heading );
2004 # similar to htmlify, but turns non-alphanumerics into underscores
2008 $anchor = htmlify($anchor);
2009 $anchor =~ s/\W/_/g;
2014 # depod - convert text by eliminating all interior sequences
2015 # Note: can be called with copy or modify semantics
2022 $E2c{amp} = '&'; # in Tk's pods
2030 ${$_[0]} = depod1( \$string );
2038 my( $rstr, $func, $closing ) = @_;
2040 return $res unless defined $$rstr;
2041 if( ! defined( $func ) ){
2042 # skip to next begin of an interior sequence
2043 while( $$rstr =~ s/\A(.*?)([BCEFILSXZ])<(<+[^\S\n]+)?//s ){
2044 # recurse into its text
2045 $res .= $1 . depod1( $rstr, $2, closing $3);
2048 } elsif( $func eq 'E' ){
2049 # E<x> - convert to character
2050 $$rstr =~ s/^([^>]*)>//;
2051 $res .= $E2c{$1} || "";
2052 } elsif( $func eq 'X' ){
2054 $$rstr =~ s/^[^>]*>//;
2055 } elsif( $func eq 'Z' ){
2059 # all others: either recurse into new function or
2060 # terminate at closing angle bracket
2061 my $term = pattern $closing;
2062 while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)//s ){
2065 $res .= depod1( $rstr, $3, closing $4 );
2067 ## If we're here and $2 ne '>': undelimited interior sequence.
2068 ## Ignored, as this is called without proper indication of where we are.
2069 ## Rely on process_text to produce diagnostics.
2075 # fragment_id - construct a fragment identifier from:
2077 # b) contents of C<...>
2082 $text =~ s/\s+\Z//s;
2084 # a method or function?
2085 return $1 if $text =~ /(\w+)\s*\(/;
2086 return $1 if $text =~ /->\s*(\w+)\s*\(?/;
2089 return $1 if $text =~ /^([\$\@%*]\S+)/;
2091 # some pattern matching operator?
2092 return $1 if $text =~ m|^(\w+/).*/\w*$|;
2094 # fancy stuff... like "do { }"
2095 return $1 if $text =~ m|^(\w+)\s*{.*}$|;
2097 # honour the perlfunc manpage: func [PAR[,[ ]PAR]...]
2098 # and some funnies with ... Module ...
2099 return $1 if $text =~ m{^([a-z\d_]+)(\s+[A-Z,/& ][A-Z\d,/& ]*)?$};
2100 return $1 if $text =~ m{^([a-z\d]+)\s+Module(\s+[A-Z\d,/& ]+)?$};
2103 $text =~ s/\s+/_/sg;
2105 defined( $HC[ord($1)] ) ? $HC[ord($1)]
2106 : ( $HC[ord($1)] = sprintf( "%%%02X", ord($1) ) ) }gxe;
2107 $text = substr( $text, 0, 50 );
2114 # make_URL_href - generate HTML href from URL
2115 # Special treatment for CGI queries.
2117 sub make_URL_href($){
2120 s{^(http:[-\w/#~:.+=&%@!]+)(\?.*)$}{<a href="$1$2">$1</a>}i ){
2121 $url = "<a href=\"$url\">$url</a>";