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