Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2025 lines
58 KiB

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