use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK);
@EXPORT = qw(pod2html htmlify);
@EXPORT_OK = qw(anchorify);
use locale
; # make \w work right in non-ASCII lands
Pod::Html - module to convert pod files to HTML
Converts files from pod format (see L<perlpod>) to HTML format. It
can automatically generate indexes and cross-references, and it keeps
a cache of things it knows how to cross-reference.
Pod::Html takes the following arguments:
Adds "Back to Top" links in front of every C<head1> heading (except for
the first). By default, no backlinks are generated.
Creates the item and directory caches in the given directory.
Specify the URL of a cascading style sheet. Also disables all HTML/CSS
C<style> attributes that are output by default (to avoid conflicts).
Flushes the item and directory caches.
Creates header and footer blocks containing the text of the C<NAME>
section. By default, no headers are generated.
Displays the usage message.
Include hidden directories in the search for POD's in podpath if recurse
The default is not to traverse any directory whose name begins with C<.>.
See L</"podpath"> and L</"recurse">.
[This option is for backward compatibility only.
It's hard to imagine that one would usefully create a module with a
name component beginning with C<.>.]
Sets the directory in which the resulting HTML file is placed. This
is used to generate relative links to other files. Not passing this
causes all links to be absolute, since this is the value that tells
Pod::Html the root of the documentation tree.
Sets the base URL for the HTML files. When cross-references are made,
the HTML root is prepended to the URL.
Generate an index at the top of the HTML file. This is the default
Specify the pod file to convert. Input is taken from STDIN if no
List of page names (eg, "perlfunc") which contain linkable C<=item>s.
B<Deprecated>, has no effect. For backwards compatibility only.
Specify the HTML file to create. Output goes to STDOUT if no outfile
Specify which subdirectories of the podroot contain pod files whose
HTML converted forms can be linked to in cross references.
Specify the base directory for finding library pods.
Don't display I<mostly harmless> warning messages. These messages
will be displayed by default. But this is not the same as C<verbose>
Recurse into subdirectories specified in podpath (default behaviour).
Specify the title of the resulting HTML file.
Display progress messages. By default, they won't be displayed.
"--podpath=lib:ext:pod:vms",
"--podroot=/usr/src/perl",
"--htmlroot=/perl/nmanual",
"--libpods=perlfunc:perlguts:perlvar:perlrun:perlop",
"--outfile=/perl/nmanual/foo.html");
Uses C<$Config{pod2html}> to setup default options.
Tom Christiansen, E<lt>tchrist@perl.comE<gt>.
This program is distributed under the Artistic License.
my($Dircache, $Itemcache);
my($Htmlroot, $Htmldir, $Htmlfile, $Htmlfileurl);
my($Podfile, @Podpath, $Podroot);
my($Listlevel, @Listend);
use vars
qw($Ignore); # need to localize it later.
my(%Items_Named, @Items_Seen);
my %Pages = (); # associative array used to find the location
# of pages referenced by L<> links.
my %Items = (); # associative array used to find the location
# of =item directives referenced by C<> links
my $Curdir = File::Spec->curdir;
$Cachedir = "."; # The directory to which item and directory
# caches will be written.
$Dircache = "pod2htmd.tmp";
$Itemcache = "pod2htmi.tmp";
@Begin_Stack = (); # begin/end stack
@Libpods = (); # files to search for links from C<> directives
$Htmlroot = "/"; # http-server base directory from which all
# relative paths in $podpath stem.
$Htmldir = ""; # The directory to which the html pages
# will (eventually) be written.
$Htmlfile = ""; # write to stdout by default
$Htmlfileurl = "" ; # The url that other files would use to
# refer to this file. This is only used
# to make relative urls that point to
$Podfile = ""; # read from stdin by default
@Podpath = (); # list of directories containing library pods.
$Podroot = $Curdir; # filesystem base directory from which all
# relative paths in $podpath stem.
$Css = ''; # Cascading style sheet
$Recurse = 1; # recurse on subdirectories in $podpath.
$Quiet = 0; # not quiet by default
$Verbose = 0; # not verbose by default
$Doindex = 1; # non-zero if we should generate an index
$Backlink = ''; # text for "back to top" links
$Listlevel = 0; # current list depth
@Listend = (); # the text to use to end the list.
$After_Lpar = 0; # set to true after a par in an =item
$Ignore = 1; # whether or not to format text. we don't
# format text until we hit our first pod
@Items_Seen = (); # for multiples of the same item in perlfunc
$Header = 0; # produce block header/footer
$Title = ''; # title to give the pod(s)
$Top = 1; # true if we are at the top of the doc. used
# to prevent the first <hr /> directive.
$Paragraph = ''; # which paragraph we're processing (used
$PTQuote = 0; # status of double-quote conversion
%Sections = (); # sections within this page
$Is83 = $^O eq 'dos'; # Is it an 8.3 filesystem?
# clean_data: global clean-up of pod data
for my $i ( 0..$#{$dataref} ) {
${$dataref}[$i] =~ s/\s+\Z//;
# have a look for all-space lines
if( ${$dataref}[$i] =~ /^\s+$/m and $dataref->[$i] !~ /^\s/ ){
my @chunks = split( /^\s+$/m, ${$dataref}[$i] );
splice( @$dataref, $i, 1, @chunks );
$Is83 = 0 if (defined (&Dos::UseLFN) && Dos::UseLFN());
# cache of %Pages and %Items from last time we ran pod2html
#undef $opt_help if defined $opt_help;
# parse the command-line parameters
# escape the backlink argument (same goes for title but is done later...)
$Backlink = html_escape($Backlink) if defined $Backlink;
# set some variables to their default values if necessary
unless (@ARGV && $ARGV[0]) {
$Podfile = "-" unless $Podfile; # stdin
|| die "$0: cannot open $Podfile file for input: $!\n";
$Podfile = $ARGV[0]; # XXX: might be more filenames
$Htmlfile = "-" unless $Htmlfile; # stdout
$Htmlroot = "" if $Htmlroot eq "/"; # so we don't get a //
$Htmldir =~ s#/\z## ; # so we don't get a //
&& substr( $Htmlfile, 0, length( $Htmldir ) ) eq $Htmldir
# Set the 'base' url for this file, so that we can use it
# as the location from which to calculate relative links
# to other files. If this is '', then absolute links will
$Htmlfileurl= "$Htmldir/" . substr( $Htmlfile, length( $Htmldir ) + 1);
# read the pod a paragraph at a time
warn "Scanning for sections in input file(s)\n" if $Verbose;
@poddata = map { s/\r\n/\n/g;
map { "$_\n\n" } split /\n\n/ :
@poddata = map { s/\r/\n/g;
map { "$_\n\n" } split /\n\n/ :
# scan the pod for =head[1-6] directives and build an index
my $index = scan_headings(\%Sections, @poddata);
warn "No headings in $Podfile\n" if $Verbose;
|| die "$0: cannot open $Htmlfile file for output: $!\n";
# put a title in the HTML file if one wasn't specified
for (my $i = 0; $i < @poddata; $i++) {
if ($poddata[$i] =~ /^=head1\s*NAME\b/m) {
for my $para ( @poddata[$i, $i+1] ) {
if ($Title) = $para =~ /(\S+\s+-+.*\S)/s;
if (!$Title and $Podfile =~ /\.pod\z/) {
# probably a split pod so take first =head[12] as title
for (my $i = 0; $i < @poddata; $i++) {
last if ($Title) = $poddata[$i] =~ /^=head[12]\s*(.*)/;
warn "adopted '$Title' as title for $Podfile\n"
warn "$0: no title for $Podfile.\n" unless $Quiet;
$Podfile =~ /^(.*)(\.[^.\/]+)?\z/s;
$Title = ($Podfile eq "-" ? 'No Title' : $1);
warn "using $Title" if $Verbose;
$Title = html_escape($Title);
my $bodystyle = ' style="background-color: white"';
my $tdstyle = ' style="background-color: #cccccc"';
$csslink = qq(\n<link rel="stylesheet" href="$Css" type="text/css" />);
$csslink =~ s,(/.):,$1|,;
my $block = $Header ? <<END_OF_BLOCK : '';
<table border="0" width="100%" cellspacing="0" cellpadding="3">
<tr><td class="block"$tdstyle valign="middle">
<big><strong><span class="block"> $Title</span></strong></big>
print HTML <<END_OF_HEAD;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<title>$Title</title>$csslink
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:$Config{perladmin}" />
# 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.
print HTML "<p><a name=\"__index__\"></a></p>\n";
print HTML "<!-- INDEX BEGIN -->\n";
print HTML "<!--\n" unless $Doindex;
print HTML "-->\n" unless $Doindex;
print HTML "<!-- INDEX END -->\n\n";
print HTML "<hr
/>\n" if $Doindex and $index;
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
if (/^(=.*)/s) { # is it a pod directive?
if (/^=begin\s+(\S+)\s*(.*)/si) {# =begin
} elsif (/^=end\s+(\S+)\s*(.*)/si) {# =end
} elsif (/^=cut/) { # =cut
} elsif (/^=pod/) { # =pod
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
$need_dd = process_item( $1 );
} elsif (/^=over\s*(.*)/) { # =over N
} elsif (/^=back/) { # =back
} elsif (/^=for\s+(\S+)\s*(.*)/si) {# =for
warn "$0: $Podfile: unknown pod directive
'$1' in "
. "paragraph
$Paragraph. ignoring
.\n" unless $Quiet;
next if @Begin_Stack && $Begin_Stack[-1] ne 'html';
print HTML and next if @Begin_Stack && $Begin_Stack[-1] eq 'html';
print HTML "<dd
>\n" if $need_dd;
print HTML "<pre
>\n$text</pre
>\n";
# experimental: check for a paragraph where all lines
# have some ...\t...\t...\n pattern
my @lines = split( "\n", $text );
foreach my $line ( @lines ){
if( $line =~ /\S/ && $line !~ /\t/ ){
$text =~ s/^/<tr><td>/gm;
$text = '<table cellspacing="0" cellpadding="0">' .
print HTML "<p
>$text</p
>\n";
print HTML "</dd
>\n" if $need_dd;
# finish off any pending directives
print HTML "<p
><a href
=\"#__index__\"><small>$Backlink</small></a></p>\n"
if $Doindex and $index and $Backlink;
print HTML
<<END_OF_TAIL;
warn "Finished\n" if $Verbose;
##############################################################################
warn "$0: $podfile: @_\n" if @_;
Usage
: $0 --help
--htmlroot
=<name
> --infile
=<name
> --outfile
=<name
>
--podpath
=<name
>:...:<name
> --podroot
=<name
>
--libpods
=<name
>:...:<name
> --recurse
--verbose
--index
--netscape
--norecurse
--noindex
--cachedir
=<name
>
--backlink
- set text
for "back to top" links
(default: none
).
--cachedir
- directory
for the item
and directory cache files
.
--flush
- flushes the item
and directory caches
.
--[no]header
- produce block header
/footer
(default is
no headers
).
--help
- prints this message
.
--hiddendirs
- search hidden directories
in podpath
--htmldir
- directory
for resulting HTML files
.
--htmlroot
- http
-server base directory from which all relative paths
in podpath stem
(default is
/).
--[no]index - generate an
index at the top of the resulting html
--infile
- filename
for the pod to convert
(input taken from stdin
--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
.
--outfile
- filename
for the resulting html file
(output sent to
--podpath
- colon
-separated list of directories containing library
--podroot
- filesystem base directory from which all relative paths
in podpath stem
(default is
.).
--[no]quiet
- suppress some benign warning messages
(default is off
).
--[no]recurse
- recurse on those subdirectories listed
in podpath
--title
- title that will appear
in resulting html file
.
--[no]verbose
- self
-explanatory
(off by
default).
--[no]netscape
- deprecated
, has
no effect
. for backwards compatibility only
.
my ($opt_backlink,$opt_cachedir,$opt_css,$opt_flush,$opt_header,$opt_help,
$opt_htmldir,$opt_htmlroot,$opt_index,$opt_infile,$opt_libpods,
$opt_netscape,$opt_outfile,$opt_podpath,$opt_podroot,$opt_quiet,
$opt_recurse,$opt_title,$opt_verbose,$opt_hiddendirs);
unshift @ARGV, split ' ', $Config{pod2html
} if $Config{pod2html
};
'backlink=s' => \
$opt_backlink,
'cachedir=s' => \
$opt_cachedir,
'header!' => \
$opt_header,
'hiddendirs!'=> \
$opt_hiddendirs,
'htmldir=s' => \
$opt_htmldir,
'htmlroot=s' => \
$opt_htmlroot,
'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,
'verbose!' => \
$opt_verbose,
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.
@Podpath = split(":", $opt_podpath) if defined $opt_podpath;
@Libpods = split(":", $opt_libpods) if defined $opt_libpods;
$Backlink = $opt_backlink if defined $opt_backlink;
$Cachedir = $opt_cachedir if defined $opt_cachedir;
$Css = $opt_css if defined $opt_css;
$Header = $opt_header if defined $opt_header;
$Htmldir = $opt_htmldir if defined $opt_htmldir;
$Htmlroot = $opt_htmlroot if defined $opt_htmlroot;
$Doindex = $opt_index if defined $opt_index;
$Podfile = $opt_infile if defined $opt_infile;
$HiddenDirs = $opt_hiddendirs if defined $opt_hiddendirs;
$Htmlfile = $opt_outfile if defined $opt_outfile;
$Podroot = $opt_podroot if defined $opt_podroot;
$Quiet = $opt_quiet if defined $opt_quiet;
$Recurse = $opt_recurse if defined $opt_recurse;
$Title = $opt_title if defined $opt_title;
$Verbose = $opt_verbose if defined $opt_verbose;
warn "Flushing item and directory caches\n"
if $opt_verbose && defined $opt_flush;
$Dircache = "$Cachedir/pod2htmd.tmp";
$Itemcache = "$Cachedir/pod2htmi.tmp";
if (defined $opt_flush) {
1 while unlink($Dircache, $Itemcache);
my($dircache, $itemcache, $podpath, $podroot, $recurse) = @_;
# 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.
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
warn "scanning directories in pod-path\n" if $Verbose;
scan_podpath
($podroot, $recurse, 0);
$Saved_Cache_Key = cache_key
(@cache_key_args);
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.
my($dircache, $itemcache, $podpath, $podroot) = @_;
open(CACHE
, "<$itemcache") ||
die "$0: error opening $itemcache for reading: $!\n";
# is it the same podpath?
$tests++ if (join(":", @
$podpath) eq $_);
# is it the same podroot?
$tests++ if ($podroot eq $_);
# load the cache if its good
warn "loading item cache\n" if $Verbose;
warn "scanning for directory cache\n" if $Verbose;
open(CACHE
, "<$dircache") ||
die "$0: error opening $dircache for reading: $!\n";
# is it the same podpath?
$tests++ if (join(":", @
$podpath) eq $_);
# is it the same podroot?
$tests++ if ($podroot eq $_);
# load the cache if its good
warn "loading directory cache\n" if $Verbose;
# 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.
my($podroot, $recurse, $append) = @_;
my($libpod, $dirname, $pod, @files, @poddata);
# scan each directory listed in @Podpath
|| 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
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
opendir(DIR
, $dirname) ||
die "$0: error opening directory $dirname: $!\n";
@files = grep(/(\.pod|\.pm)\z/ && ! -d
$_, readdir(DIR
));
# scan each .pod and .pm file for =item directives
open(POD
, "<$dirname/$pod") ||
die "$0: error opening $dirname/$pod for input: $!\n";
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
die "$0: error opening $pod for input: $!\n";
scan_items
( \
%Items, "$pod", @poddata);
warn "$0: shouldn't be here (line ".__LINE__
."\n" unless $Quiet;
@poddata = (); # clean-up a bit
|| 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";
# 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";
# 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.
my($t, @subdirs, @pods, $pod, $dirname, @dirs);
die "$0: error opening directory $dir: $!\n";
while (defined($_ = readdir(DIR
))) {
if (-d
"$dir/$_" && $_ ne "." && $_ ne ".."
&& ($HiddenDirs || !/^\./)
$Pages{$_} = "" unless defined $Pages{$_};
$Pages{$_} .= "$dir/$_:";
} elsif (/\.pod\z/) { # .pod
$Pages{$_} = "" unless defined $Pages{$_};
$Pages{$_} .= "$dir/$_.pod:";
push(@pods, "$dir/$_.pod");
} elsif (/\.html\z/) { # .html
$Pages{$_} = "" unless defined $Pages{$_};
$Pages{$_} .= "$dir/$_.pod:";
} elsif (/\.pm\z/) { # .pm
$Pages{$_} = "" unless defined $Pages{$_};
$Pages{$_} .= "$dir/$_.pm:";
push(@pods, "$dir/$_.pm");
} elsif (-T
"$dir/$_") { # script(?)
if (open(F
, "$dir/$_")) {
while (defined($line = <F
>)) {
if ($line =~ /^=(?:pod|head1)/) {
$Pages{$_} = "" unless defined $Pages{$_};
$Pages{$_} .= "$dir/$_.pod:";
# recurse on the subdirectories if necessary
foreach my $subdir (@subdirs) {
scan_dir
("$dir/$subdir", $recurse);
# scan_headings - scan a pod file for head[1-6] tags, note the tags, and
my($sections, @data) = @_;
my($tag, $which_head, $otitle, $listdepth, $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 = anchorify
( $title );
$title = process_text
( \
$otitle );
while ($which_head != $listdepth) {
if ($which_head > $listdepth) {
$index .= "\n" . ("\t" x
$listdepth) . "<ul>\n";
} elsif ($which_head < $listdepth) {
$index .= "\n" . ("\t" x
$listdepth) . "</ul>\n";
$index .= "\n" . ("\t" x
$listdepth) . "<li>" .
"<a href=\"#" . $name . "\">" .
$index .= "\n" . ("\t" x
$listdepth) . "</ul>\n";
$index =~ s
,\t*<ul
>\s
*</ul
>\n,,g
;
# scan_items - scans the pod specified by $pod for =item directives. we
# will use this information later on in resolving C<> links.
my( $itemref, $pod, @poddata ) = @_;
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
} elsif( $txt =~ /\A=item\s+(?>\d+\.?)\s*(.*)\Z/s ) { # numbered list
} elsif( $txt =~ /\A=item\s+(.*)\Z/s ) { # plain item
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.
my($tag, $heading, $hasindex) = @_;
# figure out the level of the =head
warn "$0: $Podfile: unterminated list at =head in paragraph $Paragraph. ignoring.\n" unless $Quiet;
if( $level == 1 && ! $Top ){
print HTML
"<a href=\"#__index__\"><small>$Backlink</small></a>\n"
if $hasindex and $Backlink;
print HTML
"</p>\n<hr />\n"
my $name = anchorify
( depod
( $heading ) );
my $convert = process_text
( \
$heading );
print HTML
"<h$level><a name=\"$name\">$convert</a></h$level>\n";
# emit_item_tag - print an =item's text
# Note: The global $EmittedItem is used for inhibiting self-references.
my( $otext, $text, $compact ) = @_;
my $item = fragment_id
( $text );
### print STDERR "emit_item_tag=$item ($text)\n";
if ($Items_Named{$item}++) {
print HTML process_text
( \
$otext );
my $name = 'item_' . $item;
$name = anchorify
($name);
print HTML
qq{<a name
="$name">}, process_text
( \
$otext ), '</a>';
print HTML
"</strong>\n";
if( $Items_Seen[$Listlevel]++ == 0 ){
push( @Listend, "</$tag>" );
my $emitted = $tag eq 'dl' ?
'dt' : 'li';
# process_item - convert a pod item tag and convert it to HTML format.
my $need_dd = 0; # set to 1 if we need a <dd></dd> after an item
# 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.
warn "$0: $Podfile: unexpected =item directive in paragraph $Paragraph. ignoring.\n" unless $Quiet;
# formatting: insert a paragraph if preceding item has >1 paragraph
print HTML
$need_dd ?
"</dd>\n" : "</li>\n" if $After_Lpar;
# remove formatting instructions from the text
my $text = depod
( $otext );
my $emitted; # the tag actually emitted, used for closing
if( $text =~ /\A\*/ ){ # bullet
$emitted = emit_li
( 'ul' );
if ($text =~ /\A\*\s+(.+)\Z/s ) { # with additional text
emit_item_tag
( $otext, $tag, 1 );
} elsif( $text =~ /\A\d+/ ){ # numbered list
$emitted = emit_li
( 'ol' );
if ($text =~ /\A(?>\d+\.?)\s*(.+)\Z/s ) { # with additional text
$otext =~ s/\A\d+\.?\s*//;
emit_item_tag
( $otext, $tag, 1 );
} else { # definition list
$emitted = emit_li
( 'dl' );
if ($text =~ /\A(.+)\Z/s ){ # should have text
emit_item_tag
( $otext, $text, 1 );
# process_over - process a pod over tag and start a corresponding HTML list.
# process_back - process a pod back tag and convert it to HTML format.
warn "$0: $Podfile: unexpected =back directive in paragraph $Paragraph. ignoring.\n" unless $Quiet;
# 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.
if( defined $Listend[$Listlevel] ){
print HTML
$need_dd ?
"</dd>\n" : "</li>\n" if $After_Lpar;
print HTML
$Listend[$Listlevel];
# process_cut - process a pod cut tag, thus start ignoring pod directives.
# process_pod - process a pod tag, thus stop ignoring pod directives
# until we see a corresponding cut.
# 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.
if ( $whom =~ /^(pod2)?html$/i) {
} elsif ($whom =~ /^illustration$/i) {
for my $ext (qw
[.png
.gif
.jpeg
.jpg
.tga
.pcl
.bmp
]) {
$text .= $ext, last if -r
"$text$ext";
print HTML
qq{<p align
="center"><img src
="$text" alt
="$text illustration" /></p>};
# 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.
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.
if ($Begin_Stack[-1] ne $whom ) {
die "Unmatched begin/end at chunk $Paragraph\n"
# process_pre - indented paragraph, made into <pre></pre>
# insert spaces in place of tabs
1 while $line =~ s/(\t+)/' ' x ((length($1) * 8) - $-[0] % 8)/e;
# convert some special chars to HTML escapes
$rest = html_escape
($rest);
# try and create links for all occurrences of perl.* within
if ( defined $Pages{$2} ){ # is a link
qq($1<a href
="$Htmlroot/$Pages{$2}">$2</a
>);
} elsif (defined $Pages{dosify
($2)}) { # is a link
qq($1<a href
="$Htmlroot/$Pages{dosify($2)}">$2</a
>);
(<a\ href
="?) ([^>:]*:)? ([^>:]*) \.pod: ([^>:]*:)?
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.
$old_url = "$Htmldir$old_url" if $old_url =~ m{^\/};
$url = relativize_url( "$old_url.html
", $Htmlfileurl );
# 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{
my $gunk = '/#~:.?+=&%@!\-';
my $any = "${ltrs
}${gunk
}${punc
}";
\b # start at word boundary
$urls : # need resource and a colon
(?!:) # Ignore File::, among others.
[$any] +? # followed by one or more of any valid
# character, but be conservative and
# take only what you need to....
" > # maybe pre-quoted '<a href="...">'
[$punc]* # 0 or more punctuation
[^$any] # by a non-url char
$ # then end of the string
}{<a href="$1">$1</a>}igox;
# text should be as it is (verbatim)
# pure_text/inIS_text: differ with respect to automatic C<> recognition.
# we don't want this to happen within IS
process_puretext( $text, \$PTQuote, 1 );
process_puretext( $text, \$PTQuote, 0 );
# process_puretext - process pure text (without pod-escapes) converting
# double-quotes and handling implicit C<> links.
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:
my(@words, $lead, $trail);
# convert double-quotes to single-quotes
if( $$quote && $text =~ s/"/''/s ){
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) {
next if $word =~ /^\s*$/;
# see if we can infer a link
if( $notinIS && $word =~ /^(\w+)\((.*)\)$/ ) {
# has parenthesis so should have been a C<> ref
## try for a pagename (perlXXX(1))?
my( $func, $args ) = ( $1, $2 );
my $url = page_sect( $word, '' );
$word = "<a href
=\"$url\">the
$word manpage
</a
>";
## try function name for a link, append tt'ed argument list
$word = emit_C( $func, '', "($args)");
#### 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,) {
# Don't relativize it: leave it as the author intended
$word = qq(<a href="$word">$word</a>);
} 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<a href="mailto
:$w2">$w2</a>$w3);
$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_text1
($$;$$);
sub pattern
($) { $_[0] ?
'[^\S\n]+'.('>' x
($_[0] + 1)) : '>' }
sub closing
($) { local($_) = shift; (defined && s/\s+$//) ?
length : 0 }
my $res = process_text1
( 0, $tref );
sub process_text1
($$;$$){
my( $lev, $rstr, $func, $closing ) = @_;
$res = '<strong>' . process_text1
( $lev, $rstr ) . '</strong>';
# C<code> - can be a ref or <code></code>
my $par = go_ahead
( $rstr, 'C', $closing );
## 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]</) );
# E<x> - convert to character
$escape =~ s/^(\d+|X[\dA-F]+)$/#$1/i;
# F<filename> - italizice
$res = '<em>' . process_text1
( $lev, $rstr ) . '</em>';
$res = '<em>' . process_text1
( $lev, $rstr ) . '</em>';
## L<text|cross-ref> => produce text, use cross-ref for linking
## L<cross-ref> => make text from cross-ref
my $par = go_ahead
( $rstr, 'L', $closing );
# 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<(.*)>$/ ){
return emit_C
( $text, $lev > 1 || ($par =~ /[BI]</) );
$par =~ s/\n/ /g; # undo word-wrapped tags
if( $par =~ s{^([^|]+)\|}{} ){
# make sure sections start with a /
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";
( $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 );
## try to resolve $ident as an item
( $url, $fid ) = coderef
( $page, $ident );
if( ! defined( $linktext ) ){
$linktext .= " in " if $ident && $page;
$linktext .= "the $page manpage" if $page;
### print STDERR "got coderef url=$url\n";
## no luck: go for a section (auto-quoting!)
my $htmlsection = htmlify
( $section );
$url = page_sect
( $page, $htmlsection );
if( ! defined( $linktext ) ){
$linktext .= " in " if $section && $page;
$linktext .= "the $page manpage" if $page;
### print STDERR "got page/section url=$url\n";
## no luck: go for an ident
( $url, $fid ) = coderef
( $page, $ident );
if( ! defined( $linktext ) ){
$linktext .= " in " if $ident && $page;
$linktext .= "the $page manpage" if $page;
### print STDERR "got section=>coderef url=$url\n";
# warning; show some text.
$linktext = $opar unless defined $linktext;
warn "$0: $Podfile: cannot resolve L<$opar> in paragraph $Paragraph.\n" unless $Quiet;
# now we have a URL or just plain code
$$rstr = $linktext . '>' . $$rstr;
$res = "<a href=\"$url\">" . process_text1
( $lev, $rstr ) . '</a>';
$res = '<em>' . process_text1
( $lev, $rstr ) . '</em>';
# S<text> - non-breaking spaces
$res = process_text1
( $lev, $rstr );
warn "$0: $Podfile: invalid X<> in paragraph $Paragraph.\n"
unless $$rstr =~ s/^>// or $Quiet;
my $term = pattern
$closing;
while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)//s ){
# all others: either recurse into new function or
# terminate at closing angle bracket(s)
$pt .= $2 if !$3 && $lev == 1;
$res .= $lev == 1 ? pure_text
( $pt ) : inIS_text
( $pt );
return $res if !$3 && $lev > 1;
$res .= process_text1
( $lev, $rstr, $3, closing
$4 );
$res .= pure_text
( $$rstr );
warn "$0: $Podfile: undelimited $func<> in paragraph $Paragraph.\n" unless $Quiet;
# go_ahead: extract text of an IS (can be nested)
my( $rstr, $func, $closing ) = @_;
my @closing = ($closing);
s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|@{[pattern $closing[0]]})//s ){
return $res unless @closing;
unshift @closing, closing
$4;
warn "$0: $Podfile: undelimited $func<> in paragraph $Paragraph.\n" unless $Quiet;
# emit_C - output result of C<text>
# $text is the depod-ed text
my( $text, $nocode, $args ) = @_;
$args = '' unless defined $args;
my( $url, $fid ) = coderef
( undef(), $text );
my $linktext = html_escape
( "$text$args" );
(!defined( $EmittedItem ) || $EmittedItem ne $fid ) ){
$res = "<a href=\"$url\"><code>$linktext</code></a>";
$res = "<code>$linktext</code>";
# html_escape: make text safe for HTML
# ' is only in XHTML, not HTML4. Be conservative
# dosify - convert filenames to 8.3
return lc($str) if $^O
eq 'VMS'; # VMS just needs casing
$str =~ s/(\.\w+)/substr ($1,0,4)/ge;
$str =~ s/(\w+)/substr ($1,0,8)/ge;
# page_sect - make a URL from the text of a L<>
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}) {
### print STDERR "reset page='', section=$section\n";
$page=$page83 if (defined $Pages{$page83});
$link = "#" . anchorify
( $section );
} elsif ( $page =~ /::/ ) {
# 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.
$page_name =~ s
,^.*/,,s
;
if ( defined( $Pages{ $page_name } ) &&
$Pages{ $page_name } =~ /([^:]*$page)\.(?:pod|pm):/
# 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
$link = "$Htmlroot/$page.html";
$link .= "#" . anchorify
( $section ) if ($section);
} elsif (!defined $Pages{$page}) {
$section = anchorify
( $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
$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";
# 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.
if ( $Htmlfileurl ne '' ) {
$link = "$Htmldir$link" if $link =~ m{^/}s;
$url = relativize_url
( $link, $Htmlfileurl );
# print( " b: [$link,$Htmlfileurl,$url]\n" );
# relativize_url - convert an absolute URL to one relative to a base URL.
# Assumes both end in a filename.
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, '' ) ;
$rel_path = File
::Spec
::Unix
->abs2rel( $dest, $source ) ;
substr( $rel_path, -1 ) ne '/' &&
substr( $dest_file, 0, 1 ) ne '#'
$rel_path .= "/$dest_file" ;
$rel_path .= "$dest_file" ;
# coderef - make URL from the text of a C<>
my $fid = fragment_id
( $item );
if( defined( $page ) && $page ne "" ){
# we have been given a $page...
# Do we take it? Item could be a section!
my $base = $Items{$fid} || "";
if( $base ne "$page.html" ){
### print STDERR "coderef( $page, $item ): items{$fid} = $Items{$fid} = $base => discard page!\n";
# no page - local items precede cached items
if( exists $Local_Items{$fid} ){
$page = $Local_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( exists $Pages{$page} and $Pages{$page} =~ /([^:.]*)\.[^:]*:/){
my $link = "$Htmlroot/$page#item_" . anchorify
($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 ) ;
$url = "#item_" . anchorify
($fid);
confess
"url has space: $url" if $url =~ /"[^"]*\s[^"]*"/;
# Adapted from Nick Ing-Simmons' PodToHtml package.
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.
# 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...).
# The hyphen is a disgrace to the English language.
$heading = lc( $heading );
# similar to htmlify, but turns non-alphanumerics into underscores
$anchor = htmlify
($anchor);
# depod - convert text by eliminating all interior sequences
# Note: can be called with copy or modify semantics
$E2c{amp
} = '&'; # in Tk's pods
${$_[0]} = depod1
( \
$string );
my( $rstr, $func, $closing ) = @_;
return $res unless defined $$rstr;
if( ! defined( $func ) ){
# skip to next begin of an interior sequence
while( $$rstr =~ s/\A(.*?)([BCEFILSXZ])<(<+[^\S\n]+)?// ){
$res .= $1 . depod1
( $rstr, $2, closing
$3);
# E<x> - convert to character
# all others: either recurse into new function or
# terminate at closing angle bracket
my $term = pattern
$closing;
while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|$term)// ){
$res .= depod1
( $rstr, $3, closing
$4 );
## If we're here and $2 ne '>': undelimited interior sequence.
## Ignored, as this is called without proper indication of where we are.
## Rely on process_text to produce diagnostics.
# fragment_id - construct a fragment identifier from:
return $1 if $text =~ /(\w+)\s*\(/;
return $1 if $text =~ /->\s*(\w+)\s*\(?/;
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,/& ]+)?$};
defined( $HC[ord($1)] ) ?
$HC[ord($1)]
: ( $HC[ord($1)] = sprintf( "%%%02X", ord($1) ) ) }gxe
;
$text = substr( $text, 0, 50 );
# make_URL_href - generate HTML href from URL
# Special treatment for CGI queries.
s{^(http:[-\w/#~:.+=&%@!]+)(\?.*)$}{<a href="$1$2">$1</a>}i ){
$url = "<a href=\"$url\">$url</a>";