📄 html.pm
字号:
$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 ) . '</A>'; } else { $res = '<EM>' . process_text1( $lev, $rstr ) . '</EM>'; } } elsif( $func eq 'S' ){ # S<text> - non-breaking spaces $res = process_text1( $lev, $rstr ); $res =~ s/ / /g; } 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 { 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) my $pt = $1; $pt .= $2 if !$3 && $lev == 1; $res .= $lev == 1 ? pure_text( $pt ) : inIS_text( $pt ); return $res if !$3 && $lev > 1; if( $3 ){ $res .= process_text1( $lev, $rstr, $3, closing $4 ); } } 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, $closing ) = @_; my $res = ''; my @closing = ($closing); while( $$rstr =~ s/\A(.*?)(([BCEFILSXZ])<(<+[^\S\n]+)?|@{[pattern $closing[0]]})//s ){ $res .= $1; unless( $3 ){ shift @closing; return $res unless @closing; } else { unshift @closing, closing $4; } $res .= $2; } warn "$0: $podfile: undelimited $func<> in paragraph $paragraph."; return $res;}## emit_C - output result of C<text># $text is the depod-ed text#sub emit_C($;$$){ my( $text, $nocode, $args ) = @_; $args = '' unless defined $args; my $res; my( $url, $fid ) = coderef( undef(), $text ); # need HTML-safe text my $linktext = html_escape( "$text$args" ); if( defined( $url ) && (!defined( $EmittedItem ) || $EmittedItem ne $fid ) ){ $res = "<A HREF=\"$url\"><CODE>$linktext</CODE></A>"; } elsif( 0 && $nocode ){ $res = $linktext; } else { $res = "<CODE>$linktext</CODE>"; } 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,^.*/,,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{^/}s; $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( defined( $fid ) ){ 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( exists $pages{$page} and $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 "</DL>\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} = '|';$E2c{amp} = '&'; # in Tk's podssub depod1($;$$);sub depod($){ my $string; if( ref( $_[0] ) ){ $string = ${$_[0]}; ${$_[0]} = depod1( \$string ); } else { $string = $_[0]; depod1( \$string ); } }sub depod1($;$$){ my( $rstr, $func, $closing ) = @_; my $res = ''; return $res unless defined $$rstr; if( ! defined( $func ) ){ # skip to next begin of an interior sequence while( $$rstr =~ s/\A(.*?)([BCEFILSXZ])<(<+[^\S\n]+)?// ){ # recurse into its text $res .= $1 . depod1( $rstr, $2, closing $3); } $res .= $$rstr; } elsif( $func eq 'E' ){ # E<x> - convert to character $$rstr =~ s/^([^>]*)>//; $res .= $E2c{$1} || ""; } elsif( $func eq 'X' ){ # X<> - ignore $$rstr =~ s/^[^>]*>//; } elsif( $func eq 'Z' ){ # Z<> - empty $$rstr =~ s/^>//; } else { # 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 .= $1; last unless $3; $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. } return $res;}## 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/#~:.+=&%@!]+)(\?.*)$}{<A HREF="$1$2">$1</A>}i ){ $url = "<A HREF=\"$url\">$url</A>"; } return $url;}1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -