Typo nits from Wolfgang Laun.
[p5sagit/p5-mst-13.2.git] / lib / Pod / Html.pm
CommitLineData
54310121 1package Pod::Html;
0e4548d5 2use strict;
54310121 3require Exporter;
0e4548d5 4
5use vars qw($VERSION @ISA @EXPORT);
d6a466d7 6$VERSION = 1.04;
0e4548d5 7@ISA = qw(Exporter);
54310121 8@EXPORT = qw(pod2html htmlify);
54310121 9
10use Carp;
0e4548d5 11use Config;
12use Cwd;
af75be11 13use File::Spec;
0e4548d5 14use File::Spec::Unix;
15use Getopt::Long;
54310121 16
3ec07288 17use locale; # make \w work right in non-ASCII lands
18
54310121 19=head1 NAME
20
7b8d334a 21Pod::Html - module to convert pod files to HTML
54310121 22
23=head1 SYNOPSIS
24
7b8d334a 25 use Pod::Html;
54310121 26 pod2html([options]);
27
28=head1 DESCRIPTION
29
30Converts files from pod format (see L<perlpod>) to HTML format. It
31can automatically generate indexes and cross-references, and it keeps
32a cache of things it knows how to cross-reference.
33
34=head1 ARGUMENTS
35
36Pod::Html takes the following arguments:
37
38=over 4
39
0e4548d5 40=item backlink
41
42 --backlink="Back to Top"
43
59ecbafa 44Adds "Back to Top" links in front of every C<head1> heading (except for
45the first). By default, no backlinks are generated.
0e4548d5 46
b42210d7 47=item cachedir
48
49 --cachedir=name
50
51Creates the item and directory caches in the given directory.
52
0e4548d5 53=item css
54
55 --css=stylesheet
56
59ecbafa 57Specify the URL of a cascading style sheet. Also disables all HTML/CSS
58C<style> attributes that are output by default (to avoid conflicts).
0e4548d5 59
60=item flush
61
62 --flush
63
64Flushes the item and directory caches.
65
66=item header
67
68 --header
69 --noheader
70
59ecbafa 71Creates header and footer blocks containing the text of the C<NAME>
72section. By default, no headers are generated.
0e4548d5 73
54310121 74=item help
75
76 --help
77
78Displays the usage message.
79
5a039dd3 80=item htmldir
81
82 --htmldir=name
83
84Sets the directory in which the resulting HTML file is placed. This
29f227c9 85is used to generate relative links to other files. Not passing this
86causes all links to be absolute, since this is the value that tells
87Pod::Html the root of the documentation tree.
5a039dd3 88
54310121 89=item htmlroot
90
91 --htmlroot=name
92
93Sets the base URL for the HTML files. When cross-references are made,
94the HTML root is prepended to the URL.
95
0e4548d5 96=item index
97
98 --index
99 --noindex
100
101Generate an index at the top of the HTML file. This is the default
102behaviour.
103
54310121 104=item infile
105
106 --infile=name
107
108Specify the pod file to convert. Input is taken from STDIN if no
109infile is specified.
110
54310121 111=item libpods
112
113 --libpods=name:...:name
114
115List of page names (eg, "perlfunc") which contain linkable C<=item>s.
116
117=item netscape
118
119 --netscape
0e4548d5 120 --nonetscape
54310121 121
59ecbafa 122B<Deprecated>, has no effect. For backwards compatibility only.
54310121 123
0e4548d5 124=item outfile
54310121 125
0e4548d5 126 --outfile=name
54310121 127
0e4548d5 128Specify the HTML file to create. Output goes to STDOUT if no outfile
129is specified.
54310121 130
0e4548d5 131=item podpath
54310121 132
0e4548d5 133 --podpath=name:...:name
54310121 134
0e4548d5 135Specify which subdirectories of the podroot contain pod files whose
59ecbafa 136HTML converted forms can be linked to in cross references.
54310121 137
0e4548d5 138=item podroot
54310121 139
0e4548d5 140 --podroot=name
54310121 141
0e4548d5 142Specify the base directory for finding library pods.
54310121 143
0e4548d5 144=item quiet
54310121 145
0e4548d5 146 --quiet
147 --noquiet
54310121 148
0e4548d5 149Don't display I<mostly harmless> warning messages. These messages
150will be displayed by default. But this is not the same as C<verbose>
151mode.
54310121 152
0e4548d5 153=item recurse
54310121 154
0e4548d5 155 --recurse
54310121 156 --norecurse
157
0e4548d5 158Recurse into subdirectories specified in podpath (default behaviour).
54310121 159
160=item title
161
162 --title=title
163
164Specify the title of the resulting HTML file.
165
166=item verbose
167
168 --verbose
0e4548d5 169 --noverbose
54310121 170
0e4548d5 171Display progress messages. By default, they won't be displayed.
34db337b 172
54310121 173=back
174
175=head1 EXAMPLE
176
177 pod2html("pod2html",
59ecbafa 178 "--podpath=lib:ext:pod:vms",
54310121 179 "--podroot=/usr/src/perl",
180 "--htmlroot=/perl/nmanual",
181 "--libpods=perlfunc:perlguts:perlvar:perlrun:perlop",
182 "--recurse",
183 "--infile=foo.pod",
184 "--outfile=/perl/nmanual/foo.html");
185
34db337b 186=head1 ENVIRONMENT
187
59ecbafa 188Uses C<$Config{pod2html}> to setup default options.
34db337b 189
54310121 190=head1 AUTHOR
191
192Tom Christiansen, E<lt>tchrist@perl.comE<gt>.
193
54310121 194=head1 SEE ALSO
195
196L<perlpod>
197
198=head1 COPYRIGHT
199
200This program is distributed under the Artistic License.
201
202=cut
203
b42210d7 204my $cachedir = "."; # The directory to which item and directory
205 # caches will be written.
1f763251 206my $cache_ext = $^O eq 'VMS' ? ".tmp" : ".x~~";
207my $dircache = "pod2htmd$cache_ext";
208my $itemcache = "pod2htmi$cache_ext";
54310121 209
210my @begin_stack = (); # begin/end stack
211
29f227c9 212my @libpods = (); # files to search for links from C<> directives
213my $htmlroot = "/"; # http-server base directory from which all
54310121 214 # relative paths in $podpath stem.
5a039dd3 215my $htmldir = ""; # The directory to which the html pages
216 # will (eventually) be written.
54310121 217my $htmlfile = ""; # write to stdout by default
29f227c9 218my $htmlfileurl = "" ; # The url that other files would use to
5a039dd3 219 # refer to this file. This is only used
220 # to make relative urls that point to
221 # other files.
54310121 222my $podfile = ""; # read from stdin by default
223my @podpath = (); # list of directories containing library pods.
af75be11 224my $podroot = File::Spec->curdir; # filesystem base directory from which all
54310121 225 # relative paths in $podpath stem.
34db337b 226my $css = ''; # Cascading style sheet
54310121 227my $recurse = 1; # recurse on subdirectories in $podpath.
34db337b 228my $quiet = 0; # not quiet by default
54310121 229my $verbose = 0; # not verbose by default
230my $doindex = 1; # non-zero if we should generate an index
0e4548d5 231my $backlink = ''; # text for "back to top" links
54310121 232my $listlevel = 0; # current list depth
2a28b791 233my @listend = (); # the text to use to end the list.
234my $after_lpar = 0; # set to true after a par in an =item
54310121 235my $ignore = 1; # whether or not to format text. we don't
236 # format text until we hit our first pod
237 # directive.
238
239my %items_named = (); # for the multiples of the same item in perlfunc
240my @items_seen = ();
54310121 241my $title; # title to give the pod(s)
34db337b 242my $header = 0; # produce block header/footer
54310121 243my $top = 1; # true if we are at the top of the doc. used
59ecbafa 244 # to prevent the first <hr /> directive.
54310121 245my $paragraph; # which paragraph we're processing (used
246 # for error messages)
2a28b791 247my $ptQuote = 0; # status of double-quote conversion
54310121 248my %pages = (); # associative array used to find the location
249 # of pages referenced by L<> links.
250my %sections = (); # sections within this page
251my %items = (); # associative array used to find the location
252 # of =item directives referenced by C<> links
2a28b791 253my %local_items = (); # local items - avoid destruction of %items
39e571d4 254my $Is83; # is dos with short filenames (8.3)
255
54310121 256sub init_globals {
04f720db 257$dircache = "pod2htmd$cache_ext";
258$itemcache = "pod2htmi$cache_ext";
54310121 259
260@begin_stack = (); # begin/end stack
261
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.
67398a75 265$htmldir = ""; # The directory to which the html pages
266 # will (eventually) be written.
54310121 267$htmlfile = ""; # write to stdout by default
268$podfile = ""; # read from stdin by default
269@podpath = (); # list of directories containing library pods.
af75be11 270$podroot = File::Spec->curdir; # filesystem base directory from which all
54310121 271 # relative paths in $podpath stem.
34db337b 272$css = ''; # Cascading style sheet
54310121 273$recurse = 1; # recurse on subdirectories in $podpath.
34db337b 274$quiet = 0; # not quiet by default
54310121 275$verbose = 0; # not verbose by default
276$doindex = 1; # non-zero if we should generate an index
0e4548d5 277$backlink = ''; # text for "back to top" links
54310121 278$listlevel = 0; # current list depth
2a28b791 279@listend = (); # the text to use to end the list.
280$after_lpar = 0; # set to true after a par in an =item
54310121 281$ignore = 1; # whether or not to format text. we don't
282 # format text until we hit our first pod
283 # directive.
284
285@items_seen = ();
286%items_named = ();
34db337b 287$header = 0; # produce block header/footer
54310121 288$title = ''; # title to give the pod(s)
289$top = 1; # true if we are at the top of the doc. used
59ecbafa 290 # to prevent the first <hr /> directive.
54310121 291$paragraph = ''; # which paragraph we're processing (used
292 # for error messages)
54310121 293%sections = (); # sections within this page
3e3baf6d 294
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
54310121 300 # of =item directives referenced by C<> links
2a28b791 301%local_items = ();
39e571d4 302$Is83=$^O eq 'dos';
54310121 303}
304
2a28b791 305#
306# clean_data: global clean-up of pod data
307#
308sub clean_data($){
309 my( $dataref ) = @_;
bf202ccd 310 for my $i ( 0..$#{$dataref} ) {
2a28b791 311 ${$dataref}[$i] =~ s/\s+\Z//;
312
313 # have a look for all-space lines
4135c0a0 314 if( ${$dataref}[$i] =~ /^\s+$/m and $dataref->[$i] !~ /^\s/ ){
2a28b791 315 my @chunks = split( /^\s+$/m, ${$dataref}[$i] );
316 splice( @$dataref, $i, 1, @chunks );
317 }
318 }
319}
320
321
54310121 322sub pod2html {
323 local(@ARGV) = @_;
324 local($/);
325 local $_;
326
327 init_globals();
328
39e571d4 329 $Is83 = 0 if (defined (&Dos::UseLFN) && Dos::UseLFN());
330
54310121 331 # cache of %pages and %items from last time we ran pod2html
54310121 332
333 #undef $opt_help if defined $opt_help;
334
335 # parse the command-line parameters
336 parse_command_line();
337
59ecbafa 338 # escape the backlink argument (same goes for title but is done later...)
339 $backlink = html_escape($backlink) if defined $backlink;
340
54310121 341 # set some variables to their default values if necessary
342 local *POD;
59ecbafa 343 unless (@ARGV && $ARGV[0]) {
54310121 344 $podfile = "-" unless $podfile; # stdin
345 open(POD, "<$podfile")
346 || die "$0: cannot open $podfile file for input: $!\n";
347 } else {
348 $podfile = $ARGV[0]; # XXX: might be more filenames
349 *POD = *ARGV;
59ecbafa 350 }
54310121 351 $htmlfile = "-" unless $htmlfile; # stdout
352 $htmlroot = "" if $htmlroot eq "/"; # so we don't get a //
fe6f1558 353 $htmldir =~ s#/\z## ; # so we don't get a //
29f227c9 354 if ( $htmlroot eq ''
59ecbafa 355 && defined( $htmldir )
29f227c9 356 && $htmldir ne ''
59ecbafa 357 && substr( $htmlfile, 0, length( $htmldir ) ) eq $htmldir
358 )
5a039dd3 359 {
29f227c9 360 # Set the 'base' url for this file, so that we can use it
59ecbafa 361 # as the location from which to calculate relative links
29f227c9 362 # to other files. If this is '', then absolute links will
363 # be used throughout.
364 $htmlfileurl= "$htmldir/" . substr( $htmlfile, length( $htmldir ) + 1);
5a039dd3 365 }
54310121 366
367 # read the pod a paragraph at a time
368 warn "Scanning for sections in input file(s)\n" if $verbose;
369 $/ = "";
370 my @poddata = <POD>;
371 close(POD);
bbed45f6 372
373 # be eol agnostic
374 for (@poddata) {
375 if (/\x0D/) {
376 if (/\x0D\x0A/) {
377 @poddata = map { s/\x0D\x0A/\n/g;
378 /\n\n/ ?
379 map { "$_\n\n" } split /\n\n/ :
380 $_ } @poddata;
381 } else {
382 @poddata = map { s/\x0D/\n/g;
383 /\n\n/ ?
384 map { "$_\n\n" } split /\n\n/ :
385 $_ } @poddata;
386 }
387 last;
388 }
389 }
390
2a28b791 391 clean_data( \@poddata );
54310121 392
393 # scan the pod for =head[1-6] directives and build an index
394 my $index = scan_headings(\%sections, @poddata);
395
3e3baf6d 396 unless($index) {
31e56455 397 warn "No headings in $podfile\n" if $verbose;
3e3baf6d 398 }
399
54310121 400 # open the output file
401 open(HTML, ">$htmlfile")
402 || die "$0: cannot open $htmlfile file for output: $!\n";
403
d011ffae 404 # put a title in the HTML file if one wasn't specified
405 if ($title eq '') {
406 TITLE_SEARCH: {
59ecbafa 407 for (my $i = 0; $i < @poddata; $i++) {
d011ffae 408 if ($poddata[$i] =~ /^=head1\s*NAME\b/m) {
59ecbafa 409 for my $para ( @poddata[$i, $i+1] ) {
d011ffae 410 last TITLE_SEARCH
411 if ($title) = $para =~ /(\S+\s+-+.*\S)/s;
412 }
59ecbafa 413 }
54310121 414
59ecbafa 415 }
d011ffae 416 }
417 }
fe6f1558 418 if (!$title and $podfile =~ /\.pod\z/) {
3e3baf6d 419 # probably a split pod so take first =head[12] as title
59ecbafa 420 for (my $i = 0; $i < @poddata; $i++) {
3e3baf6d 421 last if ($title) = $poddata[$i] =~ /^=head[12]\s*(.*)/;
59ecbafa 422 }
3e3baf6d 423 warn "adopted '$title' as title for $podfile\n"
424 if $verbose and $title;
59ecbafa 425 }
7b8d334a 426 if ($title) {
427 $title =~ s/\s*\(.*\)//;
428 } else {
f8f99792 429 warn "$0: no title for $podfile.\n" unless $quiet;
fe6f1558 430 $podfile =~ /^(.*)(\.[^.\/]+)?\z/s;
54310121 431 $title = ($podfile eq "-" ? 'No Title' : $1);
3e3baf6d 432 warn "using $title" if $verbose;
54310121 433 }
59ecbafa 434 $title = html_escape($title);
435
436 my $csslink = '';
437 my $bodystyle = ' style="background-color: white"';
438 my $tdstyle = ' style="background-color: #cccccc"';
439
440 if ($css) {
441 $csslink = qq(\n<link rel="stylesheet" href="$css" type="text/css" />);
442 $csslink =~ s,\\,/,g;
443 $csslink =~ s,(/.):,$1|,;
444 $bodystyle = '';
445 $tdstyle = '';
446 }
447
448 my $block = $header ? <<END_OF_BLOCK : '';
449<table border="0" width="100%" cellspacing="0" cellpadding="3">
450<tr><td class="block"$tdstyle valign="middle">
451<big><strong><span class="block">&nbsp;$title</span></strong></big>
452</td></tr>
453</table>
34db337b 454END_OF_BLOCK
455
54310121 456 print HTML <<END_OF_HEAD;
59ecbafa 457<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
458<html xmlns="http://www.w3.org/1999/xhtml">
8d481ee5 459<head>
59ecbafa 460<title>$title</title>$csslink
461<link rev="made" href="mailto:$Config{perladmin}" />
462</head>
463
464<body$bodystyle>
34db337b 465$block
54310121 466END_OF_HEAD
467
3e3baf6d 468 # load/reload/validate/cache %pages and %items
469 get_cache($dircache, $itemcache, \@podpath, $podroot, $recurse);
54310121 470
471 # scan the pod for =item directives
2a28b791 472 scan_items( \%local_items, "", @poddata);
54310121 473
474 # put an index at the top of the file. note, if $doindex is 0 we
475 # still generate an index, but surround it with an html comment.
476 # that way some other program can extract it if desired.
477 $index =~ s/--+/-/g;
59ecbafa 478 print HTML "<p><a name=\"__index__\"></a></p>\n";
54310121 479 print HTML "<!-- INDEX BEGIN -->\n";
480 print HTML "<!--\n" unless $doindex;
481 print HTML $index;
482 print HTML "-->\n" unless $doindex;
483 print HTML "<!-- INDEX END -->\n\n";
59ecbafa 484 print HTML "<hr />\n" if $doindex and $index;
54310121 485
486 # now convert this file
2a28b791 487 my $after_item; # set to true after an =item
59ecbafa 488 my $need_dd = 0;
2a28b791 489 warn "Converting input file $podfile\n" if $verbose;
490 foreach my $i (0..$#poddata){
491 $ptQuote = 0; # status of quote conversion
492
54310121 493 $_ = $poddata[$i];
494 $paragraph = $i+1;
495 if (/^(=.*)/s) { # is it a pod directive?
496 $ignore = 0;
2a28b791 497 $after_item = 0;
59ecbafa 498 $need_dd = 0;
54310121 499 $_ = $1;
500 if (/^=begin\s+(\S+)\s*(.*)/si) {# =begin
501 process_begin($1, $2);
502 } elsif (/^=end\s+(\S+)\s*(.*)/si) {# =end
503 process_end($1, $2);
504 } elsif (/^=cut/) { # =cut
505 process_cut();
506 } elsif (/^=pod/) { # =pod
507 process_pod();
508 } else {
509 next if @begin_stack && $begin_stack[-1] ne 'html';
510
7b8d334a 511 if (/^=(head[1-6])\s+(.*\S)/s) { # =head[1-6] heading
2a28b791 512 process_head( $1, $2, $doindex && $index );
513 } elsif (/^=item\s*(.*\S)?/sm) { # =item text
59ecbafa 514 $need_dd = process_item( $1 );
2a28b791 515 $after_item = 1;
54310121 516 } elsif (/^=over\s*(.*)/) { # =over N
517 process_over();
518 } elsif (/^=back/) { # =back
519 process_back();
a45bd81d 520 } elsif (/^=for\s+(\S+)\s*(.*)/si) {# =for
54310121 521 process_for($1,$2);
522 } else {
523 /^=(\S*)\s*/;
524 warn "$0: $podfile: unknown pod directive '$1' in "
525 . "paragraph $paragraph. ignoring.\n";
526 }
527 }
528 $top = 0;
529 }
530 else {
531 next if $ignore;
532 next if @begin_stack && $begin_stack[-1] ne 'html';
59ecbafa 533 print HTML and next if @begin_stack && $begin_stack[-1] eq 'html';
534 print HTML "<dd>\n" if $need_dd;
54310121 535 my $text = $_;
2a28b791 536 if( $text =~ /\A\s+/ ){
537 process_pre( \$text );
59ecbafa 538 print HTML "<pre>\n$text</pre>\n";
2a28b791 539
540 } else {
541 process_text( \$text );
542
543 # experimental: check for a paragraph where all lines
544 # have some ...\t...\t...\n pattern
545 if( $text =~ /\t/ ){
546 my @lines = split( "\n", $text );
547 if( @lines > 1 ){
548 my $all = 2;
549 foreach my $line ( @lines ){
550 if( $line =~ /\S/ && $line !~ /\t/ ){
551 $all--;
552 last if $all == 0;
553 }
554 }
555 if( $all > 0 ){
59ecbafa 556 $text =~ s/\t+/<td>/g;
557 $text =~ s/^/<tr><td>/gm;
558 $text = '<table cellspacing="0" cellpadding="0">' .
559 $text . '</table>';
2a28b791 560 }
561 }
562 }
563 ## end of experimental
564
565 if( $after_item ){
566 print HTML "$text\n";
567 $after_lpar = 1;
568 } else {
59ecbafa 569 print HTML "<p>$text</p>\n";
2a28b791 570 }
571 }
59ecbafa 572 print HTML "</dd>\n" if $need_dd;
2a28b791 573 $after_item = 0;
54310121 574 }
575 }
576
577 # finish off any pending directives
578 finish_list();
2a28b791 579
580 # link to page index
59ecbafa 581 print HTML "<p><a href=\"#__index__\"><small>$backlink</small></a></p>\n"
0e4548d5 582 if $doindex and $index and $backlink;
2a28b791 583
54310121 584 print HTML <<END_OF_TAIL;
34db337b 585$block
59ecbafa 586</body>
54310121 587
59ecbafa 588</html>
54310121 589END_OF_TAIL
590
591 # close the html file
592 close(HTML);
593
594 warn "Finished\n" if $verbose;
595}
596
597##############################################################################
598
599my $usage; # see below
600sub usage {
601 my $podfile = shift;
602 warn "$0: $podfile: @_\n" if @_;
603 die $usage;
604}
605
606$usage =<<END_OF_USAGE;
607Usage: $0 --help --htmlroot=<name> --infile=<name> --outfile=<name>
608 --podpath=<name>:...:<name> --podroot=<name>
609 --libpods=<name>:...:<name> --recurse --verbose --index
b42210d7 610 --netscape --norecurse --noindex --cachedir=<name>
54310121 611
0e4548d5 612 --backlink - set text for "back to top" links (default: none).
b42210d7 613 --cachedir - directory for the item and directory cache files.
0e4548d5 614 --css - stylesheet URL
615 --flush - flushes the item and directory caches.
616 --[no]header - produce block header/footer (default is no headers).
617 --help - prints this message.
618 --htmldir - directory for resulting HTML files.
619 --htmlroot - http-server base directory from which all relative paths
620 in podpath stem (default is /).
621 --[no]index - generate an index at the top of the resulting html
622 (default behaviour).
623 --infile - filename for the pod to convert (input taken from stdin
624 by default).
625 --libpods - colon-separated list of pages to search for =item pod
626 directives in as targets of C<> and implicit links (empty
627 by default). note, these are not filenames, but rather
628 page names like those that appear in L<> links.
0e4548d5 629 --outfile - filename for the resulting html file (output sent to
630 stdout by default).
631 --podpath - colon-separated list of directories containing library
632 pods (empty by default).
633 --podroot - filesystem base directory from which all relative paths
634 in podpath stem (default is .).
635 --[no]quiet - supress some benign warning messages (default is off).
636 --[no]recurse - recurse on those subdirectories listed in podpath
637 (default behaviour).
638 --title - title that will appear in resulting html file.
639 --[no]verbose - self-explanatory (off by default).
59ecbafa 640 --[no]netscape - deprecated, has no effect. for backwards compatibility only.
54310121 641
642END_OF_USAGE
643
644sub parse_command_line {
b42210d7 645 my ($opt_backlink,$opt_cachedir,$opt_css,$opt_flush,$opt_header,$opt_help,
646 $opt_htmldir,$opt_htmlroot,$opt_index,$opt_infile,$opt_libpods,
647 $opt_netscape,$opt_outfile,$opt_podpath,$opt_podroot,$opt_quiet,
648 $opt_recurse,$opt_title,$opt_verbose);
0e4548d5 649
34db337b 650 unshift @ARGV, split ' ', $Config{pod2html} if $Config{pod2html};
54310121 651 my $result = GetOptions(
0e4548d5 652 'backlink=s' => \$opt_backlink,
b42210d7 653 'cachedir=s' => \$opt_cachedir,
0e4548d5 654 'css=s' => \$opt_css,
29f227c9 655 'flush' => \$opt_flush,
0e4548d5 656 'header!' => \$opt_header,
29f227c9 657 'help' => \$opt_help,
658 'htmldir=s' => \$opt_htmldir,
54310121 659 'htmlroot=s' => \$opt_htmlroot,
29f227c9 660 'index!' => \$opt_index,
54310121 661 'infile=s' => \$opt_infile,
662 'libpods=s' => \$opt_libpods,
663 'netscape!' => \$opt_netscape,
664 'outfile=s' => \$opt_outfile,
665 'podpath=s' => \$opt_podpath,
666 'podroot=s' => \$opt_podroot,
0e4548d5 667 'quiet!' => \$opt_quiet,
54310121 668 'recurse!' => \$opt_recurse,
669 'title=s' => \$opt_title,
0e4548d5 670 'verbose!' => \$opt_verbose,
54310121 671 );
672 usage("-", "invalid parameters") if not $result;
673
674 usage("-") if defined $opt_help; # see if the user asked for help
675 $opt_help = ""; # just to make -w shut-up.
676
54310121 677 @podpath = split(":", $opt_podpath) if defined $opt_podpath;
678 @libpods = split(":", $opt_libpods) if defined $opt_libpods;
679
0e4548d5 680 $backlink = $opt_backlink if defined $opt_backlink;
b42210d7 681 $cachedir = $opt_cachedir if defined $opt_cachedir;
0e4548d5 682 $css = $opt_css if defined $opt_css;
683 $header = $opt_header if defined $opt_header;
684 $htmldir = $opt_htmldir if defined $opt_htmldir;
685 $htmlroot = $opt_htmlroot if defined $opt_htmlroot;
686 $doindex = $opt_index if defined $opt_index;
687 $podfile = $opt_infile if defined $opt_infile;
0e4548d5 688 $htmlfile = $opt_outfile if defined $opt_outfile;
689 $podroot = $opt_podroot if defined $opt_podroot;
690 $quiet = $opt_quiet if defined $opt_quiet;
691 $recurse = $opt_recurse if defined $opt_recurse;
692 $title = $opt_title if defined $opt_title;
693 $verbose = $opt_verbose if defined $opt_verbose;
694
54310121 695 warn "Flushing item and directory caches\n"
696 if $opt_verbose && defined $opt_flush;
b42210d7 697 $dircache = "$cachedir/pod2htmd$cache_ext";
698 $itemcache = "$cachedir/pod2htmi$cache_ext";
54310121 699 unlink($dircache, $itemcache) if defined $opt_flush;
54310121 700}
701
3e3baf6d 702
703my $saved_cache_key;
704
705sub get_cache {
706 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
707 my @cache_key_args = @_;
708
709 # A first-level cache:
710 # Don't bother reading the cache files if they still apply
711 # and haven't changed since we last read them.
712
713 my $this_cache_key = cache_key(@cache_key_args);
714
715 return if $saved_cache_key and $this_cache_key eq $saved_cache_key;
716
717 # load the cache of %pages and %items if possible. $tests will be
718 # non-zero if successful.
719 my $tests = 0;
720 if (-f $dircache && -f $itemcache) {
721 warn "scanning for item cache\n" if $verbose;
722 $tests = load_cache($dircache, $itemcache, $podpath, $podroot);
723 }
724
725 # if we didn't succeed in loading the cache then we must (re)build
726 # %pages and %items.
727 if (!$tests) {
728 warn "scanning directories in pod-path\n" if $verbose;
729 scan_podpath($podroot, $recurse, 0);
730 }
731 $saved_cache_key = cache_key(@cache_key_args);
732}
733
734sub cache_key {
735 my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
736 return join('!', $dircache, $itemcache, $recurse,
29f227c9 737 @$podpath, $podroot, stat($dircache), stat($itemcache));
3e3baf6d 738}
739
54310121 740#
3e3baf6d 741# load_cache - tries to find if the caches stored in $dircache and $itemcache
54310121 742# are valid caches of %pages and %items. if they are valid then it loads
743# them and returns a non-zero value.
744#
3e3baf6d 745sub load_cache {
54310121 746 my($dircache, $itemcache, $podpath, $podroot) = @_;
747 my($tests);
748 local $_;
749
750 $tests = 0;
751
752 open(CACHE, "<$itemcache") ||
753 die "$0: error opening $itemcache for reading: $!\n";
754 $/ = "\n";
755
756 # is it the same podpath?
757 $_ = <CACHE>;
758 chomp($_);
3e3baf6d 759 $tests++ if (join(":", @$podpath) eq $_);
54310121 760
761 # is it the same podroot?
762 $_ = <CACHE>;
763 chomp($_);
764 $tests++ if ($podroot eq $_);
765
766 # load the cache if its good
767 if ($tests != 2) {
768 close(CACHE);
54310121 769 return 0;
770 }
771
772 warn "loading item cache\n" if $verbose;
773 while (<CACHE>) {
774 /(.*?) (.*)$/;
775 $items{$1} = $2;
776 }
777 close(CACHE);
778
779 warn "scanning for directory cache\n" if $verbose;
780 open(CACHE, "<$dircache") ||
781 die "$0: error opening $dircache for reading: $!\n";
782 $/ = "\n";
783 $tests = 0;
784
785 # is it the same podpath?
786 $_ = <CACHE>;
787 chomp($_);
3e3baf6d 788 $tests++ if (join(":", @$podpath) eq $_);
54310121 789
790 # is it the same podroot?
791 $_ = <CACHE>;
792 chomp($_);
793 $tests++ if ($podroot eq $_);
794
795 # load the cache if its good
796 if ($tests != 2) {
797 close(CACHE);
54310121 798 return 0;
799 }
800
801 warn "loading directory cache\n" if $verbose;
802 while (<CACHE>) {
803 /(.*?) (.*)$/;
804 $pages{$1} = $2;
805 }
806
807 close(CACHE);
808
809 return 1;
810}
811
812#
813# scan_podpath - scans the directories specified in @podpath for directories,
814# .pod files, and .pm files. it also scans the pod files specified in
815# @libpods for =item directives.
816#
817sub scan_podpath {
3e3baf6d 818 my($podroot, $recurse, $append) = @_;
54310121 819 my($pwd, $dir);
820 my($libpod, $dirname, $pod, @files, @poddata);
821
3e3baf6d 822 unless($append) {
823 %items = ();
824 %pages = ();
825 }
826
54310121 827 # scan each directory listed in @podpath
828 $pwd = getcwd();
829 chdir($podroot)
830 || die "$0: error changing to directory $podroot: $!\n";
831 foreach $dir (@podpath) {
832 scan_dir($dir, $recurse);
833 }
834
835 # scan the pods listed in @libpods for =item directives
836 foreach $libpod (@libpods) {
837 # if the page isn't defined then we won't know where to find it
838 # on the system.
839 next unless defined $pages{$libpod} && $pages{$libpod};
840
841 # if there is a directory then use the .pod and .pm files within it.
29f227c9 842 # NOTE: Only finds the first so-named directory in the tree.
843# if ($pages{$libpod} =~ /([^:]*[^(\.pod|\.pm)]):/) {
844 if ($pages{$libpod} =~ /([^:]*(?<!\.pod)(?<!\.pm)):/) {
54310121 845 # find all the .pod and .pm files within the directory
846 $dirname = $1;
847 opendir(DIR, $dirname) ||
848 die "$0: error opening directory $dirname: $!\n";
fe6f1558 849 @files = grep(/(\.pod|\.pm)\z/ && ! -d $_, readdir(DIR));
54310121 850 closedir(DIR);
851
852 # scan each .pod and .pm file for =item directives
853 foreach $pod (@files) {
854 open(POD, "<$dirname/$pod") ||
855 die "$0: error opening $dirname/$pod for input: $!\n";
856 @poddata = <POD>;
857 close(POD);
2a28b791 858 clean_data( \@poddata );
54310121 859
2a28b791 860 scan_items( \%items, "$dirname/$pod", @poddata);
54310121 861 }
862
863 # use the names of files as =item directives too.
2a28b791 864### Don't think this should be done this way - confuses issues.(WL)
865### foreach $pod (@files) {
866### $pod =~ /^(.*)(\.pod|\.pm)$/;
867### $items{$1} = "$dirname/$1.html" if $1;
868### }
54310121 869 } elsif ($pages{$libpod} =~ /([^:]*\.pod):/ ||
870 $pages{$libpod} =~ /([^:]*\.pm):/) {
871 # scan the .pod or .pm file for =item directives
872 $pod = $1;
873 open(POD, "<$pod") ||
874 die "$0: error opening $pod for input: $!\n";
875 @poddata = <POD>;
876 close(POD);
2a28b791 877 clean_data( \@poddata );
54310121 878
2a28b791 879 scan_items( \%items, "$pod", @poddata);
54310121 880 } else {
881 warn "$0: shouldn't be here (line ".__LINE__."\n";
882 }
883 }
884 @poddata = (); # clean-up a bit
885
886 chdir($pwd)
887 || die "$0: error changing to directory $pwd: $!\n";
888
889 # cache the item list for later use
890 warn "caching items for later use\n" if $verbose;
891 open(CACHE, ">$itemcache") ||
892 die "$0: error open $itemcache for writing: $!\n";
893
894 print CACHE join(":", @podpath) . "\n$podroot\n";
895 foreach my $key (keys %items) {
896 print CACHE "$key $items{$key}\n";
897 }
898
899 close(CACHE);
900
901 # cache the directory list for later use
902 warn "caching directories for later use\n" if $verbose;
903 open(CACHE, ">$dircache") ||
904 die "$0: error open $dircache for writing: $!\n";
905
906 print CACHE join(":", @podpath) . "\n$podroot\n";
907 foreach my $key (keys %pages) {
908 print CACHE "$key $pages{$key}\n";
909 }
910
911 close(CACHE);
912}
913
914#
915# scan_dir - scans the directory specified in $dir for subdirectories, .pod
916# files, and .pm files. notes those that it finds. this information will
917# be used later in order to figure out where the pages specified in L<>
918# links are on the filesystem.
919#
920sub scan_dir {
921 my($dir, $recurse) = @_;
922 my($t, @subdirs, @pods, $pod, $dirname, @dirs);
923 local $_;
924
925 @subdirs = ();
926 @pods = ();
927
928 opendir(DIR, $dir) ||
929 die "$0: error opening directory $dir: $!\n";
930 while (defined($_ = readdir(DIR))) {
931 if (-d "$dir/$_" && $_ ne "." && $_ ne "..") { # directory
932 $pages{$_} = "" unless defined $pages{$_};
933 $pages{$_} .= "$dir/$_:";
934 push(@subdirs, $_);
fe6f1558 935 } elsif (/\.pod\z/) { # .pod
936 s/\.pod\z//;
54310121 937 $pages{$_} = "" unless defined $pages{$_};
938 $pages{$_} .= "$dir/$_.pod:";
939 push(@pods, "$dir/$_.pod");
d3f9d0fb 940 } elsif (/\.html\z/) { # .html
941 s/\.html\z//;
942 $pages{$_} = "" unless defined $pages{$_};
943 $pages{$_} .= "$dir/$_.pod:";
fe6f1558 944 } elsif (/\.pm\z/) { # .pm
945 s/\.pm\z//;
54310121 946 $pages{$_} = "" unless defined $pages{$_};
947 $pages{$_} .= "$dir/$_.pm:";
948 push(@pods, "$dir/$_.pm");
949 }
950 }
951 closedir(DIR);
952
953 # recurse on the subdirectories if necessary
954 if ($recurse) {
955 foreach my $subdir (@subdirs) {
956 scan_dir("$dir/$subdir", $recurse);
957 }
958 }
959}
960
961#
962# scan_headings - scan a pod file for head[1-6] tags, note the tags, and
963# build an index.
964#
965sub scan_headings {
966 my($sections, @data) = @_;
2a28b791 967 my($tag, $which_head, $otitle, $listdepth, $index);
54310121 968
be173d55 969 # here we need local $ignore = 0;
970 # unfortunately, we can't have it, because $ignore is lexical
971 $ignore = 0;
972
54310121 973 $listdepth = 0;
974 $index = "";
975
976 # scan for =head directives, note their name, and build an index
977 # pointing to each of them.
978 foreach my $line (@data) {
59ecbafa 979 if ($line =~ /^=(head)([1-6])\s+(.*)/) {
980 ($tag, $which_head, $otitle) = ($1,$2,$3);
2a28b791 981
59ecbafa 982 my $title = depod( $otitle );
983 my $name = htmlify( $title );
984 $$sections{$name} = 1;
985 $name =~ s/\s/_/g; # htmlify keeps spaces but we don't want them here..
986 $title = process_text( \$otitle );
54310121 987
102c538a 988 while ($which_head != $listdepth) {
989 if ($which_head > $listdepth) {
59ecbafa 990 $index .= "\n" . ("\t" x $listdepth) . "<ul>\n";
102c538a 991 $listdepth++;
992 } elsif ($which_head < $listdepth) {
993 $listdepth--;
59ecbafa 994 $index .= "\n" . ("\t" x $listdepth) . "</ul>\n";
102c538a 995 }
54310121 996 }
54310121 997
59ecbafa 998 $index .= "\n" . ("\t" x $listdepth) . "<li>" .
999 "<a href=\"#" . $name . "\">" .
1000 $title . "</a></li>";
54310121 1001 }
1002 }
1003
1004 # finish off the lists
1005 while ($listdepth--) {
59ecbafa 1006 $index .= "\n" . ("\t" x $listdepth) . "</ul>\n";
54310121 1007 }
1008
1009 # get rid of bogus lists
59ecbafa 1010 $index =~ s,\t*<ul>\s*</ul>\n,,g;
54310121 1011
bb9460ed 1012 $ignore = 1; # restore old value;
be173d55 1013
54310121 1014 return $index;
1015}
1016
1017#
1018# scan_items - scans the pod specified by $pod for =item directives. we
1019# will use this information later on in resolving C<> links.
1020#
1021sub scan_items {
2a28b791 1022 my( $itemref, $pod, @poddata ) = @_;
54310121 1023 my($i, $item);
1024 local $_;
1025
fe6f1558 1026 $pod =~ s/\.pod\z//;
54310121 1027 $pod .= ".html" if $pod;
1028
1029 foreach $i (0..$#poddata) {
2a28b791 1030 my $txt = depod( $poddata[$i] );
1031
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
1035 next unless $1;
1036 $item = $1;
1037 } elsif( $txt =~ /\A=item\s+(?>\d+\.?)\s*(.*)\Z/s ) { # numbered list
1038 $item = $1;
1039 } elsif( $txt =~ /\A=item\s+(.*)\Z/s ) { # plain item
1040 $item = $1;
1041 } else {
1042 next;
54310121 1043 }
2a28b791 1044 my $fid = fragment_id( $item );
1045 $$itemref{$fid} = "$pod" if $fid;
54310121 1046 }
1047}
1048
1049#
1050# process_head - convert a pod head[1-6] tag and convert it to HTML format.
1051#
1052sub process_head {
2a28b791 1053 my($tag, $heading, $hasindex) = @_;
54310121 1054
1055 # figure out the level of the =head
1056 $tag =~ /head([1-6])/;
1057 my $level = $1;
1058
2a28b791 1059 if( $listlevel ){
1060 warn "$0: $podfile: unterminated list at =head in paragraph $paragraph. ignoring.\n";
1061 while( $listlevel ){
1062 process_back();
1063 }
1064 }
1065
59ecbafa 1066 print HTML "<p>\n";
2a28b791 1067 if( $level == 1 && ! $top ){
59ecbafa 1068 print HTML "<a href=\"#__index__\"><small>$backlink</small></a>\n"
1069 if $hasindex and $backlink;
1070 print HTML "</p>\n<hr />\n"
1071 } else {
1072 print HTML "</p>\n";
2a28b791 1073 }
1074
0d396dd4 1075 my $name = anchorify( depod( $heading ) );
2a28b791 1076 my $convert = process_text( \$heading );
59ecbafa 1077 print HTML "<h$level><a name=\"$name\">$convert</a></h$level>\n";
54310121 1078}
1079
2a28b791 1080
54310121 1081#
2a28b791 1082# emit_item_tag - print an =item's text
1083# Note: The global $EmittedItem is used for inhibiting self-references.
54310121 1084#
2a28b791 1085my $EmittedItem;
1086
1087sub emit_item_tag($$$){
1088 my( $otext, $text, $compact ) = @_;
1089 my $item = fragment_id( $text );
54310121 1090
2a28b791 1091 $EmittedItem = $item;
1092 ### print STDERR "emit_item_tag=$item ($text)\n";
54310121 1093
59ecbafa 1094 print HTML '<strong>';
2a28b791 1095 if ($items_named{$item}++) {
1096 print HTML process_text( \$otext );
1097 } else {
0d396dd4 1098 my $name = 'item_' . $item;
1099 $name = anchorify($name);
59ecbafa 1100 print HTML qq{<a name="$name">}, process_text( \$otext ), '</a>';
2a28b791 1101 }
59ecbafa 1102 print HTML "</strong><br />\n";
2a28b791 1103 undef( $EmittedItem );
1104}
1105
1106sub emit_li {
1107 my( $tag ) = @_;
1108 if( $items_seen[$listlevel]++ == 0 ){
1109 push( @listend, "</$tag>" );
1110 print HTML "<$tag>\n";
1111 }
59ecbafa 1112 my $emitted = $tag eq 'dl' ? 'dt' : 'li';
1113 print HTML "<$emitted>";
1114 return $emitted;
2a28b791 1115}
1116
1117#
1118# process_item - convert a pod item tag and convert it to HTML format.
1119#
1120sub process_item {
1121 my( $otext ) = @_;
59ecbafa 1122 my $need_dd = 0; # set to 1 if we need a <dd></dd> after an item
54310121 1123
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.
2a28b791 1127 if( $listlevel == 0 ){
1128 warn "$0: $podfile: unexpected =item directive in paragraph $paragraph. ignoring.\n";
1129 process_over();
1130 }
54310121 1131
2a28b791 1132 # formatting: insert a paragraph if preceding item has >1 paragraph
1133 if( $after_lpar ){
59ecbafa 1134 print HTML "<p></p>\n";
2a28b791 1135 $after_lpar = 0;
1136 }
54310121 1137
1138 # remove formatting instructions from the text
2a28b791 1139 my $text = depod( $otext );
1140
59ecbafa 1141 my $emitted; # the tag actually emitted, used for closing
1142
2a28b791 1143 # all the list variants:
1144 if( $text =~ /\A\*/ ){ # bullet
59ecbafa 1145 $emitted = emit_li( 'ul' );
1146 if ($text =~ /\A\*\s+(.+)\Z/s ) { # with additional text
1147 my $tag = $1;
1148 $otext =~ s/\A\*\s+//;
1149 emit_item_tag( $otext, $tag, 1 );
1150 }
54310121 1151
2a28b791 1152 } elsif( $text =~ /\A\d+/ ){ # numbered list
59ecbafa 1153 $emitted = emit_li( 'ol' );
1154 if ($text =~ /\A(?>\d+\.?)\s*(.+)\Z/s ) { # with additional text
1155 my $tag = $1;
1156 $otext =~ s/\A\d+\.?\s*//;
1157 emit_item_tag( $otext, $tag, 1 );
1158 }
54310121 1159
2a28b791 1160 } else { # definition list
59ecbafa 1161 $emitted = emit_li( 'dl' );
1162 if ($text =~ /\A(.+)\Z/s ){ # should have text
1163 emit_item_tag( $otext, $text, 1 );
1164 }
1165 $need_dd = 1;
54310121 1166 }
59ecbafa 1167 print HTML "</$emitted>" if $emitted;
54310121 1168 print HTML "\n";
59ecbafa 1169 return $need_dd;
54310121 1170}
1171
1172#
2a28b791 1173# process_over - process a pod over tag and start a corresponding HTML list.
54310121 1174#
1175sub process_over {
1176 # start a new list
1177 $listlevel++;
2a28b791 1178 push( @items_seen, 0 );
1179 $after_lpar = 0;
54310121 1180}
1181
1182#
1183# process_back - process a pod back tag and convert it to HTML format.
1184#
1185sub process_back {
2a28b791 1186 if( $listlevel == 0 ){
1187 warn "$0: $podfile: unexpected =back directive in paragraph $paragraph. ignoring.\n";
1188 return;
1189 }
54310121 1190
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.
1194 $listlevel--;
2a28b791 1195 if( defined $listend[$listlevel] ){
59ecbafa 1196 print HTML '<p></p>' if $after_lpar;
2a28b791 1197 print HTML $listend[$listlevel];
1198 print HTML "\n";
1199 pop( @listend );
1200 }
1201 $after_lpar = 0;
54310121 1202
2a28b791 1203 # clean up item count
1204 pop( @items_seen );
54310121 1205}
1206
1207#
2a28b791 1208# process_cut - process a pod cut tag, thus start ignoring pod directives.
54310121 1209#
1210sub process_cut {
1211 $ignore = 1;
1212}
1213
1214#
d1be9408 1215# process_pod - process a pod tag, thus stop ignoring pod directives
2a28b791 1216# until we see a corresponding cut.
54310121 1217#
1218sub process_pod {
1219 # no need to set $ignore to 0 cause the main loop did it
1220}
1221
1222#
2a28b791 1223# process_for - process a =for pod tag. if it's for html, spit
c4d9b39d 1224# it out verbatim, if illustration, center it, otherwise ignore it.
54310121 1225#
1226sub process_for {
1227 my($whom, $text) = @_;
1228 if ( $whom =~ /^(pod2)?html$/i) {
1229 print HTML $text;
c4d9b39d 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";
1234 }
59ecbafa 1235 print HTML qq{<p align="center"><img src="$text" alt="$text illustration" /></p>};
c4d9b39d 1236 }
54310121 1237}
1238
1239#
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.
1243#
1244sub process_begin {
1245 my($whom, $text) = @_;
1246 $whom = lc($whom);
1247 push (@begin_stack, $whom);
1248 if ( $whom =~ /^(pod2)?html$/) {
1249 print HTML $text if $text;
1250 }
1251}
1252
1253#
1254# process_end - process a =end pod tag. pop the
1255# begin stack. die if we're mismatched.
1256#
1257sub process_end {
1258 my($whom, $text) = @_;
1259 $whom = lc($whom);
1260 if ($begin_stack[-1] ne $whom ) {
1261 die "Unmatched begin/end at chunk $paragraph\n"
59ecbafa 1262 }
2a28b791 1263 pop( @begin_stack );
54310121 1264}
1265
1266#
59ecbafa 1267# process_pre - indented paragraph, made into <pre></pre>
54310121 1268#
2a28b791 1269sub process_pre {
1270 my( $text ) = @_;
1271 my( $rest );
54310121 1272 return if $ignore;
1273
54310121 1274 $rest = $$text;
1275
2a28b791 1276 # insert spaces in place of tabs
24e08cba 1277 $rest =~ s#(.+)#
1278 my $line = $1;
1279 1 while $line =~ s/(\t+)/' ' x ((length($1) * 8) - $-[0] % 8)/e;
be173d55 1280 $line;
1281 #eg;
54310121 1282
2a28b791 1283 # convert some special chars to HTML escapes
59ecbafa 1284 $rest = html_escape($rest);
2a28b791 1285
1286 # try and create links for all occurrences of perl.* within
1287 # the preformatted text.
1288 $rest =~ s{
1289 (\s*)(perl\w+)
1290 }{
1291 if ( defined $pages{$2} ){ # is a link
59ecbafa 1292 qq($1<a href="$htmlroot/$pages{$2}">$2</a>);
2a28b791 1293 } elsif (defined $pages{dosify($2)}) { # is a link
59ecbafa 1294 qq($1<a href="$htmlroot/$pages{dosify($2)}">$2</a>);
2a28b791 1295 } else {
1296 "$1$2";
1297 }
1298 }xeg;
1299 $rest =~ s{
59ecbafa 1300 (<a\ href="?) ([^>:]*:)? ([^>:]*) \.pod: ([^>:]*:)?
2a28b791 1301 }{
1302 my $url ;
1303 if ( $htmlfileurl ne '' ){
59ecbafa 1304 # Here, we take advantage of the knowledge
2a28b791 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.
1310 my $old_url = $3 ;
1311 $old_url = "$htmldir$old_url" if $old_url =~ m{^\/};
1312 $url = relativize_url( "$old_url.html", $htmlfileurl );
1313 } else {
1314 $url = "$3.html" ;
1315 }
1316 "$1$url" ;
1317 }xeg;
1318
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.
1321
1322 my $urls = '(' . join ('|', qw{
54310121 1323 http
1324 telnet
1325 mailto
1326 news
1327 gopher
1328 file
1329 wais
1330 ftp
59ecbafa 1331 } )
54310121 1332 . ')';
59ecbafa 1333
2a28b791 1334 my $ltrs = '\w';
1335 my $gunk = '/#~:.?+=&%@!\-';
add5afb0 1336 my $punc = '.:!?\-;';
2a28b791 1337 my $any = "${ltrs}${gunk}${punc}";
54310121 1338
2a28b791 1339 $rest =~ s{
54310121 1340 \b # start at word boundary
1341 ( # begin $1 {
29f227c9 1342 $urls : # need resource and a colon
1343 (?!:) # Ignore File::, among others.
add5afb0 1344 [$any] +? # followed by one or more of any valid
1345 # character, but be conservative and
1346 # take only what you need to....
54310121 1347 ) # end $1 }
1348 (?= # look-ahead non-consumptive assertion
add5afb0 1349 [$punc]* # either 0 or more punctuation
1350 (?: # followed
1351 [^$any] # by a non-url char
1352 | # or
1353 $ # end of the string
1354 ) #
54310121 1355 | # or else
1356 $ # then end of the string
1357 )
59ecbafa 1358 }{<a href="$1">$1</a>}igox;
54310121 1359
2a28b791 1360 # text should be as it is (verbatim)
1361 $$text = $rest;
1362}
54310121 1363
54310121 1364
2a28b791 1365#
1366# pure text processing
1367#
1368# pure_text/inIS_text: differ with respect to automatic C<> recognition.
1369# we don't want this to happen within IS
1370#
1371sub pure_text($){
1372 my $text = shift();
1373 process_puretext( $text, \$ptQuote, 1 );
54310121 1374}
1375
2a28b791 1376sub inIS_text($){
1377 my $text = shift();
1378 process_puretext( $text, \$ptQuote, 0 );
1379}
54310121 1380
1381#
1382# process_puretext - process pure text (without pod-escapes) converting
1383# double-quotes and handling implicit C<> links.
1384#
1385sub process_puretext {
2a28b791 1386 my($text, $quote, $notinIS) = @_;
54310121 1387
2a28b791 1388 ## Guessing at func() or [$@%&]*var references in plain text is destined
1389 ## to produce some strange looking ref's. uncomment to disable:
1390 ## $notinIS = 0;
1391
1392 my(@words, $lead, $trail);
54310121 1393
2a28b791 1394 # convert double-quotes to single-quotes
1395 if( $$quote && $text =~ s/"/''/s ){
1396 $$quote = 0;
1397 }
1398 while ($text =~ s/"([^"]*)"/``$1''/sg) {};
1399 $$quote = 1 if $text =~ s/"/``/s;
54310121 1400
1401 # keep track of leading and trailing white-space
2a28b791 1402 $lead = ($text =~ s/\A(\s+)//s ? $1 : "");
1403 $trail = ($text =~ s/(\s+)\Z//s ? $1 : "");
54310121 1404
2a28b791 1405 # split at space/non-space boundaries
1406 @words = split( /(?<=\s)(?=\S)|(?<=\S)(?=\s)/, $text );
54310121 1407
1408 # process each word individually
1409 foreach my $word (@words) {
2a28b791 1410 # skip space runs
1411 next if $word =~ /^\s*$/;
54310121 1412 # see if we can infer a link
02369fa5 1413 if( $notinIS && $word =~ /^(\w+)\((.*)\)$/ ) {
54310121 1414 # has parenthesis so should have been a C<> ref
2a28b791 1415 ## try for a pagename (perlXXX(1))?
02369fa5 1416 my( $func, $args ) = ( $1, $2 );
1417 if( $args =~ /^\d+$/ ){
2a28b791 1418 my $url = page_sect( $word, '' );
1419 if( defined $url ){
59ecbafa 1420 $word = "<a href=\"$url\">the $word manpage</a>";
2a28b791 1421 next;
1422 }
1423 }
02369fa5 1424 ## try function name for a link, append tt'ed argument list
1425 $word = emit_C( $func, '', "($args)");
2a28b791 1426
1427#### disabled. either all (including $\W, $\w+{.*} etc.) or nothing.
1428## } elsif( $notinIS && $word =~ /^[\$\@%&*]+\w+$/) {
1429## # perl variables, should be a C<> ref
1430## $word = emit_C( $word );
1431
54310121 1432 } elsif ($word =~ m,^\w+://\w,) {
1433 # looks like a URL
5a039dd3 1434 # Don't relativize it: leave it as the author intended
59ecbafa 1435 $word = qq(<a href="$word">$word</a>);
af47ee55 1436 } elsif ($word =~ /[\w.-]+\@[\w-]+\.\w/) {
54310121 1437 # looks like an e-mail address
7b8d334a 1438 my ($w1, $w2, $w3) = ("", $word, "");
1439 ($w1, $w2, $w3) = ("(", $1, ")$2") if $word =~ /^\((.*?)\)(,?)/;
1440 ($w1, $w2, $w3) = ("&lt;", $1, "&gt;$2") if $word =~ /^<(.*?)>(,?)/;
59ecbafa 1441 $word = qq($w1<a href="mailto:$w2">$w2</a>$w3);
1442 } else {
7b8d334a 1443 $word = html_escape($word) if $word =~ /["&<>]/;
54310121 1444 }
1445 }
1446
2a28b791 1447 # put everything back together
1448 return $lead . join( '', @words ) . $trail;
1449}
1450
54310121 1451
2a28b791 1452#
1453# process_text - handles plaintext that appears in the input pod file.
1454# there may be pod commands embedded within the text so those must be
1455# converted to html commands.
1456#
7ba65c74 1457
c68ea5d1 1458sub process_text1($$;$$);
1459sub pattern ($) { $_[0] ? '[^\S\n]+'.('>' x ($_[0] + 1)) : '>' }
1460sub closing ($) { local($_) = shift; (defined && s/\s+$//) ? length : 0 }
7ba65c74 1461
2a28b791 1462sub process_text {
1463 return if $ignore;
1464 my( $tref ) = @_;
1465 my $res = process_text1( 0, $tref );
1466 $$tref = $res;
1467}
1468
c68ea5d1 1469sub process_text1($$;$$){
1470 my( $lev, $rstr, $func, $closing ) = @_;
2a28b791 1471 my $res = '';
1472
60a48b2d 1473 unless (defined $func) {
1474 $func = '';
1475 $lev++;
1476 }
1477
2a28b791 1478 if( $func eq 'B' ){
1479 # B<text> - boldface
59ecbafa 1480 $res = '<strong>' . process_text1( $lev, $rstr ) . '</strong>';
2a28b791 1481
1482 } elsif( $func eq 'C' ){
59ecbafa 1483 # C<code> - can be a ref or <code></code>
2a28b791 1484 # need to extract text
c68ea5d1 1485 my $par = go_ahead( $rstr, 'C', $closing );
2a28b791 1486
1487 ## clean-up of the link target
1488 my $text = depod( $par );
1489
1490 ### my $x = $par =~ /[BI]</ ? 'yes' : 'no' ;
59ecbafa 1491 ### print STDERR "-->call emit_C($par) lev=$lev, par with BI=$x\n";
2a28b791 1492
1493 $res = emit_C( $text, $lev > 1 || ($par =~ /[BI]</) );
1494
1495 } elsif( $func eq 'E' ){
1496 # E<x> - convert to character
be3174d2 1497 $$rstr =~ s/^([^>]*)>//;
1498 my $escape = $1;
1499 $escape =~ s/^(\d+|X[\dA-F]+)$/#$1/i;
1500 $res = "&$escape;";
2a28b791 1501
1502 } elsif( $func eq 'F' ){
1503 # F<filename> - italizice
59ecbafa 1504 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
2a28b791 1505
1506 } elsif( $func eq 'I' ){
1507 # I<text> - italizice
59ecbafa 1508 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
2a28b791 1509
1510 } elsif( $func eq 'L' ){
1511 # L<link> - link
59ecbafa 1512 ## L<text|cross-ref> => produce text, use cross-ref for linking
2a28b791 1513 ## L<cross-ref> => make text from cross-ref
1514 ## need to extract text
c68ea5d1 1515 my $par = go_ahead( $rstr, 'L', $closing );
2a28b791 1516
1517 # some L<>'s that shouldn't be:
1518 # a) full-blown URL's are emitted as-is
1519 if( $par =~ m{^\w+://}s ){
1520 return make_URL_href( $par );
1521 }
1522 # b) C<...> is stripped and treated as C<>
1523 if( $par =~ /^C<(.*)>$/ ){
1524 my $text = depod( $1 );
1525 return emit_C( $text, $lev > 1 || ($par =~ /[BI]</) );
1526 }
1527
1528 # analyze the contents
1529 $par =~ s/\n/ /g; # undo word-wrapped tags
1530 my $opar = $par;
1531 my $linktext;
1532 if( $par =~ s{^([^|]+)\|}{} ){
1533 $linktext = $1;
1534 }
59ecbafa 1535
2a28b791 1536 # make sure sections start with a /
1537 $par =~ s{^"}{/"};
1538
1539 my( $page, $section, $ident );
1540
1541 # check for link patterns
1542 if( $par =~ m{^([^/]+?)/(?!")(.*?)$} ){ # name/ident
59ecbafa 1543 # we've got a name/ident (no quotes)
2a28b791 1544 ( $page, $ident ) = ( $1, $2 );
1545 ### print STDERR "--> L<$par> to page $page, ident $ident\n";
1546
1547 } elsif( $par =~ m{^(.*?)/"?(.*?)"?$} ){ # [name]/"section"
1548 # even though this should be a "section", we go for ident first
1549 ( $page, $ident ) = ( $1, $2 );
1550 ### print STDERR "--> L<$par> to page $page, section $section\n";
1551
1552 } elsif( $par =~ /\s/ ){ # this must be a section with missing quotes
1553 ( $page, $section ) = ( '', $par );
1554 ### print STDERR "--> L<$par> to void page, section $section\n";
1555
1556 } else {
1557 ( $page, $section ) = ( $par, '' );
1558 ### print STDERR "--> L<$par> to page $par, void section\n";
1559 }
1560
1561 # now, either $section or $ident is defined. the convoluted logic
1562 # below tries to resolve L<> according to what the user specified.
1563 # failing this, we try to find the next best thing...
1564 my( $url, $ltext, $fid );
1565
1566 RESOLVE: {
1567 if( defined $ident ){
1568 ## try to resolve $ident as an item
1569 ( $url, $fid ) = coderef( $page, $ident );
1570 if( $url ){
1571 if( ! defined( $linktext ) ){
1572 $linktext = $ident;
1573 $linktext .= " in " if $ident && $page;
1574 $linktext .= "the $page manpage" if $page;
1575 }
1576 ### print STDERR "got coderef url=$url\n";
1577 last RESOLVE;
1578 }
1579 ## no luck: go for a section (auto-quoting!)
1580 $section = $ident;
1581 }
1582 ## now go for a section
1583 my $htmlsection = htmlify( $section );
1584 $url = page_sect( $page, $htmlsection );
1585 if( $url ){
1586 if( ! defined( $linktext ) ){
1587 $linktext = $section;
1588 $linktext .= " in " if $section && $page;
1589 $linktext .= "the $page manpage" if $page;
1590 }
1591 ### print STDERR "got page/section url=$url\n";
1592 last RESOLVE;
1593 }
59ecbafa 1594 ## no luck: go for an ident
2a28b791 1595 if( $section ){
1596 $ident = $section;
1597 } else {
1598 $ident = $page;
1599 $page = undef();
1600 }
1601 ( $url, $fid ) = coderef( $page, $ident );
1602 if( $url ){
1603 if( ! defined( $linktext ) ){
1604 $linktext = $ident;
1605 $linktext .= " in " if $ident && $page;
1606 $linktext .= "the $page manpage" if $page;
1607 }
1608 ### print STDERR "got section=>coderef url=$url\n";
1609 last RESOLVE;
1610 }
1611
1612 # warning; show some text.
1613 $linktext = $opar unless defined $linktext;
f8f99792 1614 warn "$0: $podfile: cannot resolve L<$opar> in paragraph $paragraph.\n";
2a28b791 1615 }
1616
d1be9408 1617 # now we have a URL or just plain code
2a28b791 1618 $$rstr = $linktext . '>' . $$rstr;
1619 if( defined( $url ) ){
59ecbafa 1620 $res = "<a href=\"$url\">" . process_text1( $lev, $rstr ) . '</a>';
2a28b791 1621 } else {
59ecbafa 1622 $res = '<em>' . process_text1( $lev, $rstr ) . '</em>';
2a28b791 1623 }
1624
1625 } elsif( $func eq 'S' ){
1626 # S<text> - non-breaking spaces
1627 $res = process_text1( $lev, $rstr );
1628 $res =~ s/ /&nbsp;/g;
1629
1630 } elsif( $func eq 'X' ){
1631 # X<> - ignore
1632 $$rstr =~ s/^[^>]*>//;
1633
1634 } elsif( $func eq 'Z' ){
59ecbafa 1635 # Z<> - empty
f8f99792 1636 warn "$0: $podfile: invalid X<> in paragraph $paragraph.\n"
2a28b791 1637 unless $$rstr =~ s/^>//;
1638
1639 } else {
c68ea5d1 1640 my $term = pattern $closing;
1641 while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)//s ){
2a28b791 1642 # all others: either recurse into new function or
c68ea5d1 1643 # terminate at closing angle bracket(s)
2a28b791 1644 my $pt = $1;
c68ea5d1 1645 $pt .= $2 if !$3 && $lev == 1;
2a28b791 1646 $res .= $lev == 1 ? pure_text( $pt ) : inIS_text( $pt );
c68ea5d1 1647 return $res if !$3 && $lev > 1;
1648 if( $3 ){
1649 $res .= process_text1( $lev, $rstr, $3, closing $4 );
1650 }
2a28b791 1651 }
1652 if( $lev == 1 ){
1653 $res .= pure_text( $$rstr );
54310121 1654 } else {
f8f99792 1655 warn "$0: $podfile: undelimited $func<> in paragraph $paragraph.\n";
54310121 1656 }
1657 }
2a28b791 1658 return $res;
1659}
54310121 1660
2a28b791 1661#
1662# go_ahead: extract text of an IS (can be nested)
1663#
c68ea5d1 1664sub go_ahead($$$){
1665 my( $rstr, $func, $closing ) = @_;
2a28b791 1666 my $res = '';
c68ea5d1 1667 my @closing = ($closing);
1668 while( $$rstr =~
1669 s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|@{[pattern $closing[0]]})//s ){
2a28b791 1670 $res .= $1;
c68ea5d1 1671 unless( $3 ){
1672 shift @closing;
1673 return $res unless @closing;
2a28b791 1674 } else {
c68ea5d1 1675 unshift @closing, closing $4;
2a28b791 1676 }
1677 $res .= $2;
1678 }
f8f99792 1679 warn "$0: $podfile: undelimited $func<> in paragraph $paragraph.\n";
2a28b791 1680 return $res;
54310121 1681}
1682
1683#
2a28b791 1684# emit_C - output result of C<text>
1685# $text is the depod-ed text
54310121 1686#
02369fa5 1687sub emit_C($;$$){
1688 my( $text, $nocode, $args ) = @_;
60a48b2d 1689 $args = '' unless defined $args;
2a28b791 1690 my $res;
1691 my( $url, $fid ) = coderef( undef(), $text );
1692
1693 # need HTML-safe text
02369fa5 1694 my $linktext = html_escape( "$text$args" );
2a28b791 1695
1696 if( defined( $url ) &&
1697 (!defined( $EmittedItem ) || $EmittedItem ne $fid ) ){
59ecbafa 1698 $res = "<a href=\"$url\"><code>$linktext</code></a>";
2a28b791 1699 } elsif( 0 && $nocode ){
1700 $res = $linktext;
1701 } else {
59ecbafa 1702 $res = "<code>$linktext</code>";
2a28b791 1703 }
1704 return $res;
54310121 1705}
1706
1707#
2a28b791 1708# html_escape: make text safe for HTML
1709#
1710sub html_escape {
1711 my $rest = $_[0];
1712 $rest =~ s/&/&amp;/g;
1713 $rest =~ s/</&lt;/g;
1714 $rest =~ s/>/&gt;/g;
1715 $rest =~ s/"/&quot;/g;
5b25816d 1716 # &apos; is only in XHTML, not HTML4. Be conservative
1717 #$rest =~ s/'/&apos;/g;
2a28b791 1718 return $rest;
59ecbafa 1719}
2a28b791 1720
1721
1722#
39e571d4 1723# dosify - convert filenames to 8.3
1724#
1725sub dosify {
1726 my($str) = @_;
fe4c6be1 1727 return lc($str) if $^O eq 'VMS'; # VMS just needs casing
39e571d4 1728 if ($Is83) {
1729 $str = lc $str;
1730 $str =~ s/(\.\w+)/substr ($1,0,4)/ge;
1731 $str =~ s/(\w+)/substr ($1,0,8)/ge;
1732 }
1733 return $str;
1734}
1735
1736#
d1be9408 1737# page_sect - make a URL from the text of a L<>
54310121 1738#
2a28b791 1739sub page_sect($$) {
1740 my( $page, $section ) = @_;
1741 my( $linktext, $page83, $link); # work strings
1742
1743 # check if we know that this is a section in this page
1744 if (!defined $pages{$page} && defined $sections{$page}) {
1745 $section = $page;
1746 $page = "";
1747 ### print STDERR "reset page='', section=$section\n";
54310121 1748 }
1749
39e571d4 1750 $page83=dosify($page);
1751 $page=$page83 if (defined $pages{$page83});
54310121 1752 if ($page eq "") {
0d396dd4 1753 $link = "#" . anchorify( $section );
350ccacd 1754 } elsif ( $page =~ /::/ ) {
350ccacd 1755 $page =~ s,::,/,g;
29f227c9 1756 # Search page cache for an entry keyed under the html page name,
1757 # then look to see what directory that page might be in. NOTE:
1758 # this will only find one page. A better solution might be to produce
1759 # an intermediate page that is an index to all such pages.
1760 my $page_name = $page ;
fe6f1558 1761 $page_name =~ s,^.*/,,s ;
59ecbafa 1762 if ( defined( $pages{ $page_name } ) &&
1763 $pages{ $page_name } =~ /([^:]*$page)\.(?:pod|pm):/
29f227c9 1764 ) {
1765 $page = $1 ;
1766 }
1767 else {
1768 # NOTE: This branch assumes that all A::B pages are located in
1769 # $htmlroot/A/B.html . This is often incorrect, since they are
1770 # often in $htmlroot/lib/A/B.html or such like. Perhaps we could
1771 # analyze the contents of %pages and figure out where any
1772 # cousins of A::B are, then assume that. So, if A::B isn't found,
1773 # but A::C is found in lib/A/C.pm, then A::B is assumed to be in
1774 # lib/A/B.pm. This is also limited, but it's an improvement.
1775 # Maybe a hints file so that the links point to the correct places
2a28b791 1776 # nonetheless?
1777
29f227c9 1778 }
350ccacd 1779 $link = "$htmlroot/$page.html";
0d396dd4 1780 $link .= "#" . anchorify( $section ) if ($section);
54310121 1781 } elsif (!defined $pages{$page}) {
54310121 1782 $link = "";
54310121 1783 } else {
0d396dd4 1784 $section = anchorify( $section ) if $section ne "";
2a28b791 1785 ### print STDERR "...section=$section\n";
54310121 1786
1787 # if there is a directory by the name of the page, then assume that an
1788 # appropriate section will exist in the subdirectory
29f227c9 1789# if ($section ne "" && $pages{$page} =~ /([^:]*[^(\.pod|\.pm)]):/) {
1790 if ($section ne "" && $pages{$page} =~ /([^:]*(?<!\.pod)(?<!\.pm)):/) {
54310121 1791 $link = "$htmlroot/$1/$section.html";
2a28b791 1792 ### print STDERR "...link=$link\n";
54310121 1793
1794 # since there is no directory by the name of the page, the section will
1795 # have to exist within a .html of the same name. thus, make sure there
1796 # is a .pod or .pm that might become that .html
1797 } else {
2a28b791 1798 $section = "#$section" if $section;
1799 ### print STDERR "...section=$section\n";
1800
54310121 1801 # check if there is a .pod with the page name
1802 if ($pages{$page} =~ /([^:]*)\.pod:/) {
1803 $link = "$htmlroot/$1.html$section";
1804 } elsif ($pages{$page} =~ /([^:]*)\.pm:/) {
1805 $link = "$htmlroot/$1.html$section";
1806 } else {
54310121 1807 $link = "";
54310121 1808 }
1809 }
1810 }
1811
54310121 1812 if ($link) {
29f227c9 1813 # Here, we take advantage of the knowledge that $htmlfileurl ne ''
1814 # implies $htmlroot eq ''. This means that the link in question
1815 # needs a prefix of $htmldir if it begins with '/'. The test for
1816 # the initial '/' is done to avoid '#'-only links, and to allow
1817 # for other kinds of links, like file:, ftp:, etc.
1818 my $url ;
1819 if ( $htmlfileurl ne '' ) {
fe6f1558 1820 $link = "$htmldir$link" if $link =~ m{^/}s;
2a28b791 1821 $url = relativize_url( $link, $htmlfileurl );
1822# print( " b: [$link,$htmlfileurl,$url]\n" );
29f227c9 1823 }
1824 else {
1825 $url = $link ;
1826 }
2a28b791 1827 return $url;
29f227c9 1828
54310121 1829 } else {
2a28b791 1830 return undef();
54310121 1831 }
54310121 1832}
1833
1834#
29f227c9 1835# relativize_url - convert an absolute URL to one relative to a base URL.
1836# Assumes both end in a filename.
1837#
1838sub relativize_url {
1839 my ($dest,$source) = @_ ;
1840
59ecbafa 1841 my ($dest_volume,$dest_directory,$dest_file) =
29f227c9 1842 File::Spec::Unix->splitpath( $dest ) ;
1843 $dest = File::Spec::Unix->catpath( $dest_volume, $dest_directory, '' ) ;
1844
59ecbafa 1845 my ($source_volume,$source_directory,$source_file) =
29f227c9 1846 File::Spec::Unix->splitpath( $source ) ;
1847 $source = File::Spec::Unix->catpath( $source_volume, $source_directory, '' ) ;
1848
1849 my $rel_path = '' ;
1850 if ( $dest ne '' ) {
1851 $rel_path = File::Spec::Unix->abs2rel( $dest, $source ) ;
1852 }
1853
59ecbafa 1854 if ( $rel_path ne '' &&
29f227c9 1855 substr( $rel_path, -1 ) ne '/' &&
59ecbafa 1856 substr( $dest_file, 0, 1 ) ne '#'
29f227c9 1857 ) {
1858 $rel_path .= "/$dest_file" ;
1859 }
1860 else {
1861 $rel_path .= "$dest_file" ;
1862 }
1863
1864 return $rel_path ;
1865}
1866
54310121 1867
1868#
2a28b791 1869# coderef - make URL from the text of a C<>
54310121 1870#
2a28b791 1871sub coderef($$){
1872 my( $page, $item ) = @_;
1873 my( $url );
1874
1875 my $fid = fragment_id( $item );
2a28b791 1876 if( defined( $page ) ){
1877 # we have been given a $page...
1878 $page =~ s{::}{/}g;
1879
1880 # Do we take it? Item could be a section!
228a48a5 1881 my $base = $items{$fid} || "";
2a28b791 1882 $base =~ s{[^/]*/}{};
1883 if( $base ne "$page.html" ){
1884 ### print STDERR "coderef( $page, $item ): items{$fid} = $items{$fid} = $base => discard page!\n";
1885 $page = undef();
1886 }
54310121 1887
2a28b791 1888 } else {
1889 # no page - local items precede cached items
67398a75 1890 if( defined( $fid ) ){
1891 if( exists $local_items{$fid} ){
1892 $page = $local_items{$fid};
1893 } else {
1894 $page = $items{$fid};
1895 }
2a28b791 1896 }
1897 }
54310121 1898
1899 # if there was a pod file that we found earlier with an appropriate
1900 # =item directive, then create a link to that page.
2a28b791 1901 if( defined $page ){
1902 if( $page ){
228a48a5 1903 if( exists $pages{$page} and $pages{$page} =~ /([^:.]*)\.[^:]*:/){
2a28b791 1904 $page = $1 . '.html';
29f227c9 1905 }
2a28b791 1906 my $link = "$htmlroot/$page#item_$fid";
54310121 1907
2a28b791 1908 # Here, we take advantage of the knowledge that $htmlfileurl
1909 # ne '' implies $htmlroot eq ''.
1910 if ( $htmlfileurl ne '' ) {
1911 $link = "$htmldir$link" ;
1912 $url = relativize_url( $link, $htmlfileurl ) ;
1913 } else {
1914 $url = $link ;
1915 }
1916 } else {
1917 $url = "#item_" . $fid;
1918 }
54310121 1919
2a28b791 1920 confess "url has space: $url" if $url =~ /"[^"]*\s[^"]*"/;
59ecbafa 1921 }
2a28b791 1922 return( $url, $fid );
54310121 1923}
1924
54310121 1925
1926
1927#
29f227c9 1928# Adapted from Nick Ing-Simmons' PodToHtml package.
1929sub relative_url {
1930 my $source_file = shift ;
1931 my $destination_file = shift;
1932
1933 my $source = URI::file->new_abs($source_file);
1934 my $uo = URI::file->new($destination_file,$source)->abs;
1935 return $uo->rel->as_string;
1936}
1937
1938
1939#
54310121 1940# finish_list - finish off any pending HTML lists. this should be called
1941# after the entire pod file has been read and converted.
1942#
1943sub finish_list {
7b8d334a 1944 while ($listlevel > 0) {
59ecbafa 1945 print HTML "</dl>\n";
54310121 1946 $listlevel--;
1947 }
1948}
1949
1950#
1951# htmlify - converts a pod section specification to a suitable section
2a28b791 1952# specification for HTML. Note that we keep spaces and special characters
1953# except ", ? (Netscape problem) and the hyphen (writer's problem...).
54310121 1954#
1955sub htmlify {
2a28b791 1956 my( $heading) = @_;
1957 $heading =~ s/(\s+)/ /g;
1958 $heading =~ s/\s+\Z//;
1959 $heading =~ s/\A\s+//;
1960 # The hyphen is a disgrace to the English language.
1961 $heading =~ s/[-"?]//g;
1962 $heading = lc( $heading );
1963 return $heading;
1964}
54310121 1965
2a28b791 1966#
0d396dd4 1967# similar to htmlify, but turns spaces into underscores
1968#
1969sub anchorify {
1970 my ($anchor) = @_;
1971 $anchor = htmlify($anchor);
1972 $anchor =~ s/\s/_/g; # fixup spaces left by htmlify
1973 return $anchor;
1974}
1975
1976#
2a28b791 1977# depod - convert text by eliminating all interior sequences
1978# Note: can be called with copy or modify semantics
1979#
1980my %E2c;
67398a75 1981$E2c{lt} = '<';
1982$E2c{gt} = '>';
1983$E2c{sol} = '/';
2a28b791 1984$E2c{verbar} = '|';
67398a75 1985$E2c{amp} = '&'; # in Tk's pods
2a28b791 1986
c68ea5d1 1987sub depod1($;$$);
7ba65c74 1988
2a28b791 1989sub depod($){
1990 my $string;
1991 if( ref( $_[0] ) ){
1992 $string = ${$_[0]};
1993 ${$_[0]} = depod1( \$string );
1994 } else {
1995 $string = $_[0];
1996 depod1( \$string );
59ecbafa 1997 }
2a28b791 1998}
54310121 1999
c68ea5d1 2000sub depod1($;$$){
2001 my( $rstr, $func, $closing ) = @_;
2a28b791 2002 my $res = '';
228a48a5 2003 return $res unless defined $$rstr;
2a28b791 2004 if( ! defined( $func ) ){
2005 # skip to next begin of an interior sequence
c68ea5d1 2006 while( $$rstr =~ s/\A(.*?)([BCEFILSXZ])<(<+[^\S\n]+)?// ){
2a28b791 2007 # recurse into its text
c68ea5d1 2008 $res .= $1 . depod1( $rstr, $2, closing $3);
2a28b791 2009 }
2010 $res .= $$rstr;
2011 } elsif( $func eq 'E' ){
2012 # E<x> - convert to character
be3174d2 2013 $$rstr =~ s/^([^>]*)>//;
228a48a5 2014 $res .= $E2c{$1} || "";
2a28b791 2015 } elsif( $func eq 'X' ){
2016 # X<> - ignore
2017 $$rstr =~ s/^[^>]*>//;
2018 } elsif( $func eq 'Z' ){
59ecbafa 2019 # Z<> - empty
2a28b791 2020 $$rstr =~ s/^>//;
2021 } else {
2022 # all others: either recurse into new function or
2023 # terminate at closing angle bracket
c68ea5d1 2024 my $term = pattern $closing;
2025 while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)// ){
2a28b791 2026 $res .= $1;
c68ea5d1 2027 last unless $3;
2028 $res .= depod1( $rstr, $3, closing $4 );
2a28b791 2029 }
2030 ## If we're here and $2 ne '>': undelimited interior sequence.
2031 ## Ignored, as this is called without proper indication of where we are.
2032 ## Rely on process_text to produce diagnostics.
2033 }
2034 return $res;
2035}
54310121 2036
2a28b791 2037#
2038# fragment_id - construct a fragment identifier from:
2039# a) =item text
2040# b) contents of C<...>
2041#
2042my @hc;
2043sub fragment_id {
2044 my $text = shift();
2045 $text =~ s/\s+\Z//s;
2046 if( $text ){
2047 # a method or function?
2048 return $1 if $text =~ /(\w+)\s*\(/;
2049 return $1 if $text =~ /->\s*(\w+)\s*\(?/;
2050
2051 # a variable name?
2052 return $1 if $text =~ /^([$@%*]\S+)/;
2053
2054 # some pattern matching operator?
2055 return $1 if $text =~ m|^(\w+/).*/\w*$|;
2056
2057 # fancy stuff... like "do { }"
2058 return $1 if $text =~ m|^(\w+)\s*{.*}$|;
2059
2060 # honour the perlfunc manpage: func [PAR[,[ ]PAR]...]
2061 # and some funnies with ... Module ...
2062 return $1 if $text =~ m{^([a-z\d]+)(\s+[A-Z\d,/& ]+)?$};
2063 return $1 if $text =~ m{^([a-z\d]+)\s+Module(\s+[A-Z\d,/& ]+)?$};
2064
2065 # text? normalize!
2066 $text =~ s/\s+/_/sg;
2067 $text =~ s{(\W)}{
2068 defined( $hc[ord($1)] ) ? $hc[ord($1)]
2069 : ( $hc[ord($1)] = sprintf( "%%%02X", ord($1) ) ) }gxe;
2070 $text = substr( $text, 0, 50 );
2071 } else {
2072 return undef();
2073 }
54310121 2074}
2075
2a28b791 2076#
2077# make_URL_href - generate HTML href from URL
2078# Special treatment for CGI queries.
2079#
2080sub make_URL_href($){
2081 my( $url ) = @_;
59ecbafa 2082 if( $url !~
2083 s{^(http:[-\w/#~:.+=&%@!]+)(\?.*)$}{<a href="$1$2">$1</a>}i ){
2084 $url = "<a href=\"$url\">$url</a>";
2a28b791 2085 }
2086 return $url;
54310121 2087}
2088
20891;