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.
235 my($Dircache, $Itemcache);
238 my($Htmlroot, $Htmldir, $Htmlfile, $Htmlfileurl);
239 my($Podfile, @Podpath, $Podroot);
249 my($Listlevel, @Listtype);
251 use vars qw($Ignore); # need to localize it later.
253 my(%Items_Named, @Items_Seen);
262 my %Pages = (); # associative array used to find the location
263 # of pages referenced by L<> links.
264 my %Items = (); # associative array used to find the location
265 # of =item directives referenced by C<> links
270 my $Curdir = File::Spec->curdir;
275 $Cachedir = "."; # The directory to which item and directory
276 # caches will be written.
278 $Dircache = "pod2htmd.tmp";
279 $Itemcache = "pod2htmi.tmp";
281 @Begin_Stack = (); # begin/end stack
283 @Libpods = (); # files to search for links from C<> directives
284 $Htmlroot = "/"; # http-server base directory from which all
285 # relative paths in $podpath stem.
286 $Htmldir = ""; # The directory to which the html pages
287 # will (eventually) be written.
288 $Htmlfile = ""; # write to stdout by default
289 $Htmlfileurl = ""; # The url that other files would use to
290 # refer to this file. This is only used
291 # to make relative urls that point to
294 $Podfile = ""; # read from stdin by default
295 @Podpath = (); # list of directories containing library pods.
296 $Podroot = $Curdir; # filesystem base directory from which all
297 # relative paths in $podpath stem.
298 $Css = ''; # Cascading style sheet
299 $Recurse = 1; # recurse on subdirectories in $podpath.
300 $Quiet = 0; # not quiet by default
301 $Verbose = 0; # not verbose by default
302 $Doindex = 1; # non-zero if we should generate an index
303 $Backlink = ''; # text for "back to top" links
304 $Listlevel = 0; # current list depth
305 @Listtype = (); # list types for open lists
306 $ListNewTerm = 0; # indicates new term in definition list; used
307 # to correctly open/close <dd> tags
308 $Ignore = 1; # whether or not to format text. we don't
309 # format text until we hit our first pod
312 @Items_Seen = (); # for multiples of the same item in perlfunc
314 $Header = 0; # produce block header/footer
315 $Title = ''; # title to give the pod(s)
316 $Top = 1; # true if we are at the top of the doc. used
317 # to prevent the first <hr /> directive.
318 $Paragraph = ''; # which paragraph we're processing (used
319 # for error messages)
320 %Sections = (); # sections within this page
323 $Is83 = $^O eq 'dos'; # Is it an 8.3 filesystem?
327 # clean_data: global clean-up of pod data
331 for my $i ( 0..$#{$dataref} ) {
332 ${$dataref}[$i] =~ s/\s+\Z//;
334 # have a look for all-space lines
335 if( ${$dataref}[$i] =~ /^\s+$/m and $dataref->[$i] !~ /^\s/ ){
336 my @chunks = split( /^\s+$/m, ${$dataref}[$i] );
337 splice( @$dataref, $i, 1, @chunks );
350 $Is83 = 0 if (defined (&Dos::UseLFN) && Dos::UseLFN());
352 # cache of %Pages and %Items from last time we ran pod2html
354 #undef $opt_help if defined $opt_help;
356 # parse the command-line parameters
357 parse_command_line();
359 # escape the backlink argument (same goes for title but is done later...)
360 $Backlink = html_escape($Backlink) if defined $Backlink;
362 # set some variables to their default values if necessary
364 unless (@ARGV && $ARGV[0]) {
365 if ($Podfile and $Podfile ne '-') {
366 open $pod, '<', $Podfile
367 or die "$0: cannot open $Podfile file for input: $!\n";
372 $Podfile = $ARGV[0]; # XXX: might be more filenames
375 $Htmlfile = "-" unless $Htmlfile; # stdout
376 $Htmlroot = "" if $Htmlroot eq "/"; # so we don't get a //
377 $Htmldir =~ s#/\z## ; # so we don't get a //
379 && defined( $Htmldir )
381 && substr( $Htmlfile, 0, length( $Htmldir ) ) eq $Htmldir
384 # Set the 'base' url for this file, so that we can use it
385 # as the location from which to calculate relative links
386 # to other files. If this is '', then absolute links will
387 # be used throughout.
388 $Htmlfileurl= "$Htmldir/" . substr( $Htmlfile, length( $Htmldir ) + 1);
391 # read the pod a paragraph at a time
392 warn "Scanning for sections in input file(s)\n" if $Verbose;
394 my @poddata = <$pod>;
401 @poddata = map { s/\r\n/\n/g;
403 map { "$_\n\n" } split /\n\n/ :
406 @poddata = map { s/\r/\n/g;
408 map { "$_\n\n" } split /\n\n/ :
415 clean_data( \@poddata );
417 # scan the pod for =head[1-6] directives and build an index
418 my $index = scan_headings(\%Sections, @poddata);
421 warn "No headings in $Podfile\n" if $Verbose;
424 # open the output file
426 if($Htmlfile and $Htmlfile ne '-') {
427 open $html, ">", $Htmlfile
428 or die "$0: cannot open $Htmlfile file for output: $!\n";
433 # put a title in the HTML file if one wasn't specified
436 for (my $i = 0; $i < @poddata; $i++) {
437 if ($poddata[$i] =~ /^=head1\s*NAME\b/m) {
438 for my $para ( @poddata[$i, $i+1] ) {
440 if ($Title) = $para =~ /(\S+\s+-+.*\S)/s;
447 if (!$Title and $Podfile =~ /\.pod\z/) {
448 # probably a split pod so take first =head[12] as title
449 for (my $i = 0; $i < @poddata; $i++) {
450 last if ($Title) = $poddata[$i] =~ /^=head[12]\s*(.*)/;
452 warn "adopted '$Title' as title for $Podfile\n"
453 if $Verbose and $Title;
456 $Title =~ s/\s*\(.*\)//;
458 warn "$0: no title for $Podfile.\n" unless $Quiet;
459 $Podfile =~ /^(.*)(\.[^.\/]+)?\z/s;
460 $Title = ($Podfile eq "-" ? 'No Title' : $1);
461 warn "using $Title" if $Verbose;
463 $Title = html_escape($Title);
466 my $bodystyle = ' style="background-color: white"';
467 my $tdstyle = ' style="background-color: #cccccc"';
470 $csslink = qq(\n<link rel="stylesheet" href="$Css" type="text/css" />);
471 $csslink =~ s,\\,/,g;
472 $csslink =~ s,(/.):,$1|,;
477 my $block = $Header ? <<END_OF_BLOCK : '';
478 <table border="0" width="100%" cellspacing="0" cellpadding="3">
479 <tr><td class="block"$tdstyle valign="middle">
480 <big><strong><span class="block"> $Title</span></strong></big>
485 print $html <<END_OF_HEAD;
486 <?xml version="1.0" ?>
487 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
488 <html xmlns="http://www.w3.org/1999/xhtml">
490 <title>$Title</title>$csslink
491 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
492 <link rev="made" href="mailto:$Config{perladmin}" />
499 # load/reload/validate/cache %Pages and %Items
500 get_cache($Dircache, $Itemcache, \@Podpath, $Podroot, $Recurse);
502 # scan the pod for =item directives
503 scan_items( \%Local_Items, "", @poddata);
505 # put an index at the top of the file. note, if $Doindex is 0 we
506 # still generate an index, but surround it with an html comment.
507 # that way some other program can extract it if desired.
510 my $hr = ($Doindex and $index) ? qq(<hr name="index" />) : "";
514 $index = qq(<!--\n$index\n-->\n);
517 print $html <<"END_OF_INDEX";
521 <p><a name=\"__index__\"></a></p>
529 # now convert this file
530 my $after_item; # set to true after an =item
531 warn "Converting input file $Podfile\n" if $Verbose;
532 foreach my $i (0..$#poddata){
535 if (/^(=.*)/s) { # is it a pod directive?
539 if (/^=begin\s+(\S+)\s*(.*)/si) {# =begin
540 process_begin($html, $1, $2);
541 } elsif (/^=end\s+(\S+)\s*(.*)/si) {# =end
543 } elsif (/^=cut/) { # =cut
545 } elsif (/^=pod/) { # =pod
548 next if @Begin_Stack && $Begin_Stack[-1] ne 'html';
550 if (/^=(head[1-6])\s+(.*\S)/s) { # =head[1-6] heading
551 process_head( $html, $1, $2, $Doindex && $index );
552 } elsif (/^=item\s*(.*\S)?/sm) { # =item text
553 process_item( $html, $1 );
555 } elsif (/^=over\s*(.*)/) { # =over N
557 } elsif (/^=back/) { # =back
558 process_back( $html );
559 } elsif (/^=for\s+(\S+)\s*(.*)/si) {# =for
560 process_for( $html, $1, $2 );
563 warn "$0: $Podfile: unknown pod directive '$1' in "
564 . "paragraph $Paragraph. ignoring.\n" unless $Quiet;
572 print $html $_ if $Begin_Stack[-1] eq 'html';
577 # Open tag for definition list as we have something to put in it
579 print $html "<dd>\n";
583 if( $text =~ /\A\s+/ ){
584 process_pre( \$text );
585 print $html "<pre>\n$text</pre>\n";
588 process_text( \$text );
590 # experimental: check for a paragraph where all lines
591 # have some ...\t...\t...\n pattern
593 my @lines = split( "\n", $text );
596 foreach my $line ( @lines ){
597 if( $line =~ /\S/ && $line !~ /\t/ ){
603 $text =~ s/\t+/<td>/g;
604 $text =~ s/^/<tr><td>/gm;
605 $text = '<table cellspacing="0" cellpadding="0">' .
610 ## end of experimental
612 print $html "<p>$text</p>\n";
618 # finish off any pending directives
619 finish_list( $html );
622 print $html "<p><a href=\"#__index__\"><small>$Backlink</small></a></p>\n"
623 if $Doindex and $index and $Backlink;
625 print $html <<END_OF_TAIL;
632 # close the html file
633 close $html or die "Failed to close $Htmlfile: $!";
635 warn "Finished\n" if $Verbose;
638 ##############################################################################
642 warn "$0: $podfile: @_\n" if @_;
644 Usage: $0 --help --htmlroot=<name> --infile=<name> --outfile=<name>
645 --podpath=<name>:...:<name> --podroot=<name>
646 --libpods=<name>:...:<name> --recurse --verbose --index
647 --netscape --norecurse --noindex --cachedir=<name>
649 --backlink - set text for "back to top" links (default: none).
650 --cachedir - directory for the item and directory cache files.
651 --css - stylesheet URL
652 --flush - flushes the item and directory caches.
653 --[no]header - produce block header/footer (default is no headers).
654 --help - prints this message.
655 --hiddendirs - search hidden directories in podpath
656 --htmldir - directory for resulting HTML files.
657 --htmlroot - http-server base directory from which all relative paths
658 in podpath stem (default is /).
659 --[no]index - generate an index at the top of the resulting html
661 --infile - filename for the pod to convert (input taken from stdin
663 --libpods - colon-separated list of pages to search for =item pod
664 directives in as targets of C<> and implicit links (empty
665 by default). note, these are not filenames, but rather
666 page names like those that appear in L<> links.
667 --outfile - filename for the resulting html file (output sent to
669 --podpath - colon-separated list of directories containing library
670 pods (empty by default).
671 --podroot - filesystem base directory from which all relative paths
672 in podpath stem (default is .).
673 --[no]quiet - suppress some benign warning messages (default is off).
674 --[no]recurse - recurse on those subdirectories listed in podpath
676 --title - title that will appear in resulting html file.
677 --[no]verbose - self-explanatory (off by default).
678 --[no]netscape - deprecated, has no effect. for backwards compatibility only.
684 sub parse_command_line {
685 my ($opt_backlink,$opt_cachedir,$opt_css,$opt_flush,$opt_header,$opt_help,
686 $opt_htmldir,$opt_htmlroot,$opt_index,$opt_infile,$opt_libpods,
687 $opt_netscape,$opt_outfile,$opt_podpath,$opt_podroot,$opt_quiet,
688 $opt_recurse,$opt_title,$opt_verbose,$opt_hiddendirs);
690 unshift @ARGV, split ' ', $Config{pod2html} if $Config{pod2html};
691 my $result = GetOptions(
692 'backlink=s' => \$opt_backlink,
693 'cachedir=s' => \$opt_cachedir,
694 'css=s' => \$opt_css,
695 'flush' => \$opt_flush,
696 'header!' => \$opt_header,
697 'help' => \$opt_help,
698 'hiddendirs!'=> \$opt_hiddendirs,
699 'htmldir=s' => \$opt_htmldir,
700 'htmlroot=s' => \$opt_htmlroot,
701 'index!' => \$opt_index,
702 'infile=s' => \$opt_infile,
703 'libpods=s' => \$opt_libpods,
704 'netscape!' => \$opt_netscape,
705 'outfile=s' => \$opt_outfile,
706 'podpath=s' => \$opt_podpath,
707 'podroot=s' => \$opt_podroot,
708 'quiet!' => \$opt_quiet,
709 'recurse!' => \$opt_recurse,
710 'title=s' => \$opt_title,
711 'verbose!' => \$opt_verbose,
713 usage("-", "invalid parameters") if not $result;
715 usage("-") if defined $opt_help; # see if the user asked for help
716 $opt_help = ""; # just to make -w shut-up.
718 @Podpath = split(":", $opt_podpath) if defined $opt_podpath;
719 @Libpods = split(":", $opt_libpods) if defined $opt_libpods;
721 $Backlink = $opt_backlink if defined $opt_backlink;
722 $Cachedir = $opt_cachedir if defined $opt_cachedir;
723 $Css = $opt_css if defined $opt_css;
724 $Header = $opt_header if defined $opt_header;
725 $Htmldir = $opt_htmldir if defined $opt_htmldir;
726 $Htmlroot = $opt_htmlroot if defined $opt_htmlroot;
727 $Doindex = $opt_index if defined $opt_index;
728 $Podfile = $opt_infile if defined $opt_infile;
729 $HiddenDirs = $opt_hiddendirs if defined $opt_hiddendirs;
730 $Htmlfile = $opt_outfile if defined $opt_outfile;
731 $Podroot = $opt_podroot if defined $opt_podroot;
732 $Quiet = $opt_quiet if defined $opt_quiet;
733 $Recurse = $opt_recurse if defined $opt_recurse;
734 $Title = $opt_title if defined $opt_title;
735 $Verbose = $opt_verbose if defined $opt_verbose;
737 warn "Flushing item and directory caches\n"
738 if $opt_verbose && defined $opt_flush;
739 $Dircache = "$Cachedir/pod2htmd.tmp";
740 $Itemcache = "$Cachedir/pod2htmi.tmp";
741 if (defined $opt_flush) {
742 1 while unlink($Dircache, $Itemcache);
750 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
751 my @cache_key_args = @_;
753 # A first-level cache:
754 # Don't bother reading the cache files if they still apply
755 # and haven't changed since we last read them.
757 my $this_cache_key = cache_key(@cache_key_args);
759 return if $Saved_Cache_Key and $this_cache_key eq $Saved_Cache_Key;
761 # load the cache of %Pages and %Items if possible. $tests will be
762 # non-zero if successful.
764 if (-f $dircache && -f $itemcache) {
765 warn "scanning for item cache\n" if $Verbose;
766 $tests = load_cache($dircache, $itemcache, $podpath, $podroot);
769 # if we didn't succeed in loading the cache then we must (re)build
772 warn "scanning directories in pod-path\n" if $Verbose;
773 scan_podpath($podroot, $recurse, 0);
775 $Saved_Cache_Key = cache_key(@cache_key_args);
779 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
780 return join('!', $dircache, $itemcache, $recurse,
781 @$podpath, $podroot, stat($dircache), stat($itemcache));
785 # load_cache - tries to find if the caches stored in $dircache and $itemcache
786 # are valid caches of %Pages and %Items. if they are valid then it loads
787 # them and returns a non-zero value.
790 my($dircache, $itemcache, $podpath, $podroot) = @_;
796 open(CACHE, "<$itemcache") ||
797 die "$0: error opening $itemcache for reading: $!\n";
800 # is it the same podpath?
803 $tests++ if (join(":", @$podpath) eq $_);
805 # is it the same podroot?
808 $tests++ if ($podroot eq $_);
810 # load the cache if its good
816 warn "loading item cache\n" if $Verbose;
823 warn "scanning for directory cache\n" if $Verbose;
824 open(CACHE, "<$dircache") ||
825 die "$0: error opening $dircache for reading: $!\n";
829 # is it the same podpath?
832 $tests++ if (join(":", @$podpath) eq $_);
834 # is it the same podroot?
837 $tests++ if ($podroot eq $_);
839 # load the cache if its good
845 warn "loading directory cache\n" if $Verbose;
857 # scan_podpath - scans the directories specified in @podpath for directories,
858 # .pod files, and .pm files. it also scans the pod files specified in
859 # @Libpods for =item directives.
862 my($podroot, $recurse, $append) = @_;
864 my($libpod, $dirname, $pod, @files, @poddata);
871 # scan each directory listed in @Podpath
874 || die "$0: error changing to directory $podroot: $!\n";
875 foreach $dir (@Podpath) {
876 scan_dir($dir, $recurse);
879 # scan the pods listed in @Libpods for =item directives
880 foreach $libpod (@Libpods) {
881 # if the page isn't defined then we won't know where to find it
883 next unless defined $Pages{$libpod} && $Pages{$libpod};
885 # if there is a directory then use the .pod and .pm files within it.
886 # NOTE: Only finds the first so-named directory in the tree.
887 # if ($Pages{$libpod} =~ /([^:]*[^(\.pod|\.pm)]):/) {
888 if ($Pages{$libpod} =~ /([^:]*(?<!\.pod)(?<!\.pm)):/) {
889 # find all the .pod and .pm files within the directory
891 opendir(DIR, $dirname) ||
892 die "$0: error opening directory $dirname: $!\n";
893 @files = grep(/(\.pod|\.pm)\z/ && ! -d $_, readdir(DIR));
896 # scan each .pod and .pm file for =item directives
897 foreach $pod (@files) {
898 open my $fh, '<', "$dirname/$pod"
899 or die "$0: error opening $dirname/$pod for input: $!\n";
902 clean_data( \@poddata );
904 scan_items( \%Items, "$dirname/$pod", @poddata);
907 # use the names of files as =item directives too.
908 ### Don't think this should be done this way - confuses issues.(WL)
909 ### foreach $pod (@files) {
910 ### $pod =~ /^(.*)(\.pod|\.pm)$/;
911 ### $Items{$1} = "$dirname/$1.html" if $1;
913 } elsif ($Pages{$libpod} =~ /([^:]*\.pod):/ ||
914 $Pages{$libpod} =~ /([^:]*\.pm):/) {
915 # scan the .pod or .pm file for =item directives
917 open my $fh, '<', $pod
918 or die "$0: error opening $pod for input: $!\n";
921 clean_data( \@poddata );
923 scan_items( \%Items, "$pod", @poddata);
925 warn "$0: shouldn't be here (line ".__LINE__."\n" unless $Quiet;
928 @poddata = (); # clean-up a bit
931 || die "$0: error changing to directory $pwd: $!\n";
933 # cache the item list for later use
934 warn "caching items for later use\n" if $Verbose;
935 open my $cache, '>', $Itemcache
936 or die "$0: error open $Itemcache for writing: $!\n";
938 print $cache join(":", @Podpath) . "\n$podroot\n";
939 foreach my $key (keys %Items) {
940 print $cache "$key $Items{$key}\n";
943 close $cache or die "error closing $Itemcache: $!";
945 # cache the directory list for later use
946 warn "caching directories for later use\n" if $Verbose;
947 open $cache, '>', $Dircache
948 or die "$0: error open $Dircache for writing: $!\n";
950 print $cache join(":", @Podpath) . "\n$podroot\n";
951 foreach my $key (keys %Pages) {
952 print $cache "$key $Pages{$key}\n";
955 close $cache or die "error closing $Dircache: $!";
959 # scan_dir - scans the directory specified in $dir for subdirectories, .pod
960 # files, and .pm files. notes those that it finds. this information will
961 # be used later in order to figure out where the pages specified in L<>
962 # links are on the filesystem.
965 my($dir, $recurse) = @_;
966 my($t, @subdirs, @pods, $pod, $dirname, @dirs);
972 opendir(DIR, $dir) ||
973 die "$0: error opening directory $dir: $!\n";
974 while (defined($_ = readdir(DIR))) {
975 if (-d "$dir/$_" && $_ ne "." && $_ ne ".."
976 && ($HiddenDirs || !/^\./)
978 $Pages{$_} = "" unless defined $Pages{$_};
979 $Pages{$_} .= "$dir/$_:";
981 } elsif (/\.pod\z/) { # .pod
983 $Pages{$_} = "" unless defined $Pages{$_};
984 $Pages{$_} .= "$dir/$_.pod:";
985 push(@pods, "$dir/$_.pod");
986 } elsif (/\.html\z/) { # .html
988 $Pages{$_} = "" unless defined $Pages{$_};
989 $Pages{$_} .= "$dir/$_.pod:";
990 } elsif (/\.pm\z/) { # .pm
992 $Pages{$_} = "" unless defined $Pages{$_};
993 $Pages{$_} .= "$dir/$_.pm:";
994 push(@pods, "$dir/$_.pm");
995 } elsif (-T "$dir/$_") { # script(?)
997 if (open(F, "$dir/$_")) {
999 while (defined($line = <F>)) {
1000 if ($line =~ /^=(?:pod|head1)/) {
1001 $Pages{$_} = "" unless defined $Pages{$_};
1002 $Pages{$_} .= "$dir/$_.pod:";
1012 # recurse on the subdirectories if necessary
1014 foreach my $subdir (@subdirs) {
1015 scan_dir("$dir/$subdir", $recurse);
1021 # scan_headings - scan a pod file for head[1-6] tags, note the tags, and
1025 my($sections, @data) = @_;
1026 my($tag, $which_head, $otitle, $listdepth, $index);
1033 # scan for =head directives, note their name, and build an index
1034 # pointing to each of them.
1035 foreach my $line (@data) {
1036 if ($line =~ /^=(head)([1-6])\s+(.*)/) {
1037 ($tag, $which_head, $otitle) = ($1,$2,$3);
1039 my $title = depod( $otitle );
1040 my $name = anchorify( $title );
1041 $$sections{$name} = 1;
1042 $title = process_text( \$otitle );
1044 while ($which_head != $listdepth) {
1045 if ($which_head > $listdepth) {
1046 $index .= "\n" . ("\t" x $listdepth) . "<ul>\n";
1048 } elsif ($which_head < $listdepth) {
1050 $index .= "\n" . ("\t" x $listdepth) . "</ul>\n";
1054 $index .= "\n" . ("\t" x $listdepth) . "<li>" .
1055 "<a href=\"#" . $name . "\">" .
1056 $title . "</a></li>";
1060 # finish off the lists
1061 while ($listdepth--) {
1062 $index .= "\n" . ("\t" x $listdepth) . "</ul>\n";
1065 # get rid of bogus lists
1066 $index =~ s,\t*<ul>\s*</ul>\n,,g;
1072 # scan_items - scans the pod specified by $pod for =item directives. we
1073 # will use this information later on in resolving C<> links.
1076 my( $itemref, $pod, @poddata ) = @_;
1080 $pod =~ s/\.pod\z//;
1081 $pod .= ".html" if $pod;
1083 foreach $i (0..$#poddata) {
1084 my $txt = depod( $poddata[$i] );
1086 # figure out what kind of item it is.
1087 # Build string for referencing this item.
1088 if ( $txt =~ /\A=item\s+\*\s*(.*)\Z/s ) { # bulleted list
1091 } elsif( $txt =~ /\A=item\s+(?>\d+\.?)\s*(.*)\Z/s ) { # numbered list
1093 } elsif( $txt =~ /\A=item\s+(.*)\Z/s ) { # definition list
1098 my $fid = fragment_id( $item );
1099 $$itemref{$fid} = "$pod" if $fid;
1104 # process_head - convert a pod head[1-6] tag and convert it to HTML format.
1107 my($fh, $tag, $heading, $hasindex) = @_;
1109 # figure out the level of the =head
1110 $tag =~ /head([1-6])/;
1116 if( $level == 1 && ! $Top ){
1117 print $fh "<a href=\"#__index__\"><small>$Backlink</small></a>\n"
1118 if $hasindex and $Backlink;
1119 print $fh "</p>\n<hr />\n"
1124 my $name = anchorify( depod( $heading ) );
1125 my $convert = process_text( \$heading );
1126 print $fh "<h$level><a name=\"$name\">$convert</a></h$level>\n";
1131 # emit_item_tag - print an =item's text
1132 # Note: The global $EmittedItem is used for inhibiting self-references.
1137 my( $fh, $otext, $text, $compact ) = @_;
1138 my $item = fragment_id( depod($text) , -generate);
1139 Carp::confess("Undefined fragment '$text' (".depod($text).") from fragment_id() in emit_item_tag() in $Podfile")
1141 $EmittedItem = $item;
1142 ### print STDERR "emit_item_tag=$item ($text)\n";
1144 print $fh '<strong>';
1145 if ($Items_Named{$item}++) {
1146 print $fh process_text( \$otext );
1149 $name = anchorify($name);
1150 print $fh qq{<a name="$name" class="item">}, process_text( \$otext ), '</a>';
1152 print $fh "</strong>";
1153 undef( $EmittedItem );
1157 my ($fh, $tag) = @_;
1158 # Open tag for definition list as we have something to put in it
1159 if( ($tag ne 'dl') && ($ListNewTerm) ){
1164 if( $Items_Seen[$Listlevel]++ == 0 ){
1166 push( @Listtype, "$tag" );
1167 print $fh "<$tag>\n";
1169 # if this is not the first item, close the previous one
1170 if ( $tag eq 'dl' ){
1171 print $fh "</dd>\n" unless $ListNewTerm;
1173 print $fh "</li>\n";
1176 my $opentag = $tag eq 'dl' ? 'dt' : 'li';
1177 print $fh "<$opentag>";
1181 # process_item - convert a pod item tag and convert it to HTML format.
1184 my ($fh, $otext) = @_;
1186 # lots of documents start a list without doing an =over. this is
1187 # bad! but, the proper thing to do seems to be to just assume
1188 # they did do an =over. so warn them once and then continue.
1189 if( $Listlevel == 0 ){
1190 warn "$0: $Podfile: unexpected =item directive in paragraph $Paragraph. ignoring.\n" unless $Quiet;
1194 # remove formatting instructions from the text
1195 my $text = depod( $otext );
1197 # all the list variants:
1198 if( $text =~ /\A\*/ ){ # bullet
1199 new_listitem( $fh, 'ul' );
1200 if ($text =~ /\A\*\s+(\S.*)\Z/s ) { # with additional text
1202 $otext =~ s/\A\*\s+//;
1203 emit_item_tag( $fh, $otext, $tag, 1 );
1207 } elsif( $text =~ /\A\d+/ ){ # numbered list
1208 new_listitem( $fh, 'ol' );
1209 if ($text =~ /\A(?>\d+\.?)\s*(\S.*)\Z/s ) { # with additional text
1211 $otext =~ s/\A\d+\.?\s*//;
1212 emit_item_tag( $fh, $otext, $tag, 1 );
1216 } else { # definition list
1217 # new_listitem takes care of opening the <dt> tag
1218 new_listitem( $fh, 'dl' );
1219 if ($text =~ /\A(.+)\Z/s ){ # should have text
1220 emit_item_tag( $fh, $otext, $text, 1 );
1221 # write the definition term and close <dt> tag
1222 print $fh "</dt>\n";
1224 # trigger opening a <dd> tag for the actual definition; will not
1225 # happen if next paragraph is also a definition term (=item)
1232 # process_over - process a pod over tag and start a corresponding HTML list.
1237 push( @Items_Seen, 0 );
1241 # process_back - process a pod back tag and convert it to HTML format.
1245 if( $Listlevel == 0 ){
1246 warn "$0: $Podfile: unexpected =back directive in paragraph $Paragraph. ignoring.\n" unless $Quiet;
1250 # close off the list. note, I check to see if $Listtype[$Listlevel] is
1251 # defined because an =item directive may have never appeared and thus
1252 # $Listtype[$Listlevel] may have never been initialized.
1254 if( defined $Listtype[$Listlevel] ){
1255 if ( $Listtype[$Listlevel] eq 'dl' ){
1256 print $fh "</dd>\n" unless $ListNewTerm;
1258 print $fh "</li>\n";
1260 print $fh "</$Listtype[$Listlevel]>\n";
1265 # clean up item count
1270 # process_cut - process a pod cut tag, thus start ignoring pod directives.
1277 # process_pod - process a pod tag, thus stop ignoring pod directives
1278 # until we see a corresponding cut.
1281 # no need to set $Ignore to 0 cause the main loop did it
1285 # process_for - process a =for pod tag. if it's for html, spit
1286 # it out verbatim, if illustration, center it, otherwise ignore it.
1289 my ($fh, $whom, $text) = @_;
1290 if ( $whom =~ /^(pod2)?html$/i) {
1292 } elsif ($whom =~ /^illustration$/i) {
1293 1 while chomp $text;
1294 for my $ext (qw[.png .gif .jpeg .jpg .tga .pcl .bmp]) {
1295 $text .= $ext, last if -r "$text$ext";
1297 print $fh qq{<p align="center"><img src="$text" alt="$text illustration" /></p>};
1302 # process_begin - process a =begin pod tag. this pushes
1303 # whom we're beginning on the begin stack. if there's a
1304 # begin stack, we only print if it us.
1307 my ($fh, $whom, $text) = @_;
1309 push (@Begin_Stack, $whom);
1310 if ( $whom =~ /^(pod2)?html$/) {
1311 print $fh $text if $text;
1316 # process_end - process a =end pod tag. pop the
1317 # begin stack. die if we're mismatched.
1320 my($whom, $text) = @_;
1322 if (!defined $Begin_Stack[-1] or $Begin_Stack[-1] ne $whom ) {
1323 Carp::confess("Unmatched begin/end at chunk $Paragraph in pod $Podfile\n")
1325 pop( @Begin_Stack );
1329 # process_pre - indented paragraph, made into <pre></pre>
1338 # insert spaces in place of tabs
1341 1 while $line =~ s/(\t+)/' ' x ((length($1) * 8) - $-[0] % 8)/e;
1345 # convert some special chars to HTML escapes
1346 $rest = html_escape($rest);
1348 # try and create links for all occurrences of perl.* within
1349 # the preformatted text.
1353 if ( defined $Pages{$2} ){ # is a link
1354 qq($1<a href="$Htmlroot/$Pages{$2}">$2</a>);
1355 } elsif (defined $Pages{dosify($2)}) { # is a link
1356 qq($1<a href="$Htmlroot/$Pages{dosify($2)}">$2</a>);
1362 (<a\ href="?) ([^>:]*:)? ([^>:]*) \.pod: ([^>:]*:)?
1365 if ( $Htmlfileurl ne '' ){
1366 # Here, we take advantage of the knowledge
1367 # that $Htmlfileurl ne '' implies $Htmlroot eq ''.
1368 # Since $Htmlroot eq '', we need to prepend $Htmldir
1369 # on the fron of the link to get the absolute path
1370 # of the link's target. We check for a leading '/'
1371 # to avoid corrupting links that are #, file:, etc.
1373 $old_url = "$Htmldir$old_url" if $old_url =~ m{^\/};
1374 $url = relativize_url( "$old_url.html", $Htmlfileurl );
1381 # Look for embedded URLs and make them into links. We don't
1382 # relativize them since they are best left as the author intended.
1384 my $urls = '(' . join ('|', qw{
1397 my $gunk = '/#~:.?+=&%@!\-';
1398 my $punc = '.:!?\-;';
1399 my $any = "${ltrs}${gunk}${punc}";
1402 \b # start at word boundary
1404 $urls : # need resource and a colon
1405 (?!:) # Ignore File::, among others.
1406 [$any] +? # followed by one or more of any valid
1407 # character, but be conservative and
1408 # take only what you need to....
1411 " > # maybe pre-quoted '<a href="...">'
1413 [$punc]* # 0 or more punctuation
1415 [^$any] # by a non-url char
1417 $ # end of the string
1420 $ # then end of the string
1422 }{<a href="$1">$1</a>}igox;
1424 # text should be as it is (verbatim)
1430 # pure text processing
1432 # pure_text/inIS_text: differ with respect to automatic C<> recognition.
1433 # we don't want this to happen within IS
1437 process_puretext( $text, 1 );
1442 process_puretext( $text, 0 );
1446 # process_puretext - process pure text (without pod-escapes) converting
1447 # double-quotes and handling implicit C<> links.
1449 sub process_puretext {
1450 my($text, $notinIS) = @_;
1452 ## Guessing at func() or [\$\@%&]*var references in plain text is destined
1453 ## to produce some strange looking ref's. uncomment to disable:
1456 my(@words, $lead, $trail);
1458 # keep track of leading and trailing white-space
1459 $lead = ($text =~ s/\A(\s+)//s ? $1 : "");
1460 $trail = ($text =~ s/(\s+)\Z//s ? $1 : "");
1462 # split at space/non-space boundaries
1463 @words = split( /(?<=\s)(?=\S)|(?<=\S)(?=\s)/, $text );
1465 # process each word individually
1466 foreach my $word (@words) {
1468 next if $word =~ /^\s*$/;
1469 # see if we can infer a link or a function call
1471 # NOTE: This is a word based search, it won't automatically
1472 # mark "substr($var, 1, 2)" because the 1st word would be "substr($var"
1473 # User has to enclose those with proper C<>
1475 if( $notinIS && $word =~
1477 ^([a-z_]{2,}) # The function name
1479 ([0-9][a-z]* # Manual page(1) or page(1M)
1480 |[^)]*[\$\@\%][^)]+ # ($foo), (1, @foo), (%hash)
1484 ([.,;]?)$ # a possible punctuation follows
1487 # has parenthesis so should have been a C<> ref
1488 ## try for a pagename (perlXXX(1))?
1489 my( $func, $args, $rest ) = ( $1, $2, $3 || '' );
1490 if( $args =~ /^\d+$/ ){
1491 my $url = page_sect( $word, '' );
1493 $word = qq(<a href="$url" class="man">the $word manpage</a>$rest);
1497 ## try function name for a link, append tt'ed argument list
1498 $word = emit_C( $func, '', "($args)") . $rest;
1500 #### disabled. either all (including $\W, $\w+{.*} etc.) or nothing.
1501 ## } elsif( $notinIS && $word =~ /^[\$\@%&*]+\w+$/) {
1502 ## # perl variables, should be a C<> ref
1503 ## $word = emit_C( $word );
1505 } elsif ($word =~ m,^\w+://\w,) {
1507 # Don't relativize it: leave it as the author intended
1508 $word = qq(<a href="$word">$word</a>);
1509 } elsif ($word =~ /[\w.-]+\@[\w-]+\.\w/) {
1510 # looks like an e-mail address
1511 my ($w1, $w2, $w3) = ("", $word, "");
1512 ($w1, $w2, $w3) = ("(", $1, ")$2") if $word =~ /^\((.*?)\)(,?)/;
1513 ($w1, $w2, $w3) = ("<", $1, ">$2") if $word =~ /^<(.*?)>(,?)/;
1514 $word = qq($w1<a href="mailto:$w2">$w2</a>$w3);
1516 $word = html_escape($word) if $word =~ /["&<>]/;
1520 # put everything back together
1521 return $lead . join( '', @words ) . $trail;
1526 # process_text - handles plaintext that appears in the input pod file.
1527 # there may be pod commands embedded within the text so those must be
1528 # converted to html commands.
1531 sub process_text1($$;$$);
1532 sub pattern ($) { $_[0] ? '\s+'.('>' x ($_[0] + 1)) : '>' }
1533 sub closing ($) { local($_) = shift; (defined && s/\s+\z//) ? length : 0 }
1538 my $res = process_text1( 0, $tref );
1543 sub process_text_rfc_links {
1546 # For every "RFCnnnn" or "RFC nnn", link it to the authoritative
1547 # ource. Do not use the /i modifier here. Require "RFC" to be written in
1548 # in capital letters.
1551 (?<=[^<>[:alpha:]]) # Make sure this is not an URL already
1552 (RFC\s*([0-9]{1,5}))(?![0-9]) # max 5 digits
1554 {<a href="http://www.ietf.org/rfc/rfc$2.txt" class="rfc">$1</a>}gx;
1559 sub process_text1($$;$$){
1560 my( $lev, $rstr, $func, $closing ) = @_;
1563 unless (defined $func) {
1569 # B<text> - boldface
1570 $res = '<strong>' . process_text1( $lev, $rstr ) . '</strong>';
1572 } elsif( $func eq 'C' ){
1573 # C<code> - can be a ref or <code></code>
1574 # need to extract text
1575 my $par = go_ahead( $rstr, 'C', $closing );
1577 ## clean-up of the link target
1578 my $text = depod( $par );
1580 ### my $x = $par =~ /[BI]</ ? 'yes' : 'no' ;
1581 ### print STDERR "-->call emit_C($par) lev=$lev, par with BI=$x\n";
1583 $res = emit_C( $text, $lev > 1 || ($par =~ /[BI]</) );
1585 } elsif( $func eq 'E' ){
1586 # E<x> - convert to character
1587 $$rstr =~ s/^([^>]*)>//;
1589 $escape =~ s/^0?x([\dA-F]+)$/#x$1/i
1590 or $escape =~ s/^0([0-7]+)$/'#'.oct($1)/ei
1591 or $escape =~ s/^(\d+)$/#$1/;
1594 } elsif( $func eq 'F' ){
1595 # F<filename> - italicize
1596 $res = '<em class="file">' . process_text1( $lev, $rstr ) . '</em>';
1598 } elsif( $func eq 'I' ){
1599 # I<text> - italicize
1600 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1602 } elsif( $func eq 'L' ){
1604 ## L<text|cross-ref> => produce text, use cross-ref for linking
1605 ## L<cross-ref> => make text from cross-ref
1606 ## need to extract text
1607 my $par = go_ahead( $rstr, 'L', $closing );
1609 # some L<>'s that shouldn't be:
1610 # a) full-blown URL's are emitted as-is
1611 if( $par =~ m{^\w+://}s ){
1612 return make_URL_href( $par );
1614 # b) C<...> is stripped and treated as C<>
1615 if( $par =~ /^C<(.*)>$/ ){
1616 my $text = depod( $1 );
1617 return emit_C( $text, $lev > 1 || ($par =~ /[BI]</) );
1620 # analyze the contents
1621 $par =~ s/\n/ /g; # undo word-wrapped tags
1624 if( $par =~ s{^([^|]+)\|}{} ){
1628 # make sure sections start with a /
1631 my( $page, $section, $ident );
1633 # check for link patterns
1634 if( $par =~ m{^([^/]+?)/(?!")(.*?)$} ){ # name/ident
1635 # we've got a name/ident (no quotes)
1637 ( $page, $ident ) = ( $1, $2 );
1639 ( $page, $section ) = ( $1, $2 );
1641 ### print STDERR "--> L<$par> to page $page, ident $ident\n";
1643 } elsif( $par =~ m{^(.*?)/"?(.*?)"?$} ){ # [name]/"section"
1644 # even though this should be a "section", we go for ident first
1645 ( $page, $ident ) = ( $1, $2 );
1646 ### print STDERR "--> L<$par> to page $page, section $section\n";
1648 } elsif( $par =~ /\s/ ){ # this must be a section with missing quotes
1649 ( $page, $section ) = ( '', $par );
1650 ### print STDERR "--> L<$par> to void page, section $section\n";
1653 ( $page, $section ) = ( $par, '' );
1654 ### print STDERR "--> L<$par> to page $par, void section\n";
1657 # now, either $section or $ident is defined. the convoluted logic
1658 # below tries to resolve L<> according to what the user specified.
1659 # failing this, we try to find the next best thing...
1660 my( $url, $ltext, $fid );
1663 if( defined $ident ){
1664 ## try to resolve $ident as an item
1665 ( $url, $fid ) = coderef( $page, $ident );
1667 if( ! defined( $linktext ) ){
1669 $linktext .= " in " if $ident && $page;
1670 $linktext .= "the $page manpage" if $page;
1672 ### print STDERR "got coderef url=$url\n";
1675 ## no luck: go for a section (auto-quoting!)
1678 ## now go for a section
1679 my $htmlsection = htmlify( $section );
1680 $url = page_sect( $page, $htmlsection );
1682 if( ! defined( $linktext ) ){
1683 $linktext = $section;
1684 $linktext .= " in " if $section && $page;
1685 $linktext .= "the $page manpage" if $page;
1687 ### print STDERR "got page/section url=$url\n";
1690 ## no luck: go for an ident
1697 ( $url, $fid ) = coderef( $page, $ident );
1699 if( ! defined( $linktext ) ){
1701 $linktext .= " in " if $ident && $page;
1702 $linktext .= "the $page manpage" if $page;
1704 ### print STDERR "got section=>coderef url=$url\n";
1708 # warning; show some text.
1709 $linktext = $opar unless defined $linktext;
1710 warn "$0: $Podfile: cannot resolve L<$opar> in paragraph $Paragraph.\n" unless $Quiet;
1713 # now we have a URL or just plain code
1714 $$rstr = $linktext . '>' . $$rstr;
1715 if( defined( $url ) ){
1716 $res = "<a href=\"$url\">" . process_text1( $lev, $rstr ) . '</a>';
1718 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1721 } elsif( $func eq 'S' ){
1722 # S<text> - non-breaking spaces
1723 $res = process_text1( $lev, $rstr );
1724 $res =~ s/ / /g;
1726 } elsif( $func eq 'X' ){
1728 warn "$0: $Podfile: invalid X<> in paragraph $Paragraph.\n"
1729 unless $$rstr =~ s/^[^>]*>// or $Quiet;
1730 } elsif( $func eq 'Z' ){
1732 warn "$0: $Podfile: invalid Z<> in paragraph $Paragraph.\n"
1733 unless $$rstr =~ s/^>// or $Quiet;
1736 my $term = pattern $closing;
1737 while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)//s ){
1738 # all others: either recurse into new function or
1739 # terminate at closing angle bracket(s)
1741 $pt .= $2 if !$3 && $lev == 1;
1742 $res .= $lev == 1 ? pure_text( $pt ) : inIS_text( $pt );
1743 return $res if !$3 && $lev > 1;
1745 $res .= process_text1( $lev, $rstr, $3, closing $4 );
1749 $res .= pure_text( $$rstr );
1750 } elsif( ! $Quiet ) {
1751 my $snippet = substr($$rstr,0,60);
1752 warn "$0: $Podfile: undelimited $func<> in paragraph $Paragraph: '$snippet'.\n"
1755 $res = process_text_rfc_links($res);
1761 # go_ahead: extract text of an IS (can be nested)
1764 my( $rstr, $func, $closing ) = @_;
1766 my @closing = ($closing);
1768 s/\A(.*?)(([BCEFILSXZ])<(<+\s+)?|@{[pattern $closing[0]]})//s ){
1772 return $res unless @closing;
1774 unshift @closing, closing $4;
1779 my $snippet = substr($$rstr,0,60);
1780 warn "$0: $Podfile: undelimited $func<> in paragraph $Paragraph (go_ahead): '$snippet'.\n"
1786 # emit_C - output result of C<text>
1787 # $text is the depod-ed text
1790 my( $text, $nocode, $args ) = @_;
1791 $args = '' unless defined $args;
1793 my( $url, $fid ) = coderef( undef(), $text );
1795 # need HTML-safe text
1796 my $linktext = html_escape( "$text$args" );
1798 if( defined( $url ) &&
1799 (!defined( $EmittedItem ) || $EmittedItem ne $fid ) ){
1800 $res = "<a href=\"$url\"><code>$linktext</code></a>";
1801 } elsif( 0 && $nocode ){
1804 $res = "<code>$linktext</code>";
1810 # html_escape: make text safe for HTML
1814 $rest =~ s/&/&/g;
1815 $rest =~ s/</</g;
1816 $rest =~ s/>/>/g;
1817 $rest =~ s/"/"/g;
1818 # ' is only in XHTML, not HTML4. Be conservative
1819 #$rest =~ s/'/'/g;
1825 # dosify - convert filenames to 8.3
1829 return lc($str) if $^O eq 'VMS'; # VMS just needs casing
1832 $str =~ s/(\.\w+)/substr ($1,0,4)/ge;
1833 $str =~ s/(\w+)/substr ($1,0,8)/ge;
1839 # page_sect - make a URL from the text of a L<>
1842 my( $page, $section ) = @_;
1843 my( $linktext, $page83, $link); # work strings
1845 # check if we know that this is a section in this page
1846 if (!defined $Pages{$page} && defined $Sections{$page}) {
1849 ### print STDERR "reset page='', section=$section\n";
1852 $page83=dosify($page);
1853 $page=$page83 if (defined $Pages{$page83});
1855 $link = "#" . anchorify( $section );
1856 } elsif ( $page =~ /::/ ) {
1858 # Search page cache for an entry keyed under the html page name,
1859 # then look to see what directory that page might be in. NOTE:
1860 # this will only find one page. A better solution might be to produce
1861 # an intermediate page that is an index to all such pages.
1862 my $page_name = $page ;
1863 $page_name =~ s,^.*/,,s ;
1864 if ( defined( $Pages{ $page_name } ) &&
1865 $Pages{ $page_name } =~ /([^:]*$page)\.(?:pod|pm):/
1870 # NOTE: This branch assumes that all A::B pages are located in
1871 # $Htmlroot/A/B.html . This is often incorrect, since they are
1872 # often in $Htmlroot/lib/A/B.html or such like. Perhaps we could
1873 # analyze the contents of %Pages and figure out where any
1874 # cousins of A::B are, then assume that. So, if A::B isn't found,
1875 # but A::C is found in lib/A/C.pm, then A::B is assumed to be in
1876 # lib/A/B.pm. This is also limited, but it's an improvement.
1877 # Maybe a hints file so that the links point to the correct places
1881 $link = "$Htmlroot/$page.html";
1882 $link .= "#" . anchorify( $section ) if ($section);
1883 } elsif (!defined $Pages{$page}) {
1886 $section = anchorify( $section ) if $section ne "";
1887 ### print STDERR "...section=$section\n";
1889 # if there is a directory by the name of the page, then assume that an
1890 # appropriate section will exist in the subdirectory
1891 # if ($section ne "" && $Pages{$page} =~ /([^:]*[^(\.pod|\.pm)]):/) {
1892 if ($section ne "" && $Pages{$page} =~ /([^:]*(?<!\.pod)(?<!\.pm)):/) {
1893 $link = "$Htmlroot/$1/$section.html";
1894 ### print STDERR "...link=$link\n";
1896 # since there is no directory by the name of the page, the section will
1897 # have to exist within a .html of the same name. thus, make sure there
1898 # is a .pod or .pm that might become that .html
1900 $section = "#$section" if $section;
1901 ### print STDERR "...section=$section\n";
1903 # check if there is a .pod with the page name.
1904 # for L<Foo>, Foo.(pod|pm) is preferred to A/Foo.(pod|pm)
1905 if ($Pages{$page} =~ /([^:]*)\.(?:pod|pm):/) {
1906 $link = "$Htmlroot/$1.html$section";
1914 # Here, we take advantage of the knowledge that $Htmlfileurl ne ''
1915 # implies $Htmlroot eq ''. This means that the link in question
1916 # needs a prefix of $Htmldir if it begins with '/'. The test for
1917 # the initial '/' is done to avoid '#'-only links, and to allow
1918 # for other kinds of links, like file:, ftp:, etc.
1920 if ( $Htmlfileurl ne '' ) {
1921 $link = "$Htmldir$link" if $link =~ m{^/}s;
1922 $url = relativize_url( $link, $Htmlfileurl );
1923 # print( " b: [$link,$Htmlfileurl,$url]\n" );
1936 # relativize_url - convert an absolute URL to one relative to a base URL.
1937 # Assumes both end in a filename.
1939 sub relativize_url {
1940 my ($dest,$source) = @_ ;
1942 my ($dest_volume,$dest_directory,$dest_file) =
1943 File::Spec::Unix->splitpath( $dest ) ;
1944 $dest = File::Spec::Unix->catpath( $dest_volume, $dest_directory, '' ) ;
1946 my ($source_volume,$source_directory,$source_file) =
1947 File::Spec::Unix->splitpath( $source ) ;
1948 $source = File::Spec::Unix->catpath( $source_volume, $source_directory, '' ) ;
1951 if ( $dest ne '' ) {
1952 $rel_path = File::Spec::Unix->abs2rel( $dest, $source ) ;
1955 if ( $rel_path ne '' &&
1956 substr( $rel_path, -1 ) ne '/' &&
1957 substr( $dest_file, 0, 1 ) ne '#'
1959 $rel_path .= "/$dest_file" ;
1962 $rel_path .= "$dest_file" ;
1970 # coderef - make URL from the text of a C<>
1973 my( $page, $item ) = @_;
1976 my $fid = fragment_id( $item );
1978 if( defined( $page ) && $page ne "" ){
1979 # we have been given a $page...
1982 Carp::confess("Undefined fragment '$item' from fragment_id() in coderef() in $Podfile")
1984 # Do we take it? Item could be a section!
1985 my $base = $Items{$fid} || "";
1986 $base =~ s{[^/]*/}{};
1987 if( $base ne "$page.html" ){
1988 ### print STDERR "coderef( $page, $item ): items{$fid} = $Items{$fid} = $base => discard page!\n";
1993 # no page - local items precede cached items
1994 if( defined( $fid ) ){
1995 if( exists $Local_Items{$fid} ){
1996 $page = $Local_Items{$fid};
1998 $page = $Items{$fid};
2003 # if there was a pod file that we found earlier with an appropriate
2004 # =item directive, then create a link to that page.
2005 if( defined $page ){
2007 if( exists $Pages{$page} and $Pages{$page} =~ /([^:.]*)\.[^:]*:/){
2008 $page = $1 . '.html';
2010 my $link = "$Htmlroot/$page#" . anchorify($fid);
2012 # Here, we take advantage of the knowledge that $Htmlfileurl
2013 # ne '' implies $Htmlroot eq ''.
2014 if ( $Htmlfileurl ne '' ) {
2015 $link = "$Htmldir$link" ;
2016 $url = relativize_url( $link, $Htmlfileurl ) ;
2021 $url = "#" . anchorify($fid);
2024 confess "url has space: $url" if $url =~ /"[^"]*\s[^"]*"/;
2026 return( $url, $fid );
2032 # Adapted from Nick Ing-Simmons' PodToHtml package.
2034 my $source_file = shift ;
2035 my $destination_file = shift;
2037 my $source = URI::file->new_abs($source_file);
2038 my $uo = URI::file->new($destination_file,$source)->abs;
2039 return $uo->rel->as_string;
2044 # finish_list - finish off any pending HTML lists. this should be called
2045 # after the entire pod file has been read and converted.
2050 warn "$0: $Podfile: unterminated list(s) at =head in paragraph $Paragraph. ignoring.\n" unless $Quiet;
2051 while( $Listlevel ){
2052 process_back( $fh );
2058 # htmlify - converts a pod section specification to a suitable section
2059 # specification for HTML. Note that we keep spaces and special characters
2060 # except ", ? (Netscape problem) and the hyphen (writer's problem...).
2064 $heading =~ s/(\s+)/ /g;
2065 $heading =~ s/\s+\Z//;
2066 $heading =~ s/\A\s+//;
2067 # The hyphen is a disgrace to the English language.
2068 # $heading =~ s/[-"?]//g;
2069 $heading =~ s/["?]//g;
2070 $heading = lc( $heading );
2075 # similar to htmlify, but turns non-alphanumerics into underscores
2079 $anchor = htmlify($anchor);
2080 $anchor =~ s/\W/_/g;
2085 # depod - convert text by eliminating all interior sequences
2086 # Note: can be called with copy or modify semantics
2093 $E2c{amp} = '&'; # in Tk's pods
2101 ${$_[0]} = depod1( \$string );
2109 my( $rstr, $func, $closing ) = @_;
2111 return $res unless defined $$rstr;
2112 if( ! defined( $func ) ){
2113 # skip to next begin of an interior sequence
2114 while( $$rstr =~ s/\A(.*?)([BCEFILSXZ])<(<+[^\S\n]+)?//s ){
2115 # recurse into its text
2116 $res .= $1 . depod1( $rstr, $2, closing $3);
2119 } elsif( $func eq 'E' ){
2120 # E<x> - convert to character
2121 $$rstr =~ s/^([^>]*)>//;
2122 $res .= $E2c{$1} || "";
2123 } elsif( $func eq 'X' ){
2125 $$rstr =~ s/^[^>]*>//;
2126 } elsif( $func eq 'Z' ){
2130 # all others: either recurse into new function or
2131 # terminate at closing angle bracket
2132 my $term = pattern $closing;
2133 while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)//s ){
2136 $res .= depod1( $rstr, $3, closing $4 );
2138 ## If we're here and $2 ne '>': undelimited interior sequence.
2139 ## Ignored, as this is called without proper indication of where we are.
2140 ## Rely on process_text to produce diagnostics.
2146 my %seen; # static fragment record hash
2148 sub fragment_id_readable {
2150 my $generate = shift; # optional flag
2154 # leave the words for the fragment identifier,
2155 # change everything else to underbars.
2156 $text =~ s/[^A-Za-z0-9_]+/_/g; # do not use \W to avoid locale dependency.
2157 $text =~ s/_{2,}/_/g;
2163 # Nothing left after removing punctuation, so leave it as is
2164 # E.g. if option is named: "=item -#"
2170 if ( exists $seen{$text} ) {
2171 # This already exists, make it unique
2173 $text = $text . $seen{$text};
2175 $seen{$text} = 1; # first time seen this fragment
2183 sub fragment_id_obfuscated { # This was the old "_2d_2d__"
2185 my $generate = shift; # optional flag
2187 # text? Normalize by obfuscating the fragment id to make it unique
2188 $text =~ s/\s+/_/sg;
2191 defined( $HC[ord($1)] ) ? $HC[ord($1)]
2192 : ( $HC[ord($1)] = sprintf( "%%%02X", ord($1) ) ) }gxe;
2193 $text = substr( $text, 0, 50 );
2199 # fragment_id - construct a fragment identifier from:
2201 # b) contents of C<...>
2206 my $generate = shift; # optional flag
2208 $text =~ s/\s+\Z//s;
2210 # a method or function?
2211 return $1 if $text =~ /(\w+)\s*\(/;
2212 return $1 if $text =~ /->\s*(\w+)\s*\(?/;
2215 return $1 if $text =~ /^([\$\@%*]\S+)/;
2217 # some pattern matching operator?
2218 return $1 if $text =~ m|^(\w+/).*/\w*$|;
2220 # fancy stuff... like "do { }"
2221 return $1 if $text =~ m|^(\w+)\s*{.*}$|;
2223 # honour the perlfunc manpage: func [PAR[,[ ]PAR]...]
2224 # and some funnies with ... Module ...
2225 return $1 if $text =~ m{^([a-z\d_]+)(\s+[A-Z,/& ][A-Z\d,/& ]*)?$};
2226 return $1 if $text =~ m{^([a-z\d]+)\s+Module(\s+[A-Z\d,/& ]+)?$};
2228 return fragment_id_readable($text, $generate);
2235 # make_URL_href - generate HTML href from URL
2236 # Special treatment for CGI queries.
2238 sub make_URL_href($){
2241 s{^(http:[-\w/#~:.+=&%@!]+)(\?.*)$}{<a href="$1$2">$1</a>}i ){
2242 $url = "<a href=\"$url\">$url</a>";