5 use vars qw($VERSION @ISA @EXPORT);
8 @EXPORT = qw(pod2html htmlify);
17 use locale; # make \w work right in non-ASCII lands
21 Pod::Html - module to convert pod files to HTML
30 Converts files from pod format (see L<perlpod>) to HTML format. It
31 can automatically generate indexes and cross-references, and it keeps
32 a cache of things it knows how to cross-reference.
36 Pod::Html takes the following arguments:
42 --backlink="Back to Top"
44 Adds "Back to Top" links in front of every C<head1> heading (except for
45 the first). By default, no backlinks are generated.
51 Creates the item and directory caches in the given directory.
57 Specify the URL of a cascading style sheet. Also disables all HTML/CSS
58 C<style> attributes that are output by default (to avoid conflicts).
64 Flushes the item and directory caches.
71 Creates header and footer blocks containing the text of the C<NAME>
72 section. By default, no headers are generated.
78 Displays the usage message.
84 Sets the directory in which the resulting HTML file is placed. This
85 is used to generate relative links to other files. Not passing this
86 causes all links to be absolute, since this is the value that tells
87 Pod::Html the root of the documentation tree.
93 Sets the base URL for the HTML files. When cross-references are made,
94 the HTML root is prepended to the URL.
101 Generate an index at the top of the HTML file. This is the default
108 Specify the pod file to convert. Input is taken from STDIN if no
113 --libpods=name:...:name
115 List of page names (eg, "perlfunc") which contain linkable C<=item>s.
122 B<Deprecated>, has no effect. For backwards compatibility only.
128 Specify the HTML file to create. Output goes to STDOUT if no outfile
133 --podpath=name:...:name
135 Specify which subdirectories of the podroot contain pod files whose
136 HTML converted forms can be linked to in cross references.
142 Specify the base directory for finding library pods.
149 Don't display I<mostly harmless> warning messages. These messages
150 will be displayed by default. But this is not the same as C<verbose>
158 Recurse into subdirectories specified in podpath (default behaviour).
164 Specify the title of the resulting HTML file.
171 Display progress messages. By default, they won't be displayed.
178 "--podpath=lib:ext:pod:vms",
179 "--podroot=/usr/src/perl",
180 "--htmlroot=/perl/nmanual",
181 "--libpods=perlfunc:perlguts:perlvar:perlrun:perlop",
184 "--outfile=/perl/nmanual/foo.html");
188 Uses C<$Config{pod2html}> to setup default options.
192 Tom Christiansen, E<lt>tchrist@perl.comE<gt>.
200 This program is distributed under the Artistic License.
204 my $cachedir = "."; # The directory to which item and directory
205 # caches will be written.
206 my $cache_ext = $^O eq 'VMS' ? ".tmp" : ".x~~";
207 my $dircache = "pod2htmd$cache_ext";
208 my $itemcache = "pod2htmi$cache_ext";
210 my @begin_stack = (); # begin/end stack
212 my @libpods = (); # files to search for links from C<> directives
213 my $htmlroot = "/"; # http-server base directory from which all
214 # relative paths in $podpath stem.
215 my $htmldir = ""; # The directory to which the html pages
216 # will (eventually) be written.
217 my $htmlfile = ""; # write to stdout by default
218 my $htmlfileurl = "" ; # The url that other files would use to
219 # refer to this file. This is only used
220 # to make relative urls that point to
222 my $podfile = ""; # read from stdin by default
223 my @podpath = (); # list of directories containing library pods.
224 my $podroot = File::Spec->curdir; # filesystem base directory from which all
225 # relative paths in $podpath stem.
226 my $css = ''; # Cascading style sheet
227 my $recurse = 1; # recurse on subdirectories in $podpath.
228 my $quiet = 0; # not quiet by default
229 my $verbose = 0; # not verbose by default
230 my $doindex = 1; # non-zero if we should generate an index
231 my $backlink = ''; # text for "back to top" links
232 my $listlevel = 0; # current list depth
233 my @listend = (); # the text to use to end the list.
234 my $after_lpar = 0; # set to true after a par in an =item
235 my $ignore = 1; # whether or not to format text. we don't
236 # format text until we hit our first pod
239 my %items_named = (); # for the multiples of the same item in perlfunc
241 my $title; # title to give the pod(s)
242 my $header = 0; # produce block header/footer
243 my $top = 1; # true if we are at the top of the doc. used
244 # to prevent the first <hr /> directive.
245 my $paragraph; # which paragraph we're processing (used
246 # for error messages)
247 my $ptQuote = 0; # status of double-quote conversion
248 my %pages = (); # associative array used to find the location
249 # of pages referenced by L<> links.
250 my %sections = (); # sections within this page
251 my %items = (); # associative array used to find the location
252 # of =item directives referenced by C<> links
253 my %local_items = (); # local items - avoid destruction of %items
254 my $Is83; # is dos with short filenames (8.3)
257 $dircache = "pod2htmd$cache_ext";
258 $itemcache = "pod2htmi$cache_ext";
260 @begin_stack = (); # begin/end stack
262 @libpods = (); # files to search for links from C<> directives
263 $htmlroot = "/"; # http-server base directory from which all
264 # relative paths in $podpath stem.
265 $htmldir = ""; # The directory to which the html pages
266 # will (eventually) be written.
267 $htmlfile = ""; # write to stdout by default
268 $podfile = ""; # read from stdin by default
269 @podpath = (); # list of directories containing library pods.
270 $podroot = File::Spec->curdir; # filesystem base directory from which all
271 # relative paths in $podpath stem.
272 $css = ''; # Cascading style sheet
273 $recurse = 1; # recurse on subdirectories in $podpath.
274 $quiet = 0; # not quiet by default
275 $verbose = 0; # not verbose by default
276 $doindex = 1; # non-zero if we should generate an index
277 $backlink = ''; # text for "back to top" links
278 $listlevel = 0; # current list depth
279 @listend = (); # the text to use to end the list.
280 $after_lpar = 0; # set to true after a par in an =item
281 $ignore = 1; # whether or not to format text. we don't
282 # format text until we hit our first pod
287 $header = 0; # produce block header/footer
288 $title = ''; # title to give the pod(s)
289 $top = 1; # true if we are at the top of the doc. used
290 # to prevent the first <hr /> directive.
291 $paragraph = ''; # which paragraph we're processing (used
292 # for error messages)
293 %sections = (); # sections within this page
295 # These are not reinitialised here but are kept as a cache.
296 # See get_cache and related cache management code.
297 #%pages = (); # associative array used to find the location
298 # of pages referenced by L<> links.
299 #%items = (); # associative array used to find the location
300 # of =item directives referenced by C<> links
306 # clean_data: global clean-up of pod data
310 for my $i ( 0..$#{$dataref} ) {
311 ${$dataref}[$i] =~ s/\s+\Z//;
313 # have a look for all-space lines
314 if( ${$dataref}[$i] =~ /^\s+$/m and $dataref->[$i] !~ /^\s/ ){
315 my @chunks = split( /^\s+$/m, ${$dataref}[$i] );
316 splice( @$dataref, $i, 1, @chunks );
329 $Is83 = 0 if (defined (&Dos::UseLFN) && Dos::UseLFN());
331 # cache of %pages and %items from last time we ran pod2html
333 #undef $opt_help if defined $opt_help;
335 # parse the command-line parameters
336 parse_command_line();
338 # escape the backlink argument (same goes for title but is done later...)
339 $backlink = html_escape($backlink) if defined $backlink;
341 # set some variables to their default values if necessary
343 unless (@ARGV && $ARGV[0]) {
344 $podfile = "-" unless $podfile; # stdin
345 open(POD, "<$podfile")
346 || die "$0: cannot open $podfile file for input: $!\n";
348 $podfile = $ARGV[0]; # XXX: might be more filenames
351 $htmlfile = "-" unless $htmlfile; # stdout
352 $htmlroot = "" if $htmlroot eq "/"; # so we don't get a //
353 $htmldir =~ s#/\z## ; # so we don't get a //
355 && defined( $htmldir )
357 && substr( $htmlfile, 0, length( $htmldir ) ) eq $htmldir
360 # Set the 'base' url for this file, so that we can use it
361 # as the location from which to calculate relative links
362 # to other files. If this is '', then absolute links will
363 # be used throughout.
364 $htmlfileurl= "$htmldir/" . substr( $htmlfile, length( $htmldir ) + 1);
367 # read the pod a paragraph at a time
368 warn "Scanning for sections in input file(s)\n" if $verbose;
372 clean_data( \@poddata );
374 # scan the pod for =head[1-6] directives and build an index
375 my $index = scan_headings(\%sections, @poddata);
378 warn "No headings in $podfile\n" if $verbose;
381 # open the output file
382 open(HTML, ">$htmlfile")
383 || die "$0: cannot open $htmlfile file for output: $!\n";
385 # put a title in the HTML file if one wasn't specified
388 for (my $i = 0; $i < @poddata; $i++) {
389 if ($poddata[$i] =~ /^=head1\s*NAME\b/m) {
390 for my $para ( @poddata[$i, $i+1] ) {
392 if ($title) = $para =~ /(\S+\s+-+.*\S)/s;
399 if (!$title and $podfile =~ /\.pod\z/) {
400 # probably a split pod so take first =head[12] as title
401 for (my $i = 0; $i < @poddata; $i++) {
402 last if ($title) = $poddata[$i] =~ /^=head[12]\s*(.*)/;
404 warn "adopted '$title' as title for $podfile\n"
405 if $verbose and $title;
408 $title =~ s/\s*\(.*\)//;
410 warn "$0: no title for $podfile" unless $quiet;
411 $podfile =~ /^(.*)(\.[^.\/]+)?\z/s;
412 $title = ($podfile eq "-" ? 'No Title' : $1);
413 warn "using $title" if $verbose;
415 $title = html_escape($title);
418 my $bodystyle = ' style="background-color: white"';
419 my $tdstyle = ' style="background-color: #cccccc"';
422 $csslink = qq(\n<link rel="stylesheet" href="$css" type="text/css" />);
423 $csslink =~ s,\\,/,g;
424 $csslink =~ s,(/.):,$1|,;
429 my $block = $header ? <<END_OF_BLOCK : '';
430 <table border="0" width="100%" cellspacing="0" cellpadding="3">
431 <tr><td class="block"$tdstyle valign="middle">
432 <big><strong><span class="block"> $title</span></strong></big>
437 print HTML <<END_OF_HEAD;
438 <?xml version="1.0" encoding="UTF-8"?>
439 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
440 <html xmlns="http://www.w3.org/1999/xhtml">
442 <title>$title</title>$csslink
443 <link rev="made" href="mailto:$Config{perladmin}" />
450 # load/reload/validate/cache %pages and %items
451 get_cache($dircache, $itemcache, \@podpath, $podroot, $recurse);
453 # scan the pod for =item directives
454 scan_items( \%local_items, "", @poddata);
456 # put an index at the top of the file. note, if $doindex is 0 we
457 # still generate an index, but surround it with an html comment.
458 # that way some other program can extract it if desired.
460 print HTML "<p><a name=\"__index__\"></a></p>\n";
461 print HTML "<!-- INDEX BEGIN -->\n";
462 print HTML "<!--\n" unless $doindex;
464 print HTML "-->\n" unless $doindex;
465 print HTML "<!-- INDEX END -->\n\n";
466 print HTML "<hr />\n" if $doindex and $index;
468 # now convert this file
469 my $after_item; # set to true after an =item
471 warn "Converting input file $podfile\n" if $verbose;
472 foreach my $i (0..$#poddata){
473 $ptQuote = 0; # status of quote conversion
477 if (/^(=.*)/s) { # is it a pod directive?
482 if (/^=begin\s+(\S+)\s*(.*)/si) {# =begin
483 process_begin($1, $2);
484 } elsif (/^=end\s+(\S+)\s*(.*)/si) {# =end
486 } elsif (/^=cut/) { # =cut
488 } elsif (/^=pod/) { # =pod
491 next if @begin_stack && $begin_stack[-1] ne 'html';
493 if (/^=(head[1-6])\s+(.*\S)/s) { # =head[1-6] heading
494 process_head( $1, $2, $doindex && $index );
495 } elsif (/^=item\s*(.*\S)?/sm) { # =item text
496 warn "$0: $podfile: =item without bullet, number or text"
497 . " in paragraph $paragraph.\n" if !defined($1) or $1 eq '';
498 $need_dd = process_item( $1 );
500 } elsif (/^=over\s*(.*)/) { # =over N
502 } elsif (/^=back/) { # =back
504 } elsif (/^=for\s+(\S+)\s*(.*)/si) {# =for
508 warn "$0: $podfile: unknown pod directive '$1' in "
509 . "paragraph $paragraph. ignoring.\n";
516 next if @begin_stack && $begin_stack[-1] ne 'html';
517 print HTML and next if @begin_stack && $begin_stack[-1] eq 'html';
518 print HTML "<dd>\n" if $need_dd;
520 if( $text =~ /\A\s+/ ){
521 process_pre( \$text );
522 print HTML "<pre>\n$text</pre>\n";
525 process_text( \$text );
527 # experimental: check for a paragraph where all lines
528 # have some ...\t...\t...\n pattern
530 my @lines = split( "\n", $text );
533 foreach my $line ( @lines ){
534 if( $line =~ /\S/ && $line !~ /\t/ ){
540 $text =~ s/\t+/<td>/g;
541 $text =~ s/^/<tr><td>/gm;
542 $text = '<table cellspacing="0" cellpadding="0">' .
547 ## end of experimental
550 print HTML "$text\n";
553 print HTML "<p>$text</p>\n";
556 print HTML "</dd>\n" if $need_dd;
561 # finish off any pending directives
565 print HTML "<p><a href=\"#__index__\"><small>$backlink</small></a></p>\n"
566 if $doindex and $index and $backlink;
568 print HTML <<END_OF_TAIL;
575 # close the html file
578 warn "Finished\n" if $verbose;
581 ##############################################################################
583 my $usage; # see below
586 warn "$0: $podfile: @_\n" if @_;
590 $usage =<<END_OF_USAGE;
591 Usage: $0 --help --htmlroot=<name> --infile=<name> --outfile=<name>
592 --podpath=<name>:...:<name> --podroot=<name>
593 --libpods=<name>:...:<name> --recurse --verbose --index
594 --netscape --norecurse --noindex --cachedir=<name>
596 --backlink - set text for "back to top" links (default: none).
597 --cachedir - directory for the item and directory cache files.
598 --css - stylesheet URL
599 --flush - flushes the item and directory caches.
600 --[no]header - produce block header/footer (default is no headers).
601 --help - prints this message.
602 --htmldir - directory for resulting HTML files.
603 --htmlroot - http-server base directory from which all relative paths
604 in podpath stem (default is /).
605 --[no]index - generate an index at the top of the resulting html
607 --infile - filename for the pod to convert (input taken from stdin
609 --libpods - colon-separated list of pages to search for =item pod
610 directives in as targets of C<> and implicit links (empty
611 by default). note, these are not filenames, but rather
612 page names like those that appear in L<> links.
613 --outfile - filename for the resulting html file (output sent to
615 --podpath - colon-separated list of directories containing library
616 pods (empty by default).
617 --podroot - filesystem base directory from which all relative paths
618 in podpath stem (default is .).
619 --[no]quiet - supress some benign warning messages (default is off).
620 --[no]recurse - recurse on those subdirectories listed in podpath
622 --title - title that will appear in resulting html file.
623 --[no]verbose - self-explanatory (off by default).
624 --[no]netscape - deprecated, has no effect. for backwards compatibility only.
628 sub parse_command_line {
629 my ($opt_backlink,$opt_cachedir,$opt_css,$opt_flush,$opt_header,$opt_help,
630 $opt_htmldir,$opt_htmlroot,$opt_index,$opt_infile,$opt_libpods,
631 $opt_netscape,$opt_outfile,$opt_podpath,$opt_podroot,$opt_quiet,
632 $opt_recurse,$opt_title,$opt_verbose);
634 unshift @ARGV, split ' ', $Config{pod2html} if $Config{pod2html};
635 my $result = GetOptions(
636 'backlink=s' => \$opt_backlink,
637 'cachedir=s' => \$opt_cachedir,
638 'css=s' => \$opt_css,
639 'flush' => \$opt_flush,
640 'header!' => \$opt_header,
641 'help' => \$opt_help,
642 'htmldir=s' => \$opt_htmldir,
643 'htmlroot=s' => \$opt_htmlroot,
644 'index!' => \$opt_index,
645 'infile=s' => \$opt_infile,
646 'libpods=s' => \$opt_libpods,
647 'netscape!' => \$opt_netscape,
648 'outfile=s' => \$opt_outfile,
649 'podpath=s' => \$opt_podpath,
650 'podroot=s' => \$opt_podroot,
651 'quiet!' => \$opt_quiet,
652 'recurse!' => \$opt_recurse,
653 'title=s' => \$opt_title,
654 'verbose!' => \$opt_verbose,
656 usage("-", "invalid parameters") if not $result;
658 usage("-") if defined $opt_help; # see if the user asked for help
659 $opt_help = ""; # just to make -w shut-up.
661 @podpath = split(":", $opt_podpath) if defined $opt_podpath;
662 @libpods = split(":", $opt_libpods) if defined $opt_libpods;
664 $backlink = $opt_backlink if defined $opt_backlink;
665 $cachedir = $opt_cachedir if defined $opt_cachedir;
666 $css = $opt_css if defined $opt_css;
667 $header = $opt_header if defined $opt_header;
668 $htmldir = $opt_htmldir if defined $opt_htmldir;
669 $htmlroot = $opt_htmlroot if defined $opt_htmlroot;
670 $doindex = $opt_index if defined $opt_index;
671 $podfile = $opt_infile if defined $opt_infile;
672 $htmlfile = $opt_outfile if defined $opt_outfile;
673 $podroot = $opt_podroot if defined $opt_podroot;
674 $quiet = $opt_quiet if defined $opt_quiet;
675 $recurse = $opt_recurse if defined $opt_recurse;
676 $title = $opt_title if defined $opt_title;
677 $verbose = $opt_verbose if defined $opt_verbose;
679 warn "Flushing item and directory caches\n"
680 if $opt_verbose && defined $opt_flush;
681 $dircache = "$cachedir/pod2htmd$cache_ext";
682 $itemcache = "$cachedir/pod2htmi$cache_ext";
683 unlink($dircache, $itemcache) if defined $opt_flush;
690 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
691 my @cache_key_args = @_;
693 # A first-level cache:
694 # Don't bother reading the cache files if they still apply
695 # and haven't changed since we last read them.
697 my $this_cache_key = cache_key(@cache_key_args);
699 return if $saved_cache_key and $this_cache_key eq $saved_cache_key;
701 # load the cache of %pages and %items if possible. $tests will be
702 # non-zero if successful.
704 if (-f $dircache && -f $itemcache) {
705 warn "scanning for item cache\n" if $verbose;
706 $tests = load_cache($dircache, $itemcache, $podpath, $podroot);
709 # if we didn't succeed in loading the cache then we must (re)build
712 warn "scanning directories in pod-path\n" if $verbose;
713 scan_podpath($podroot, $recurse, 0);
715 $saved_cache_key = cache_key(@cache_key_args);
719 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
720 return join('!', $dircache, $itemcache, $recurse,
721 @$podpath, $podroot, stat($dircache), stat($itemcache));
725 # load_cache - tries to find if the caches stored in $dircache and $itemcache
726 # are valid caches of %pages and %items. if they are valid then it loads
727 # them and returns a non-zero value.
730 my($dircache, $itemcache, $podpath, $podroot) = @_;
736 open(CACHE, "<$itemcache") ||
737 die "$0: error opening $itemcache for reading: $!\n";
740 # is it the same podpath?
743 $tests++ if (join(":", @$podpath) eq $_);
745 # is it the same podroot?
748 $tests++ if ($podroot eq $_);
750 # load the cache if its good
756 warn "loading item cache\n" if $verbose;
763 warn "scanning for directory cache\n" if $verbose;
764 open(CACHE, "<$dircache") ||
765 die "$0: error opening $dircache for reading: $!\n";
769 # is it the same podpath?
772 $tests++ if (join(":", @$podpath) eq $_);
774 # is it the same podroot?
777 $tests++ if ($podroot eq $_);
779 # load the cache if its good
785 warn "loading directory cache\n" if $verbose;
797 # scan_podpath - scans the directories specified in @podpath for directories,
798 # .pod files, and .pm files. it also scans the pod files specified in
799 # @libpods for =item directives.
802 my($podroot, $recurse, $append) = @_;
804 my($libpod, $dirname, $pod, @files, @poddata);
811 # scan each directory listed in @podpath
814 || die "$0: error changing to directory $podroot: $!\n";
815 foreach $dir (@podpath) {
816 scan_dir($dir, $recurse);
819 # scan the pods listed in @libpods for =item directives
820 foreach $libpod (@libpods) {
821 # if the page isn't defined then we won't know where to find it
823 next unless defined $pages{$libpod} && $pages{$libpod};
825 # if there is a directory then use the .pod and .pm files within it.
826 # NOTE: Only finds the first so-named directory in the tree.
827 # if ($pages{$libpod} =~ /([^:]*[^(\.pod|\.pm)]):/) {
828 if ($pages{$libpod} =~ /([^:]*(?<!\.pod)(?<!\.pm)):/) {
829 # find all the .pod and .pm files within the directory
831 opendir(DIR, $dirname) ||
832 die "$0: error opening directory $dirname: $!\n";
833 @files = grep(/(\.pod|\.pm)\z/ && ! -d $_, readdir(DIR));
836 # scan each .pod and .pm file for =item directives
837 foreach $pod (@files) {
838 open(POD, "<$dirname/$pod") ||
839 die "$0: error opening $dirname/$pod for input: $!\n";
842 clean_data( \@poddata );
844 scan_items( \%items, "$dirname/$pod", @poddata);
847 # use the names of files as =item directives too.
848 ### Don't think this should be done this way - confuses issues.(WL)
849 ### foreach $pod (@files) {
850 ### $pod =~ /^(.*)(\.pod|\.pm)$/;
851 ### $items{$1} = "$dirname/$1.html" if $1;
853 } elsif ($pages{$libpod} =~ /([^:]*\.pod):/ ||
854 $pages{$libpod} =~ /([^:]*\.pm):/) {
855 # scan the .pod or .pm file for =item directives
857 open(POD, "<$pod") ||
858 die "$0: error opening $pod for input: $!\n";
861 clean_data( \@poddata );
863 scan_items( \%items, "$pod", @poddata);
865 warn "$0: shouldn't be here (line ".__LINE__."\n";
868 @poddata = (); # clean-up a bit
871 || die "$0: error changing to directory $pwd: $!\n";
873 # cache the item list for later use
874 warn "caching items for later use\n" if $verbose;
875 open(CACHE, ">$itemcache") ||
876 die "$0: error open $itemcache for writing: $!\n";
878 print CACHE join(":", @podpath) . "\n$podroot\n";
879 foreach my $key (keys %items) {
880 print CACHE "$key $items{$key}\n";
885 # cache the directory list for later use
886 warn "caching directories for later use\n" if $verbose;
887 open(CACHE, ">$dircache") ||
888 die "$0: error open $dircache for writing: $!\n";
890 print CACHE join(":", @podpath) . "\n$podroot\n";
891 foreach my $key (keys %pages) {
892 print CACHE "$key $pages{$key}\n";
899 # scan_dir - scans the directory specified in $dir for subdirectories, .pod
900 # files, and .pm files. notes those that it finds. this information will
901 # be used later in order to figure out where the pages specified in L<>
902 # links are on the filesystem.
905 my($dir, $recurse) = @_;
906 my($t, @subdirs, @pods, $pod, $dirname, @dirs);
912 opendir(DIR, $dir) ||
913 die "$0: error opening directory $dir: $!\n";
914 while (defined($_ = readdir(DIR))) {
915 if (-d "$dir/$_" && $_ ne "." && $_ ne "..") { # directory
916 $pages{$_} = "" unless defined $pages{$_};
917 $pages{$_} .= "$dir/$_:";
919 } elsif (/\.pod\z/) { # .pod
921 $pages{$_} = "" unless defined $pages{$_};
922 $pages{$_} .= "$dir/$_.pod:";
923 push(@pods, "$dir/$_.pod");
924 } elsif (/\.html\z/) { # .html
926 $pages{$_} = "" unless defined $pages{$_};
927 $pages{$_} .= "$dir/$_.pod:";
928 } elsif (/\.pm\z/) { # .pm
930 $pages{$_} = "" unless defined $pages{$_};
931 $pages{$_} .= "$dir/$_.pm:";
932 push(@pods, "$dir/$_.pm");
937 # recurse on the subdirectories if necessary
939 foreach my $subdir (@subdirs) {
940 scan_dir("$dir/$subdir", $recurse);
946 # scan_headings - scan a pod file for head[1-6] tags, note the tags, and
950 my($sections, @data) = @_;
951 my($tag, $which_head, $otitle, $listdepth, $index);
953 # here we need local $ignore = 0;
954 # unfortunately, we can't have it, because $ignore is lexical
960 # scan for =head directives, note their name, and build an index
961 # pointing to each of them.
962 foreach my $line (@data) {
963 if ($line =~ /^=(head)([1-6])\s+(.*)/) {
964 ($tag, $which_head, $otitle) = ($1,$2,$3);
966 my $title = depod( $otitle );
967 my $name = htmlify( $title );
968 $$sections{$name} = 1;
969 $name =~ s/\s/_/g; # htmlify keeps spaces but we don't want them here..
970 $title = process_text( \$otitle );
972 while ($which_head != $listdepth) {
973 if ($which_head > $listdepth) {
974 $index .= "\n" . ("\t" x $listdepth) . "<ul>\n";
976 } elsif ($which_head < $listdepth) {
978 $index .= "\n" . ("\t" x $listdepth) . "</ul>\n";
982 $index .= "\n" . ("\t" x $listdepth) . "<li>" .
983 "<a href=\"#" . $name . "\">" .
984 $title . "</a></li>";
988 # finish off the lists
989 while ($listdepth--) {
990 $index .= "\n" . ("\t" x $listdepth) . "</ul>\n";
993 # get rid of bogus lists
994 $index =~ s,\t*<ul>\s*</ul>\n,,g;
996 $ignore = 1; # restore old value;
1002 # scan_items - scans the pod specified by $pod for =item directives. we
1003 # will use this information later on in resolving C<> links.
1006 my( $itemref, $pod, @poddata ) = @_;
1010 $pod =~ s/\.pod\z//;
1011 $pod .= ".html" if $pod;
1013 foreach $i (0..$#poddata) {
1014 my $txt = depod( $poddata[$i] );
1016 # figure out what kind of item it is.
1017 # Build string for referencing this item.
1018 if ( $txt =~ /\A=item\s+\*\s*(.*)\Z/s ) { # bullet
1021 } elsif( $txt =~ /\A=item\s+(?>\d+\.?)\s*(.*)\Z/s ) { # numbered list
1023 } elsif( $txt =~ /\A=item\s+(.*)\Z/s ) { # plain item
1028 my $fid = fragment_id( $item );
1029 $$itemref{$fid} = "$pod" if $fid;
1034 # process_head - convert a pod head[1-6] tag and convert it to HTML format.
1037 my($tag, $heading, $hasindex) = @_;
1039 # figure out the level of the =head
1040 $tag =~ /head([1-6])/;
1044 warn "$0: $podfile: unterminated list at =head in paragraph $paragraph. ignoring.\n";
1045 while( $listlevel ){
1051 if( $level == 1 && ! $top ){
1052 print HTML "<a href=\"#__index__\"><small>$backlink</small></a>\n"
1053 if $hasindex and $backlink;
1054 print HTML "</p>\n<hr />\n"
1056 print HTML "</p>\n";
1059 my $name = htmlify( depod( $heading ) );
1060 $name =~ s/\s/_/g; # htmlify keeps spaces but we don't want them here...
1061 my $convert = process_text( \$heading );
1062 print HTML "<h$level><a name=\"$name\">$convert</a></h$level>\n";
1067 # emit_item_tag - print an =item's text
1068 # Note: The global $EmittedItem is used for inhibiting self-references.
1072 sub emit_item_tag($$$){
1073 my( $otext, $text, $compact ) = @_;
1074 my $item = fragment_id( $text );
1076 $EmittedItem = $item;
1077 ### print STDERR "emit_item_tag=$item ($text)\n";
1079 print HTML '<strong>';
1080 if ($items_named{$item}++) {
1081 print HTML process_text( \$otext );
1083 my $name = 'item_' . $item;
1084 $name =~ s/\s/_/g; # we don't want spaces here...
1085 print HTML qq{<a name="$name">}, process_text( \$otext ), '</a>';
1087 print HTML "</strong><br />\n";
1088 undef( $EmittedItem );
1093 if( $items_seen[$listlevel]++ == 0 ){
1094 push( @listend, "</$tag>" );
1095 print HTML "<$tag>\n";
1097 my $emitted = $tag eq 'dl' ? 'dt' : 'li';
1098 print HTML "<$emitted>";
1103 # process_item - convert a pod item tag and convert it to HTML format.
1107 my $need_dd = 0; # set to 1 if we need a <dd></dd> after an item
1109 # lots of documents start a list without doing an =over. this is
1110 # bad! but, the proper thing to do seems to be to just assume
1111 # they did do an =over. so warn them once and then continue.
1112 if( $listlevel == 0 ){
1113 warn "$0: $podfile: unexpected =item directive in paragraph $paragraph. ignoring.\n";
1117 # formatting: insert a paragraph if preceding item has >1 paragraph
1119 print HTML "<p></p>\n";
1123 # remove formatting instructions from the text
1124 my $text = depod( $otext );
1126 my $emitted; # the tag actually emitted, used for closing
1128 # all the list variants:
1129 if( $text =~ /\A\*/ ){ # bullet
1130 $emitted = emit_li( 'ul' );
1131 if ($text =~ /\A\*\s+(.+)\Z/s ) { # with additional text
1133 $otext =~ s/\A\*\s+//;
1134 emit_item_tag( $otext, $tag, 1 );
1137 } elsif( $text =~ /\A\d+/ ){ # numbered list
1138 $emitted = emit_li( 'ol' );
1139 if ($text =~ /\A(?>\d+\.?)\s*(.+)\Z/s ) { # with additional text
1141 $otext =~ s/\A\d+\.?\s*//;
1142 emit_item_tag( $otext, $tag, 1 );
1145 } else { # definition list
1146 $emitted = emit_li( 'dl' );
1147 if ($text =~ /\A(.+)\Z/s ){ # should have text
1148 emit_item_tag( $otext, $text, 1 );
1152 print HTML "</$emitted>" if $emitted;
1158 # process_over - process a pod over tag and start a corresponding HTML list.
1163 push( @items_seen, 0 );
1168 # process_back - process a pod back tag and convert it to HTML format.
1171 if( $listlevel == 0 ){
1172 warn "$0: $podfile: unexpected =back directive in paragraph $paragraph. ignoring.\n";
1176 # close off the list. note, I check to see if $listend[$listlevel] is
1177 # defined because an =item directive may have never appeared and thus
1178 # $listend[$listlevel] may have never been initialized.
1180 if( defined $listend[$listlevel] ){
1181 print HTML '<p></p>' if $after_lpar;
1182 print HTML $listend[$listlevel];
1188 # clean up item count
1193 # process_cut - process a pod cut tag, thus start ignoring pod directives.
1200 # process_pod - process a pod tag, thus stop ignoring pod directives
1201 # until we see a corresponding cut.
1204 # no need to set $ignore to 0 cause the main loop did it
1208 # process_for - process a =for pod tag. if it's for html, spit
1209 # it out verbatim, if illustration, center it, otherwise ignore it.
1212 my($whom, $text) = @_;
1213 if ( $whom =~ /^(pod2)?html$/i) {
1215 } elsif ($whom =~ /^illustration$/i) {
1216 1 while chomp $text;
1217 for my $ext (qw[.png .gif .jpeg .jpg .tga .pcl .bmp]) {
1218 $text .= $ext, last if -r "$text$ext";
1220 print HTML qq{<p align="center"><img src="$text" alt="$text illustration" /></p>};
1225 # process_begin - process a =begin pod tag. this pushes
1226 # whom we're beginning on the begin stack. if there's a
1227 # begin stack, we only print if it us.
1230 my($whom, $text) = @_;
1232 push (@begin_stack, $whom);
1233 if ( $whom =~ /^(pod2)?html$/) {
1234 print HTML $text if $text;
1239 # process_end - process a =end pod tag. pop the
1240 # begin stack. die if we're mismatched.
1243 my($whom, $text) = @_;
1245 if ($begin_stack[-1] ne $whom ) {
1246 die "Unmatched begin/end at chunk $paragraph\n"
1248 pop( @begin_stack );
1252 # process_pre - indented paragraph, made into <pre></pre>
1261 # insert spaces in place of tabs
1264 1 while $line =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
1268 # convert some special chars to HTML escapes
1269 $rest = html_escape($rest);
1271 # try and create links for all occurrences of perl.* within
1272 # the preformatted text.
1276 if ( defined $pages{$2} ){ # is a link
1277 qq($1<a href="$htmlroot/$pages{$2}">$2</a>);
1278 } elsif (defined $pages{dosify($2)}) { # is a link
1279 qq($1<a href="$htmlroot/$pages{dosify($2)}">$2</a>);
1285 (<a\ href="?) ([^>:]*:)? ([^>:]*) \.pod: ([^>:]*:)?
1288 if ( $htmlfileurl ne '' ){
1289 # Here, we take advantage of the knowledge
1290 # that $htmlfileurl ne '' implies $htmlroot eq ''.
1291 # Since $htmlroot eq '', we need to prepend $htmldir
1292 # on the fron of the link to get the absolute path
1293 # of the link's target. We check for a leading '/'
1294 # to avoid corrupting links that are #, file:, etc.
1296 $old_url = "$htmldir$old_url" if $old_url =~ m{^\/};
1297 $url = relativize_url( "$old_url.html", $htmlfileurl );
1304 # Look for embedded URLs and make them into links. We don't
1305 # relativize them since they are best left as the author intended.
1307 my $urls = '(' . join ('|', qw{
1320 my $gunk = '/#~:.?+=&%@!\-';
1322 my $any = "${ltrs}${gunk}${punc}";
1325 \b # start at word boundary
1327 $urls : # need resource and a colon
1328 (?!:) # Ignore File::, among others.
1329 [$any] +? # followed by on or more
1330 # of any valid character, but
1331 # be conservative and take only
1332 # what you need to....
1334 (?= # look-ahead non-consumptive assertion
1335 [$punc]* # either 0 or more puntuation
1336 [^$any] # followed by a non-url char
1338 $ # then end of the string
1340 }{<a href="$1">$1</a>}igox;
1342 # text should be as it is (verbatim)
1348 # pure text processing
1350 # pure_text/inIS_text: differ with respect to automatic C<> recognition.
1351 # we don't want this to happen within IS
1355 process_puretext( $text, \$ptQuote, 1 );
1360 process_puretext( $text, \$ptQuote, 0 );
1364 # process_puretext - process pure text (without pod-escapes) converting
1365 # double-quotes and handling implicit C<> links.
1367 sub process_puretext {
1368 my($text, $quote, $notinIS) = @_;
1370 ## Guessing at func() or [$@%&]*var references in plain text is destined
1371 ## to produce some strange looking ref's. uncomment to disable:
1374 my(@words, $lead, $trail);
1376 # convert double-quotes to single-quotes
1377 if( $$quote && $text =~ s/"/''/s ){
1380 while ($text =~ s/"([^"]*)"/``$1''/sg) {};
1381 $$quote = 1 if $text =~ s/"/``/s;
1383 # keep track of leading and trailing white-space
1384 $lead = ($text =~ s/\A(\s+)//s ? $1 : "");
1385 $trail = ($text =~ s/(\s+)\Z//s ? $1 : "");
1387 # split at space/non-space boundaries
1388 @words = split( /(?<=\s)(?=\S)|(?<=\S)(?=\s)/, $text );
1390 # process each word individually
1391 foreach my $word (@words) {
1393 next if $word =~ /^\s*$/;
1394 # see if we can infer a link
1395 if( $notinIS && $word =~ /^(\w+)\((.*)\)$/ ) {
1396 # has parenthesis so should have been a C<> ref
1397 ## try for a pagename (perlXXX(1))?
1398 my( $func, $args ) = ( $1, $2 );
1399 if( $args =~ /^\d+$/ ){
1400 my $url = page_sect( $word, '' );
1402 $word = "<a href=\"$url\">the $word manpage</a>";
1406 ## try function name for a link, append tt'ed argument list
1407 $word = emit_C( $func, '', "($args)");
1409 #### disabled. either all (including $\W, $\w+{.*} etc.) or nothing.
1410 ## } elsif( $notinIS && $word =~ /^[\$\@%&*]+\w+$/) {
1411 ## # perl variables, should be a C<> ref
1412 ## $word = emit_C( $word );
1414 } elsif ($word =~ m,^\w+://\w,) {
1416 # Don't relativize it: leave it as the author intended
1417 $word = qq(<a href="$word">$word</a>);
1418 } elsif ($word =~ /[\w.-]+\@[\w-]+\.\w/) {
1419 # looks like an e-mail address
1420 my ($w1, $w2, $w3) = ("", $word, "");
1421 ($w1, $w2, $w3) = ("(", $1, ")$2") if $word =~ /^\((.*?)\)(,?)/;
1422 ($w1, $w2, $w3) = ("<", $1, ">$2") if $word =~ /^<(.*?)>(,?)/;
1423 $word = qq($w1<a href="mailto:$w2">$w2</a>$w3);
1425 $word = html_escape($word) if $word =~ /["&<>]/;
1429 # put everything back together
1430 return $lead . join( '', @words ) . $trail;
1435 # process_text - handles plaintext that appears in the input pod file.
1436 # there may be pod commands embedded within the text so those must be
1437 # converted to html commands.
1440 sub process_text1($$;$$);
1441 sub pattern ($) { $_[0] ? '[^\S\n]+'.('>' x ($_[0] + 1)) : '>' }
1442 sub closing ($) { local($_) = shift; (defined && s/\s+$//) ? length : 0 }
1447 my $res = process_text1( 0, $tref );
1451 sub process_text1($$;$$){
1452 my( $lev, $rstr, $func, $closing ) = @_;
1455 unless (defined $func) {
1461 # B<text> - boldface
1462 $res = '<strong>' . process_text1( $lev, $rstr ) . '</strong>';
1464 } elsif( $func eq 'C' ){
1465 # C<code> - can be a ref or <code></code>
1466 # need to extract text
1467 my $par = go_ahead( $rstr, 'C', $closing );
1469 ## clean-up of the link target
1470 my $text = depod( $par );
1472 ### my $x = $par =~ /[BI]</ ? 'yes' : 'no' ;
1473 ### print STDERR "-->call emit_C($par) lev=$lev, par with BI=$x\n";
1475 $res = emit_C( $text, $lev > 1 || ($par =~ /[BI]</) );
1477 } elsif( $func eq 'E' ){
1478 # E<x> - convert to character
1479 $$rstr =~ s/^([^>]*)>//;
1481 $escape =~ s/^(\d+|X[\dA-F]+)$/#$1/i;
1484 } elsif( $func eq 'F' ){
1485 # F<filename> - italizice
1486 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1488 } elsif( $func eq 'I' ){
1489 # I<text> - italizice
1490 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1492 } elsif( $func eq 'L' ){
1494 ## L<text|cross-ref> => produce text, use cross-ref for linking
1495 ## L<cross-ref> => make text from cross-ref
1496 ## need to extract text
1497 my $par = go_ahead( $rstr, 'L', $closing );
1499 # some L<>'s that shouldn't be:
1500 # a) full-blown URL's are emitted as-is
1501 if( $par =~ m{^\w+://}s ){
1502 return make_URL_href( $par );
1504 # b) C<...> is stripped and treated as C<>
1505 if( $par =~ /^C<(.*)>$/ ){
1506 my $text = depod( $1 );
1507 return emit_C( $text, $lev > 1 || ($par =~ /[BI]</) );
1510 # analyze the contents
1511 $par =~ s/\n/ /g; # undo word-wrapped tags
1514 if( $par =~ s{^([^|]+)\|}{} ){
1518 # make sure sections start with a /
1521 my( $page, $section, $ident );
1523 # check for link patterns
1524 if( $par =~ m{^([^/]+?)/(?!")(.*?)$} ){ # name/ident
1525 # we've got a name/ident (no quotes)
1526 ( $page, $ident ) = ( $1, $2 );
1527 ### print STDERR "--> L<$par> to page $page, ident $ident\n";
1529 } elsif( $par =~ m{^(.*?)/"?(.*?)"?$} ){ # [name]/"section"
1530 # even though this should be a "section", we go for ident first
1531 ( $page, $ident ) = ( $1, $2 );
1532 ### print STDERR "--> L<$par> to page $page, section $section\n";
1534 } elsif( $par =~ /\s/ ){ # this must be a section with missing quotes
1535 ( $page, $section ) = ( '', $par );
1536 ### print STDERR "--> L<$par> to void page, section $section\n";
1539 ( $page, $section ) = ( $par, '' );
1540 ### print STDERR "--> L<$par> to page $par, void section\n";
1543 # now, either $section or $ident is defined. the convoluted logic
1544 # below tries to resolve L<> according to what the user specified.
1545 # failing this, we try to find the next best thing...
1546 my( $url, $ltext, $fid );
1549 if( defined $ident ){
1550 ## try to resolve $ident as an item
1551 ( $url, $fid ) = coderef( $page, $ident );
1553 if( ! defined( $linktext ) ){
1555 $linktext .= " in " if $ident && $page;
1556 $linktext .= "the $page manpage" if $page;
1558 ### print STDERR "got coderef url=$url\n";
1561 ## no luck: go for a section (auto-quoting!)
1564 ## now go for a section
1565 my $htmlsection = htmlify( $section );
1566 $url = page_sect( $page, $htmlsection );
1568 if( ! defined( $linktext ) ){
1569 $linktext = $section;
1570 $linktext .= " in " if $section && $page;
1571 $linktext .= "the $page manpage" if $page;
1573 ### print STDERR "got page/section url=$url\n";
1576 ## no luck: go for an ident
1583 ( $url, $fid ) = coderef( $page, $ident );
1585 if( ! defined( $linktext ) ){
1587 $linktext .= " in " if $ident && $page;
1588 $linktext .= "the $page manpage" if $page;
1590 ### print STDERR "got section=>coderef url=$url\n";
1594 # warning; show some text.
1595 $linktext = $opar unless defined $linktext;
1596 warn "$0: $podfile: cannot resolve L<$opar> in paragraph $paragraph.";
1599 # now we have a URL or just plain code
1600 $$rstr = $linktext . '>' . $$rstr;
1601 if( defined( $url ) ){
1602 $res = "<a href=\"$url\">" . process_text1( $lev, $rstr ) . '</a>';
1604 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
1607 } elsif( $func eq 'S' ){
1608 # S<text> - non-breaking spaces
1609 $res = process_text1( $lev, $rstr );
1610 $res =~ s/ / /g;
1612 } elsif( $func eq 'X' ){
1614 $$rstr =~ s/^[^>]*>//;
1616 } elsif( $func eq 'Z' ){
1618 warn "$0: $podfile: invalid X<> in paragraph $paragraph."
1619 unless $$rstr =~ s/^>//;
1622 my $term = pattern $closing;
1623 while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)//s ){
1624 # all others: either recurse into new function or
1625 # terminate at closing angle bracket(s)
1627 $pt .= $2 if !$3 && $lev == 1;
1628 $res .= $lev == 1 ? pure_text( $pt ) : inIS_text( $pt );
1629 return $res if !$3 && $lev > 1;
1631 $res .= process_text1( $lev, $rstr, $3, closing $4 );
1635 $res .= pure_text( $$rstr );
1637 warn "$0: $podfile: undelimited $func<> in paragraph $paragraph.";
1644 # go_ahead: extract text of an IS (can be nested)
1647 my( $rstr, $func, $closing ) = @_;
1649 my @closing = ($closing);
1651 s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|@{[pattern $closing[0]]})//s ){
1655 return $res unless @closing;
1657 unshift @closing, closing $4;
1661 warn "$0: $podfile: undelimited $func<> in paragraph $paragraph.";
1666 # emit_C - output result of C<text>
1667 # $text is the depod-ed text
1670 my( $text, $nocode, $args ) = @_;
1671 $args = '' unless defined $args;
1673 my( $url, $fid ) = coderef( undef(), $text );
1675 # need HTML-safe text
1676 my $linktext = html_escape( "$text$args" );
1678 if( defined( $url ) &&
1679 (!defined( $EmittedItem ) || $EmittedItem ne $fid ) ){
1680 $res = "<a href=\"$url\"><code>$linktext</code></a>";
1681 } elsif( 0 && $nocode ){
1684 $res = "<code>$linktext</code>";
1690 # html_escape: make text safe for HTML
1694 $rest =~ s/&/&/g;
1695 $rest =~ s/</</g;
1696 $rest =~ s/>/>/g;
1697 $rest =~ s/"/"/g;
1698 $rest =~ s/'/'/g;
1704 # dosify - convert filenames to 8.3
1708 return lc($str) if $^O eq 'VMS'; # VMS just needs casing
1711 $str =~ s/(\.\w+)/substr ($1,0,4)/ge;
1712 $str =~ s/(\w+)/substr ($1,0,8)/ge;
1718 # page_sect - make a URL from the text of a L<>
1721 my( $page, $section ) = @_;
1722 my( $linktext, $page83, $link); # work strings
1724 # check if we know that this is a section in this page
1725 if (!defined $pages{$page} && defined $sections{$page}) {
1728 ### print STDERR "reset page='', section=$section\n";
1731 $page83=dosify($page);
1732 $page=$page83 if (defined $pages{$page83});
1734 $link = "#" . htmlify( $section );
1735 } elsif ( $page =~ /::/ ) {
1737 # Search page cache for an entry keyed under the html page name,
1738 # then look to see what directory that page might be in. NOTE:
1739 # this will only find one page. A better solution might be to produce
1740 # an intermediate page that is an index to all such pages.
1741 my $page_name = $page ;
1742 $page_name =~ s,^.*/,,s ;
1743 if ( defined( $pages{ $page_name } ) &&
1744 $pages{ $page_name } =~ /([^:]*$page)\.(?:pod|pm):/
1749 # NOTE: This branch assumes that all A::B pages are located in
1750 # $htmlroot/A/B.html . This is often incorrect, since they are
1751 # often in $htmlroot/lib/A/B.html or such like. Perhaps we could
1752 # analyze the contents of %pages and figure out where any
1753 # cousins of A::B are, then assume that. So, if A::B isn't found,
1754 # but A::C is found in lib/A/C.pm, then A::B is assumed to be in
1755 # lib/A/B.pm. This is also limited, but it's an improvement.
1756 # Maybe a hints file so that the links point to the correct places
1760 $link = "$htmlroot/$page.html";
1761 $link .= "#" . htmlify( $section ) if ($section);
1762 } elsif (!defined $pages{$page}) {
1765 $section = htmlify( $section ) if $section ne "";
1766 ### print STDERR "...section=$section\n";
1768 # if there is a directory by the name of the page, then assume that an
1769 # appropriate section will exist in the subdirectory
1770 # if ($section ne "" && $pages{$page} =~ /([^:]*[^(\.pod|\.pm)]):/) {
1771 if ($section ne "" && $pages{$page} =~ /([^:]*(?<!\.pod)(?<!\.pm)):/) {
1772 $link = "$htmlroot/$1/$section.html";
1773 ### print STDERR "...link=$link\n";
1775 # since there is no directory by the name of the page, the section will
1776 # have to exist within a .html of the same name. thus, make sure there
1777 # is a .pod or .pm that might become that .html
1779 $section = "#$section" if $section;
1780 ### print STDERR "...section=$section\n";
1782 # check if there is a .pod with the page name
1783 if ($pages{$page} =~ /([^:]*)\.pod:/) {
1784 $link = "$htmlroot/$1.html$section";
1785 } elsif ($pages{$page} =~ /([^:]*)\.pm:/) {
1786 $link = "$htmlroot/$1.html$section";
1794 # Here, we take advantage of the knowledge that $htmlfileurl ne ''
1795 # implies $htmlroot eq ''. This means that the link in question
1796 # needs a prefix of $htmldir if it begins with '/'. The test for
1797 # the initial '/' is done to avoid '#'-only links, and to allow
1798 # for other kinds of links, like file:, ftp:, etc.
1800 if ( $htmlfileurl ne '' ) {
1801 $link = "$htmldir$link" if $link =~ m{^/}s;
1802 $url = relativize_url( $link, $htmlfileurl );
1803 # print( " b: [$link,$htmlfileurl,$url]\n" );
1816 # relativize_url - convert an absolute URL to one relative to a base URL.
1817 # Assumes both end in a filename.
1819 sub relativize_url {
1820 my ($dest,$source) = @_ ;
1822 my ($dest_volume,$dest_directory,$dest_file) =
1823 File::Spec::Unix->splitpath( $dest ) ;
1824 $dest = File::Spec::Unix->catpath( $dest_volume, $dest_directory, '' ) ;
1826 my ($source_volume,$source_directory,$source_file) =
1827 File::Spec::Unix->splitpath( $source ) ;
1828 $source = File::Spec::Unix->catpath( $source_volume, $source_directory, '' ) ;
1831 if ( $dest ne '' ) {
1832 $rel_path = File::Spec::Unix->abs2rel( $dest, $source ) ;
1835 if ( $rel_path ne '' &&
1836 substr( $rel_path, -1 ) ne '/' &&
1837 substr( $dest_file, 0, 1 ) ne '#'
1839 $rel_path .= "/$dest_file" ;
1842 $rel_path .= "$dest_file" ;
1850 # coderef - make URL from the text of a C<>
1853 my( $page, $item ) = @_;
1856 my $fid = fragment_id( $item );
1857 if( defined( $page ) ){
1858 # we have been given a $page...
1861 # Do we take it? Item could be a section!
1862 my $base = $items{$fid} || "";
1863 $base =~ s{[^/]*/}{};
1864 if( $base ne "$page.html" ){
1865 ### print STDERR "coderef( $page, $item ): items{$fid} = $items{$fid} = $base => discard page!\n";
1870 # no page - local items precede cached items
1871 if( defined( $fid ) ){
1872 if( exists $local_items{$fid} ){
1873 $page = $local_items{$fid};
1875 $page = $items{$fid};
1880 # if there was a pod file that we found earlier with an appropriate
1881 # =item directive, then create a link to that page.
1882 if( defined $page ){
1884 if( exists $pages{$page} and $pages{$page} =~ /([^:.]*)\.[^:]*:/){
1885 $page = $1 . '.html';
1887 my $link = "$htmlroot/$page#item_$fid";
1889 # Here, we take advantage of the knowledge that $htmlfileurl
1890 # ne '' implies $htmlroot eq ''.
1891 if ( $htmlfileurl ne '' ) {
1892 $link = "$htmldir$link" ;
1893 $url = relativize_url( $link, $htmlfileurl ) ;
1898 $url = "#item_" . $fid;
1901 confess "url has space: $url" if $url =~ /"[^"]*\s[^"]*"/;
1903 return( $url, $fid );
1909 # Adapted from Nick Ing-Simmons' PodToHtml package.
1911 my $source_file = shift ;
1912 my $destination_file = shift;
1914 my $source = URI::file->new_abs($source_file);
1915 my $uo = URI::file->new($destination_file,$source)->abs;
1916 return $uo->rel->as_string;
1921 # finish_list - finish off any pending HTML lists. this should be called
1922 # after the entire pod file has been read and converted.
1925 while ($listlevel > 0) {
1926 print HTML "</dl>\n";
1932 # htmlify - converts a pod section specification to a suitable section
1933 # specification for HTML. Note that we keep spaces and special characters
1934 # except ", ? (Netscape problem) and the hyphen (writer's problem...).
1938 $heading =~ s/(\s+)/ /g;
1939 $heading =~ s/\s+\Z//;
1940 $heading =~ s/\A\s+//;
1941 # The hyphen is a disgrace to the English language.
1942 $heading =~ s/[-"?]//g;
1943 $heading = lc( $heading );
1948 # depod - convert text by eliminating all interior sequences
1949 # Note: can be called with copy or modify semantics
1956 $E2c{amp} = '&'; # in Tk's pods
1964 ${$_[0]} = depod1( \$string );
1972 my( $rstr, $func, $closing ) = @_;
1974 return $res unless defined $$rstr;
1975 if( ! defined( $func ) ){
1976 # skip to next begin of an interior sequence
1977 while( $$rstr =~ s/\A(.*?)([BCEFILSXZ])<(<+[^\S\n]+)?// ){
1978 # recurse into its text
1979 $res .= $1 . depod1( $rstr, $2, closing $3);
1982 } elsif( $func eq 'E' ){
1983 # E<x> - convert to character
1984 $$rstr =~ s/^([^>]*)>//;
1985 $res .= $E2c{$1} || "";
1986 } elsif( $func eq 'X' ){
1988 $$rstr =~ s/^[^>]*>//;
1989 } elsif( $func eq 'Z' ){
1993 # all others: either recurse into new function or
1994 # terminate at closing angle bracket
1995 my $term = pattern $closing;
1996 while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)// ){
1999 $res .= depod1( $rstr, $3, closing $4 );
2001 ## If we're here and $2 ne '>': undelimited interior sequence.
2002 ## Ignored, as this is called without proper indication of where we are.
2003 ## Rely on process_text to produce diagnostics.
2009 # fragment_id - construct a fragment identifier from:
2011 # b) contents of C<...>
2016 $text =~ s/\s+\Z//s;
2018 # a method or function?
2019 return $1 if $text =~ /(\w+)\s*\(/;
2020 return $1 if $text =~ /->\s*(\w+)\s*\(?/;
2023 return $1 if $text =~ /^([$@%*]\S+)/;
2025 # some pattern matching operator?
2026 return $1 if $text =~ m|^(\w+/).*/\w*$|;
2028 # fancy stuff... like "do { }"
2029 return $1 if $text =~ m|^(\w+)\s*{.*}$|;
2031 # honour the perlfunc manpage: func [PAR[,[ ]PAR]...]
2032 # and some funnies with ... Module ...
2033 return $1 if $text =~ m{^([a-z\d]+)(\s+[A-Z\d,/& ]+)?$};
2034 return $1 if $text =~ m{^([a-z\d]+)\s+Module(\s+[A-Z\d,/& ]+)?$};
2037 $text =~ s/\s+/_/sg;
2039 defined( $hc[ord($1)] ) ? $hc[ord($1)]
2040 : ( $hc[ord($1)] = sprintf( "%%%02X", ord($1) ) ) }gxe;
2041 $text = substr( $text, 0, 50 );
2048 # make_URL_href - generate HTML href from URL
2049 # Special treatment for CGI queries.
2051 sub make_URL_href($){
2054 s{^(http:[-\w/#~:.+=&%@!]+)(\?.*)$}{<a href="$1$2">$1</a>}i ){
2055 $url = "<a href=\"$url\">$url</a>";