📄 pod2man.pl
字号:
: #!/usr/bin/perl-5.005 eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if $running_under_some_shell;$DEF_PM_SECTION = '3pm' || '3';=head1 NAMEpod2man - translate embedded Perl pod directives into man pages=head1 SYNOPSISB<pod2man>[ B<--section=>I<manext> ][ B<--release=>I<relpatch> ][ B<--center=>I<string> ][ B<--date=>I<string> ][ B<--fixed=>I<font> ][ B<--official> ][ B<--lax> ]I<inputfile>=head1 DESCRIPTIONB<pod2man> converts its input file containing embedded pod directives (seeL<perlpod>) into nroff source suitable for viewing with nroff(1) ortroff(1) using the man(7) macro set.Besides the obvious pod conversions, B<pod2man> also takes care offunc(), func(n), and simple variable references like $foo or @bar soyou don't have to use code escapes for them; complex expressions likeC<$fred{'stuff'}> will still need to be escaped, though. Other nagginglittle roffish things that it catches include translating the minus insomething like foo-bar, making a long dash--like this--into a real emdash, fixing up "paired quotes", putting a little space after theparens in something like func(), making C++ and PI look right, makingdouble underbars have a little tiny space between them, making ALLCAPSa teeny bit smaller in troff(1), and escaping backslashes so you don'thave to.=head1 OPTIONS=over 8=item centerSet the centered header to a specific string. The default is"User Contributed Perl Documentation", unless the C<--official> flag isgiven, in which case the default is "Perl Programmers Reference Guide".=item dateSet the left-hand footer string to this value. By default,the modification date of the input file will be used.=item fixedThe fixed font to use for code refs. Defaults to CW.=item officialSet the default header to indicate that this page is ofthe standard release in case C<--center> is not given.=item releaseSet the centered footer. By default, this is the currentperl release.=item sectionSet the section for the C<.TH> macro. The standard conventions onsections are to use 1 for user commands, 2 for system calls, 3 forfunctions, 4 for devices, 5 for file formats, 6 for games, 7 formiscellaneous information, and 8 for administrator commands. This worksbest if you put your Perl man pages in a separate tree, likeF</usr/local/perl/man/>. By default, section 1 will be usedunless the file ends in F<.pm> in which case section 3 will be selected.=item laxDon't complain when required sections aren't present.=back=head1 Anatomy of a Proper Man PageFor those not sure of the proper layout of a man page, here'san example of the skeleton of a proper man page. Head of themajor headers should be setout as a C<=head1> directive, andare historically written in the rather startling ALL UPPER CASEformat, although this is not mandatory.Minor headers may be included using C<=head2>, and aretypically in mixed case.=over 10=item NAMEMandatory section; should be a comma-separated list of programs orfunctions documented by this podpage, such as: foo, bar - programs to do something=item SYNOPSISA short usage summary for programs and functions, whichmay someday be deemed mandatory.=item DESCRIPTIONLong drawn out discussion of the program. It's a good idea to break thisup into subsections using the C<=head2> directives, like =head2 A Sample Subection =head2 Yet Another Sample Subection=item OPTIONSSome people make this separate from the description.=item RETURN VALUEWhat the program or function returns if successful.=item ERRORSExceptions, return codes, exit stati, and errno settings.=item EXAMPLESGive some example uses of the program.=item ENVIRONMENTEnvariables this program might care about.=item FILESAll files used by the program. You should probably use the FE<lt>E<gt>for these.=item SEE ALSOOther man pages to check out, like man(1), man(7), makewhatis(8), or catman(8).=item NOTESMiscellaneous commentary.=item CAVEATSThings to take special care with; sometimes called WARNINGS.=item DIAGNOSTICSAll possible messages the program can print out--andwhat they mean.=item BUGSThings that are broken or just don't work quite right.=item RESTRICTIONSBugs you don't plan to fix :-)=item AUTHORWho wrote it (or AUTHORS if multiple).=item HISTORYPrograms derived from other sources sometimes have this, oryou might keep a modification log here.=back=head1 EXAMPLES pod2man program > program.1 pod2man some_module.pm > /usr/perl/man/man3/some_module.3 pod2man --section=7 note.pod > note.7=head1 DIAGNOSTICSThe following diagnostics are generated by B<pod2man>. Itemsmarked "(W)" are non-fatal, whereas the "(F)" errors will causeB<pod2man> to immediately exit with a non-zero status.=over 4=item bad option in paragraph %d of %s: ``%s'' should be [%s]<%s>(W) If you start include an option, you should set it offas bold, italic, or code.=item can't open %s: %s(F) The input file wasn't available for the given reason.=item Improper man page - no dash in NAME header in paragraph %d of %s(W) The NAME header did not have an isolated dash in it. This isconsidered important.=item Invalid man page - no NAME line in %s(F) You did not include a NAME header, which is essential.=item roff font should be 1 or 2 chars, not `%s' (F)(F) The font specified with the C<--fixed> option was nota one- or two-digit roff font.=item %s is missing required section: %s(W) Required sections include NAME, DESCRIPTION, and if you'reusing a section starting with a 3, also a SYNOPSIS. Actually,not having a NAME is a fatal.=item Unknown escape: %s in %s(W) An unknown HTML entity (probably for an 8-bit character) was given viaa C<EE<lt>E<gt>> directive. Besides amp, lt, gt, and quot, recognizedentities are Aacute, aacute, Acirc, acirc, AElig, aelig, Agrave, agrave,Aring, aring, Atilde, atilde, Auml, auml, Ccedil, ccedil, Eacute, eacute,Ecirc, ecirc, Egrave, egrave, ETH, eth, Euml, euml, Iacute, iacute, Icirc,icirc, Igrave, igrave, Iuml, iuml, Ntilde, ntilde, Oacute, oacute, Ocirc,ocirc, Ograve, ograve, Oslash, oslash, Otilde, otilde, Ouml, ouml, szlig,THORN, thorn, Uacute, uacute, Ucirc, ucirc, Ugrave, ugrave, Uuml, uuml,Yacute, yacute, and yuml.=item Unmatched =back(W) You have a C<=back> without a corresponding C<=over>.=item Unrecognized pod directive: %s(W) You specified a pod directive that isn't in the known list ofC<=head1>, C<=head2>, C<=item>, C<=over>, C<=back>, or C<=cut>.=back=head1 NOTESIf you would like to print out a lot of man page continuously, youprobably want to set the C and D registers to set contiguous pagenumbering and even/odd paging, at least on some versions of man(7).Settting the F register will get you some additional experimentalindexing: troff -man -rC1 -rD1 -rF1 perl.1 perldata.1 perlsyn.1 ...The indexing merely outputs messages via C<.tm> for eachmajor page, section, subsection, item, and any C<XE<lt>E<gt>>directives.=head1 RESTRICTIONSNone at this time.=head1 BUGSThe =over and =back directives don't really work right. Theytake absolute positions instead of offsets, don't nest well, andmaking people count is suboptimal in any event.=head1 AUTHORSOriginal prototype by Larry Wall, but so massively hacked over byTom Christiansen such that Larry probably doesn't recognize it anymore.=cut$/ = "";$cutting = 1;@Indices = ();# We try first to get the version number from a local binary, in case we're# running an installed version of Perl to produce documentation from an# uninstalled newer version's pod files.if ($^O ne 'plan9' and $^O ne 'dos' and $^O ne 'os2' and $^O ne 'MSWin32') { my $perl = (-x './perl' && -f './perl' ) ? './perl' : ((-x '../perl' && -f '../perl') ? '../perl' : ''); ($version,$patch) = `$perl -e 'print $]'` =~ /^(\d\.\d{3})(\d{2})?/ if $perl;}# No luck; we'll just go with the running Perl's version($version,$patch) = $] =~ /^(.{5})(\d{2})?/ unless $version;$DEF_RELEASE = "perl $version";$DEF_RELEASE .= ", patch $patch" if $patch;sub makedate { my $secs = shift; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($secs); my $mname = (qw{Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec})[$mon]; $year += 1900; return "$mday/$mname/$year";}use Getopt::Long;$DEF_SECTION = 1;$DEF_CENTER = "User Contributed Perl Documentation";$STD_CENTER = "Perl Programmers Reference Guide";$DEF_FIXED = 'CW';$DEF_LAX = 0;sub usage { warn "$0: @_\n" if @_; die <<EOF;usage: $0 [options] podpageOptions are: --section=manext (default "$DEF_SECTION") --release=relpatch (default "$DEF_RELEASE") --center=string (default "$DEF_CENTER") --date=string (default "$DEF_DATE") --fixed=font (default "$DEF_FIXED") --official (default NOT) --lax (default NOT)EOF}$uok = GetOptions( qw( section=s release=s center=s date=s fixed=s official lax help));$DEF_DATE = makedate((stat($ARGV[0]))[9] || time());usage("Usage error!") unless $uok;usage() if $opt_help;usage("Need one and only one podpage argument") unless @ARGV == 1;$section = $opt_section || ($ARGV[0] =~ /\.pm$/ ? $DEF_PM_SECTION : $DEF_SECTION);$RP = $opt_release || $DEF_RELEASE;$center = $opt_center || ($opt_official ? $STD_CENTER : $DEF_CENTER);$lax = $opt_lax || $DEF_LAX;$CFont = $opt_fixed || $DEF_FIXED;if (length($CFont) == 2) { $CFont_embed = "\\f($CFont";}elsif (length($CFont) == 1) { $CFont_embed = "\\f$CFont";}else { die "roff font should be 1 or 2 chars, not `$CFont_embed'";}$date = $opt_date || $DEF_DATE;for (qw{NAME DESCRIPTION}) {# for (qw{NAME DESCRIPTION AUTHOR}) { $wanna_see{$_}++;}$wanna_see{SYNOPSIS}++ if $section =~ /^3/;$name = @ARGV ? $ARGV[0] : "<STDIN>";$Filename = $name;if ($section =~ /^1/) { require File::Basename; $name = uc File::Basename::basename($name);}$name =~ s/\.(pod|p[lm])$//i;# Lose everything up to the first of# */lib/*perl* standard or site_perl module# */*perl*/lib from -D prefix=/opt/perl# */*perl*/ random module hierarchy# which works.$name =~ s-//+-/-g;if ($name =~ s-^.*?/lib/[^/]*perl[^/]*/--i or $name =~ s-^.*?/[^/]*perl[^/]*/lib/--i or $name =~ s-^.*?/[^/]*perl[^/]*/--i) { # Lose ^site(_perl)?/. $name =~ s-^site(_perl)?/--; # Lose ^arch/. (XXX should we use Config? Just for archname?) $name =~ s~^(.*-$^O|$^O-.*)/~~o; # Lose ^version/. $name =~ s-^\d+\.\d+/--;}# Translate Getopt/Long to Getopt::Long, etc.$name =~ s(/)(::)g;if ($name ne 'something') { FCHECK: { open(F, "< $ARGV[0]") || die "can't open $ARGV[0]: $!"; while (<F>) { next unless /^=\b/; if (/^=head1\s+NAME\s*$/) { # an /m would forgive mistakes $_ = <F>; unless (/\s*-+\s+/) { $oops++; warn "$0: Improper man page - no dash in NAME header in paragraph $. of $ARGV[0]\n" } else { my @n = split /\s+-+\s+/; if (@n != 2) { $oops++; warn "$0: Improper man page - malformed NAME header in paragraph $. of $ARGV[0]\n" } else { $n[0] =~ s/\n/ /g; $n[1] =~ s/\n/ /g; %namedesc = @n; } } last FCHECK; } next if /^=cut\b/; # DB_File and Net::Ping have =cut before NAME next if /^=pod\b/; # It is OK to have =pod before NAME next if /^=for\s+comment\b/; # It is OK to have =for comment before NAME die "$0: Invalid man page - 1st pod line is not NAME in $ARGV[0]\n" unless $lax; } die "$0: Invalid man page - no documentation in $ARGV[0]\n" unless $lax; } close F;}print <<"END";.rn '' }`''' \$RCSfile\$\$Revision\$\$Date\$'''''' \$Log\$'''.de Sh.br.if t .Sp.ne 5.PP\\fB\\\\\$1\\fR.PP...de Sp.if t .sp .5v.if n .sp...de Ip.br.ie \\\\n(.\$>=3 .ne \\\\\$3.el .ne 3.IP "\\\\\$1" \\\\\$2...de Vb.ft $CFont.nf.ne \\\\\$1...de Ve.ft R.fi..''''''''' Set up \\*(-- to give an unbreakable dash;''' string Tr holds user defined translation string.''' Bell System Logo is used as a dummy character.'''.tr \\(*W-|\\(bv\\*(Tr.ie n \\{\\.ds -- \\(*W-.ds PI pi.if (\\n(.H=4u)&(1m=24u) .ds -- \\(*W\\h'-12u'\\(*W\\h'-12u'-\\" diablo 10 pitch.if (\\n(.H=4u)&(1m=20u) .ds -- \\(*W\\h'-12u'\\(*W\\h'-8u'-\\" diablo 12 pitch.ds L" "".ds R" ""''' \\*(M", \\*(S", \\*(N" and \\*(T" are the equivalent of''' \\*(L" and \\*(R", except that they are used on ".xx" lines,''' such as .IP and .SH, which do another additional levels of''' double-quote interpretation.ds M" """.ds S" """.ds N" """"".ds T" """"".ds L' '.ds R' '.ds M' '.ds S' '.ds N' '.ds T' ''br\\}.el\\{\\.ds -- \\(em\\|.tr \\*(Tr.ds L" ``.ds R" ''.ds M" ``.ds S" ''.ds N" ``.ds T" ''.ds L' `.ds R' '.ds M' `.ds S' '.ds N' `.ds T' '.ds PI \\(*p'br\\}ENDprint <<'END';.\" If the F register is turned on, we'll generate.\" index entries out stderr for the following things:.\" TH Title .\" SH Header.\" Sh Subsection .\" Ip Item.\" X<> Xref (embedded.\" Of course, you have to process the output yourself.\" in some meaninful fashion..if \nF \{.de IX.tm Index:\\$1\t\\n%\t"\\$2"...nr % 0.rr F.\}ENDprint <<"END";.TH $name $section "$RP" "$date" "$center".UCENDpush(@Indices, qq{.IX Title "$name $section"});while (($name, $desc) = each %namedesc) { for ($name, $desc) { s/^\s+//; s/\s+$//; } push(@Indices, qq(.IX Name "$name - $desc"\n));}print <<'END';.if n .hy 0.if n .na.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.de CQ \" put $1 in typewriter fontENDprint ".ft $CFont\n";print <<'END';'if n "\c'if t \\&\\$1\c'if n \\&\\$1\c'if n \&"\\&\\$2 \\$3 \\$4 \\$5 \\$6 \\$7'.ft R...\" @(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2. \" AM - accent mark definitions.bd B 3. \" fudge factors for nroff and troff.if n \{\. ds #H 0. ds #V .8m. ds #F .3m. ds #[ \f1. ds #] \fP.\}.if t \{\. ds #H ((1u-(\\\\n(.fu%2u))*.13m). ds #V .6m. ds #F 0. ds #[ \&. ds #] \&.\}. \" simple accents for nroff and troff.if n \{\. ds ' \&. ds ` \&. ds ^ \&. ds , \&. ds ~ ~. ds ? ?. ds ! !. ds /. ds q.\}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -