Closed p5pRT closed 20 years ago
(Note: I'm aware of the ongoing work concerning Brad Appleton's Pod::Parser and Nick Ing-Simmon's podtohtml. Still\, IMHO\, the core Perl distribution should have a tool for converting pod to HTML\, and podtohtml requires not only the POD::Parser family\, but also Font-AFM\, HTML-Format\, HTML-Parser\, HTML-Tree\, MIME-Base64\, PodToHTML and URI. So...)
This was inspired by the failure of pod2html (Pod::Html.pm versions 1.01 and 1.02) to convert pod's with =item's showing method syntax ($obj->func(arg))\, as found in the Tk manpages and many others. While trying to add a few patches to process the text in =head's and =item's\, several things went awry\, so much of the basic text processing had to be rewritten.
All of file handling\, scanning and caching is still as it was.
I've run this on all the pod's from Tk800.015. These look quite good (Netscape 4.61 on Linux) except for a few spots that are not so well written in the original pod.
Perl 5.00503/pod/*.pod produce a handy set of HTML files\, with many of the cross reference links working nicely. Some link failures result from misquoted things\, and there are one or two syntax errors in the .pod files. Weird things like "...see L\<$/>..." (in perlvar.pod) are not resolved (and should never be\, IMO).
There seems to be some confusion and/or disagreement on the pod authors'
side as to what is possible in L\<>. The perlpod manpage says:
L\
Recognition of URL's in verbatim paragraphs should probably be removed. See e.g. perlfaq1\, "http://x1.dejanews.com/...". In other places\, code examples show nonsense links.
Bugs fixed:
Recognition of preformatted pod paragraphs removed from process_text as
this was causing trouble in the recursive descent when there was
leading space in an S\<> interior sequence.
---
Cleaned up the item list stack arrays. (Pushing and popping of
@items_seen could run wild and cause additional list starts.)
---
process_item: Numbered lists were showing a spurious "." after the item
number.
---
"&" appeared in place of a "&" in an =item text.
---
Process text on =item and =head to honor all escapes. (Avoids
garbled =item and =head text\, particularly in Tk pod's where -E\
Improvements:
Links to headers use full text\, with only a few chars removed. Likewise\,
links to items use as much text as possible\, with special recognition for
functions\, methods (...->method(...)\, variables and items in perlfunc.
---
Locating L\<> and C\<> href targets (sections\, idents) has been improved.
---
L\<> also handles "outside" URL's (eg. http://...)
---
Mail adresses are recognized and mailto-ed even when inside F\<>
---
removed $opt_norecurse from options (as there is a negatable
$opt_recurse).
---
process_item: no need to have # in the pattern together with \d.
---
The =item handling uses now some heuristics to produce nice paragraphing:
* Handling of text on the =item command:
=item * [text]
"text" is written on the same line as the bullet\, and in boldface if
it does not contain any POD escapes\, with \
at the end.
=item 1. [text]
"text" is written on the same line as the number\, and in boldface if
it does not contain any POD escapes\, with \
at the end.
=item text
"text" is used as the DT\, and in boldface if it does not contain any
POD escapes\, with \
at the end.
* The first subsequent pod paragraph is not made into a HTML paragraph.
* If =item's are not followed by a paragraph\, they are densely packed.
(Cf. the Name:/Class:/Switch: in Tk pod OPTIONS lists).
---
There is now a precise warning for =item without bullet/number/text.
---
Emit rules only if header level is 1\, plus a link to the page index.
---
Do automatic =back's before =head if listlevel > 0. (Sloppy pods need
correction.)
---
A non-empty line with all spaces splits paragraphs.
---
process_puretext now preserves white space between words. (Was causing
insertion of spurious white space at begin/end of a stretch of text -
not that it mattered much\, but it caused problems when trying to implement
the new feature below. - There's no need to have the line breaking algorithm
in there any more.)
---
Experimental feature: An ordinary block of text containing at least one
Tab in all lines (except at most one) is made into a \
\\\ $title\ \\ \ | \
print HTML \<\<END_OF_HEAD; \ \
\\
$block END_OF_HEAD# load/reload/validate/cache %pages and %items get_cache($dircache\, $itemcache\, \@podpath\, $podroot\, $recurse);
# scan the pod for =item directives scan_items( \%local_items\, ""\, @poddata);
# put an index at the top of the file. note\, if $doindex is 0 we # still generate an index\, but surround it with an html comment. # that way some other program can extract it if desired. $index =~ s/--+/-/g; print HTML "\<A NAME=\"__index__\">\\n"; print HTML "\<!-- INDEX BEGIN -->\n"; print HTML "\<!--\n" unless $doindex; print HTML $index; print HTML "-->\n" unless $doindex; print HTML "\<!-- INDEX END -->\n\n"; print HTML "\
# now convert this file my $after_item; # set to true after an =item warn "Converting input file $podfile\n" if $verbose; foreach my $i (0..$#poddata){ $ptQuote = 0; # status of quote conversion
$_ = $poddata[$i]; $paragraph = $i+1; if (/^(=.*)/s) { # is it a pod directive? $ignore = 0; $after_item = 0; $_ = $1; if (/^=begin\s+(\S+)\s*(.*)/si) {# =begin process_begin($1\, $2); } elsif (/^=end\s+(\S+)\s*(.*)/si) {# =end process_end($1\, $2); } elsif (/^=cut/) { # =cut process_cut(); } elsif (/^=pod/) { # =pod process_pod(); } else { next if @begin_stack && $begin_stack[-1] ne 'html';
if (/^=(head[1-6])\s+(.*\S)/s) { # =head[1-6] heading process_head( $1\, $2\, $doindex && $index ); } elsif (/^=item\s*(.*\S)?/sm) { # =item text warn "$0: $podfile: =item without bullet\, number or text" . " in paragraph $paragraph.\n" if $1 eq ''; process_item( $1 ); $after_item = 1; } elsif (/^=over\s*(.*)/) { # =over N process_over(); } elsif (/^=back/) { # =back process_back(); } elsif (/^=for\s+(\S+)\s+(.*)/si) {# =for process_for($1\,$2); } else { /^=(\S*)\s*/; warn "$0: $podfile: unknown pod directive '$1' in " . "paragraph $paragraph. ignoring.\n"; } } $top = 0; } else { next if $ignore; next if @begin_stack && $begin_stack[-1] ne 'html'; my $text = $_; if( $text =~ /\A\s+/ ){ process_pre( \$text ); print HTML "\
\n$text\\n";
} else { process_text( \$text );
# experimental: check for a paragraph where all lines # have some ...\t...\t...\n pattern if( $text =~ /\t/ ){ my @lines = split( "\n"\, $text ); if( @lines > 1 ){ my $all = 2; foreach my $line ( @lines ){ if( $line =~ /\S/ && $line !~ /\t/ ){ $all--; last if $all == 0; } } if( $all > 0 ){ $text =~ s/\t+/\
if( $after_item ){ print HTML "$text\n"; $after_lpar = 1; } else { print HTML "\
$text\
\n"; } } $after_item = 0; } }# finish off any pending directives finish_list();
# link to page index print HTML "\
\<A HREF=\"#__index__\">\page index\\\
\n" if $doindex and $index;print HTML \<\<END_OF_TAIL; $block \
\ END_OF_TAIL
# close the html file close(HTML);
warn "Finished\n" if $verbose; }
##############################################################################
my $usage; # see below sub usage { my $podfile = shift; warn "$0: $podfile: @_\n" if @_; die $usage; }
$usage =\<\<END_OF_USAGE;
Usage: $0 --help --htmlroot=\
--flush - flushes the item and directory caches. --help - prints this message. --htmlroot - http-server base directory from which all relative paths in podpath stem (default is /). --index - generate an index at the top of the resulting html (default). --infile - filename for the pod to convert (input taken from stdin by default). --libpods - colon-separated list of pages to search for =item pod directives in as targets of C\<> and implicit links (empty by default). note\, these are not filenames\, but rather page names like those that appear in L\<> links. --netscape - will use netscape html directives when applicable. --nonetscape - will not use netscape directives (default). --outfile - filename for the resulting html file (output sent to stdout by default). --podpath - colon-separated list of directories containing library pods. empty by default. --podroot - filesystem base directory from which all relative paths in podpath stem (default is .). --noindex - don't generate an index at the top of the resulting html. --norecurse - don't recurse on those subdirectories listed in podpath. --recurse - recurse on those subdirectories listed in podpath (default behavior). --title - title that will appear in resulting html file. --header - produce block header/footer --css - stylesheet URL --verbose - self-explanatory --quiet - supress some benign warning messages
END_OF_USAGE
sub parse_command_line { my ($opt_flush\,$opt_help\,$opt_htmldir\,$opt_htmlroot\,$opt_index\,$opt_infile\,$opt_libpods\,$opt_netscape\,$opt_outfile\,$opt_podpath\,$opt_podroot\,$opt_recurse\,$opt_title\,$opt_verbose\,$opt_css\,$opt_header\,$opt_quiet); unshift @ARGV\, split ' '\, $Config{pod2html} if $Config{pod2html}; my $result = GetOptions( 'flush' => \$opt_flush\, 'help' => \$opt_help\, 'htmldir=s' => \$opt_htmldir\, 'htmlroot=s' => \$opt_htmlroot\, 'index!' => \$opt_index\, 'infile=s' => \$opt_infile\, 'libpods=s' => \$opt_libpods\, 'netscape!' => \$opt_netscape\, 'outfile=s' => \$opt_outfile\, 'podpath=s' => \$opt_podpath\, 'podroot=s' => \$opt_podroot\, 'recurse!' => \$opt_recurse\, 'title=s' => \$opt_title\, 'header' => \$opt_header\, 'css=s' => \$opt_css\, 'verbose' => \$opt_verbose\, 'quiet' => \$opt_quiet\, ); usage("-"\, "invalid parameters") if not $result;
usage("-") if defined $opt_help; # see if the user asked for help $opt_help = ""; # just to make -w shut-up.
$podfile = $opt_infile if defined $opt_infile; $htmlfile = $opt_outfile if defined $opt_outfile; $htmldir = $opt_htmldir if defined $opt_outfile;
@podpath = split(":"\, $opt_podpath) if defined $opt_podpath; @libpods = split(":"\, $opt_libpods) if defined $opt_libpods;
warn "Flushing item and directory caches\n" if $opt_verbose && defined $opt_flush; unlink($dircache\, $itemcache) if defined $opt_flush;
$htmlroot = $opt_htmlroot if defined $opt_htmlroot; $podroot = $opt_podroot if defined $opt_podroot;
$doindex = $opt_index if defined $opt_index; $recurse = $opt_recurse if defined $opt_recurse; $title = $opt_title if defined $opt_title; $header = defined $opt_header ? 1 : 0; $css = $opt_css if defined $opt_css; $verbose = defined $opt_verbose ? 1 : 0; $quiet = defined $opt_quiet ? 1 : 0; $netscape = $opt_netscape if defined $opt_netscape; }
my $saved_cache_key;
sub get_cache { my($dircache\, $itemcache\, $podpath\, $podroot\, $recurse) = @_; my @cache_key_args = @_;
# A first-level cache: # Don't bother reading the cache files if they still apply # and haven't changed since we last read them.
my $this_cache_key = cache_key(@cache_key_args);
return if $saved_cache_key and $this_cache_key eq $saved_cache_key;
# load the cache of %pages and %items if possible. $tests will be # non-zero if successful. my $tests = 0; if (-f $dircache && -f $itemcache) { warn "scanning for item cache\n" if $verbose; $tests = load_cache($dircache\, $itemcache\, $podpath\, $podroot); }
# if we didn't succeed in loading the cache then we must (re)build # %pages and %items. if (!$tests) { warn "scanning directories in pod-path\n" if $verbose; scan_podpath($podroot\, $recurse\, 0); } $saved_cache_key = cache_key(@cache_key_args); }
sub cache_key { my($dircache\, $itemcache\, $podpath\, $podroot\, $recurse) = @_; return join('!'\, $dircache\, $itemcache\, $recurse\, @$podpath\, $podroot\, stat($dircache)\, stat($itemcache)); }
# # load_cache - tries to find if the caches stored in $dircache and $itemcache # are valid caches of %pages and %items. if they are valid then it loads # them and returns a non-zero value. # sub load_cache { my($dircache\, $itemcache\, $podpath\, $podroot) = @_; my($tests); local $_;
$tests = 0;
open(CACHE\, "\<$itemcache") || die "$0: error opening $itemcache for reading: $!\n"; $/ = "\n";
# is it the same podpath?
$_ = \
# is it the same podroot?
$_ = \
# load the cache if its good if ($tests != 2) { close(CACHE); return 0; }
warn "loading item cache\n" if $verbose;
while (\
warn "scanning for directory cache\n" if $verbose; open(CACHE\, "\<$dircache") || die "$0: error opening $dircache for reading: $!\n"; $/ = "\n"; $tests = 0;
# is it the same podpath?
$_ = \
# is it the same podroot?
$_ = \
# load the cache if its good if ($tests != 2) { close(CACHE); return 0; }
warn "loading directory cache\n" if $verbose;
while (\
close(CACHE);
return 1; }
# # scan_podpath - scans the directories specified in @podpath for directories\, # .pod files\, and .pm files. it also scans the pod files specified in # @libpods for =item directives. # sub scan_podpath { my($podroot\, $recurse\, $append) = @_; my($pwd\, $dir); my($libpod\, $dirname\, $pod\, @files\, @poddata);
unless($append) { %items = (); %pages = (); }
# scan each directory listed in @podpath $pwd = getcwd(); chdir($podroot) || die "$0: error changing to directory $podroot: $!\n"; foreach $dir (@podpath) { scan_dir($dir\, $recurse); }
# scan the pods listed in @libpods for =item directives foreach $libpod (@libpods) { # if the page isn't defined then we won't know where to find it # on the system. next unless defined $pages{$libpod} && $pages{$libpod};
# if there is a directory then use the .pod and .pm files within it. # NOTE: Only finds the first so-named directory in the tree. # if ($pages{$libpod} =~ /([^:]*[^(\.pod|\.pm)]):/) { if ($pages{$libpod} =~ /([^:]*(?\<!\.pod)(?\<!\.pm)):/) { # find all the .pod and .pm files within the directory $dirname = $1; opendir(DIR\, $dirname) || die "$0: error opening directory $dirname: $!\n"; @files = grep(/(\.pod|\.pm)$/ && ! -d $_\, readdir(DIR)); closedir(DIR);
# scan each .pod and .pm file for =item directives
foreach $pod (@files) {
open(POD\, "\<$dirname/$pod") ||
die "$0: error opening $dirname/$pod for input: $!\n";
@poddata = \
scan_items( \%items\, "$dirname/$pod"\, @poddata); }
# use the names of files as =item directives too.
### Don't think this should be done this way - confuses issues.(WL)
### foreach $pod (@files) {
### $pod =~ /^(.*)(\.pod|\.pm)$/;
### $items{$1} = "$dirname/$1.html" if $1;
### }
} elsif ($pages{$libpod} =~ /([^:]*\.pod):/ ||
$pages{$libpod} =~ /([^:]*\.pm):/) {
# scan the .pod or .pm file for =item directives
$pod = $1;
open(POD\, "\<$pod") ||
die "$0: error opening $pod for input: $!\n";
@poddata = \
scan_items( \%items\, "$pod"\, @poddata); } else { warn "$0: shouldn't be here (line ".__LINE__."\n"; } } @poddata = (); # clean-up a bit
chdir($pwd) || die "$0: error changing to directory $pwd: $!\n";
# cache the item list for later use warn "caching items for later use\n" if $verbose; open(CACHE\, ">$itemcache") || die "$0: error open $itemcache for writing: $!\n";
print CACHE join(":"\, @podpath) . "\n$podroot\n"; foreach my $key (keys %items) { print CACHE "$key $items{$key}\n"; }
close(CACHE);
# cache the directory list for later use warn "caching directories for later use\n" if $verbose; open(CACHE\, ">$dircache") || die "$0: error open $dircache for writing: $!\n";
print CACHE join(":"\, @podpath) . "\n$podroot\n"; foreach my $key (keys %pages) { print CACHE "$key $pages{$key}\n"; }
close(CACHE); }
# # scan_dir - scans the directory specified in $dir for subdirectories\, .pod # files\, and .pm files. notes those that it finds. this information will # be used later in order to figure out where the pages specified in L\<> # links are on the filesystem. # sub scan_dir { my($dir\, $recurse) = @_; my($t\, @subdirs\, @pods\, $pod\, $dirname\, @dirs); local $_;
@subdirs = (); @pods = ();
opendir(DIR\, $dir) || die "$0: error opening directory $dir: $!\n"; while (defined($_ = readdir(DIR))) { if (-d "$dir/$_" && $_ ne "." && $_ ne "..") { # directory $pages{$_} = "" unless defined $pages{$_}; $pages{$_} .= "$dir/$_:"; push(@subdirs\, $_); } elsif (/\.pod$/) { # .pod s/\.pod$//; $pages{$_} = "" unless defined $pages{$_}; $pages{$_} .= "$dir/$_.pod:"; push(@pods\, "$dir/$_.pod"); } elsif (/\.pm$/) { # .pm s/\.pm$//; $pages{$_} = "" unless defined $pages{$_}; $pages{$_} .= "$dir/$_.pm:"; push(@pods\, "$dir/$_.pm"); } } closedir(DIR);
# recurse on the subdirectories if necessary if ($recurse) { foreach my $subdir (@subdirs) { scan_dir("$dir/$subdir"\, $recurse); } } }
# # scan_headings - scan a pod file for head[1-6] tags\, note the tags\, and # build an index. # sub scan_headings { my($sections\, @data) = @_; my($tag\, $which_head\, $otitle\, $listdepth\, $index);
# here we need local $ignore = 0; # unfortunately\, we can't have it\, because $ignore is lexical $ignore = 0;
$listdepth = 0; $index = "";
# scan for =head directives\, note their name\, and build an index # pointing to each of them. foreach my $line (@data) { if ($line =~ /^=(head)([1-6])\s+(.*)/) { ($tag\, $which_head\, $otitle) = ($1\,$2\,$3);
my $title = depod( $otitle ); my $name = htmlify( $title ); $$sections{$name} = 1; $title = process_text( \$otitle );
while ($which_head != $listdepth) { if ($which_head > $listdepth) { $index .= "\n" . ("\t" x $listdepth) . "\
$index .= "\n" . ("\t" x $listdepth) . "\
# finish off the lists while ($listdepth--) { $index .= "\n" . ("\t" x $listdepth) . "\\n"; }
# get rid of bogus lists $index =~ s\,\t*\
$ignore = 1; # restore old value;
return $index; }
# # scan_items - scans the pod specified by $pod for =item directives. we # will use this information later on in resolving C\<> links. # sub scan_items { my( $itemref\, $pod\, @poddata ) = @_; my($i\, $item); local $_;
$pod =~ s/\.pod$//; $pod .= ".html" if $pod;
foreach $i (0..$#poddata) { my $txt = depod( $poddata[$i] );
# figure out what kind of item it is. # Build string for referencing this item. if ( $txt =~ /\A=item\s+\*\s*(.*)\Z/s ) { # bullet next unless $1; $item = $1; } elsif( $txt =~ /\A=item\s+(?>\d+\.?)\s*(.*)\Z/s ) { # numbered list $item = $1; } elsif( $txt =~ /\A=item\s+(.*)\Z/s ) { # plain item $item = $1; } else { next; } my $fid = fragment_id( $item ); $$itemref{$fid} = "$pod" if $fid; } }
# # process_head - convert a pod head[1-6] tag and convert it to HTML format. # sub process_head { my($tag\, $heading\, $hasindex) = @_;
# figure out the level of the =head $tag =~ /head([1-6])/; my $level = $1;
if( $listlevel ){ warn "$0: $podfile: unterminated list at =head in paragraph $paragraph. ignoring.\n"; while( $listlevel ){ process_back(); } }
print HTML "\
\n"; if( $level == 1 && ! $top ){ print HTML "\<A HREF=\"#__index__\">\page index\\\n" if $hasindex; print HTML "\
my $name = htmlify( depod( $heading ) ); my $convert = process_text( \$heading ); print HTML "\<H$level>\<A NAME=\"$name\">$convert\\</H$level>\n"; }
# # emit_item_tag - print an =item's text # Note: The global $EmittedItem is used for inhibiting self-references. # my $EmittedItem;
sub emit_item_tag($$$){ my( $otext\, $text\, $compact ) = @_; my $item = fragment_id( $text );
$EmittedItem = $item; ### print STDERR "emit_item_tag=$item ($text)\n";
print HTML '\';
if ($items_named{$item}++) {
print HTML process_text( \$otext );
} else {
my $name = 'item_' . $item;
print HTML qq{\}\, process_text( \$otext )\, '\';
}
print HTML "\\
\n";
undef( $EmittedItem );
}
sub emit_li { my( $tag ) = @_; if( $items_seen[$listlevel]++ == 0 ){ push( @listend\, "\</$tag>" ); print HTML "\<$tag>\n"; } print HTML $tag eq 'DL' ? '\
# # process_item - convert a pod item tag and convert it to HTML format. # sub process_item { my( $otext ) = @_;
# lots of documents start a list without doing an =over. this is # bad! but\, the proper thing to do seems to be to just assume # they did do an =over. so warn them once and then continue. if( $listlevel == 0 ){ warn "$0: $podfile: unexpected =item directive in paragraph $paragraph. ignoring.\n"; process_over(); }
# formatting: insert a paragraph if preceding item has >1 paragraph if( $after_lpar ){ print HTML "\
\
\n"; $after_lpar = 0; }# remove formatting instructions from the text my $text = depod( $otext );
# all the list variants: if( $text =~ /\A\*/ ){ # bullet emit_li( 'UL' ); if ($text =~ /\A\*\s+(.+)\Z/s ) { # with additional text my $tag = $1; $otext =~ s/\A\*\s+//; emit_item_tag( $otext\, $tag\, 1 ); }
} elsif( $text =~ /\A\d+/ ){ # numbered list emit_li( 'OL' ); if ($text =~ /\A(?>\d+\.?)\s*(.+)\Z/s ) { # with additional text my $tag = $1; $otext =~ s/\A\d+\.?\s*//; emit_item_tag( $otext\, $tag\, 1 ); }
} else { # definition list emit_li( 'DL' ); if ($text =~ /\A(.+)\Z/s ){ # should have text emit_item_tag( $otext\, $text\, 1 ); } print HTML '\
# # process_over - process a pod over tag and start a corresponding HTML list. # sub process_over { # start a new list $listlevel++; push( @items_seen\, 0 ); $after_lpar = 0; }
# # process_back - process a pod back tag and convert it to HTML format. # sub process_back { if( $listlevel == 0 ){ warn "$0: $podfile: unexpected =back directive in paragraph $paragraph. ignoring.\n"; return; }
# close off the list. note\, I check to see if $listend[$listlevel] is # defined because an =item directive may have never appeared and thus # $listend[$listlevel] may have never been initialized. $listlevel--; if( defined $listend[$listlevel] ){ print HTML '\
\
' if $after_lpar; print HTML $listend[$listlevel]; print HTML "\n"; pop( @listend ); } $after_lpar = 0;# clean up item count pop( @items_seen ); }
# # process_cut - process a pod cut tag\, thus start ignoring pod directives. # sub process_cut { $ignore = 1; }
# # process_pod - process a pod pod tag\, thus stop ignoring pod directives # until we see a corresponding cut. # sub process_pod { # no need to set $ignore to 0 cause the main loop did it }
# # process_for - process a =for pod tag. if it's for html\, spit # it out verbatim\, if illustration\, center it\, otherwise ignore it. # sub process_for { my($whom\, $text) = @_; if ( $whom =~ /^(pod2)?html$/i) { print HTML $text; } elsif ($whom =~ /^illustration$/i) { 1 while chomp $text; for my $ext (qw[.png .gif .jpeg .jpg .tga .pcl .bmp]) { $text .= $ext\, last if -r "$text$ext"; } print HTML qq{\
\\
# # process_begin - process a =begin pod tag. this pushes # whom we're beginning on the begin stack. if there's a # begin stack\, we only print if it us. # sub process_begin { my($whom\, $text) = @_; $whom = lc($whom); push (@begin_stack\, $whom); if ( $whom =~ /^(pod2)?html$/) { print HTML $text if $text; } }
# # process_end - process a =end pod tag. pop the # begin stack. die if we're mismatched. # sub process_end { my($whom\, $text) = @_; $whom = lc($whom); if ($begin_stack[-1] ne $whom ) { die "Unmatched begin/end at chunk $paragraph\n" } pop( @begin_stack ); }
# # process_pre - indented paragraph\, made into \
\# sub process_pre { my( $text ) = @_; my( $rest ); return if $ignore;
$rest = $$text;
# insert spaces in place of tabs $rest =~ s#.*# my $line = $&; 1 while $line =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e; $line; #eg;
# convert some special chars to HTML escapes $rest =~ s/&/&/g; $rest =~ s/\</</g; $rest =~ s/>/>/g; $rest =~ s/"/"/g;
# try and create links for all occurrences of perl.* within # the preformatted text. $rest =~ s{ (\s*)(perl\w+) }{ if ( defined $pages{$2} ){ # is a link qq($1\$2\); } elsif (defined $pages{dosify($2)}) { # is a link qq($1\$2\); } else { "$1$2"; } }xeg; $rest =~ s{ (\<A\ HREF="?) ([^>:]*:)? ([^>:]*) \.pod: ([^>:]*:)? }{ my $url ; if ( $htmlfileurl ne '' ){ # Here\, we take advantage of the knowledge # that $htmlfileurl ne '' implies $htmlroot eq ''. # Since $htmlroot eq ''\, we need to prepend $htmldir # on the fron of the link to get the absolute path # of the link's target. We check for a leading '/' # to avoid corrupting links that are #\, file:\, etc. my $old_url = $3 ; $old_url = "$htmldir$old_url" if $old_url =~ m{^\/}; $url = relativize_url( "$old_url.html"\, $htmlfileurl ); } else { $url = "$3.html" ; } "$1$url" ; }xeg;
# Look for embedded URLs and make them into links. We don't # relativize them since they are best left as the author intended.
my $urls = '(' . join ('|'\, qw{
http
telnet
mailto
news
gopher
file
wais
ftp
} )
. ')';
my $ltrs = '\w';
my $gunk = '/#~:.?+=&%@!\-';
my $punc = '.:?\-';
my $any = "${ltrs}${gunk}${punc}";
$rest =~ s{ \b # start at word boundary ( # begin $1 { $urls : # need resource and a colon (?!:) # Ignore File::\, among others. [$any] +? # followed by on or more # of any valid character\, but # be conservative and take only # what you need to.... ) # end $1 } (?= # look-ahead non-consumptive assertion [$punc]* # either 0 or more puntuation [^$any] # followed by a non-url char | # or else $ # then end of the string ) }{\$1\}igox;
# text should be as it is (verbatim) $$text = $rest; }
# # pure text processing # # pure_text/inIS_text: differ with respect to automatic C\<> recognition. # we don't want this to happen within IS # sub pure_text($){ my $text = shift(); process_puretext( $text\, \$ptQuote\, 1 ); }
sub inIS_text($){ my $text = shift(); process_puretext( $text\, \$ptQuote\, 0 ); }
# # process_puretext - process pure text (without pod-escapes) converting # double-quotes and handling implicit C\<> links. # sub process_puretext { my($text\, $quote\, $notinIS) = @_;
## Guessing at func() or [$@%&]*var references in plain text is destined ## to produce some strange looking ref's. uncomment to disable: ## $notinIS = 0;
my(@words\, $lead\, $trail);
# convert double-quotes to single-quotes if( $$quote && $text =~ s/"/''/s ){ $$quote = 0; } while ($text =~ s/"([^"]*)"/``$1''/sg) {}; $$quote = 1 if $text =~ s/"/``/s;
# keep track of leading and trailing white-space $lead = ($text =~ s/\A(\s+)//s ? $1 : ""); $trail = ($text =~ s/(\s+)\Z//s ? $1 : "");
# split at space/non-space boundaries @words = split( /(?\<=\s)(?=\S)|(?\<=\S)(?=\s)/\, $text );
# process each word individually foreach my $word (@words) { # skip space runs next if $word =~ /^\s*$/; # see if we can infer a link if( $notinIS && $word =~ s/^(\w+)\((.*)\)\W*$/$1/ ) { # has parenthesis so should have been a C\<> ref ## try for a pagename (perlXXX(1))? if( $2 =~ /^\d+$/ ){ my $url = page_sect( $word\, '' ); if( defined $url ){ $word = "\<A HREF=\"$url\">the $word manpage\"; next; } } $word = emit_C( $word );
#### disabled. either all (including $\W\, $\w+{.*} etc.) or nothing. ## } elsif( $notinIS && $word =~ /^[\$\@%&*]+\w+$/) { ## # perl variables\, should be a C\<> ref ## $word = emit_C( $word );
} elsif ($word =~ m\,^\w+://\w\,) { # looks like a URL # Don't relativize it: leave it as the author intended $word = qq(\$word\); } elsif ($word =~ /[\w.-]+\@[\w-]+\.\w/) { # looks like an e-mail address my ($w1\, $w2\, $w3) = (""\, $word\, ""); ($w1\, $w2\, $w3) = ("("\, $1\, ")$2") if $word =~ /^\((.*?)\)(\,?)/; ($w1\, $w2\, $w3) = ("<"\, $1\, ">$2") if $word =~ /^\<(.*?)>(\,?)/; $word = qq($w1\$w2\$w3); } elsif ($word !~ /[a-z]/ && $word =~ /[A-Z]/) { # all uppercase? $word = html_escape($word) if $word =~ /["&\<>]/; $word = "\n\$word\" if $netscape; } else { $word = html_escape($word) if $word =~ /["&\<>]/; } }
# put everything back together return $lead . join( ''\, @words ) . $trail; }
# # process_text - handles plaintext that appears in the input pod file. # there may be pod commands embedded within the text so those must be # converted to html commands. # sub process_text { return if $ignore; my( $tref ) = @_; my $res = process_text1( 0\, $tref ); $$tref = $res; }
sub process_text1($$;$){ my( $lev\, $rstr\, $func ) = @_; $lev++ unless defined $func; my $res = '';
if( $func eq 'B' ){
# B\
} elsif( $func eq 'C' ){
# C\ - can be a ref or \
\
# need to extract text
my $par = go_ahead( $rstr\, 'C' );
## clean-up of the link target my $text = depod( $par );
### my $x = $par =~ /[BI]\</ ? 'yes' : 'no' ; ### print STDERR "-->call emit_C($par) lev=$lev\, par with BI=$x\n";
$res = emit_C( $text\, $lev > 1 || ($par =~ /[BI]\</) );
} elsif( $func eq 'E' ){
# E\
} elsif( $func eq 'F' ){
# F\
} elsif( $func eq 'I' ){
# I\
} elsif( $func eq 'L' ){ # L\ - link ## L\<text|cross-ref> => produce text\, use cross-ref for linking ## L\<cross-ref> => make text from cross-ref ## need to extract text my $par = go_ahead( $rstr\, 'L' );
# some L\<>'s that shouldn't be: # a) full-blown URL's are emitted as-is if( $par =~ m{^\w+://}s ){ return make_URL_href( $par ); } # b) C\<...> is stripped and treated as C\<> if( $par =~ /^C\<(.*)>$/ ){ my $text = depod( $1 ); return emit_C( $text\, $lev > 1 || ($par =~ /[BI]\</) ); }
# analyze the contents
$par =~ s/\n/ /g; # undo word-wrapped tags
my $opar = $par;
my $linktext;
if( $par =~ s{^([^|]+)\|}{} ){
$linktext = $1;
}
# make sure sections start with a /
$par =~ s{^"}{/"};
my( $page\, $section\, $ident );
# check for link patterns if( $par =~ m{^([^/]+?)/(?!")(.*?)$} ){ # name/ident # we've got a name/ident (no quotes) ( $page\, $ident ) = ( $1\, $2 ); ### print STDERR "--> L\<$par> to page $page\, ident $ident\n";
} elsif( $par =~ m{^(.*?)/"?(.*?)"?$} ){ # [name]/"section" # even though this should be a "section"\, we go for ident first ( $page\, $ident ) = ( $1\, $2 ); ### print STDERR "--> L\<$par> to page $page\, section $section\n";
} elsif( $par =~ /\s/ ){ # this must be a section with missing quotes ( $page\, $section ) = ( ''\, $par ); ### print STDERR "--> L\<$par> to void page\, section $section\n";
} else { ( $page\, $section ) = ( $par\, '' ); ### print STDERR "--> L\<$par> to page $par\, void section\n"; }
# now\, either $section or $ident is defined. the convoluted logic # below tries to resolve L\<> according to what the user specified. # failing this\, we try to find the next best thing... my( $url\, $ltext\, $fid );
RESOLVE: { if( defined $ident ){ ## try to resolve $ident as an item ( $url\, $fid ) = coderef( $page\, $ident ); if( $url ){ if( ! defined( $linktext ) ){ $linktext = $ident; $linktext .= " in " if $ident && $page; $linktext .= "the $page manpage" if $page; } ### print STDERR "got coderef url=$url\n"; last RESOLVE; } ## no luck: go for a section (auto-quoting!) $section = $ident; } ## now go for a section my $htmlsection = htmlify( $section ); $url = page_sect( $page\, $htmlsection ); if( $url ){ if( ! defined( $linktext ) ){ $linktext = $section; $linktext .= " in " if $section && $page; $linktext .= "the $page manpage" if $page; } ### print STDERR "got page/section url=$url\n"; last RESOLVE; } ## no luck: go for an ident if( $section ){ $ident = $section; } else { $ident = $page; $page = undef(); } ( $url\, $fid ) = coderef( $page\, $ident ); if( $url ){ if( ! defined( $linktext ) ){ $linktext = $ident; $linktext .= " in " if $ident && $page; $linktext .= "the $page manpage" if $page; } ### print STDERR "got section=>coderef url=$url\n"; last RESOLVE; }
# warning; show some text. $linktext = $opar unless defined $linktext; warn "$0: $podfile: cannot resolve L\<$opar> in paragraph $paragraph."; }
# now we have an URL or just plain code $$rstr = $linktext . '>' . $$rstr; if( defined( $url ) ){ $res = "\<A HREF=\"$url\">" . process_text1( $lev\, $rstr ) . '\'; } else { $res = '\' . process_text1( $lev\, $rstr ) . '\'; }
} elsif( $func eq 'S' ){
# S\
} elsif( $func eq 'X' ){ # X\<> - ignore $$rstr =~ s/^[^>]*>//;
} elsif( $func eq 'Z' ){ # Z\<> - empty warn "$0: $podfile: invalid X\<> in paragraph $paragraph." unless $$rstr =~ s/^>//;
} else { while( $$rstr =~ s/\A(.*?)([BCEFILSXZ]\<|>)//s ){ # all others: either recurse into new function or # terminate at closing angle bracket my $pt = $1; $pt .= '>' if $2 eq '>' && $lev == 1; $res .= $lev == 1 ? pure_text( $pt ) : inIS_text( $pt ); return $res if $2 eq '>' && $lev > 1; if( $2 ne '>' ){ $res .= process_text1( $lev\, $rstr\, substr($2\,0\,1) ); }
} if( $lev == 1 ){ $res .= pure_text( $$rstr ); } else { warn "$0: $podfile: undelimited $func\<> in paragraph $paragraph."; } } return $res; }
# # go_ahead: extract text of an IS (can be nested) # sub go_ahead($$){ my( $rstr\, $func ) = @_; my $res = ''; my $level = 1; while( $$rstr =~ s/\A(.*?)([BCEFILSXZ]\<|>)//s ){ $res .= $1; if( $2 eq '>' ){ return $res if --$level == 0; } else { ++$level; } $res .= $2; } warn "$0: $podfile: undelimited $func\<> in paragraph $paragraph."; return $res; }
#
# emit_C - output result of C\
# need HTML-safe text my $linktext = html_escape( $text );
if( defined( $url ) &&
(!defined( $EmittedItem ) || $EmittedItem ne $fid ) ){
$res = "\<A HREF=\"$url\">\$linktext\
\";
} elsif( 0 && $nocode ){
$res = $linktext;
} else {
$res = "\$linktext\
";
}
return $res;
}
# # html_escape: make text safe for HTML # sub html_escape { my $rest = $_[0]; $rest =~ s/&/&/g; $rest =~ s/\</</g; $rest =~ s/>/>/g; $rest =~ s/"/"/g; return $rest; }
# # dosify - convert filenames to 8.3 # sub dosify { my($str) = @_; return lc($str) if $^O eq 'VMS'; # VMS just needs casing if ($Is83) { $str = lc $str; $str =~ s/(\.\w+)/substr ($1\,0\,4)/ge; $str =~ s/(\w+)/substr ($1\,0\,8)/ge; } return $str; }
# # page_sect - make an URL from the text of a L\<> # sub page_sect($$) { my( $page\, $section ) = @_; my( $linktext\, $page83\, $link); # work strings
# check if we know that this is a section in this page if (!defined $pages{$page} && defined $sections{$page}) { $section = $page; $page = ""; ### print STDERR "reset page=''\, section=$section\n"; }
$page83=dosify($page); $page=$page83 if (defined $pages{$page83}); if ($page eq "") { $link = "#" . htmlify( $section ); } elsif ( $page =~ /::/ ) { $page =~ s\,::\,/\,g; # Search page cache for an entry keyed under the html page name\, # then look to see what directory that page might be in. NOTE: # this will only find one page. A better solution might be to produce # an intermediate page that is an index to all such pages. my $page_name = $page ; $page_name =~ s\,^.*/\,\, ; if ( defined( $pages{ $page_name } ) && $pages{ $page_name } =~ /([^:]*$page)\.(?:pod|pm):/ ) { $page = $1 ; } else { # NOTE: This branch assumes that all A::B pages are located in # $htmlroot/A/B.html . This is often incorrect\, since they are # often in $htmlroot/lib/A/B.html or such like. Perhaps we could # analyze the contents of %pages and figure out where any # cousins of A::B are\, then assume that. So\, if A::B isn't found\, # but A::C is found in lib/A/C.pm\, then A::B is assumed to be in # lib/A/B.pm. This is also limited\, but it's an improvement. # Maybe a hints file so that the links point to the correct places # nonetheless?
} $link = "$htmlroot/$page.html"; $link .= "#" . htmlify( $section ) if ($section); } elsif (!defined $pages{$page}) { $link = ""; } else { $section = htmlify( $section ) if $section ne ""; ### print STDERR "...section=$section\n";
# if there is a directory by the name of the page\, then assume that an # appropriate section will exist in the subdirectory # if ($section ne "" && $pages{$page} =~ /([^:]*[^(\.pod|\.pm)]):/) { if ($section ne "" && $pages{$page} =~ /([^:]*(?\<!\.pod)(?\<!\.pm)):/) { $link = "$htmlroot/$1/$section.html"; ### print STDERR "...link=$link\n";
# since there is no directory by the name of the page\, the section will # have to exist within a .html of the same name. thus\, make sure there # is a .pod or .pm that might become that .html } else { $section = "#$section" if $section; ### print STDERR "...section=$section\n";
# check if there is a .pod with the page name if ($pages{$page} =~ /([^:]*)\.pod:/) { $link = "$htmlroot/$1.html$section"; } elsif ($pages{$page} =~ /([^:]*)\.pm:/) { $link = "$htmlroot/$1.html$section"; } else { $link = ""; } } }
if ($link) { # Here\, we take advantage of the knowledge that $htmlfileurl ne '' # implies $htmlroot eq ''. This means that the link in question # needs a prefix of $htmldir if it begins with '/'. The test for # the initial '/' is done to avoid '#'-only links\, and to allow # for other kinds of links\, like file:\, ftp:\, etc. my $url ; if ( $htmlfileurl ne '' ) { $link = "$htmldir$link" if $link =~ m{^/}; $url = relativize_url( $link\, $htmlfileurl ); # print( " b: [$link\,$htmlfileurl\,$url]\n" ); } else { $url = $link ; } return $url;
} else { return undef(); } }
# # relativize_url - convert an absolute URL to one relative to a base URL. # Assumes both end in a filename. # sub relativize_url { my ($dest\,$source) = @_ ;
my ($dest_volume\,$dest_directory\,$dest_file) = File::Spec::Unix->splitpath( $dest ) ; $dest = File::Spec::Unix->catpath( $dest_volume\, $dest_directory\, '' ) ;
my ($source_volume\,$source_directory\,$source_file) = File::Spec::Unix->splitpath( $source ) ; $source = File::Spec::Unix->catpath( $source_volume\, $source_directory\, '' ) ;
my $rel_path = '' ; if ( $dest ne '' ) { $rel_path = File::Spec::Unix->abs2rel( $dest\, $source ) ; }
if ( $rel_path ne '' && substr( $rel_path\, -1 ) ne '/' && substr( $dest_file\, 0\, 1 ) ne '#' ) { $rel_path .= "/$dest_file" ; } else { $rel_path .= "$dest_file" ; }
return $rel_path ; }
# # coderef - make URL from the text of a C\<> # sub coderef($$){ my( $page\, $item ) = @_; my( $url );
my $fid = fragment_id( $item );
if( defined( $page ) ){ # we have been given a $page... $page =~ s{::}{/}g;
# Do we take it? Item could be a section! my $base = $items{$fid}; $base =~ s{[^/]*/}{}; if( $base ne "$page.html" ){ ### print STDERR "coderef( $page\, $item ): items{$fid} = $items{$fid} = $base => discard page!\n"; $page = undef(); }
} else { # no page - local items precede cached items if( exists $local_items{$fid} ){ $page = $local_items{$fid}; } else { $page = $items{$fid}; } }
# if there was a pod file that we found earlier with an appropriate # =item directive\, then create a link to that page. if( defined $page ){ if( $page ){ if( $pages{$page} =~ /([^:.]*)\.[^:]*:/){ $page = $1 . '.html'; } my $link = "$htmlroot/$page#item_$fid";
# Here\, we take advantage of the knowledge that $htmlfileurl # ne '' implies $htmlroot eq ''. if ( $htmlfileurl ne '' ) { $link = "$htmldir$link" ; $url = relativize_url( $link\, $htmlfileurl ) ; } else { $url = $link ; } } else { $url = "#item_" . $fid; }
confess "url has space: $url" if $url =~ /"[^"]*\s[^"]*"/;
}
return( $url\, $fid );
}
# # Adapted from Nick Ing-Simmons' PodToHtml package. sub relative_url { my $source_file = shift ; my $destination_file = shift;
my $source = URI::file->new_abs($source_file); my $uo = URI::file->new($destination_file\,$source)->abs; return $uo->rel->as_string; }
# # finish_list - finish off any pending HTML lists. this should be called # after the entire pod file has been read and converted. # sub finish_list { while ($listlevel > 0) { print HTML "\\n"; $listlevel--; } }
# # htmlify - converts a pod section specification to a suitable section # specification for HTML. Note that we keep spaces and special characters # except "\, ? (Netscape problem) and the hyphen (writer's problem...). # sub htmlify { my( $heading) = @_; $heading =~ s/(\s+)/ /g; $heading =~ s/\s+\Z//; $heading =~ s/\A\s+//; # The hyphen is a disgrace to the English language. $heading =~ s/[-"?]//g; $heading = lc( $heading ); return $heading; }
# # depod - convert text by eliminating all interior sequences # Note: can be called with copy or modify semantics # my %E2c; $E2c{lt} = '\<'; $E2c{gt} = '>'; $E2c{sol} = '/'; $E2c{verbar} = '|';
sub depod($){
my $string;
if( ref( $_[0] ) ){
$string = ${$_[0]};
${$_[0]} = depod1( \$string );
} else {
$string = $_[0];
depod1( \$string );
}
}
sub depod1($;$){
my( $rstr\, $func ) = @_;
my $res = '';
if( ! defined( $func ) ){
# skip to next begin of an interior sequence
while( $$rstr =~ s/\A(.*?)([BCEFILSXZ])\<// ){
# recurse into its text
$res .= $1 . depod1( $rstr\, $2 );
}
$res .= $$rstr;
} elsif( $func eq 'E' ){
# E\
# # fragment_id - construct a fragment identifier from: # a) =item text # b) contents of C\<...> # my @hc; sub fragment_id { my $text = shift(); $text =~ s/\s+\Z//s; if( $text ){ # a method or function? return $1 if $text =~ /(\w+)\s*\(/; return $1 if $text =~ /->\s*(\w+)\s*\(?/;
# a variable name? return $1 if $text =~ /^([$@%*]\S+)/;
# some pattern matching operator? return $1 if $text =~ m|^(\w+/).*/\w*$|;
# fancy stuff... like "do { }" return $1 if $text =~ m|^(\w+)\s*{.*}$|;
# honour the perlfunc manpage: func [PAR[\,[ ]PAR]...] # and some funnies with ... Module ... return $1 if $text =~ m{^([a-z\d]+)(\s+[A-Z\d\,/& ]+)?$}; return $1 if $text =~ m{^([a-z\d]+)\s+Module(\s+[A-Z\d\,/& ]+)?$};
# text? normalize! $text =~ s/\s+/_/sg; $text =~ s{(\W)}{ defined( $hc[ord($1)] ) ? $hc[ord($1)] : ( $hc[ord($1)] = sprintf( "%%%02X"\, ord($1) ) ) }gxe; $text = substr( $text\, 0\, 50 ); } else { return undef(); } }
# # make_URL_href - generate HTML href from URL # Special treatment for CGI queries. # sub make_URL_href($){ my( $url ) = @_; if( $url !~ s{^(http:[-\w/#~:.+=&%@!]+)(\?.*)?$}{\$1\}i ){ $url = "\<A HREF=\"$url\">$url\"; } return $url; }
BEGIN { }
1;
Wolfgang Laun \Wolfgang\.Laun@​alcatel\.at writes:
podtohtml requires not only the POD::Parser family\, but also Font-AFM\, HTML-Format\, HTML-Parser\, HTML-Tree\, MIME-Base64\, PodToHTML and URI.
No it doesn't.
It uses HTML::Element which happens to be bundled with some other module which in turn requires the other stuff. It we re-packaged the various classes the "footprint" would be much smaller.
I seem to recall us at the conference discussing doing a pod2xml which just depended on Pod::Parser\, but did all the heuristics. And then using an xml2html ...
-- Nick Ing-Simmons \nik@​tiuk\.ti\.com Via\, but not speaking for: Texas Instruments Ltd.
Migrated from rt.perl.org#1974 (status was 'resolved')
Searchable as RT1974$