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.
207 my($Dircache, $Itemcache);
210 my($Htmlroot, $Htmldir, $Htmlfile, $Htmlfileurl);
211 my($Podfile, @Podpath, $Podroot);
220 my($Listlevel, @Listend);
222 use vars qw($Ignore); # need to localize it later.
224 my(%Items_Named, @Items_Seen);
233 my %Pages = (); # associative array used to find the location
234 # of pages referenced by L<> links.
235 my %Items = (); # associative array used to find the location
236 # of =item directives referenced by C<> links
242 my $Curdir = File::Spec->curdir;
247 $Cachedir = "."; # The directory to which item and directory
248 # caches will be written.
250 $Dircache = "pod2htmd.tmp";
251 $Itemcache = "pod2htmi.tmp";
253 @Begin_Stack = (); # begin/end stack
255 @Libpods = (); # files to search for links from C<> directives
256 $Htmlroot = "/"; # http-server base directory from which all
257 # relative paths in $podpath stem.
258 $Htmldir = ""; # The directory to which the html pages
259 # will (eventually) be written.
260 $Htmlfile = ""; # write to stdout by default
261 $Htmlfileurl = "" ; # The url that other files would use to
262 # refer to this file. This is only used
263 # to make relative urls that point to
266 $Podfile = ""; # read from stdin by default
267 @Podpath = (); # list of directories containing library pods.
268 $Podroot = $Curdir; # filesystem base directory from which all
269 # relative paths in $podpath stem.
270 $Css = ''; # Cascading style sheet
271 $Recurse = 1; # recurse on subdirectories in $podpath.
272 $Quiet = 0; # not quiet by default
273 $Verbose = 0; # not verbose by default
274 $Doindex = 1; # non-zero if we should generate an index
275 $Backlink = ''; # text for "back to top" links
276 $Listlevel = 0; # current list depth
277 @Listend = (); # the text to use to end the list.
278 $After_Lpar = 0; # set to true after a par in an =item
279 $Ignore = 1; # whether or not to format text. we don't
280 # format text until we hit our first pod
283 @Items_Seen = (); # for multiples of the same item in perlfunc
285 $Header = 0; # produce block header/footer
286 $Title = ''; # title to give the pod(s)
287 $Top = 1; # true if we are at the top of the doc. used
288 # to prevent the first <hr /> directive.
289 $Paragraph = ''; # which paragraph we're processing (used
290 # for error messages)
291 $PTQuote = 0; # status of double-quote conversion
292 %Sections = (); # sections within this page
295 $Is83 = $^O eq 'dos'; # Is it an 8.3 filesystem?
299 # clean_data: global clean-up of pod data
303 for my $i ( 0..$#{$dataref} ) {
304 ${$dataref}[$i] =~ s/\s+\Z//;
306 # have a look for all-space lines
307 if( ${$dataref}[$i] =~ /^\s+$/m and $dataref->[$i] !~ /^\s/ ){
308 my @chunks = split( /^\s+$/m, ${$dataref}[$i] );
309 splice( @$dataref, $i, 1, @chunks );
322 $Is83 = 0 if (defined (&Dos::UseLFN) && Dos::UseLFN());
324 # cache of %Pages and %Items from last time we ran pod2html
326 #undef $opt_help if defined $opt_help;
328 # parse the command-line parameters
329 parse_command_line();
331 # escape the backlink argument (same goes for title but is done later...)
332 $Backlink = html_escape($Backlink) if defined $Backlink;
334 # set some variables to their default values if necessary
336 unless (@ARGV && $ARGV[0]) {
337 $Podfile = "-" unless $Podfile; # stdin
338 open(POD, "<$Podfile")
339 || die "$0: cannot open $Podfile file for input: $!\n";
341 $Podfile = $ARGV[0]; # XXX: might be more filenames
344 $Htmlfile = "-" unless $Htmlfile; # stdout
345 $Htmlroot = "" if $Htmlroot eq "/"; # so we don't get a //
346 $Htmldir =~ s#/\z## ; # so we don't get a //
348 && defined( $Htmldir )
350 && substr( $Htmlfile, 0, length( $Htmldir ) ) eq $Htmldir
353 # Set the 'base' url for this file, so that we can use it
354 # as the location from which to calculate relative links
355 # to other files. If this is '', then absolute links will
356 # be used throughout.
357 $Htmlfileurl= "$Htmldir/" . substr( $Htmlfile, length( $Htmldir ) + 1);
360 # read the pod a paragraph at a time
361 warn "Scanning for sections in input file(s)\n" if $Verbose;
370 @poddata = map { s/\r\n/\n/g;
372 map { "$_\n\n" } split /\n\n/ :
375 @poddata = map { s/\r/\n/g;
377 map { "$_\n\n" } split /\n\n/ :
384 clean_data( \@poddata );
386 # scan the pod for =head[1-6] directives and build an index
387 my $index = scan_headings(\%Sections, @poddata);
390 warn "No headings in $Podfile\n" if $Verbose;
393 # open the output file
394 open(HTML, ">$Htmlfile")
395 || die "$0: cannot open $Htmlfile file for output: $!\n";
397 # put a title in the HTML file if one wasn't specified
400 for (my $i = 0; $i < @poddata; $i++) {
401 if ($poddata[$i] =~ /^=head1\s*NAME\b/m) {
402 for my $para ( @poddata[$i, $i+1] ) {
404 if ($Title) = $para =~ /(\S+\s+-+.*\S)/s;
411 if (!$Title and $Podfile =~ /\.pod\z/) {
412 # probably a split pod so take first =head[12] as title
413 for (my $i = 0; $i < @poddata; $i++) {
414 last if ($Title) = $poddata[$i] =~ /^=head[12]\s*(.*)/;
416 warn "adopted '$Title' as title for $Podfile\n"
417 if $Verbose and $Title;
420 $Title =~ s/\s*\(.*\)//;
422 warn "$0: no title for $Podfile.\n" unless $Quiet;
423 $Podfile =~ /^(.*)(\.[^.\/]+)?\z/s;
424 $Title = ($Podfile eq "-" ? 'No Title' : $1);
425 warn "using $Title" if $Verbose;
427 $Title = html_escape($Title);
430 my $bodystyle = ' style="background-color: white"';
431 my $tdstyle = ' style="background-color: #cccccc"';
434 $csslink = qq(\n<link rel="stylesheet" href="$Css" type="text/css" />);
435 $csslink =~ s,\\,/,g;
436 $csslink =~ s,(/.):,$1|,;
441 my $block = $Header ? <<END_OF_BLOCK : '';
442 <table border="0" width="100%" cellspacing="0" cellpadding="3">
443 <tr><td class="block"$tdstyle valign="middle">
444 <big><strong><span class="block"> $Title</span></strong></big>
449 print HTML <<END_OF_HEAD;
450 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
451 <html xmlns="http://www.w3.org/1999/xhtml">
453 <title>$Title</title>$csslink
454 <link rev="made" href="mailto:$Config{perladmin}" />
461 # load/reload/validate/cache %Pages and %Items
462 get_cache($Dircache, $Itemcache, \@Podpath, $Podroot, $Recurse);
464 # scan the pod for =item directives
465 scan_items( \%Local_Items, "", @poddata);
467 # put an index at the top of the file. note, if $Doindex is 0 we
468 # still generate an index, but surround it with an html comment.
469 # that way some other program can extract it if desired.
471 print HTML "<p><a name=\"__index__\"></a></p>\n";
472 print HTML "<!-- INDEX BEGIN -->\n";
473 print HTML "<!--\n" unless $Doindex;
475 print HTML "-->\n" unless $Doindex;
476 print HTML "<!-- INDEX END -->\n\n";
477 print HTML "<hr />\n" if $Doindex and $index;
479 # now convert this file
480 my $after_item; # set to true after an =item
482 warn "Converting input file $Podfile\n" if $Verbose;
483 foreach my $i (0..$#poddata){
484 $PTQuote = 0; # status of quote conversion
488 if (/^(=.*)/s) { # is it a pod directive?
493 if (/^=begin\s+(\S+)\s*(.*)/si) {# =begin
494 process_begin($1, $2);
495 } elsif (/^=end\s+(\S+)\s*(.*)/si) {# =end
497 } elsif (/^=cut/) { # =cut
499 } elsif (/^=pod/) { # =pod
502 next if @Begin_Stack && $Begin_Stack[-1] ne 'html';
504 if (/^=(head[1-6])\s+(.*\S)/s) { # =head[1-6] heading
505 process_head( $1, $2, $Doindex && $index );
506 } elsif (/^=item\s*(.*\S)?/sm) { # =item text
507 $need_dd = process_item( $1 );
509 } elsif (/^=over\s*(.*)/) { # =over N
511 } elsif (/^=back/) { # =back
513 } elsif (/^=for\s+(\S+)\s*(.*)/si) {# =for
517 warn "$0: $Podfile: unknown pod directive '$1' in "
518 . "paragraph $Paragraph. ignoring.\n";
525 next if @Begin_Stack && $Begin_Stack[-1] ne 'html';
526 print HTML and next if @Begin_Stack && $Begin_Stack[-1] eq 'html';
527 print HTML "<dd>\n" if $need_dd;
529 if( $text =~ /\A\s+/ ){
530 process_pre( \$text );
531 print HTML "<pre>\n$text</pre>\n";
534 process_text( \$text );
536 # experimental: check for a paragraph where all lines
537 # have some ...\t...\t...\n pattern
539 my @lines = split( "\n", $text );
542 foreach my $line ( @lines ){
543 if( $line =~ /\S/ && $line !~ /\t/ ){
549 $text =~ s/\t+/<td>/g;
550 $text =~ s/^/<tr><td>/gm;
551 $text = '<table cellspacing="0" cellpadding="0">' .
556 ## end of experimental
559 print HTML "$text\n";
562 print HTML "<p>$text</p>\n";
565 print HTML "</dd>\n" if $need_dd;
570 # finish off any pending directives
574 print HTML "<p><a href=\"#__index__\"><small>$Backlink</small></a></p>\n"
575 if $Doindex and $index and $Backlink;
577 print HTML <<END_OF_TAIL;
584 # close the html file
587 warn "Finished\n" if $Verbose;
590 ##############################################################################
594 warn "$0: $podfile: @_\n" if @_;
596 Usage: $0 --help --htmlroot=<name> --infile=<name> --outfile=<name>
597 --podpath=<name>:...:<name> --podroot=<name>
598 --libpods=<name>:...:<name> --recurse --verbose --index
599 --netscape --norecurse --noindex --cachedir=<name>
601 --backlink - set text for "back to top" links (default: none).
602 --cachedir - directory for the item and directory cache files.
603 --css - stylesheet URL
604 --flush - flushes the item and directory caches.
605 --[no]header - produce block header/footer (default is no headers).
606 --help - prints this message.
607 --htmldir - directory for resulting HTML files.
608 --htmlroot - http-server base directory from which all relative paths
609 in podpath stem (default is /).
610 --[no]index - generate an index at the top of the resulting html
612 --infile - filename for the pod to convert (input taken from stdin
614 --libpods - colon-separated list of pages to search for =item pod
615 directives in as targets of C<> and implicit links (empty
616 by default). note, these are not filenames, but rather
617 page names like those that appear in L<> links.
618 --outfile - filename for the resulting html file (output sent to
620 --podpath - colon-separated list of directories containing library
621 pods (empty by default).
622 --podroot - filesystem base directory from which all relative paths
623 in podpath stem (default is .).
624 --[no]quiet - supress some benign warning messages (default is off).
625 --[no]recurse - recurse on those subdirectories listed in podpath
627 --title - title that will appear in resulting html file.
628 --[no]verbose - self-explanatory (off by default).
629 --[no]netscape - deprecated, has no effect. for backwards compatibility only.
635 sub parse_command_line {
636 my ($opt_backlink,$opt_cachedir,$opt_css,$opt_flush,$opt_header,$opt_help,
637 $opt_htmldir,$opt_htmlroot,$opt_index,$opt_infile,$opt_libpods,
638 $opt_netscape,$opt_outfile,$opt_podpath,$opt_podroot,$opt_quiet,
639 $opt_recurse,$opt_title,$opt_verbose);
641 unshift @ARGV, split ' ', $Config{pod2html} if $Config{pod2html};
642 my $result = GetOptions(
643 'backlink=s' => \$opt_backlink,
644 'cachedir=s' => \$opt_cachedir,
645 'css=s' => \$opt_css,
646 'flush' => \$opt_flush,
647 'header!' => \$opt_header,
648 'help' => \$opt_help,
649 'htmldir=s' => \$opt_htmldir,
650 'htmlroot=s' => \$opt_htmlroot,
651 'index!' => \$opt_index,
652 'infile=s' => \$opt_infile,
653 'libpods=s' => \$opt_libpods,
654 'netscape!' => \$opt_netscape,
655 'outfile=s' => \$opt_outfile,
656 'podpath=s' => \$opt_podpath,
657 'podroot=s' => \$opt_podroot,
658 'quiet!' => \$opt_quiet,
659 'recurse!' => \$opt_recurse,
660 'title=s' => \$opt_title,
661 'verbose!' => \$opt_verbose,
663 usage("-", "invalid parameters") if not $result;
665 usage("-") if defined $opt_help; # see if the user asked for help
666 $opt_help = ""; # just to make -w shut-up.
668 @Podpath = split(":", $opt_podpath) if defined $opt_podpath;
669 @Libpods = split(":", $opt_libpods) if defined $opt_libpods;
671 $Backlink = $opt_backlink if defined $opt_backlink;
672 $Cachedir = $opt_cachedir if defined $opt_cachedir;
673 $Css = $opt_css if defined $opt_css;
674 $Header = $opt_header if defined $opt_header;
675 $Htmldir = $opt_htmldir if defined $opt_htmldir;
676 $Htmlroot = $opt_htmlroot if defined $opt_htmlroot;
677 $Doindex = $opt_index if defined $opt_index;
678 $Podfile = $opt_infile if defined $opt_infile;
679 $Htmlfile = $opt_outfile if defined $opt_outfile;
680 $Podroot = $opt_podroot if defined $opt_podroot;
681 $Quiet = $opt_quiet if defined $opt_quiet;
682 $Recurse = $opt_recurse if defined $opt_recurse;
683 $Title = $opt_title if defined $opt_title;
684 $Verbose = $opt_verbose if defined $opt_verbose;
686 warn "Flushing item and directory caches\n"
687 if $opt_verbose && defined $opt_flush;
688 $Dircache = "$Cachedir/pod2htmd.tmp";
689 $Itemcache = "$Cachedir/pod2htmi.tmp";
690 if (defined $opt_flush) {
691 1 while unlink($Dircache, $Itemcache);
699 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
700 my @cache_key_args = @_;
702 # A first-level cache:
703 # Don't bother reading the cache files if they still apply
704 # and haven't changed since we last read them.
706 my $this_cache_key = cache_key(@cache_key_args);
708 return if $Saved_Cache_Key and $this_cache_key eq $Saved_Cache_Key;
710 # load the cache of %Pages and %Items if possible. $tests will be
711 # non-zero if successful.
713 if (-f $dircache && -f $itemcache) {
714 warn "scanning for item cache\n" if $Verbose;
715 $tests = load_cache($dircache, $itemcache, $podpath, $podroot);
718 # if we didn't succeed in loading the cache then we must (re)build
721 warn "scanning directories in pod-path\n" if $Verbose;
722 scan_podpath($podroot, $recurse, 0);
724 $Saved_Cache_Key = cache_key(@cache_key_args);
728 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
729 return join('!', $dircache, $itemcache, $recurse,
730 @$podpath, $podroot, stat($dircache), stat($itemcache));
734 # load_cache - tries to find if the caches stored in $dircache and $itemcache
735 # are valid caches of %Pages and %Items. if they are valid then it loads
736 # them and returns a non-zero value.
739 my($dircache, $itemcache, $podpath, $podroot) = @_;
745 open(CACHE, "<$itemcache") ||
746 die "$0: error opening $itemcache for reading: $!\n";
749 # is it the same podpath?
752 $tests++ if (join(":", @$podpath) eq $_);
754 # is it the same podroot?
757 $tests++ if ($podroot eq $_);
759 # load the cache if its good
765 warn "loading item cache\n" if $Verbose;
772 warn "scanning for directory cache\n" if $Verbose;
773 open(CACHE, "<$dircache") ||
774 die "$0: error opening $dircache for reading: $!\n";
778 # is it the same podpath?
781 $tests++ if (join(":", @$podpath) eq $_);
783 # is it the same podroot?
786 $tests++ if ($podroot eq $_);
788 # load the cache if its good
794 warn "loading directory cache\n" if $Verbose;
806 # scan_podpath - scans the directories specified in @podpath for directories,
807 # .pod files, and .pm files. it also scans the pod files specified in
808 # @Libpods for =item directives.
811 my($podroot, $recurse, $append) = @_;
813 my($libpod, $dirname, $pod, @files, @poddata);
820 # scan each directory listed in @Podpath
823 || die "$0: error changing to directory $podroot: $!\n";
824 foreach $dir (@Podpath) {
825 scan_dir($dir, $recurse);
828 # scan the pods listed in @Libpods for =item directives
829 foreach $libpod (@Libpods) {
830 # if the page isn't defined then we won't know where to find it
832 next unless defined $Pages{$libpod} && $Pages{$libpod};
834 # if there is a directory then use the .pod and .pm files within it.
835 # NOTE: Only finds the first so-named directory in the tree.
836 # if ($Pages{$libpod} =~ /([^:]*[^(\.pod|\.pm)]):/) {
837 if ($Pages{$libpod} =~ /([^:]*(?<!\.pod)(?<!\.pm)):/) {
838 # find all the .pod and .pm files within the directory
840 opendir(DIR, $dirname) ||
841 die "$0: error opening directory $dirname: $!\n";
842 @files = grep(/(\.pod|\.pm)\z/ && ! -d $_, readdir(DIR));
845 # scan each .pod and .pm file for =item directives
846 foreach $pod (@files) {
847 open(POD, "<$dirname/$pod") ||
848 die "$0: error opening $dirname/$pod for input: $!\n";
851 clean_data( \@poddata );
853 scan_items( \%Items, "$dirname/$pod", @poddata);
856 # use the names of files as =item directives too.
857 ### Don't think this should be done this way - confuses issues.(WL)
858 ### foreach $pod (@files) {
859 ### $pod =~ /^(.*)(\.pod|\.pm)$/;
860 ### $Items{$1} = "$dirname/$1.html" if $1;
862 } elsif ($Pages{$libpod} =~ /([^:]*\.pod):/ ||
863 $Pages{$libpod} =~ /([^:]*\.pm):/) {
864 # scan the .pod or .pm file for =item directives
866 open(POD, "<$pod") ||
867 die "$0: error opening $pod for input: $!\n";
870 clean_data( \@poddata );
872 scan_items( \%Items, "$pod", @poddata);
874 warn "$0: shouldn't be here (line ".__LINE__."\n";
877 @poddata = (); # clean-up a bit
880 || die "$0: error changing to directory $pwd: $!\n";
882 # cache the item list for later use
883 warn "caching items for later use\n" if $Verbose;
884 open(CACHE, ">$Itemcache") ||
885 die "$0: error open $Itemcache for writing: $!\n";
887 print CACHE join(":", @Podpath) . "\n$podroot\n";
888 foreach my $key (keys %Items) {
889 print CACHE "$key $Items{$key}\n";
894 # cache the directory list for later use
895 warn "caching directories for later use\n" if $Verbose;
896 open(CACHE, ">$Dircache") ||
897 die "$0: error open $Dircache for writing: $!\n";
899 print CACHE join(":", @Podpath) . "\n$podroot\n";
900 foreach my $key (keys %Pages) {
901 print CACHE "$key $Pages{$key}\n";
908 # scan_dir - scans the directory specified in $dir for subdirectories, .pod
909 # files, and .pm files. notes those that it finds. this information will
910 # be used later in order to figure out where the pages specified in L<>
911 # links are on the filesystem.
914 my($dir, $recurse) = @_;
915 my($t, @subdirs, @pods, $pod, $dirname, @dirs);
921 opendir(DIR, $dir) ||
922 die "$0: error opening directory $dir: $!\n";
923 while (defined($_ = readdir(DIR))) {
924 if (-d "$dir/$_" && $_ ne "." && $_ ne "..") { # directory
925 $Pages{$_} = "" unless defined $Pages{$_};
926 $Pages{$_} .= "$dir/$_:";
928 } elsif (/\.pod\z/) { # .pod
930 $Pages{$_} = "" unless defined $Pages{$_};
931 $Pages{$_} .= "$dir/$_.pod:";
932 push(@pods, "$dir/$_.pod");
933 } elsif (/\.html\z/) { # .html
935 $Pages{$_} = "" unless defined $Pages{$_};
936 $Pages{$_} .= "$dir/$_.pod:";
937 } elsif (/\.pm\z/) { # .pm
939 $Pages{$_} = "" unless defined $Pages{$_};
940 $Pages{$_} .= "$dir/$_.pm:";
941 push(@pods, "$dir/$_.pm");
946 # recurse on the subdirectories if necessary
948 foreach my $subdir (@subdirs) {
949 scan_dir("$dir/$subdir", $recurse);
955 # scan_headings - scan a pod file for head[1-6] tags, note the tags, and
959 my($sections, @data) = @_;
960 my($tag, $which_head, $otitle, $listdepth, $index);
967 # scan for =head directives, note their name, and build an index
968 # pointing to each of them.
969 foreach my $line (@data) {
970 if ($line =~ /^=(head)([1-6])\s+(.*)/) {
971 ($tag, $which_head, $otitle) = ($1,$2,$3);
973 my $title = depod( $otitle );
974 my $name = anchorify( $title );
975 $$sections{$name} = 1;
976 $title = process_text( \$otitle );
978 while ($which_head != $listdepth) {
979 if ($which_head > $listdepth) {
980 $index .= "\n" . ("\t" x $listdepth) . "<ul>\n";
982 } elsif ($which_head < $listdepth) {
984 $index .= "\n" . ("\t" x $listdepth) . "</ul>\n";
988 $index .= "\n" . ("\t" x $listdepth) . "<li>" .
989 "<a href=\"#" . $name . "\">" .
990 $title . "</a></li>";
994 # finish off the lists
995 while ($listdepth--) {
996 $index .= "\n" . ("\t" x $listdepth) . "</ul>\n";
999 # get rid of bogus lists
1000 $index =~ s,\t*<ul>\s*</ul>\n,,g;
1006 # scan_items - scans the pod specified by $pod for =item directives. we
1007 # will use this information later on in resolving C<> links.
1010 my( $itemref, $pod, @poddata ) = @_;
1014 $pod =~ s/\.pod\z//;
1015 $pod .= ".html" if $pod;
1017 foreach $i (0..$#poddata) {
1018 my $txt = depod( $poddata[$i] );
1020 # figure out what kind of item it is.
1021 # Build string for referencing this item.
1022 if ( $txt =~ /\A=item\s+\*\s*(.*)\Z/s ) { # bullet
1025 } elsif( $txt =~ /\A=item\s+(?>\d+\.?)\s*(.*)\Z/s ) { # numbered list
1027 } elsif( $txt =~ /\A=item\s+(.*)\Z/s ) { # plain item
1032 my $fid = fragment_id( $item );
1033 $$itemref{$fid} = "$pod" if $fid;
1038 # process_head - convert a pod head[1-6] tag and convert it to HTML format.
1041 my($tag, $heading, $hasindex) = @_;
1043 # figure out the level of the =head
1044 $tag =~ /head([1-6])/;
1048 warn "$0: $Podfile: unterminated list at =head in paragraph $Paragraph. ignoring.\n";
1049 while( $Listlevel ){
1055 if( $level == 1 && ! $Top ){
1056 print HTML "<a href=\"#__index__\"><small>$Backlink</small></a>\n"
1057 if $hasindex and $Backlink;
1058 print HTML "</p>\n<hr />\n"
1060 print HTML "</p>\n";
1063 my $name = anchorify( depod( $heading ) );
1064 my $convert = process_text( \$heading );
1065 print HTML "<h$level><a name=\"$name\">$convert</a></h$level>\n";
1070 # emit_item_tag - print an =item's text
1071 # Note: The global $EmittedItem is used for inhibiting self-references.
1075 sub emit_item_tag($$$){
1076 my( $otext, $text, $compact ) = @_;
1077 my $item = fragment_id( $text );
1079 $EmittedItem = $item;
1080 ### print STDERR "emit_item_tag=$item ($text)\n";
1082 print HTML '<strong>';
1083 if ($Items_Named{$item}++) {
1084 print HTML process_text( \$otext );
1086 my $name = 'item_' . $item;
1087 $name = anchorify($name);
1088 print HTML qq{<a name="$name">}, process_text( \$otext ), '</a>';
1090 print HTML "</strong><br />\n";
1091 undef( $EmittedItem );
1096 if( $Items_Seen[$Listlevel]++ == 0 ){
1097 push( @Listend, "</$tag>" );
1098 print HTML "<$tag>\n";
1100 my $emitted = $tag eq 'dl' ? 'dt' : 'li';
1101 print HTML "<$emitted>";
1106 # process_item - convert a pod item tag and convert it to HTML format.
1110 my $need_dd = 0; # set to 1 if we need a <dd></dd> after an item
1112 # lots of documents start a list without doing an =over. this is
1113 # bad! but, the proper thing to do seems to be to just assume
1114 # they did do an =over. so warn them once and then continue.
1115 if( $Listlevel == 0 ){
1116 warn "$0: $Podfile: unexpected =item directive in paragraph $Paragraph. ignoring.\n";
1120 # formatting: insert a paragraph if preceding item has >1 paragraph
1122 print HTML "<p></p>\n";
1126 # remove formatting instructions from the text
1127 my $text = depod( $otext );
1129 my $emitted; # the tag actually emitted, used for closing
1131 # all the list variants:
1132 if( $text =~ /\A\*/ ){ # bullet
1133 $emitted = emit_li( 'ul' );
1134 if ($text =~ /\A\*\s+(.+)\Z/s ) { # with additional text
1136 $otext =~ s/\A\*\s+//;
1137 emit_item_tag( $otext, $tag, 1 );
1140 } elsif( $text =~ /\A\d+/ ){ # numbered list
1141 $emitted = emit_li( 'ol' );
1142 if ($text =~ /\A(?>\d+\.?)\s*(.+)\Z/s ) { # with additional text
1144 $otext =~ s/\A\d+\.?\s*//;
1145 emit_item_tag( $otext, $tag, 1 );
1148 } else { # definition list
1149 $emitted = emit_li( 'dl' );
1150 if ($text =~ /\A(.+)\Z/s ){ # should have text
1151 emit_item_tag( $otext, $text, 1 );
1155 print HTML "</$emitted>" if $emitted;
1161 # process_over - process a pod over tag and start a corresponding HTML list.
1166 push( @Items_Seen, 0 );
1171 # process_back - process a pod back tag and convert it to HTML format.
1174 if( $Listlevel == 0 ){
1175 warn "$0: $Podfile: unexpected =back directive in paragraph $Paragraph. ignoring.\n";
1179 # close off the list. note, I check to see if $Listend[$Listlevel] is
1180 # defined because an =item directive may have never appeared and thus
1181 # $Listend[$Listlevel] may have never been initialized.
1183 if( defined $Listend[$Listlevel] ){
1184 print HTML '<p></p>' if $After_Lpar;
1185 print HTML $Listend[$Listlevel];
1191 # clean up item count
1196 # process_cut - process a pod cut tag, thus start ignoring pod directives.
1203 # process_pod - process a pod tag, thus stop ignoring pod directives
1204 # until we see a corresponding cut.
1207 # no need to set $Ignore to 0 cause the main loop did it
1211 # process_for - process a =for pod tag. if it's for html, spit
1212 # it out verbatim, if illustration, center it, otherwise ignore it.
1215 my($whom, $text) = @_;
1216 if ( $whom =~ /^(pod2)?html$/i) {
1218 } elsif ($whom =~ /^illustration$/i) {
1219 1 while chomp $text;
1220 for my $ext (qw[.png .gif .jpeg .jpg .tga .pcl .bmp]) {
1221 $text .= $ext, last if -r "$text$ext";
1223 print HTML qq{<p align="center"><img src="$text" alt="$text illustration" /></p>};
1228 # process_begin - process a =begin pod tag. this pushes
1229 # whom we're beginning on the begin stack. if there's a
1230 # begin stack, we only print if it us.
1233 my($whom, $text) = @_;
1235 push (@Begin_Stack, $whom);
1236 if ( $whom =~ /^(pod2)?html$/) {
1237 print HTML $text if $text;
1242 # process_end - process a =end pod tag. pop the
1243 # begin stack. die if we're mismatched.
1246 my($whom, $text) = @_;
1248 if ($Begin_Stack[-1] ne $whom ) {
1249 die "Unmatched begin/end at chunk $Paragraph\n"
1251 pop( @Begin_Stack );
1255 # process_pre - indented paragraph, made into <pre></pre>
1264 # insert spaces in place of tabs
1267 1 while $line =~ s/(\t+)/' ' x ((length($1) * 8) - $-[0] % 8)/e;
1271 # convert some special chars to HTML escapes
1272 $rest = html_escape($rest);
1274 # try and create links for all occurrences of perl.* within
1275 # the preformatted text.
1279 if ( defined $Pages{$2} ){ # is a link
1280 qq($1<a href="$Htmlroot/$Pages{$2}">$2</a>);
1281 } elsif (defined $Pages{dosify($2)}) { # is a link
1282 qq($1<a href="$Htmlroot/$Pages{dosify($2)}">$2</a>);
1288 (<a\ href="?) ([^>:]*:)? ([^>:]*) \.pod: ([^>:]*:)?
1291 if ( $Htmlfileurl ne '' ){
1292 # Here, we take advantage of the knowledge
1293 # that $Htmlfileurl ne '' implies $Htmlroot eq ''.
1294 # Since $Htmlroot eq '', we need to prepend $Htmldir
1295 # on the fron of the link to get the absolute path
1296 # of the link's target. We check for a leading '/'
1297 # to avoid corrupting links that are #, file:, etc.
1299 $old_url = "$Htmldir$old_url" if $old_url =~ m{^\/};
1300 $url = relativize_url( "$old_url.html", $Htmlfileurl );
1307 # Look for embedded URLs and make them into links. We don't
1308 # relativize them since they are best left as the author intended.
1310 my $urls = '(' . join ('|', qw{
1323 my $gunk = '/#~:.?+=&%@!\-';
1324 my $punc = '.:!?\-;';
1325 my $any = "${ltrs}${gunk}${punc}";
1328 \b # start at word boundary
1330 $urls : # need resource and a colon
1331 (?!:) # Ignore File::, among others.
1332 [$any] +? # followed by one or more of any valid
1333 # character, but be conservative and
1334 # take only what you need to....
1337 " > # maybe pre-quoted '<a href="...">'
1339 [$punc]* # 0 or more punctuation
1341 [^$any] # by a non-url char
1343 $ # end of the string
1346 $ # then end of the string
1348 }{<a href="$1">$1</a>}igox;
1350 # text should be as it is (verbatim)
1356 # pure text processing
1358 # pure_text/inIS_text: differ with respect to automatic C<> recognition.
1359 # we don't want this to happen within IS
1363 process_puretext( $text, \$PTQuote, 1 );
1368 process_puretext( $text, \$PTQuote, 0 );
1372 # process_puretext - process pure text (without pod-escapes) converting
1373 # double-quotes and handling implicit C<> links.
1375 sub process_puretext {
1376 my($text, $quote, $notinIS) = @_;
1378 ## Guessing at func() or [$@%&]*var references in plain text is destined
1379 ## to produce some strange looking ref's. uncomment to disable:
1382 my(@words, $lead, $trail);
1384 # convert double-quotes to single-quotes
1385 if( $$quote && $text =~ s/"/''/s ){
1388 while ($text =~ s/"([^"]*)"/``$1''/sg) {};
1389 $$quote = 1 if $text =~ s/"/``/s;
1391 # keep track of leading and trailing white-space
1392 $lead = ($text =~ s/\A(\s+)//s ? $1 : "");
1393 $trail = ($text =~ s/(\s+)\Z//s ? $1 : "");
1395 # split at space/non-space boundaries
1396 @words = split( /(?<=\s)(?=\S)|(?<=\S)(?=\s)/, $text );
1398 # process each word individually
1399 foreach my $word (@words) {
1401 next if $word =~ /^\s*$/;
1402 # see if we can infer a link
1403 if( $notinIS && $word =~ /^(\w+)\((.*)\)$/ ) {
1404 # has parenthesis so should have been a C<> ref
1405 ## try for a pagename (perlXXX(1))?
1406 my( $func, $args ) = ( $1, $2 );
1407 if( $args =~ /^\d+$/ ){
1408 my $url = page_sect( $word, '' );
1410 $word = "<a href=\"$url\">the $word manpage</a>";
1414 ## try function name for a link, append tt'ed argument list
1415 $word = emit_C( $func, '', "($args)");
1417 #### disabled. either all (including $\W, $\w+{.*} etc.) or nothing.
1418 ## } elsif( $notinIS && $word =~ /^[\$\@%&*]+\w+$/) {
1419 ## # perl variables, should be a C<> ref
1420 ## $word = emit_C( $word );
1422 } elsif ($word =~ m,^\w+://\w,) {
1424 # Don't relativize it: leave it as the author intended
1425 $word = qq(<a href="$word">$word</a>);
1426 } elsif ($word =~ /[\w.-]+\@[\w-]+\.\w/) {
1427 # looks like an e-mail address
1428 my ($w1, $w2, $w3) = ("", $word, "");
1429 ($w1, $w2, $w3) = ("(", $1, ")$2") if $word =~ /^\((.*?)\)(,?)/;
1430 ($w1, $w2, $w3) = ("<", $1, ">$2") if $word =~ /^<(.*?)>(,?)/;
1431 $word = qq($w1<a href="mailto:$w2">$w2</a>$w3);
1433 $word = html_escape($word) if $word =~ /["&<>]/;
1437 # put everything back together
1438 return $lead . join( '', @words ) . $trail;
1443 # process_text - handles plaintext that appears in the input pod file.
1444 # there may be pod commands embedded within the text so those must be
1445 # converted to html commands.
1448 sub process_text1($$;$$);
1449 sub pattern ($) { $_[0] ? '[^\S\n]+'.('>' x ($_[0] + 1)) : '>' }
1450 sub closing ($) { local($_) = shift; (defined && s/\s+$//) ? length : 0 }
1455 my $res = process_text1( 0, $tref );
1459 sub process_text1($$;$$){
1460 my( $lev, $rstr, $func, $closing ) = @_;
1463 unless (defined $func) {
1469 # B<text> - boldface
1470 $res = '<strong>' . process_text1( $lev, $rstr ) . '</strong>';
1472 } elsif( $func eq 'C' ){
1473 # C<code> - can be a ref or <code></code>
1474 # need to extract text
1475 my $par = go_ahead( $rstr, 'C', $closing );
1477 ## clean-up of the link target
1478 my $text = depod( $par );
1480 ### my $x = $par =~ /[BI]</ ? 'yes' : 'no' ;
1481 ### print STDERR "-->call emit_C($par) lev=$lev, par with BI=$x\n";
1483 $res = emit_C( $text, $lev > 1 || ($par =~ /[BI]</) );
1485 } elsif( $func eq 'E' ){
1486 # E<x> - convert to character
1487 $$rstr =~ s/^([^>]*)>//;
1489 $escape =~ s/^(\d+|X[\dA-F]+)$/#$1/i;
1492 } elsif( $func eq 'F' ){
1493 # F<filename> - italizice
1494 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1496 } elsif( $func eq 'I' ){
1497 # I<text> - italizice
1498 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1500 } elsif( $func eq 'L' ){
1502 ## L<text|cross-ref> => produce text, use cross-ref for linking
1503 ## L<cross-ref> => make text from cross-ref
1504 ## need to extract text
1505 my $par = go_ahead( $rstr, 'L', $closing );
1507 # some L<>'s that shouldn't be:
1508 # a) full-blown URL's are emitted as-is
1509 if( $par =~ m{^\w+://}s ){
1510 return make_URL_href( $par );
1512 # b) C<...> is stripped and treated as C<>
1513 if( $par =~ /^C<(.*)>$/ ){
1514 my $text = depod( $1 );
1515 return emit_C( $text, $lev > 1 || ($par =~ /[BI]</) );
1518 # analyze the contents
1519 $par =~ s/\n/ /g; # undo word-wrapped tags
1522 if( $par =~ s{^([^|]+)\|}{} ){
1526 # make sure sections start with a /
1529 my( $page, $section, $ident );
1531 # check for link patterns
1532 if( $par =~ m{^([^/]+?)/(?!")(.*?)$} ){ # name/ident
1533 # we've got a name/ident (no quotes)
1534 ( $page, $ident ) = ( $1, $2 );
1535 ### print STDERR "--> L<$par> to page $page, ident $ident\n";
1537 } elsif( $par =~ m{^(.*?)/"?(.*?)"?$} ){ # [name]/"section"
1538 # even though this should be a "section", we go for ident first
1539 ( $page, $ident ) = ( $1, $2 );
1540 ### print STDERR "--> L<$par> to page $page, section $section\n";
1542 } elsif( $par =~ /\s/ ){ # this must be a section with missing quotes
1543 ( $page, $section ) = ( '', $par );
1544 ### print STDERR "--> L<$par> to void page, section $section\n";
1547 ( $page, $section ) = ( $par, '' );
1548 ### print STDERR "--> L<$par> to page $par, void section\n";
1551 # now, either $section or $ident is defined. the convoluted logic
1552 # below tries to resolve L<> according to what the user specified.
1553 # failing this, we try to find the next best thing...
1554 my( $url, $ltext, $fid );
1557 if( defined $ident ){
1558 ## try to resolve $ident as an item
1559 ( $url, $fid ) = coderef( $page, $ident );
1561 if( ! defined( $linktext ) ){
1563 $linktext .= " in " if $ident && $page;
1564 $linktext .= "the $page manpage" if $page;
1566 ### print STDERR "got coderef url=$url\n";
1569 ## no luck: go for a section (auto-quoting!)
1572 ## now go for a section
1573 my $htmlsection = htmlify( $section );
1574 $url = page_sect( $page, $htmlsection );
1576 if( ! defined( $linktext ) ){
1577 $linktext = $section;
1578 $linktext .= " in " if $section && $page;
1579 $linktext .= "the $page manpage" if $page;
1581 ### print STDERR "got page/section url=$url\n";
1584 ## no luck: go for an ident
1591 ( $url, $fid ) = coderef( $page, $ident );
1593 if( ! defined( $linktext ) ){
1595 $linktext .= " in " if $ident && $page;
1596 $linktext .= "the $page manpage" if $page;
1598 ### print STDERR "got section=>coderef url=$url\n";
1602 # warning; show some text.
1603 $linktext = $opar unless defined $linktext;
1604 warn "$0: $Podfile: cannot resolve L<$opar> in paragraph $Paragraph.\n";
1607 # now we have a URL or just plain code
1608 $$rstr = $linktext . '>' . $$rstr;
1609 if( defined( $url ) ){
1610 $res = "<a href=\"$url\">" . process_text1( $lev, $rstr ) . '</a>';
1612 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1615 } elsif( $func eq 'S' ){
1616 # S<text> - non-breaking spaces
1617 $res = process_text1( $lev, $rstr );
1618 $res =~ s/ / /g;
1620 } elsif( $func eq 'X' ){
1622 $$rstr =~ s/^[^>]*>//;
1624 } elsif( $func eq 'Z' ){
1626 warn "$0: $Podfile: invalid X<> in paragraph $Paragraph.\n"
1627 unless $$rstr =~ s/^>//;
1630 my $term = pattern $closing;
1631 while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)//s ){
1632 # all others: either recurse into new function or
1633 # terminate at closing angle bracket(s)
1635 $pt .= $2 if !$3 && $lev == 1;
1636 $res .= $lev == 1 ? pure_text( $pt ) : inIS_text( $pt );
1637 return $res if !$3 && $lev > 1;
1639 $res .= process_text1( $lev, $rstr, $3, closing $4 );
1643 $res .= pure_text( $$rstr );
1645 warn "$0: $Podfile: undelimited $func<> in paragraph $Paragraph.\n";
1652 # go_ahead: extract text of an IS (can be nested)
1655 my( $rstr, $func, $closing ) = @_;
1657 my @closing = ($closing);
1659 s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|@{[pattern $closing[0]]})//s ){
1663 return $res unless @closing;
1665 unshift @closing, closing $4;
1669 warn "$0: $Podfile: undelimited $func<> in paragraph $Paragraph.\n";
1674 # emit_C - output result of C<text>
1675 # $text is the depod-ed text
1678 my( $text, $nocode, $args ) = @_;
1679 $args = '' unless defined $args;
1681 my( $url, $fid ) = coderef( undef(), $text );
1683 # need HTML-safe text
1684 my $linktext = html_escape( "$text$args" );
1686 if( defined( $url ) &&
1687 (!defined( $EmittedItem ) || $EmittedItem ne $fid ) ){
1688 $res = "<a href=\"$url\"><code>$linktext</code></a>";
1689 } elsif( 0 && $nocode ){
1692 $res = "<code>$linktext</code>";
1698 # html_escape: make text safe for HTML
1702 $rest =~ s/&/&/g;
1703 $rest =~ s/</</g;
1704 $rest =~ s/>/>/g;
1705 $rest =~ s/"/"/g;
1706 # ' is only in XHTML, not HTML4. Be conservative
1707 #$rest =~ s/'/'/g;
1713 # dosify - convert filenames to 8.3
1717 return lc($str) if $^O eq 'VMS'; # VMS just needs casing
1720 $str =~ s/(\.\w+)/substr ($1,0,4)/ge;
1721 $str =~ s/(\w+)/substr ($1,0,8)/ge;
1727 # page_sect - make a URL from the text of a L<>
1730 my( $page, $section ) = @_;
1731 my( $linktext, $page83, $link); # work strings
1733 # check if we know that this is a section in this page
1734 if (!defined $Pages{$page} && defined $Sections{$page}) {
1737 ### print STDERR "reset page='', section=$section\n";
1740 $page83=dosify($page);
1741 $page=$page83 if (defined $Pages{$page83});
1743 $link = "#" . anchorify( $section );
1744 } elsif ( $page =~ /::/ ) {
1746 # Search page cache for an entry keyed under the html page name,
1747 # then look to see what directory that page might be in. NOTE:
1748 # this will only find one page. A better solution might be to produce
1749 # an intermediate page that is an index to all such pages.
1750 my $page_name = $page ;
1751 $page_name =~ s,^.*/,,s ;
1752 if ( defined( $Pages{ $page_name } ) &&
1753 $Pages{ $page_name } =~ /([^:]*$page)\.(?:pod|pm):/
1758 # NOTE: This branch assumes that all A::B pages are located in
1759 # $Htmlroot/A/B.html . This is often incorrect, since they are
1760 # often in $Htmlroot/lib/A/B.html or such like. Perhaps we could
1761 # analyze the contents of %Pages and figure out where any
1762 # cousins of A::B are, then assume that. So, if A::B isn't found,
1763 # but A::C is found in lib/A/C.pm, then A::B is assumed to be in
1764 # lib/A/B.pm. This is also limited, but it's an improvement.
1765 # Maybe a hints file so that the links point to the correct places
1769 $link = "$Htmlroot/$page.html";
1770 $link .= "#" . anchorify( $section ) if ($section);
1771 } elsif (!defined $Pages{$page}) {
1774 $section = anchorify( $section ) if $section ne "";
1775 ### print STDERR "...section=$section\n";
1777 # if there is a directory by the name of the page, then assume that an
1778 # appropriate section will exist in the subdirectory
1779 # if ($section ne "" && $Pages{$page} =~ /([^:]*[^(\.pod|\.pm)]):/) {
1780 if ($section ne "" && $Pages{$page} =~ /([^:]*(?<!\.pod)(?<!\.pm)):/) {
1781 $link = "$Htmlroot/$1/$section.html";
1782 ### print STDERR "...link=$link\n";
1784 # since there is no directory by the name of the page, the section will
1785 # have to exist within a .html of the same name. thus, make sure there
1786 # is a .pod or .pm that might become that .html
1788 $section = "#$section" if $section;
1789 ### print STDERR "...section=$section\n";
1791 # check if there is a .pod with the page name
1792 if ($Pages{$page} =~ /([^:]*)\.pod:/) {
1793 $link = "$Htmlroot/$1.html$section";
1794 } elsif ($Pages{$page} =~ /([^:]*)\.pm:/) {
1795 $link = "$Htmlroot/$1.html$section";
1803 # Here, we take advantage of the knowledge that $Htmlfileurl ne ''
1804 # implies $Htmlroot eq ''. This means that the link in question
1805 # needs a prefix of $Htmldir if it begins with '/'. The test for
1806 # the initial '/' is done to avoid '#'-only links, and to allow
1807 # for other kinds of links, like file:, ftp:, etc.
1809 if ( $Htmlfileurl ne '' ) {
1810 $link = "$Htmldir$link" if $link =~ m{^/}s;
1811 $url = relativize_url( $link, $Htmlfileurl );
1812 # print( " b: [$link,$Htmlfileurl,$url]\n" );
1825 # relativize_url - convert an absolute URL to one relative to a base URL.
1826 # Assumes both end in a filename.
1828 sub relativize_url {
1829 my ($dest,$source) = @_ ;
1831 my ($dest_volume,$dest_directory,$dest_file) =
1832 File::Spec::Unix->splitpath( $dest ) ;
1833 $dest = File::Spec::Unix->catpath( $dest_volume, $dest_directory, '' ) ;
1835 my ($source_volume,$source_directory,$source_file) =
1836 File::Spec::Unix->splitpath( $source ) ;
1837 $source = File::Spec::Unix->catpath( $source_volume, $source_directory, '' ) ;
1840 if ( $dest ne '' ) {
1841 $rel_path = File::Spec::Unix->abs2rel( $dest, $source ) ;
1844 if ( $rel_path ne '' &&
1845 substr( $rel_path, -1 ) ne '/' &&
1846 substr( $dest_file, 0, 1 ) ne '#'
1848 $rel_path .= "/$dest_file" ;
1851 $rel_path .= "$dest_file" ;
1859 # coderef - make URL from the text of a C<>
1862 my( $page, $item ) = @_;
1865 my $fid = fragment_id( $item );
1866 if( defined( $page ) ){
1867 # we have been given a $page...
1870 # Do we take it? Item could be a section!
1871 my $base = $Items{$fid} || "";
1872 $base =~ s{[^/]*/}{};
1873 if( $base ne "$page.html" ){
1874 ### print STDERR "coderef( $page, $item ): items{$fid} = $Items{$fid} = $base => discard page!\n";
1879 # no page - local items precede cached items
1880 if( defined( $fid ) ){
1881 if( exists $Local_Items{$fid} ){
1882 $page = $Local_Items{$fid};
1884 $page = $Items{$fid};
1889 # if there was a pod file that we found earlier with an appropriate
1890 # =item directive, then create a link to that page.
1891 if( defined $page ){
1893 if( exists $Pages{$page} and $Pages{$page} =~ /([^:.]*)\.[^:]*:/){
1894 $page = $1 . '.html';
1896 my $link = "$Htmlroot/$page#item_" . anchorify($fid);
1898 # Here, we take advantage of the knowledge that $Htmlfileurl
1899 # ne '' implies $Htmlroot eq ''.
1900 if ( $Htmlfileurl ne '' ) {
1901 $link = "$Htmldir$link" ;
1902 $url = relativize_url( $link, $Htmlfileurl ) ;
1907 $url = "#item_" . anchorify($fid);
1910 confess "url has space: $url" if $url =~ /"[^"]*\s[^"]*"/;
1912 return( $url, $fid );
1918 # Adapted from Nick Ing-Simmons' PodToHtml package.
1920 my $source_file = shift ;
1921 my $destination_file = shift;
1923 my $source = URI::file->new_abs($source_file);
1924 my $uo = URI::file->new($destination_file,$source)->abs;
1925 return $uo->rel->as_string;
1930 # finish_list - finish off any pending HTML lists. this should be called
1931 # after the entire pod file has been read and converted.
1934 while ($Listlevel > 0) {
1935 print HTML "</dl>\n";
1941 # htmlify - converts a pod section specification to a suitable section
1942 # specification for HTML. Note that we keep spaces and special characters
1943 # except ", ? (Netscape problem) and the hyphen (writer's problem...).
1947 $heading =~ s/(\s+)/ /g;
1948 $heading =~ s/\s+\Z//;
1949 $heading =~ s/\A\s+//;
1950 # The hyphen is a disgrace to the English language.
1951 $heading =~ s/[-"?]//g;
1952 $heading = lc( $heading );
1957 # similar to htmlify, but turns non-alphanumerics into underscores
1961 $anchor = htmlify($anchor);
1962 $anchor =~ s/\W/_/g;
1967 # depod - convert text by eliminating all interior sequences
1968 # Note: can be called with copy or modify semantics
1975 $E2c{amp} = '&'; # in Tk's pods
1983 ${$_[0]} = depod1( \$string );
1991 my( $rstr, $func, $closing ) = @_;
1993 return $res unless defined $$rstr;
1994 if( ! defined( $func ) ){
1995 # skip to next begin of an interior sequence
1996 while( $$rstr =~ s/\A(.*?)([BCEFILSXZ])<(<+[^\S\n]+)?// ){
1997 # recurse into its text
1998 $res .= $1 . depod1( $rstr, $2, closing $3);
2001 } elsif( $func eq 'E' ){
2002 # E<x> - convert to character
2003 $$rstr =~ s/^([^>]*)>//;
2004 $res .= $E2c{$1} || "";
2005 } elsif( $func eq 'X' ){
2007 $$rstr =~ s/^[^>]*>//;
2008 } elsif( $func eq 'Z' ){
2012 # all others: either recurse into new function or
2013 # terminate at closing angle bracket
2014 my $term = pattern $closing;
2015 while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)// ){
2018 $res .= depod1( $rstr, $3, closing $4 );
2020 ## If we're here and $2 ne '>': undelimited interior sequence.
2021 ## Ignored, as this is called without proper indication of where we are.
2022 ## Rely on process_text to produce diagnostics.
2028 # fragment_id - construct a fragment identifier from:
2030 # b) contents of C<...>
2035 $text =~ s/\s+\Z//s;
2037 # a method or function?
2038 return $1 if $text =~ /(\w+)\s*\(/;
2039 return $1 if $text =~ /->\s*(\w+)\s*\(?/;
2042 return $1 if $text =~ /^([$@%*]\S+)/;
2044 # some pattern matching operator?
2045 return $1 if $text =~ m|^(\w+/).*/\w*$|;
2047 # fancy stuff... like "do { }"
2048 return $1 if $text =~ m|^(\w+)\s*{.*}$|;
2050 # honour the perlfunc manpage: func [PAR[,[ ]PAR]...]
2051 # and some funnies with ... Module ...
2052 return $1 if $text =~ m{^([a-z\d]+)(\s+[A-Z\d,/& ]+)?$};
2053 return $1 if $text =~ m{^([a-z\d]+)\s+Module(\s+[A-Z\d,/& ]+)?$};
2056 $text =~ s/\s+/_/sg;
2058 defined( $HC[ord($1)] ) ? $HC[ord($1)]
2059 : ( $HC[ord($1)] = sprintf( "%%%02X", ord($1) ) ) }gxe;
2060 $text = substr( $text, 0, 50 );
2067 # make_URL_href - generate HTML href from URL
2068 # Special treatment for CGI queries.
2070 sub make_URL_href($){
2073 s{^(http:[-\w/#~:.+=&%@!]+)(\?.*)$}{<a href="$1$2">$1</a>}i ){
2074 $url = "<a href=\"$url\">$url</a>";