5 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
8 @EXPORT = qw(pod2html htmlify);
9 @EXPORT_OK = qw(anchorify);
18 use locale; # make \w work right in non-ASCII lands
22 Pod::Html - module to convert pod files to HTML
31 Converts files from pod format (see L<perlpod>) to HTML format. It
32 can automatically generate indexes and cross-references, and it keeps
33 a cache of things it knows how to cross-reference.
37 Pod::Html takes the following arguments:
43 --backlink="Back to Top"
45 Adds "Back to Top" links in front of every C<head1> heading (except for
46 the first). By default, no backlinks are generated.
52 Creates the item and directory caches in the given directory.
58 Specify the URL of a cascading style sheet. Also disables all HTML/CSS
59 C<style> attributes that are output by default (to avoid conflicts).
65 Flushes the item and directory caches.
72 Creates header and footer blocks containing the text of the C<NAME>
73 section. By default, no headers are generated.
79 Displays the usage message.
85 Sets the directory in which the resulting HTML file is placed. This
86 is used to generate relative links to other files. Not passing this
87 causes all links to be absolute, since this is the value that tells
88 Pod::Html the root of the documentation tree.
94 Sets the base URL for the HTML files. When cross-references are made,
95 the HTML root is prepended to the URL.
102 Generate an index at the top of the HTML file. This is the default
109 Specify the pod file to convert. Input is taken from STDIN if no
114 --libpods=name:...:name
116 List of page names (eg, "perlfunc") which contain linkable C<=item>s.
123 B<Deprecated>, has no effect. For backwards compatibility only.
129 Specify the HTML file to create. Output goes to STDOUT if no outfile
134 --podpath=name:...:name
136 Specify which subdirectories of the podroot contain pod files whose
137 HTML converted forms can be linked to in cross references.
143 Specify the base directory for finding library pods.
150 Don't display I<mostly harmless> warning messages. These messages
151 will be displayed by default. But this is not the same as C<verbose>
159 Recurse into subdirectories specified in podpath (default behaviour).
165 Specify the title of the resulting HTML file.
172 Display progress messages. By default, they won't be displayed.
179 "--podpath=lib:ext:pod:vms",
180 "--podroot=/usr/src/perl",
181 "--htmlroot=/perl/nmanual",
182 "--libpods=perlfunc:perlguts:perlvar:perlrun:perlop",
185 "--outfile=/perl/nmanual/foo.html");
189 Uses C<$Config{pod2html}> to setup default options.
193 Tom Christiansen, E<lt>tchrist@perl.comE<gt>.
201 This program is distributed under the Artistic License.
205 my $cachedir = "."; # The directory to which item and directory
206 # caches will be written.
207 my $cache_ext = $^O eq 'VMS' ? ".tmp" : ".x~~";
208 my $dircache = "pod2htmd$cache_ext";
209 my $itemcache = "pod2htmi$cache_ext";
211 my @begin_stack = (); # begin/end stack
213 my @libpods = (); # files to search for links from C<> directives
214 my $htmlroot = "/"; # http-server base directory from which all
215 # relative paths in $podpath stem.
216 my $htmldir = ""; # The directory to which the html pages
217 # will (eventually) be written.
218 my $htmlfile = ""; # write to stdout by default
219 my $htmlfileurl = "" ; # The url that other files would use to
220 # refer to this file. This is only used
221 # to make relative urls that point to
223 my $podfile = ""; # read from stdin by default
224 my @podpath = (); # list of directories containing library pods.
225 my $podroot = File::Spec->curdir; # filesystem base directory from which all
226 # relative paths in $podpath stem.
227 my $css = ''; # Cascading style sheet
228 my $recurse = 1; # recurse on subdirectories in $podpath.
229 my $quiet = 0; # not quiet by default
230 my $verbose = 0; # not verbose by default
231 my $doindex = 1; # non-zero if we should generate an index
232 my $backlink = ''; # text for "back to top" links
233 my $listlevel = 0; # current list depth
234 my @listend = (); # the text to use to end the list.
235 my $after_lpar = 0; # set to true after a par in an =item
236 my $ignore = 1; # whether or not to format text. we don't
237 # format text until we hit our first pod
240 my %items_named = (); # for the multiples of the same item in perlfunc
242 my $title; # title to give the pod(s)
243 my $header = 0; # produce block header/footer
244 my $top = 1; # true if we are at the top of the doc. used
245 # to prevent the first <hr /> directive.
246 my $paragraph; # which paragraph we're processing (used
247 # for error messages)
248 my $ptQuote = 0; # status of double-quote conversion
249 my %pages = (); # associative array used to find the location
250 # of pages referenced by L<> links.
251 my %sections = (); # sections within this page
252 my %items = (); # associative array used to find the location
253 # of =item directives referenced by C<> links
254 my %local_items = (); # local items - avoid destruction of %items
255 my $Is83; # is dos with short filenames (8.3)
258 $dircache = "pod2htmd$cache_ext";
259 $itemcache = "pod2htmi$cache_ext";
261 @begin_stack = (); # begin/end stack
263 @libpods = (); # files to search for links from C<> directives
264 $htmlroot = "/"; # http-server base directory from which all
265 # relative paths in $podpath stem.
266 $htmldir = ""; # The directory to which the html pages
267 # will (eventually) be written.
268 $htmlfile = ""; # write to stdout by default
269 $podfile = ""; # read from stdin by default
270 @podpath = (); # list of directories containing library pods.
271 $podroot = File::Spec->curdir; # filesystem base directory from which all
272 # relative paths in $podpath stem.
273 $css = ''; # Cascading style sheet
274 $recurse = 1; # recurse on subdirectories in $podpath.
275 $quiet = 0; # not quiet by default
276 $verbose = 0; # not verbose by default
277 $doindex = 1; # non-zero if we should generate an index
278 $backlink = ''; # text for "back to top" links
279 $listlevel = 0; # current list depth
280 @listend = (); # the text to use to end the list.
281 $after_lpar = 0; # set to true after a par in an =item
282 $ignore = 1; # whether or not to format text. we don't
283 # format text until we hit our first pod
288 $header = 0; # produce block header/footer
289 $title = ''; # title to give the pod(s)
290 $top = 1; # true if we are at the top of the doc. used
291 # to prevent the first <hr /> directive.
292 $paragraph = ''; # which paragraph we're processing (used
293 # for error messages)
294 %sections = (); # sections within this page
296 # These are not reinitialised here but are kept as a cache.
297 # See get_cache and related cache management code.
298 #%pages = (); # associative array used to find the location
299 # of pages referenced by L<> links.
300 #%items = (); # associative array used to find the location
301 # of =item directives referenced by C<> links
307 # clean_data: global clean-up of pod data
311 for my $i ( 0..$#{$dataref} ) {
312 ${$dataref}[$i] =~ s/\s+\Z//;
314 # have a look for all-space lines
315 if( ${$dataref}[$i] =~ /^\s+$/m and $dataref->[$i] !~ /^\s/ ){
316 my @chunks = split( /^\s+$/m, ${$dataref}[$i] );
317 splice( @$dataref, $i, 1, @chunks );
330 $Is83 = 0 if (defined (&Dos::UseLFN) && Dos::UseLFN());
332 # cache of %pages and %items from last time we ran pod2html
334 #undef $opt_help if defined $opt_help;
336 # parse the command-line parameters
337 parse_command_line();
339 # escape the backlink argument (same goes for title but is done later...)
340 $backlink = html_escape($backlink) if defined $backlink;
342 # set some variables to their default values if necessary
344 unless (@ARGV && $ARGV[0]) {
345 $podfile = "-" unless $podfile; # stdin
346 open(POD, "<$podfile")
347 || die "$0: cannot open $podfile file for input: $!\n";
349 $podfile = $ARGV[0]; # XXX: might be more filenames
352 $htmlfile = "-" unless $htmlfile; # stdout
353 $htmlroot = "" if $htmlroot eq "/"; # so we don't get a //
354 $htmldir =~ s#/\z## ; # so we don't get a //
356 && defined( $htmldir )
358 && substr( $htmlfile, 0, length( $htmldir ) ) eq $htmldir
361 # Set the 'base' url for this file, so that we can use it
362 # as the location from which to calculate relative links
363 # to other files. If this is '', then absolute links will
364 # be used throughout.
365 $htmlfileurl= "$htmldir/" . substr( $htmlfile, length( $htmldir ) + 1);
368 # read the pod a paragraph at a time
369 warn "Scanning for sections in input file(s)\n" if $verbose;
378 @poddata = map { s/\r\n/\n/g;
380 map { "$_\n\n" } split /\n\n/ :
383 @poddata = map { s/\r/\n/g;
385 map { "$_\n\n" } split /\n\n/ :
392 clean_data( \@poddata );
394 # scan the pod for =head[1-6] directives and build an index
395 my $index = scan_headings(\%sections, @poddata);
398 warn "No headings in $podfile\n" if $verbose;
401 # open the output file
402 open(HTML, ">$htmlfile")
403 || die "$0: cannot open $htmlfile file for output: $!\n";
405 # put a title in the HTML file if one wasn't specified
408 for (my $i = 0; $i < @poddata; $i++) {
409 if ($poddata[$i] =~ /^=head1\s*NAME\b/m) {
410 for my $para ( @poddata[$i, $i+1] ) {
412 if ($title) = $para =~ /(\S+\s+-+.*\S)/s;
419 if (!$title and $podfile =~ /\.pod\z/) {
420 # probably a split pod so take first =head[12] as title
421 for (my $i = 0; $i < @poddata; $i++) {
422 last if ($title) = $poddata[$i] =~ /^=head[12]\s*(.*)/;
424 warn "adopted '$title' as title for $podfile\n"
425 if $verbose and $title;
428 $title =~ s/\s*\(.*\)//;
430 warn "$0: no title for $podfile.\n" unless $quiet;
431 $podfile =~ /^(.*)(\.[^.\/]+)?\z/s;
432 $title = ($podfile eq "-" ? 'No Title' : $1);
433 warn "using $title" if $verbose;
435 $title = html_escape($title);
438 my $bodystyle = ' style="background-color: white"';
439 my $tdstyle = ' style="background-color: #cccccc"';
442 $csslink = qq(\n<link rel="stylesheet" href="$css" type="text/css" />);
443 $csslink =~ s,\\,/,g;
444 $csslink =~ s,(/.):,$1|,;
449 my $block = $header ? <<END_OF_BLOCK : '';
450 <table border="0" width="100%" cellspacing="0" cellpadding="3">
451 <tr><td class="block"$tdstyle valign="middle">
452 <big><strong><span class="block"> $title</span></strong></big>
457 print HTML <<END_OF_HEAD;
458 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
459 <html xmlns="http://www.w3.org/1999/xhtml">
461 <title>$title</title>$csslink
462 <link rev="made" href="mailto:$Config{perladmin}" />
469 # load/reload/validate/cache %pages and %items
470 get_cache($dircache, $itemcache, \@podpath, $podroot, $recurse);
472 # scan the pod for =item directives
473 scan_items( \%local_items, "", @poddata);
475 # put an index at the top of the file. note, if $doindex is 0 we
476 # still generate an index, but surround it with an html comment.
477 # that way some other program can extract it if desired.
479 print HTML "<p><a name=\"__index__\"></a></p>\n";
480 print HTML "<!-- INDEX BEGIN -->\n";
481 print HTML "<!--\n" unless $doindex;
483 print HTML "-->\n" unless $doindex;
484 print HTML "<!-- INDEX END -->\n\n";
485 print HTML "<hr />\n" if $doindex and $index;
487 # now convert this file
488 my $after_item; # set to true after an =item
490 warn "Converting input file $podfile\n" if $verbose;
491 foreach my $i (0..$#poddata){
492 $ptQuote = 0; # status of quote conversion
496 if (/^(=.*)/s) { # is it a pod directive?
501 if (/^=begin\s+(\S+)\s*(.*)/si) {# =begin
502 process_begin($1, $2);
503 } elsif (/^=end\s+(\S+)\s*(.*)/si) {# =end
505 } elsif (/^=cut/) { # =cut
507 } elsif (/^=pod/) { # =pod
510 next if @begin_stack && $begin_stack[-1] ne 'html';
512 if (/^=(head[1-6])\s+(.*\S)/s) { # =head[1-6] heading
513 process_head( $1, $2, $doindex && $index );
514 } elsif (/^=item\s*(.*\S)?/sm) { # =item text
515 $need_dd = process_item( $1 );
517 } elsif (/^=over\s*(.*)/) { # =over N
519 } elsif (/^=back/) { # =back
521 } elsif (/^=for\s+(\S+)\s*(.*)/si) {# =for
525 warn "$0: $podfile: unknown pod directive '$1' in "
526 . "paragraph $paragraph. ignoring.\n";
533 next if @begin_stack && $begin_stack[-1] ne 'html';
534 print HTML and next if @begin_stack && $begin_stack[-1] eq 'html';
535 print HTML "<dd>\n" if $need_dd;
537 if( $text =~ /\A\s+/ ){
538 process_pre( \$text );
539 print HTML "<pre>\n$text</pre>\n";
542 process_text( \$text );
544 # experimental: check for a paragraph where all lines
545 # have some ...\t...\t...\n pattern
547 my @lines = split( "\n", $text );
550 foreach my $line ( @lines ){
551 if( $line =~ /\S/ && $line !~ /\t/ ){
557 $text =~ s/\t+/<td>/g;
558 $text =~ s/^/<tr><td>/gm;
559 $text = '<table cellspacing="0" cellpadding="0">' .
564 ## end of experimental
567 print HTML "$text\n";
570 print HTML "<p>$text</p>\n";
573 print HTML "</dd>\n" if $need_dd;
578 # finish off any pending directives
582 print HTML "<p><a href=\"#__index__\"><small>$backlink</small></a></p>\n"
583 if $doindex and $index and $backlink;
585 print HTML <<END_OF_TAIL;
592 # close the html file
595 warn "Finished\n" if $verbose;
598 ##############################################################################
600 my $usage; # see below
603 warn "$0: $podfile: @_\n" if @_;
607 $usage =<<END_OF_USAGE;
608 Usage: $0 --help --htmlroot=<name> --infile=<name> --outfile=<name>
609 --podpath=<name>:...:<name> --podroot=<name>
610 --libpods=<name>:...:<name> --recurse --verbose --index
611 --netscape --norecurse --noindex --cachedir=<name>
613 --backlink - set text for "back to top" links (default: none).
614 --cachedir - directory for the item and directory cache files.
615 --css - stylesheet URL
616 --flush - flushes the item and directory caches.
617 --[no]header - produce block header/footer (default is no headers).
618 --help - prints this message.
619 --htmldir - directory for resulting HTML files.
620 --htmlroot - http-server base directory from which all relative paths
621 in podpath stem (default is /).
622 --[no]index - generate an index at the top of the resulting html
624 --infile - filename for the pod to convert (input taken from stdin
626 --libpods - colon-separated list of pages to search for =item pod
627 directives in as targets of C<> and implicit links (empty
628 by default). note, these are not filenames, but rather
629 page names like those that appear in L<> links.
630 --outfile - filename for the resulting html file (output sent to
632 --podpath - colon-separated list of directories containing library
633 pods (empty by default).
634 --podroot - filesystem base directory from which all relative paths
635 in podpath stem (default is .).
636 --[no]quiet - supress some benign warning messages (default is off).
637 --[no]recurse - recurse on those subdirectories listed in podpath
639 --title - title that will appear in resulting html file.
640 --[no]verbose - self-explanatory (off by default).
641 --[no]netscape - deprecated, has no effect. for backwards compatibility only.
645 sub parse_command_line {
646 my ($opt_backlink,$opt_cachedir,$opt_css,$opt_flush,$opt_header,$opt_help,
647 $opt_htmldir,$opt_htmlroot,$opt_index,$opt_infile,$opt_libpods,
648 $opt_netscape,$opt_outfile,$opt_podpath,$opt_podroot,$opt_quiet,
649 $opt_recurse,$opt_title,$opt_verbose);
651 unshift @ARGV, split ' ', $Config{pod2html} if $Config{pod2html};
652 my $result = GetOptions(
653 'backlink=s' => \$opt_backlink,
654 'cachedir=s' => \$opt_cachedir,
655 'css=s' => \$opt_css,
656 'flush' => \$opt_flush,
657 'header!' => \$opt_header,
658 'help' => \$opt_help,
659 'htmldir=s' => \$opt_htmldir,
660 'htmlroot=s' => \$opt_htmlroot,
661 'index!' => \$opt_index,
662 'infile=s' => \$opt_infile,
663 'libpods=s' => \$opt_libpods,
664 'netscape!' => \$opt_netscape,
665 'outfile=s' => \$opt_outfile,
666 'podpath=s' => \$opt_podpath,
667 'podroot=s' => \$opt_podroot,
668 'quiet!' => \$opt_quiet,
669 'recurse!' => \$opt_recurse,
670 'title=s' => \$opt_title,
671 'verbose!' => \$opt_verbose,
673 usage("-", "invalid parameters") if not $result;
675 usage("-") if defined $opt_help; # see if the user asked for help
676 $opt_help = ""; # just to make -w shut-up.
678 @podpath = split(":", $opt_podpath) if defined $opt_podpath;
679 @libpods = split(":", $opt_libpods) if defined $opt_libpods;
681 $backlink = $opt_backlink if defined $opt_backlink;
682 $cachedir = $opt_cachedir if defined $opt_cachedir;
683 $css = $opt_css if defined $opt_css;
684 $header = $opt_header if defined $opt_header;
685 $htmldir = $opt_htmldir if defined $opt_htmldir;
686 $htmlroot = $opt_htmlroot if defined $opt_htmlroot;
687 $doindex = $opt_index if defined $opt_index;
688 $podfile = $opt_infile if defined $opt_infile;
689 $htmlfile = $opt_outfile if defined $opt_outfile;
690 $podroot = $opt_podroot if defined $opt_podroot;
691 $quiet = $opt_quiet if defined $opt_quiet;
692 $recurse = $opt_recurse if defined $opt_recurse;
693 $title = $opt_title if defined $opt_title;
694 $verbose = $opt_verbose if defined $opt_verbose;
696 warn "Flushing item and directory caches\n"
697 if $opt_verbose && defined $opt_flush;
698 $dircache = "$cachedir/pod2htmd$cache_ext";
699 $itemcache = "$cachedir/pod2htmi$cache_ext";
700 unlink($dircache, $itemcache) if defined $opt_flush;
707 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
708 my @cache_key_args = @_;
710 # A first-level cache:
711 # Don't bother reading the cache files if they still apply
712 # and haven't changed since we last read them.
714 my $this_cache_key = cache_key(@cache_key_args);
716 return if $saved_cache_key and $this_cache_key eq $saved_cache_key;
718 # load the cache of %pages and %items if possible. $tests will be
719 # non-zero if successful.
721 if (-f $dircache && -f $itemcache) {
722 warn "scanning for item cache\n" if $verbose;
723 $tests = load_cache($dircache, $itemcache, $podpath, $podroot);
726 # if we didn't succeed in loading the cache then we must (re)build
729 warn "scanning directories in pod-path\n" if $verbose;
730 scan_podpath($podroot, $recurse, 0);
732 $saved_cache_key = cache_key(@cache_key_args);
736 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
737 return join('!', $dircache, $itemcache, $recurse,
738 @$podpath, $podroot, stat($dircache), stat($itemcache));
742 # load_cache - tries to find if the caches stored in $dircache and $itemcache
743 # are valid caches of %pages and %items. if they are valid then it loads
744 # them and returns a non-zero value.
747 my($dircache, $itemcache, $podpath, $podroot) = @_;
753 open(CACHE, "<$itemcache") ||
754 die "$0: error opening $itemcache for reading: $!\n";
757 # is it the same podpath?
760 $tests++ if (join(":", @$podpath) eq $_);
762 # is it the same podroot?
765 $tests++ if ($podroot eq $_);
767 # load the cache if its good
773 warn "loading item cache\n" if $verbose;
780 warn "scanning for directory cache\n" if $verbose;
781 open(CACHE, "<$dircache") ||
782 die "$0: error opening $dircache for reading: $!\n";
786 # is it the same podpath?
789 $tests++ if (join(":", @$podpath) eq $_);
791 # is it the same podroot?
794 $tests++ if ($podroot eq $_);
796 # load the cache if its good
802 warn "loading directory cache\n" if $verbose;
814 # scan_podpath - scans the directories specified in @podpath for directories,
815 # .pod files, and .pm files. it also scans the pod files specified in
816 # @libpods for =item directives.
819 my($podroot, $recurse, $append) = @_;
821 my($libpod, $dirname, $pod, @files, @poddata);
828 # scan each directory listed in @podpath
831 || die "$0: error changing to directory $podroot: $!\n";
832 foreach $dir (@podpath) {
833 scan_dir($dir, $recurse);
836 # scan the pods listed in @libpods for =item directives
837 foreach $libpod (@libpods) {
838 # if the page isn't defined then we won't know where to find it
840 next unless defined $pages{$libpod} && $pages{$libpod};
842 # if there is a directory then use the .pod and .pm files within it.
843 # NOTE: Only finds the first so-named directory in the tree.
844 # if ($pages{$libpod} =~ /([^:]*[^(\.pod|\.pm)]):/) {
845 if ($pages{$libpod} =~ /([^:]*(?<!\.pod)(?<!\.pm)):/) {
846 # find all the .pod and .pm files within the directory
848 opendir(DIR, $dirname) ||
849 die "$0: error opening directory $dirname: $!\n";
850 @files = grep(/(\.pod|\.pm)\z/ && ! -d $_, readdir(DIR));
853 # scan each .pod and .pm file for =item directives
854 foreach $pod (@files) {
855 open(POD, "<$dirname/$pod") ||
856 die "$0: error opening $dirname/$pod for input: $!\n";
859 clean_data( \@poddata );
861 scan_items( \%items, "$dirname/$pod", @poddata);
864 # use the names of files as =item directives too.
865 ### Don't think this should be done this way - confuses issues.(WL)
866 ### foreach $pod (@files) {
867 ### $pod =~ /^(.*)(\.pod|\.pm)$/;
868 ### $items{$1} = "$dirname/$1.html" if $1;
870 } elsif ($pages{$libpod} =~ /([^:]*\.pod):/ ||
871 $pages{$libpod} =~ /([^:]*\.pm):/) {
872 # scan the .pod or .pm file for =item directives
874 open(POD, "<$pod") ||
875 die "$0: error opening $pod for input: $!\n";
878 clean_data( \@poddata );
880 scan_items( \%items, "$pod", @poddata);
882 warn "$0: shouldn't be here (line ".__LINE__."\n";
885 @poddata = (); # clean-up a bit
888 || die "$0: error changing to directory $pwd: $!\n";
890 # cache the item list for later use
891 warn "caching items for later use\n" if $verbose;
892 open(CACHE, ">$itemcache") ||
893 die "$0: error open $itemcache for writing: $!\n";
895 print CACHE join(":", @podpath) . "\n$podroot\n";
896 foreach my $key (keys %items) {
897 print CACHE "$key $items{$key}\n";
902 # cache the directory list for later use
903 warn "caching directories for later use\n" if $verbose;
904 open(CACHE, ">$dircache") ||
905 die "$0: error open $dircache for writing: $!\n";
907 print CACHE join(":", @podpath) . "\n$podroot\n";
908 foreach my $key (keys %pages) {
909 print CACHE "$key $pages{$key}\n";
916 # scan_dir - scans the directory specified in $dir for subdirectories, .pod
917 # files, and .pm files. notes those that it finds. this information will
918 # be used later in order to figure out where the pages specified in L<>
919 # links are on the filesystem.
922 my($dir, $recurse) = @_;
923 my($t, @subdirs, @pods, $pod, $dirname, @dirs);
929 opendir(DIR, $dir) ||
930 die "$0: error opening directory $dir: $!\n";
931 while (defined($_ = readdir(DIR))) {
932 if (-d "$dir/$_" && $_ ne "." && $_ ne "..") { # directory
933 $pages{$_} = "" unless defined $pages{$_};
934 $pages{$_} .= "$dir/$_:";
936 } elsif (/\.pod\z/) { # .pod
938 $pages{$_} = "" unless defined $pages{$_};
939 $pages{$_} .= "$dir/$_.pod:";
940 push(@pods, "$dir/$_.pod");
941 } elsif (/\.html\z/) { # .html
943 $pages{$_} = "" unless defined $pages{$_};
944 $pages{$_} .= "$dir/$_.pod:";
945 } elsif (/\.pm\z/) { # .pm
947 $pages{$_} = "" unless defined $pages{$_};
948 $pages{$_} .= "$dir/$_.pm:";
949 push(@pods, "$dir/$_.pm");
954 # recurse on the subdirectories if necessary
956 foreach my $subdir (@subdirs) {
957 scan_dir("$dir/$subdir", $recurse);
963 # scan_headings - scan a pod file for head[1-6] tags, note the tags, and
967 my($sections, @data) = @_;
968 my($tag, $which_head, $otitle, $listdepth, $index);
970 # here we need local $ignore = 0;
971 # unfortunately, we can't have it, because $ignore is lexical
977 # scan for =head directives, note their name, and build an index
978 # pointing to each of them.
979 foreach my $line (@data) {
980 if ($line =~ /^=(head)([1-6])\s+(.*)/) {
981 ($tag, $which_head, $otitle) = ($1,$2,$3);
983 my $title = depod( $otitle );
984 my $name = anchorify( $title );
985 $$sections{$name} = 1;
986 $title = process_text( \$otitle );
988 while ($which_head != $listdepth) {
989 if ($which_head > $listdepth) {
990 $index .= "\n" . ("\t" x $listdepth) . "<ul>\n";
992 } elsif ($which_head < $listdepth) {
994 $index .= "\n" . ("\t" x $listdepth) . "</ul>\n";
998 $index .= "\n" . ("\t" x $listdepth) . "<li>" .
999 "<a href=\"#" . $name . "\">" .
1000 $title . "</a></li>";
1004 # finish off the lists
1005 while ($listdepth--) {
1006 $index .= "\n" . ("\t" x $listdepth) . "</ul>\n";
1009 # get rid of bogus lists
1010 $index =~ s,\t*<ul>\s*</ul>\n,,g;
1012 $ignore = 1; # restore old value;
1018 # scan_items - scans the pod specified by $pod for =item directives. we
1019 # will use this information later on in resolving C<> links.
1022 my( $itemref, $pod, @poddata ) = @_;
1026 $pod =~ s/\.pod\z//;
1027 $pod .= ".html" if $pod;
1029 foreach $i (0..$#poddata) {
1030 my $txt = depod( $poddata[$i] );
1032 # figure out what kind of item it is.
1033 # Build string for referencing this item.
1034 if ( $txt =~ /\A=item\s+\*\s*(.*)\Z/s ) { # bullet
1037 } elsif( $txt =~ /\A=item\s+(?>\d+\.?)\s*(.*)\Z/s ) { # numbered list
1039 } elsif( $txt =~ /\A=item\s+(.*)\Z/s ) { # plain item
1044 my $fid = fragment_id( $item );
1045 $$itemref{$fid} = "$pod" if $fid;
1050 # process_head - convert a pod head[1-6] tag and convert it to HTML format.
1053 my($tag, $heading, $hasindex) = @_;
1055 # figure out the level of the =head
1056 $tag =~ /head([1-6])/;
1060 warn "$0: $podfile: unterminated list at =head in paragraph $paragraph. ignoring.\n";
1061 while( $listlevel ){
1067 if( $level == 1 && ! $top ){
1068 print HTML "<a href=\"#__index__\"><small>$backlink</small></a>\n"
1069 if $hasindex and $backlink;
1070 print HTML "</p>\n<hr />\n"
1072 print HTML "</p>\n";
1075 my $name = anchorify( depod( $heading ) );
1076 my $convert = process_text( \$heading );
1077 print HTML "<h$level><a name=\"$name\">$convert</a></h$level>\n";
1082 # emit_item_tag - print an =item's text
1083 # Note: The global $EmittedItem is used for inhibiting self-references.
1087 sub emit_item_tag($$$){
1088 my( $otext, $text, $compact ) = @_;
1089 my $item = fragment_id( $text );
1091 $EmittedItem = $item;
1092 ### print STDERR "emit_item_tag=$item ($text)\n";
1094 print HTML '<strong>';
1095 if ($items_named{$item}++) {
1096 print HTML process_text( \$otext );
1098 my $name = 'item_' . $item;
1099 $name = anchorify($name);
1100 print HTML qq{<a name="$name">}, process_text( \$otext ), '</a>';
1102 print HTML "</strong><br />\n";
1103 undef( $EmittedItem );
1108 if( $items_seen[$listlevel]++ == 0 ){
1109 push( @listend, "</$tag>" );
1110 print HTML "<$tag>\n";
1112 my $emitted = $tag eq 'dl' ? 'dt' : 'li';
1113 print HTML "<$emitted>";
1118 # process_item - convert a pod item tag and convert it to HTML format.
1122 my $need_dd = 0; # set to 1 if we need a <dd></dd> after an item
1124 # lots of documents start a list without doing an =over. this is
1125 # bad! but, the proper thing to do seems to be to just assume
1126 # they did do an =over. so warn them once and then continue.
1127 if( $listlevel == 0 ){
1128 warn "$0: $podfile: unexpected =item directive in paragraph $paragraph. ignoring.\n";
1132 # formatting: insert a paragraph if preceding item has >1 paragraph
1134 print HTML "<p></p>\n";
1138 # remove formatting instructions from the text
1139 my $text = depod( $otext );
1141 my $emitted; # the tag actually emitted, used for closing
1143 # all the list variants:
1144 if( $text =~ /\A\*/ ){ # bullet
1145 $emitted = emit_li( 'ul' );
1146 if ($text =~ /\A\*\s+(.+)\Z/s ) { # with additional text
1148 $otext =~ s/\A\*\s+//;
1149 emit_item_tag( $otext, $tag, 1 );
1152 } elsif( $text =~ /\A\d+/ ){ # numbered list
1153 $emitted = emit_li( 'ol' );
1154 if ($text =~ /\A(?>\d+\.?)\s*(.+)\Z/s ) { # with additional text
1156 $otext =~ s/\A\d+\.?\s*//;
1157 emit_item_tag( $otext, $tag, 1 );
1160 } else { # definition list
1161 $emitted = emit_li( 'dl' );
1162 if ($text =~ /\A(.+)\Z/s ){ # should have text
1163 emit_item_tag( $otext, $text, 1 );
1167 print HTML "</$emitted>" if $emitted;
1173 # process_over - process a pod over tag and start a corresponding HTML list.
1178 push( @items_seen, 0 );
1183 # process_back - process a pod back tag and convert it to HTML format.
1186 if( $listlevel == 0 ){
1187 warn "$0: $podfile: unexpected =back directive in paragraph $paragraph. ignoring.\n";
1191 # close off the list. note, I check to see if $listend[$listlevel] is
1192 # defined because an =item directive may have never appeared and thus
1193 # $listend[$listlevel] may have never been initialized.
1195 if( defined $listend[$listlevel] ){
1196 print HTML '<p></p>' if $after_lpar;
1197 print HTML $listend[$listlevel];
1203 # clean up item count
1208 # process_cut - process a pod cut tag, thus start ignoring pod directives.
1215 # process_pod - process a pod tag, thus stop ignoring pod directives
1216 # until we see a corresponding cut.
1219 # no need to set $ignore to 0 cause the main loop did it
1223 # process_for - process a =for pod tag. if it's for html, spit
1224 # it out verbatim, if illustration, center it, otherwise ignore it.
1227 my($whom, $text) = @_;
1228 if ( $whom =~ /^(pod2)?html$/i) {
1230 } elsif ($whom =~ /^illustration$/i) {
1231 1 while chomp $text;
1232 for my $ext (qw[.png .gif .jpeg .jpg .tga .pcl .bmp]) {
1233 $text .= $ext, last if -r "$text$ext";
1235 print HTML qq{<p align="center"><img src="$text" alt="$text illustration" /></p>};
1240 # process_begin - process a =begin pod tag. this pushes
1241 # whom we're beginning on the begin stack. if there's a
1242 # begin stack, we only print if it us.
1245 my($whom, $text) = @_;
1247 push (@begin_stack, $whom);
1248 if ( $whom =~ /^(pod2)?html$/) {
1249 print HTML $text if $text;
1254 # process_end - process a =end pod tag. pop the
1255 # begin stack. die if we're mismatched.
1258 my($whom, $text) = @_;
1260 if ($begin_stack[-1] ne $whom ) {
1261 die "Unmatched begin/end at chunk $paragraph\n"
1263 pop( @begin_stack );
1267 # process_pre - indented paragraph, made into <pre></pre>
1276 # insert spaces in place of tabs
1279 1 while $line =~ s/(\t+)/' ' x ((length($1) * 8) - $-[0] % 8)/e;
1283 # convert some special chars to HTML escapes
1284 $rest = html_escape($rest);
1286 # try and create links for all occurrences of perl.* within
1287 # the preformatted text.
1291 if ( defined $pages{$2} ){ # is a link
1292 qq($1<a href="$htmlroot/$pages{$2}">$2</a>);
1293 } elsif (defined $pages{dosify($2)}) { # is a link
1294 qq($1<a href="$htmlroot/$pages{dosify($2)}">$2</a>);
1300 (<a\ href="?) ([^>:]*:)? ([^>:]*) \.pod: ([^>:]*:)?
1303 if ( $htmlfileurl ne '' ){
1304 # Here, we take advantage of the knowledge
1305 # that $htmlfileurl ne '' implies $htmlroot eq ''.
1306 # Since $htmlroot eq '', we need to prepend $htmldir
1307 # on the fron of the link to get the absolute path
1308 # of the link's target. We check for a leading '/'
1309 # to avoid corrupting links that are #, file:, etc.
1311 $old_url = "$htmldir$old_url" if $old_url =~ m{^\/};
1312 $url = relativize_url( "$old_url.html", $htmlfileurl );
1319 # Look for embedded URLs and make them into links. We don't
1320 # relativize them since they are best left as the author intended.
1322 my $urls = '(' . join ('|', qw{
1335 my $gunk = '/#~:.?+=&%@!\-';
1336 my $punc = '.:!?\-;';
1337 my $any = "${ltrs}${gunk}${punc}";
1340 \b # start at word boundary
1342 $urls : # need resource and a colon
1343 (?!:) # Ignore File::, among others.
1344 [$any] +? # followed by one or more of any valid
1345 # character, but be conservative and
1346 # take only what you need to....
1349 " > # maybe pre-quoted '<a href="...">'
1351 [$punc]* # 0 or more punctuation
1353 [^$any] # by a non-url char
1355 $ # end of the string
1358 $ # then end of the string
1360 }{<a href="$1">$1</a>}igox;
1362 # text should be as it is (verbatim)
1368 # pure text processing
1370 # pure_text/inIS_text: differ with respect to automatic C<> recognition.
1371 # we don't want this to happen within IS
1375 process_puretext( $text, \$ptQuote, 1 );
1380 process_puretext( $text, \$ptQuote, 0 );
1384 # process_puretext - process pure text (without pod-escapes) converting
1385 # double-quotes and handling implicit C<> links.
1387 sub process_puretext {
1388 my($text, $quote, $notinIS) = @_;
1390 ## Guessing at func() or [$@%&]*var references in plain text is destined
1391 ## to produce some strange looking ref's. uncomment to disable:
1394 my(@words, $lead, $trail);
1396 # convert double-quotes to single-quotes
1397 if( $$quote && $text =~ s/"/''/s ){
1400 while ($text =~ s/"([^"]*)"/``$1''/sg) {};
1401 $$quote = 1 if $text =~ s/"/``/s;
1403 # keep track of leading and trailing white-space
1404 $lead = ($text =~ s/\A(\s+)//s ? $1 : "");
1405 $trail = ($text =~ s/(\s+)\Z//s ? $1 : "");
1407 # split at space/non-space boundaries
1408 @words = split( /(?<=\s)(?=\S)|(?<=\S)(?=\s)/, $text );
1410 # process each word individually
1411 foreach my $word (@words) {
1413 next if $word =~ /^\s*$/;
1414 # see if we can infer a link
1415 if( $notinIS && $word =~ /^(\w+)\((.*)\)$/ ) {
1416 # has parenthesis so should have been a C<> ref
1417 ## try for a pagename (perlXXX(1))?
1418 my( $func, $args ) = ( $1, $2 );
1419 if( $args =~ /^\d+$/ ){
1420 my $url = page_sect( $word, '' );
1422 $word = "<a href=\"$url\">the $word manpage</a>";
1426 ## try function name for a link, append tt'ed argument list
1427 $word = emit_C( $func, '', "($args)");
1429 #### disabled. either all (including $\W, $\w+{.*} etc.) or nothing.
1430 ## } elsif( $notinIS && $word =~ /^[\$\@%&*]+\w+$/) {
1431 ## # perl variables, should be a C<> ref
1432 ## $word = emit_C( $word );
1434 } elsif ($word =~ m,^\w+://\w,) {
1436 # Don't relativize it: leave it as the author intended
1437 $word = qq(<a href="$word">$word</a>);
1438 } elsif ($word =~ /[\w.-]+\@[\w-]+\.\w/) {
1439 # looks like an e-mail address
1440 my ($w1, $w2, $w3) = ("", $word, "");
1441 ($w1, $w2, $w3) = ("(", $1, ")$2") if $word =~ /^\((.*?)\)(,?)/;
1442 ($w1, $w2, $w3) = ("<", $1, ">$2") if $word =~ /^<(.*?)>(,?)/;
1443 $word = qq($w1<a href="mailto:$w2">$w2</a>$w3);
1445 $word = html_escape($word) if $word =~ /["&<>]/;
1449 # put everything back together
1450 return $lead . join( '', @words ) . $trail;
1455 # process_text - handles plaintext that appears in the input pod file.
1456 # there may be pod commands embedded within the text so those must be
1457 # converted to html commands.
1460 sub process_text1($$;$$);
1461 sub pattern ($) { $_[0] ? '[^\S\n]+'.('>' x ($_[0] + 1)) : '>' }
1462 sub closing ($) { local($_) = shift; (defined && s/\s+$//) ? length : 0 }
1467 my $res = process_text1( 0, $tref );
1471 sub process_text1($$;$$){
1472 my( $lev, $rstr, $func, $closing ) = @_;
1475 unless (defined $func) {
1481 # B<text> - boldface
1482 $res = '<strong>' . process_text1( $lev, $rstr ) . '</strong>';
1484 } elsif( $func eq 'C' ){
1485 # C<code> - can be a ref or <code></code>
1486 # need to extract text
1487 my $par = go_ahead( $rstr, 'C', $closing );
1489 ## clean-up of the link target
1490 my $text = depod( $par );
1492 ### my $x = $par =~ /[BI]</ ? 'yes' : 'no' ;
1493 ### print STDERR "-->call emit_C($par) lev=$lev, par with BI=$x\n";
1495 $res = emit_C( $text, $lev > 1 || ($par =~ /[BI]</) );
1497 } elsif( $func eq 'E' ){
1498 # E<x> - convert to character
1499 $$rstr =~ s/^([^>]*)>//;
1501 $escape =~ s/^(\d+|X[\dA-F]+)$/#$1/i;
1504 } elsif( $func eq 'F' ){
1505 # F<filename> - italizice
1506 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1508 } elsif( $func eq 'I' ){
1509 # I<text> - italizice
1510 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1512 } elsif( $func eq 'L' ){
1514 ## L<text|cross-ref> => produce text, use cross-ref for linking
1515 ## L<cross-ref> => make text from cross-ref
1516 ## need to extract text
1517 my $par = go_ahead( $rstr, 'L', $closing );
1519 # some L<>'s that shouldn't be:
1520 # a) full-blown URL's are emitted as-is
1521 if( $par =~ m{^\w+://}s ){
1522 return make_URL_href( $par );
1524 # b) C<...> is stripped and treated as C<>
1525 if( $par =~ /^C<(.*)>$/ ){
1526 my $text = depod( $1 );
1527 return emit_C( $text, $lev > 1 || ($par =~ /[BI]</) );
1530 # analyze the contents
1531 $par =~ s/\n/ /g; # undo word-wrapped tags
1534 if( $par =~ s{^([^|]+)\|}{} ){
1538 # make sure sections start with a /
1541 my( $page, $section, $ident );
1543 # check for link patterns
1544 if( $par =~ m{^([^/]+?)/(?!")(.*?)$} ){ # name/ident
1545 # we've got a name/ident (no quotes)
1546 ( $page, $ident ) = ( $1, $2 );
1547 ### print STDERR "--> L<$par> to page $page, ident $ident\n";
1549 } elsif( $par =~ m{^(.*?)/"?(.*?)"?$} ){ # [name]/"section"
1550 # even though this should be a "section", we go for ident first
1551 ( $page, $ident ) = ( $1, $2 );
1552 ### print STDERR "--> L<$par> to page $page, section $section\n";
1554 } elsif( $par =~ /\s/ ){ # this must be a section with missing quotes
1555 ( $page, $section ) = ( '', $par );
1556 ### print STDERR "--> L<$par> to void page, section $section\n";
1559 ( $page, $section ) = ( $par, '' );
1560 ### print STDERR "--> L<$par> to page $par, void section\n";
1563 # now, either $section or $ident is defined. the convoluted logic
1564 # below tries to resolve L<> according to what the user specified.
1565 # failing this, we try to find the next best thing...
1566 my( $url, $ltext, $fid );
1569 if( defined $ident ){
1570 ## try to resolve $ident as an item
1571 ( $url, $fid ) = coderef( $page, $ident );
1573 if( ! defined( $linktext ) ){
1575 $linktext .= " in " if $ident && $page;
1576 $linktext .= "the $page manpage" if $page;
1578 ### print STDERR "got coderef url=$url\n";
1581 ## no luck: go for a section (auto-quoting!)
1584 ## now go for a section
1585 my $htmlsection = htmlify( $section );
1586 $url = page_sect( $page, $htmlsection );
1588 if( ! defined( $linktext ) ){
1589 $linktext = $section;
1590 $linktext .= " in " if $section && $page;
1591 $linktext .= "the $page manpage" if $page;
1593 ### print STDERR "got page/section url=$url\n";
1596 ## no luck: go for an ident
1603 ( $url, $fid ) = coderef( $page, $ident );
1605 if( ! defined( $linktext ) ){
1607 $linktext .= " in " if $ident && $page;
1608 $linktext .= "the $page manpage" if $page;
1610 ### print STDERR "got section=>coderef url=$url\n";
1614 # warning; show some text.
1615 $linktext = $opar unless defined $linktext;
1616 warn "$0: $podfile: cannot resolve L<$opar> in paragraph $paragraph.\n";
1619 # now we have a URL or just plain code
1620 $$rstr = $linktext . '>' . $$rstr;
1621 if( defined( $url ) ){
1622 $res = "<a href=\"$url\">" . process_text1( $lev, $rstr ) . '</a>';
1624 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1627 } elsif( $func eq 'S' ){
1628 # S<text> - non-breaking spaces
1629 $res = process_text1( $lev, $rstr );
1630 $res =~ s/ / /g;
1632 } elsif( $func eq 'X' ){
1634 $$rstr =~ s/^[^>]*>//;
1636 } elsif( $func eq 'Z' ){
1638 warn "$0: $podfile: invalid X<> in paragraph $paragraph.\n"
1639 unless $$rstr =~ s/^>//;
1642 my $term = pattern $closing;
1643 while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)//s ){
1644 # all others: either recurse into new function or
1645 # terminate at closing angle bracket(s)
1647 $pt .= $2 if !$3 && $lev == 1;
1648 $res .= $lev == 1 ? pure_text( $pt ) : inIS_text( $pt );
1649 return $res if !$3 && $lev > 1;
1651 $res .= process_text1( $lev, $rstr, $3, closing $4 );
1655 $res .= pure_text( $$rstr );
1657 warn "$0: $podfile: undelimited $func<> in paragraph $paragraph.\n";
1664 # go_ahead: extract text of an IS (can be nested)
1667 my( $rstr, $func, $closing ) = @_;
1669 my @closing = ($closing);
1671 s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|@{[pattern $closing[0]]})//s ){
1675 return $res unless @closing;
1677 unshift @closing, closing $4;
1681 warn "$0: $podfile: undelimited $func<> in paragraph $paragraph.\n";
1686 # emit_C - output result of C<text>
1687 # $text is the depod-ed text
1690 my( $text, $nocode, $args ) = @_;
1691 $args = '' unless defined $args;
1693 my( $url, $fid ) = coderef( undef(), $text );
1695 # need HTML-safe text
1696 my $linktext = html_escape( "$text$args" );
1698 if( defined( $url ) &&
1699 (!defined( $EmittedItem ) || $EmittedItem ne $fid ) ){
1700 $res = "<a href=\"$url\"><code>$linktext</code></a>";
1701 } elsif( 0 && $nocode ){
1704 $res = "<code>$linktext</code>";
1710 # html_escape: make text safe for HTML
1714 $rest =~ s/&/&/g;
1715 $rest =~ s/</</g;
1716 $rest =~ s/>/>/g;
1717 $rest =~ s/"/"/g;
1718 # ' is only in XHTML, not HTML4. Be conservative
1719 #$rest =~ s/'/'/g;
1725 # dosify - convert filenames to 8.3
1729 return lc($str) if $^O eq 'VMS'; # VMS just needs casing
1732 $str =~ s/(\.\w+)/substr ($1,0,4)/ge;
1733 $str =~ s/(\w+)/substr ($1,0,8)/ge;
1739 # page_sect - make a URL from the text of a L<>
1742 my( $page, $section ) = @_;
1743 my( $linktext, $page83, $link); # work strings
1745 # check if we know that this is a section in this page
1746 if (!defined $pages{$page} && defined $sections{$page}) {
1749 ### print STDERR "reset page='', section=$section\n";
1752 $page83=dosify($page);
1753 $page=$page83 if (defined $pages{$page83});
1755 $link = "#" . anchorify( $section );
1756 } elsif ( $page =~ /::/ ) {
1758 # Search page cache for an entry keyed under the html page name,
1759 # then look to see what directory that page might be in. NOTE:
1760 # this will only find one page. A better solution might be to produce
1761 # an intermediate page that is an index to all such pages.
1762 my $page_name = $page ;
1763 $page_name =~ s,^.*/,,s ;
1764 if ( defined( $pages{ $page_name } ) &&
1765 $pages{ $page_name } =~ /([^:]*$page)\.(?:pod|pm):/
1770 # NOTE: This branch assumes that all A::B pages are located in
1771 # $htmlroot/A/B.html . This is often incorrect, since they are
1772 # often in $htmlroot/lib/A/B.html or such like. Perhaps we could
1773 # analyze the contents of %pages and figure out where any
1774 # cousins of A::B are, then assume that. So, if A::B isn't found,
1775 # but A::C is found in lib/A/C.pm, then A::B is assumed to be in
1776 # lib/A/B.pm. This is also limited, but it's an improvement.
1777 # Maybe a hints file so that the links point to the correct places
1781 $link = "$htmlroot/$page.html";
1782 $link .= "#" . anchorify( $section ) if ($section);
1783 } elsif (!defined $pages{$page}) {
1786 $section = anchorify( $section ) if $section ne "";
1787 ### print STDERR "...section=$section\n";
1789 # if there is a directory by the name of the page, then assume that an
1790 # appropriate section will exist in the subdirectory
1791 # if ($section ne "" && $pages{$page} =~ /([^:]*[^(\.pod|\.pm)]):/) {
1792 if ($section ne "" && $pages{$page} =~ /([^:]*(?<!\.pod)(?<!\.pm)):/) {
1793 $link = "$htmlroot/$1/$section.html";
1794 ### print STDERR "...link=$link\n";
1796 # since there is no directory by the name of the page, the section will
1797 # have to exist within a .html of the same name. thus, make sure there
1798 # is a .pod or .pm that might become that .html
1800 $section = "#$section" if $section;
1801 ### print STDERR "...section=$section\n";
1803 # check if there is a .pod with the page name
1804 if ($pages{$page} =~ /([^:]*)\.pod:/) {
1805 $link = "$htmlroot/$1.html$section";
1806 } elsif ($pages{$page} =~ /([^:]*)\.pm:/) {
1807 $link = "$htmlroot/$1.html$section";
1815 # Here, we take advantage of the knowledge that $htmlfileurl ne ''
1816 # implies $htmlroot eq ''. This means that the link in question
1817 # needs a prefix of $htmldir if it begins with '/'. The test for
1818 # the initial '/' is done to avoid '#'-only links, and to allow
1819 # for other kinds of links, like file:, ftp:, etc.
1821 if ( $htmlfileurl ne '' ) {
1822 $link = "$htmldir$link" if $link =~ m{^/}s;
1823 $url = relativize_url( $link, $htmlfileurl );
1824 # print( " b: [$link,$htmlfileurl,$url]\n" );
1837 # relativize_url - convert an absolute URL to one relative to a base URL.
1838 # Assumes both end in a filename.
1840 sub relativize_url {
1841 my ($dest,$source) = @_ ;
1843 my ($dest_volume,$dest_directory,$dest_file) =
1844 File::Spec::Unix->splitpath( $dest ) ;
1845 $dest = File::Spec::Unix->catpath( $dest_volume, $dest_directory, '' ) ;
1847 my ($source_volume,$source_directory,$source_file) =
1848 File::Spec::Unix->splitpath( $source ) ;
1849 $source = File::Spec::Unix->catpath( $source_volume, $source_directory, '' ) ;
1852 if ( $dest ne '' ) {
1853 $rel_path = File::Spec::Unix->abs2rel( $dest, $source ) ;
1856 if ( $rel_path ne '' &&
1857 substr( $rel_path, -1 ) ne '/' &&
1858 substr( $dest_file, 0, 1 ) ne '#'
1860 $rel_path .= "/$dest_file" ;
1863 $rel_path .= "$dest_file" ;
1871 # coderef - make URL from the text of a C<>
1874 my( $page, $item ) = @_;
1877 my $fid = fragment_id( $item );
1878 if( defined( $page ) ){
1879 # we have been given a $page...
1882 # Do we take it? Item could be a section!
1883 my $base = $items{$fid} || "";
1884 $base =~ s{[^/]*/}{};
1885 if( $base ne "$page.html" ){
1886 ### print STDERR "coderef( $page, $item ): items{$fid} = $items{$fid} = $base => discard page!\n";
1891 # no page - local items precede cached items
1892 if( defined( $fid ) ){
1893 if( exists $local_items{$fid} ){
1894 $page = $local_items{$fid};
1896 $page = $items{$fid};
1901 # if there was a pod file that we found earlier with an appropriate
1902 # =item directive, then create a link to that page.
1903 if( defined $page ){
1905 if( exists $pages{$page} and $pages{$page} =~ /([^:.]*)\.[^:]*:/){
1906 $page = $1 . '.html';
1908 my $link = "$htmlroot/$page#item_" . anchorify($fid);
1910 # Here, we take advantage of the knowledge that $htmlfileurl
1911 # ne '' implies $htmlroot eq ''.
1912 if ( $htmlfileurl ne '' ) {
1913 $link = "$htmldir$link" ;
1914 $url = relativize_url( $link, $htmlfileurl ) ;
1919 $url = "#item_" . anchorify($fid);
1922 confess "url has space: $url" if $url =~ /"[^"]*\s[^"]*"/;
1924 return( $url, $fid );
1930 # Adapted from Nick Ing-Simmons' PodToHtml package.
1932 my $source_file = shift ;
1933 my $destination_file = shift;
1935 my $source = URI::file->new_abs($source_file);
1936 my $uo = URI::file->new($destination_file,$source)->abs;
1937 return $uo->rel->as_string;
1942 # finish_list - finish off any pending HTML lists. this should be called
1943 # after the entire pod file has been read and converted.
1946 while ($listlevel > 0) {
1947 print HTML "</dl>\n";
1953 # htmlify - converts a pod section specification to a suitable section
1954 # specification for HTML. Note that we keep spaces and special characters
1955 # except ", ? (Netscape problem) and the hyphen (writer's problem...).
1959 $heading =~ s/(\s+)/ /g;
1960 $heading =~ s/\s+\Z//;
1961 $heading =~ s/\A\s+//;
1962 # The hyphen is a disgrace to the English language.
1963 $heading =~ s/[-"?]//g;
1964 $heading = lc( $heading );
1969 # similar to htmlify, but turns non-alphanumerics into underscores
1973 $anchor = htmlify($anchor);
1974 $anchor =~ s/\W/_/g;
1979 # depod - convert text by eliminating all interior sequences
1980 # Note: can be called with copy or modify semantics
1987 $E2c{amp} = '&'; # in Tk's pods
1995 ${$_[0]} = depod1( \$string );
2003 my( $rstr, $func, $closing ) = @_;
2005 return $res unless defined $$rstr;
2006 if( ! defined( $func ) ){
2007 # skip to next begin of an interior sequence
2008 while( $$rstr =~ s/\A(.*?)([BCEFILSXZ])<(<+[^\S\n]+)?// ){
2009 # recurse into its text
2010 $res .= $1 . depod1( $rstr, $2, closing $3);
2013 } elsif( $func eq 'E' ){
2014 # E<x> - convert to character
2015 $$rstr =~ s/^([^>]*)>//;
2016 $res .= $E2c{$1} || "";
2017 } elsif( $func eq 'X' ){
2019 $$rstr =~ s/^[^>]*>//;
2020 } elsif( $func eq 'Z' ){
2024 # all others: either recurse into new function or
2025 # terminate at closing angle bracket
2026 my $term = pattern $closing;
2027 while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)// ){
2030 $res .= depod1( $rstr, $3, closing $4 );
2032 ## If we're here and $2 ne '>': undelimited interior sequence.
2033 ## Ignored, as this is called without proper indication of where we are.
2034 ## Rely on process_text to produce diagnostics.
2040 # fragment_id - construct a fragment identifier from:
2042 # b) contents of C<...>
2047 $text =~ s/\s+\Z//s;
2049 # a method or function?
2050 return $1 if $text =~ /(\w+)\s*\(/;
2051 return $1 if $text =~ /->\s*(\w+)\s*\(?/;
2054 return $1 if $text =~ /^([$@%*]\S+)/;
2056 # some pattern matching operator?
2057 return $1 if $text =~ m|^(\w+/).*/\w*$|;
2059 # fancy stuff... like "do { }"
2060 return $1 if $text =~ m|^(\w+)\s*{.*}$|;
2062 # honour the perlfunc manpage: func [PAR[,[ ]PAR]...]
2063 # and some funnies with ... Module ...
2064 return $1 if $text =~ m{^([a-z\d]+)(\s+[A-Z\d,/& ]+)?$};
2065 return $1 if $text =~ m{^([a-z\d]+)\s+Module(\s+[A-Z\d,/& ]+)?$};
2068 $text =~ s/\s+/_/sg;
2070 defined( $hc[ord($1)] ) ? $hc[ord($1)]
2071 : ( $hc[ord($1)] = sprintf( "%%%02X", ord($1) ) ) }gxe;
2072 $text = substr( $text, 0, 50 );
2079 # make_URL_href - generate HTML href from URL
2080 # Special treatment for CGI queries.
2082 sub make_URL_href($){
2085 s{^(http:[-\w/#~:.+=&%@!]+)(\?.*)$}{<a href="$1$2">$1</a>}i ){
2086 $url = "<a href=\"$url\">$url</a>";