Cross Reference: Text.pm
xref
: /
osnet-11
/
usr
/
src
/
cmd
/
perl
/
5.8.4
/
distrib
/
lib
/
Pod
/
Text.pm
Home
History
Annotate
Line#
Navigate
Download
Search
only in
./
1
N/A
# Pod::Text -- Convert POD data to formatted ASCII text.
1
N/A
# $Id:
Text.pm
,v 2.21 2002/08/04 03:34:58 eagle Exp $
1
N/A
#
1
N/A
# Copyright 1999, 2000, 2001, 2002 by Russ Allbery <rra@stanford.edu>
1
N/A
#
1
N/A
# This program is free software; you may redistribute it
and
/
or
modify it
1
N/A
# under the same terms as Perl itself.
1
N/A
#
1
N/A
# This module converts POD to formatted text. It replaces the old Pod::Text
1
N/A
# module that came with versions of Perl prior to 5.6.0 and attempts to match
1
N/A
# its output except for some specific circumstances where other decisions
1
N/A
# seemed to produce better output. It uses Pod::Parser and is designed to be
1
N/A
# very easy to subclass.
1
N/A
#
1
N/A
# Perl core hackers, please note that this module is also separately
1
N/A
# maintained outside of the Perl core as part of the podlators. Please send
1
N/A
# me any patches at the address above in addition to sending them to the
1
N/A
# standard Perl mailing lists.
1
N/A
1
N/A
##############################################################################
1
N/A
# Modules and declarations
1
N/A
##############################################################################
1
N/A
1
N/A
package
Pod
::
Text
;
1
N/A
1
N/A
require
5.004
;
1
N/A
1
N/A
use
Carp
qw
(
carp
croak
);
1
N/A
use
Exporter
();
1
N/A
use
Pod
::
ParseLink
qw
(
parselink
);
1
N/A
use
Pod
::
Select
();
1
N/A
1
N/A
use
strict
;
1
N/A
use
vars
qw
(@
ISA
@
EXPORT
%
ESCAPES
$
VERSION
);
1
N/A
1
N/A
# We inherit from Pod::Select instead of Pod::Parser so that we can be used by
1
N/A
# Pod::Usage.
1
N/A
@
ISA
=
qw
(
Pod
::
Select
Exporter
);
1
N/A
1
N/A
# We have to export pod2text for backward compatibility.
1
N/A
@
EXPORT
=
qw
(
pod2text
);
1
N/A
1
N/A
# Don't use the CVS revision as the version, since this module is also in Perl
1
N/A
# core and too many things could munge CVS magic revision strings. This
1
N/A
# number should ideally be the same as the CVS revision in podlators, however.
1
N/A
$
VERSION
=
2.21
;
1
N/A
1
N/A
1
N/A
##############################################################################
1
N/A
# Table of supported E<> escapes
1
N/A
##############################################################################
1
N/A
1
N/A
# This table is taken near verbatim from Pod::PlainText in Pod::Parser, which
1
N/A
# got it near verbatim from the original Pod::Text. It is therefore credited
1
N/A
# to Tom Christiansen, and I'm glad I didn't have to write it. :) "iexcl" to
1
N/A
# "divide" added by Tim Jenness.
1
N/A
%
ESCAPES
= (
1
N/A
'amp'
=>
'&'
,
# ampersand
1
N/A
'apos'
=>
"'"
,
# apostrophe
1
N/A
'lt'
=>
'<'
,
# left chevron, less-than
1
N/A
'gt'
=>
'>'
,
# right chevron, greater-than
1
N/A
'quot'
=>
'"'
,
# double quote
1
N/A
'sol'
=>
'/'
,
# solidus (forward slash)
1
N/A
'verbar'
=>
'|'
,
# vertical bar
1
N/A
1
N/A
"Aacute"
=>
"\xC1"
,
# capital A, acute accent
1
N/A
"aacute"
=>
"\xE1"
,
# small a, acute accent
1
N/A
"Acirc"
=>
"\xC2"
,
# capital A, circumflex accent
1
N/A
"acirc"
=>
"\xE2"
,
# small a, circumflex accent
1
N/A
"AElig"
=>
"\xC6"
,
# capital AE diphthong (ligature)
1
N/A
"aelig"
=>
"\xE6"
,
# small ae diphthong (ligature)
1
N/A
"Agrave"
=>
"\xC0"
,
# capital A, grave accent
1
N/A
"agrave"
=>
"\xE0"
,
# small a, grave accent
1
N/A
"Aring"
=>
"\xC5"
,
# capital A, ring
1
N/A
"aring"
=>
"\xE5"
,
# small a, ring
1
N/A
"Atilde"
=>
"\xC3"
,
# capital A, tilde
1
N/A
"atilde"
=>
"\xE3"
,
# small a, tilde
1
N/A
"Auml"
=>
"\xC4"
,
# capital A, dieresis or umlaut mark
1
N/A
"auml"
=>
"\xE4"
,
# small a, dieresis or umlaut mark
1
N/A
"Ccedil"
=>
"\xC7"
,
# capital C, cedilla
1
N/A
"ccedil"
=>
"\xE7"
,
# small c, cedilla
1
N/A
"Eacute"
=>
"\xC9"
,
# capital E, acute accent
1
N/A
"eacute"
=>
"\xE9"
,
# small e, acute accent
1
N/A
"Ecirc"
=>
"\xCA"
,
# capital E, circumflex accent
1
N/A
"ecirc"
=>
"\xEA"
,
# small e, circumflex accent
1
N/A
"Egrave"
=>
"\xC8"
,
# capital E, grave accent
1
N/A
"egrave"
=>
"\xE8"
,
# small e, grave accent
1
N/A
"ETH"
=>
"\xD0"
,
# capital Eth, Icelandic
1
N/A
"eth"
=>
"\xF0"
,
# small eth, Icelandic
1
N/A
"Euml"
=>
"\xCB"
,
# capital E, dieresis or umlaut mark
1
N/A
"euml"
=>
"\xEB"
,
# small e, dieresis or umlaut mark
1
N/A
"Iacute"
=>
"\xCD"
,
# capital I, acute accent
1
N/A
"iacute"
=>
"\xED"
,
# small i, acute accent
1
N/A
"Icirc"
=>
"\xCE"
,
# capital I, circumflex accent
1
N/A
"icirc"
=>
"\xEE"
,
# small i, circumflex accent
1
N/A
"Igrave"
=>
"\xCC"
,
# capital I, grave accent
1
N/A
"igrave"
=>
"\xEC"
,
# small i, grave accent
1
N/A
"Iuml"
=>
"\xCF"
,
# capital I, dieresis or umlaut mark
1
N/A
"iuml"
=>
"\xEF"
,
# small i, dieresis or umlaut mark
1
N/A
"Ntilde"
=>
"\xD1"
,
# capital N, tilde
1
N/A
"ntilde"
=>
"\xF1"
,
# small n, tilde
1
N/A
"Oacute"
=>
"\xD3"
,
# capital O, acute accent
1
N/A
"oacute"
=>
"\xF3"
,
# small o, acute accent
1
N/A
"Ocirc"
=>
"\xD4"
,
# capital O, circumflex accent
1
N/A
"ocirc"
=>
"\xF4"
,
# small o, circumflex accent
1
N/A
"Ograve"
=>
"\xD2"
,
# capital O, grave accent
1
N/A
"ograve"
=>
"\xF2"
,
# small o, grave accent
1
N/A
"Oslash"
=>
"\xD8"
,
# capital O, slash
1
N/A
"oslash"
=>
"\xF8"
,
# small o, slash
1
N/A
"Otilde"
=>
"\xD5"
,
# capital O, tilde
1
N/A
"otilde"
=>
"\xF5"
,
# small o, tilde
1
N/A
"Ouml"
=>
"\xD6"
,
# capital O, dieresis or umlaut mark
1
N/A
"ouml"
=>
"\xF6"
,
# small o, dieresis or umlaut mark
1
N/A
"szlig"
=>
"\xDF"
,
# small sharp s, German (sz ligature)
1
N/A
"THORN"
=>
"\xDE"
,
# capital THORN, Icelandic
1
N/A
"thorn"
=>
"\xFE"
,
# small thorn, Icelandic
1
N/A
"Uacute"
=>
"\xDA"
,
# capital U, acute accent
1
N/A
"uacute"
=>
"\xFA"
,
# small u, acute accent
1
N/A
"Ucirc"
=>
"\xDB"
,
# capital U, circumflex accent
1
N/A
"ucirc"
=>
"\xFB"
,
# small u, circumflex accent
1
N/A
"Ugrave"
=>
"\xD9"
,
# capital U, grave accent
1
N/A
"ugrave"
=>
"\xF9"
,
# small u, grave accent
1
N/A
"Uuml"
=>
"\xDC"
,
# capital U, dieresis or umlaut mark
1
N/A
"uuml"
=>
"\xFC"
,
# small u, dieresis or umlaut mark
1
N/A
"Yacute"
=>
"\xDD"
,
# capital Y, acute accent
1
N/A
"yacute"
=>
"\xFD"
,
# small y, acute accent
1
N/A
"yuml"
=>
"\xFF"
,
# small y, dieresis or umlaut mark
1
N/A
1
N/A
"laquo"
=>
"\xAB"
,
# left pointing double angle quotation mark
1
N/A
"lchevron"
=>
"\xAB"
,
# synonym (backwards compatibility)
1
N/A
"raquo"
=>
"\xBB"
,
# right pointing double angle quotation mark
1
N/A
"rchevron"
=>
"\xBB"
,
# synonym (backwards compatibility)
1
N/A
1
N/A
"iexcl"
=>
"\xA1"
,
# inverted exclamation mark
1
N/A
"cent"
=>
"\xA2"
,
# cent sign
1
N/A
"pound"
=>
"\xA3"
,
# (UK) pound sign
1
N/A
"curren"
=>
"\xA4"
,
# currency sign
1
N/A
"yen"
=>
"\xA5"
,
# yen sign
1
N/A
"brvbar"
=>
"\xA6"
,
# broken vertical bar
1
N/A
"sect"
=>
"\xA7"
,
# section sign
1
N/A
"uml"
=>
"\xA8"
,
# diaresis
1
N/A
"copy"
=>
"\xA9"
,
# Copyright symbol
1
N/A
"ordf"
=>
"\xAA"
,
# feminine ordinal indicator
1
N/A
"not"
=>
"\xAC"
,
# not sign
1
N/A
"shy"
=>
''
,
# soft (discretionary) hyphen
1
N/A
"reg"
=>
"\xAE"
,
# registered trademark
1
N/A
"macr"
=>
"\xAF"
,
# macron, overline
1
N/A
"deg"
=>
"\xB0"
,
# degree sign
1
N/A
"plusmn"
=>
"\xB1"
,
# plus-minus sign
1
N/A
"sup2"
=>
"\xB2"
,
# superscript 2
1
N/A
"sup3"
=>
"\xB3"
,
# superscript 3
1
N/A
"acute"
=>
"\xB4"
,
# acute accent
1
N/A
"micro"
=>
"\xB5"
,
# micro sign
1
N/A
"para"
=>
"\xB6"
,
# pilcrow sign = paragraph sign
1
N/A
"middot"
=>
"\xB7"
,
# middle dot = Georgian comma
1
N/A
"cedil"
=>
"\xB8"
,
# cedilla
1
N/A
"sup1"
=>
"\xB9"
,
# superscript 1
1
N/A
"ordm"
=>
"\xBA"
,
# masculine ordinal indicator
1
N/A
"frac14"
=>
"\xBC"
,
# vulgar fraction one quarter
1
N/A
"frac12"
=>
"\xBD"
,
# vulgar fraction one half
1
N/A
"frac34"
=>
"\xBE"
,
# vulgar fraction three quarters
1
N/A
"iquest"
=>
"\xBF"
,
# inverted question mark
1
N/A
"times"
=>
"\xD7"
,
# multiplication sign
1
N/A
"divide"
=>
"\xF7"
,
# division sign
1
N/A
1
N/A
"nbsp"
=>
"\x01"
,
# non-breaking space
1
N/A
);
1
N/A
1
N/A
1
N/A
##############################################################################
1
N/A
# Initialization
1
N/A
##############################################################################
1
N/A
1
N/A
# Initialize the object. Must be sure to call our parent initializer.
1
N/A
sub
initialize
{
1
N/A
my
$
self
=
shift
;
1
N/A
1
N/A
$$
self
{
alt
} =
0
unless
defined
$$
self
{
alt
};
1
N/A
$$
self
{
indent
} =
4
unless
defined
$$
self
{
indent
};
1
N/A
$$
self
{
margin
} =
0
unless
defined
$$
self
{
margin
};
1
N/A
$$
self
{
loose
} =
0
unless
defined
$$
self
{
loose
};
1
N/A
$$
self
{
sentence
} =
0
unless
defined
$$
self
{
sentence
};
1
N/A
$$
self
{
width
} =
76
unless
defined
$$
self
{
width
};
1
N/A
1
N/A
# Figure out what quotes we'll be using for C<> text.
1
N/A
$$
self
{
quotes
} ||=
'"'
;
1
N/A
if
($$
self
{
quotes
}
eq
'none'
) {
1
N/A
$$
self
{
LQUOTE
} = $$
self
{
RQUOTE
} =
''
;
1
N/A
}
elsif
(
length
($$
self
{
quotes
}) ==
1
) {
1
N/A
$$
self
{
LQUOTE
} = $$
self
{
RQUOTE
} = $$
self
{
quotes
};
1
N/A
}
elsif
($$
self
{
quotes
} =~ /^(.)(.)$/
1
N/A
|| $$
self
{
quotes
} =~ /^(..)(..)$/) {
1
N/A
$$
self
{
LQUOTE
} = $
1
;
1
N/A
$$
self
{
RQUOTE
} = $
2
;
1
N/A
}
else
{
1
N/A
croak
qq
(
Invalid
quote
specification
"$$self{quotes}"
);
1
N/A
}
1
N/A
1
N/A
# Stack of indentations.
1
N/A
$$
self
{
INDENTS
} = [];
1
N/A
1
N/A
# Current left margin.
1
N/A
$$
self
{
MARGIN
} = $$
self
{
indent
} + $$
self
{
margin
};
1
N/A
1
N/A
$
self
->
SUPER
::
initialize
;
1
N/A
1
N/A
# Tell Pod::Parser that we want the non-POD stuff too if code was set.
1
N/A
$
self
->
parseopts
(
'-want_nonPODs'
=>
1
)
if
$$
self
{
code
};
1
N/A
}
1
N/A
1
N/A
1
N/A
##############################################################################
1
N/A
# Core overrides
1
N/A
##############################################################################
1
N/A
1
N/A
# Called for each command paragraph. Gets the command, the associated
1
N/A
# paragraph, the line number, and a Pod::Paragraph object. Just dispatches
1
N/A
# the command to a method named the same as the command. =cut is handled
1
N/A
# internally by Pod::Parser.
1
N/A
sub
command
{
1
N/A
my
$
self
=
shift
;
1
N/A
my
$
command
=
shift
;
1
N/A
return
if
$
command
eq
'pod'
;
1
N/A
return
if
($$
self
{
EXCLUDE
} && $
command
ne
'end'
);
1
N/A
if
($
self
->
can
(
'cmd_'
. $
command
)) {
1
N/A
$
command
=
'cmd_'
. $
command
;
1
N/A
$
self
->$
command
(@_);
1
N/A
}
else
{
1
N/A
my
($
text
, $
line
, $
paragraph
) = @_;
1
N/A
my
$
file
;
1
N/A
($
file
, $
line
) = $
paragraph
->
file_line
;
1
N/A
$
text
=~ s/\n+\z//;
1
N/A
$
text
=
" $text"
if
($
text
=~ /^\S/);
1
N/A
warn
qq
($
file
:$
line
:
Unknown
command
paragraph
: =$
command
$
text
\n);
1
N/A
return
;
1
N/A
}
1
N/A
}
1
N/A
1
N/A
# Called for a verbatim paragraph. Gets the paragraph, the line number, and a
1
N/A
# Pod::Paragraph object. Just output it verbatim, but with tabs converted to
1
N/A
# spaces.
1
N/A
sub
verbatim
{
1
N/A
my
$
self
=
shift
;
1
N/A
return
if
$$
self
{
EXCLUDE
};
1
N/A
$
self
->
item
if
defined
$$
self
{
ITEM
};
1
N/A
local
$_ =
shift
;
1
N/A
return
if
/^\s*$/;
1
N/A
s/^(\s*\S+)/(
' '
x $$
self
{
MARGIN
}) . $
1
/
gme
;
1
N/A
$
self
->
output
($_);
1
N/A
}
1
N/A
1
N/A
# Called for a regular text block. Gets the paragraph, the line number, and a
1
N/A
# Pod::Paragraph object. Perform interpolation and output the results.
1
N/A
sub
textblock
{
1
N/A
my
$
self
=
shift
;
1
N/A
return
if
$$
self
{
EXCLUDE
};
1
N/A
$
self
->
output
($_[
0
]),
return
if
$$
self
{
VERBATIM
};
1
N/A
local
$_ =
shift
;
1
N/A
my
$
line
=
shift
;
1
N/A
1
N/A
# Interpolate and output the paragraph.
1
N/A
$_ = $
self
->
interpolate
($_, $
line
);
1
N/A
s/\s+$/\n/;
1
N/A
if
(
defined
$$
self
{
ITEM
}) {
1
N/A
$
self
->
item
($_ .
"\n"
);
1
N/A
}
else
{
1
N/A
$
self
->
output
($
self
->
reformat
($_ .
"\n"
));
1
N/A
}
1
N/A
}
1
N/A
1
N/A
# Called for a formatting code. Gets the command, argument, and a
1
N/A
# Pod::InteriorSequence object and is expected to return the resulting text.
1
N/A
# Calls methods for code, bold, italic, file, and link to handle those types
1
N/A
# of codes, and handles S<>, E<>, X<>, and Z<> directly.
1
N/A
sub
interior_sequence
{
1
N/A
local
$_;
1
N/A
my
($
self
, $
command
, $
seq
);
1
N/A
($
self
, $
command
, $_, $
seq
) = @_;
1
N/A
1
N/A
# We have to defer processing of the inside of an L<> formatting code. If
1
N/A
# this code is nested inside an L<> code, return the literal raw text of
1
N/A
# it.
1
N/A
my
$
parent
= $
seq
->
nested
;
1
N/A
while
(
defined
$
parent
) {
1
N/A
return
$
seq
->
raw_text
if
($
parent
->
cmd_name
eq
'L'
);
1
N/A
$
parent
= $
parent
->
nested
;
1
N/A
}
1
N/A
1
N/A
# Index entries are ignored in plain text.
1
N/A
return
''
if
($
command
eq
'X'
|| $
command
eq
'Z'
);
1
N/A
1
N/A
# Expand escapes into the actual character now, warning if invalid.
1
N/A
if
($
command
eq
'E'
) {
1
N/A
if
(/^\d+$/) {
1
N/A
return
chr
;
1
N/A
}
else
{
1
N/A
return
$
ESCAPES
{$_}
if
defined
$
ESCAPES
{$_};
1
N/A
my
($
file
, $
line
) = $
seq
->
file_line
;
1
N/A
warn
"$file:$line: Unknown escape: E<$_>\n"
;
1
N/A
return
"E<$_>"
;
1
N/A
}
1
N/A
}
1
N/A
1
N/A
# For all the other formatting codes, empty content produces no output.
1
N/A
return
if
$_
eq
''
;
1
N/A
1
N/A
# For S<>, compress all internal whitespace and then map spaces to \01.
1
N/A
# When we output the text, we'll map this back.
1
N/A
if
($
command
eq
'S'
) {
1
N/A
s/\s+/ /g;
1
N/A
tr
/ /\
01
/;
1
N/A
return
$_;
1
N/A
}
1
N/A
1
N/A
# Anything else needs to get dispatched to another method.
1
N/A
if
($
command
eq
'B'
) {
return
$
self
->
seq_b
($_) }
1
N/A
elsif
($
command
eq
'C'
) {
return
$
self
->
seq_c
($_) }
1
N/A
elsif
($
command
eq
'F'
) {
return
$
self
->
seq_f
($_) }
1
N/A
elsif
($
command
eq
'I'
) {
return
$
self
->
seq_i
($_) }
1
N/A
elsif
($
command
eq
'L'
) {
return
$
self
->
seq_l
($_, $
seq
) }
1
N/A
else
{
1
N/A
my
($
file
, $
line
) = $
seq
->
file_line
;
1
N/A
warn
"$file:$line: Unknown formatting code: $command<$_>\n"
;
1
N/A
}
1
N/A
}
1
N/A
1
N/A
# Called for each paragraph that's actually part of the POD. We take
1
N/A
# advantage of this opportunity to untabify the input. Also, if given the
1
N/A
# code option, we may see paragraphs that aren't part of the POD and need to
1
N/A
# output them directly.
1
N/A
sub
preprocess_paragraph
{
1
N/A
my
$
self
=
shift
;
1
N/A
local
$_ =
shift
;
1
N/A
1
while
s/^(.*?)(\t+)/$
1
.
' '
x (
length
($
2
) *
8
-
length
($
1
) %
8
)/
me
;
1
N/A
$
self
->
output_code
($_)
if
$
self
->
cutting
;
1
N/A
$_;
1
N/A
}
1
N/A
1
N/A
1
N/A
##############################################################################
1
N/A
# Command paragraphs
1
N/A
##############################################################################
1
N/A
1
N/A
# All command paragraphs take the paragraph and the line number.
1
N/A
1
N/A
# First level heading.
1
N/A
sub
cmd_head1
{
1
N/A
my
($
self
, $
text
, $
line
) = @_;
1
N/A
$
self
->
heading
($
text
, $
line
,
0
,
'===='
);
1
N/A
}
1
N/A
1
N/A
# Second level heading.
1
N/A
sub
cmd_head2
{
1
N/A
my
($
self
, $
text
, $
line
) = @_;
1
N/A
$
self
->
heading
($
text
, $
line
, $$
self
{
indent
} /
2
,
'== '
);
1
N/A
}
1
N/A
1
N/A
# Third level heading.
1
N/A
sub
cmd_head3
{
1
N/A
my
($
self
, $
text
, $
line
) = @_;
1
N/A
$
self
->
heading
($
text
, $
line
, $$
self
{
indent
} *
2
/
3
+
0.5
,
'= '
);
1
N/A
}
1
N/A
1
N/A
# Third level heading.
1
N/A
sub
cmd_head4
{
1
N/A
my
($
self
, $
text
, $
line
) = @_;
1
N/A
$
self
->
heading
($
text
, $
line
, $$
self
{
indent
} *
3
/
4
+
0.5
,
'- '
);
1
N/A
}
1
N/A
1
N/A
# Start a list.
1
N/A
sub
cmd_over
{
1
N/A
my
$
self
=
shift
;
1
N/A
local
$_ =
shift
;
1
N/A
$
self
->
item
(
"\n\n"
)
if
defined
$$
self
{
ITEM
};
1
N/A
unless
(/^[-+]?\d+\s+$/) { $_ = $$
self
{
indent
} }
1
N/A
push
(@{ $$
self
{
INDENTS
} }, $$
self
{
MARGIN
});
1
N/A
$$
self
{
MARGIN
} += ($_ +
0
);
1
N/A
}
1
N/A
1
N/A
# End a list.
1
N/A
sub
cmd_back
{
1
N/A
my
($
self
, $
text
, $
line
, $
paragraph
) = @_;
1
N/A
$
self
->
item
(
"\n\n"
)
if
defined
$$
self
{
ITEM
};
1
N/A
$$
self
{
MARGIN
} =
pop
@{ $$
self
{
INDENTS
} };
1
N/A
unless
(
defined
$$
self
{
MARGIN
}) {
1
N/A
my
$
file
;
1
N/A
($
file
, $
line
) = $
paragraph
->
file_line
;
1
N/A
warn
"$file:$line: Unmatched =back\n"
;
1
N/A
$$
self
{
MARGIN
} = $$
self
{
indent
};
1
N/A
}
1
N/A
}
1
N/A
1
N/A
# An individual list item.
1
N/A
sub
cmd_item
{
1
N/A
my
$
self
=
shift
;
1
N/A
if
(
defined
$$
self
{
ITEM
}) { $
self
->
item
}
1
N/A
local
$_ =
shift
;
1
N/A
s/\s+$//;
1
N/A
$$
self
{
ITEM
} = $_ ? $
self
->
interpolate
($_) :
'*'
;
1
N/A
}
1
N/A
1
N/A
# Begin a block for a particular translator. Setting VERBATIM triggers
1
N/A
# special handling in textblock().
1
N/A
sub
cmd_begin
{
1
N/A
my
$
self
=
shift
;
1
N/A
local
$_ =
shift
;
1
N/A
my
($
kind
) = /^(\S+)/
or
return
;
1
N/A
if
($
kind
eq
'text'
) {
1
N/A
$$
self
{
VERBATIM
} =
1
;
1
N/A
}
else
{
1
N/A
$$
self
{
EXCLUDE
} =
1
;
1
N/A
}
1
N/A
}
1
N/A
1
N/A
# End a block for a particular translator. We assume that all =begin/=end
1
N/A
# pairs are properly closed.
1
N/A
sub
cmd_end
{
1
N/A
my
$
self
=
shift
;
1
N/A
$$
self
{
EXCLUDE
} =
0
;
1
N/A
$$
self
{
VERBATIM
} =
0
;
1
N/A
}
1
N/A
1
N/A
# One paragraph for a particular translator. Ignore it unless it's intended
1
N/A
# for text, in which case we treat it as a verbatim text block.
1
N/A
sub
cmd_for
{
1
N/A
my
$
self
=
shift
;
1
N/A
local
$_ =
shift
;
1
N/A
my
$
line
=
shift
;
1
N/A
return
unless
s/^
text
\b[ \t]*\n?//;
1
N/A
$
self
->
verbatim
($_, $
line
);
1
N/A
}
1
N/A
1
N/A
1
N/A
##############################################################################
1
N/A
# Formatting codes
1
N/A
##############################################################################
1
N/A
1
N/A
# The simple ones. These are here mostly so that subclasses can override them
1
N/A
# and do more complicated things.
1
N/A
sub
seq_b
{
return
$_[
0
]{
alt
} ?
"``$_[1]''"
: $_[
1
] }
1
N/A
sub
seq_f
{
return
$_[
0
]{
alt
} ?
"\"$_[1]\""
: $_[
1
] }
1
N/A
sub
seq_i
{
return
'*'
. $_[
1
] .
'*'
}
1
N/A
1
N/A
# Apply a whole bunch of messy heuristics to not quote things that don't
1
N/A
# benefit from being quoted. These originally come from Barrie Slaymaker and
1
N/A
# largely duplicate code in Pod::Man.
1
N/A
sub
seq_c
{
1
N/A
my
$
self
=
shift
;
1
N/A
local
$_ =
shift
;
1
N/A
1
N/A
# A regex that matches the portion of a variable reference that's the
1
N/A
# array or hash index, separated out just because we want to use it in
1
N/A
# several places in the following regex.
1
N/A
my
$
index
=
'(?: \[.*\] | \{.*\} )?'
;
1
N/A
1
N/A
# Check for things that we don't want to quote, and if we find any of
1
N/A
# them, return the string with just a font change and no quoting.
1
N/A
m{
1
N/A
^\s*
1
N/A
(?:
1
N/A
( [\
'\`\"] ) .* \1 # already quoted
1
N/A
| \` .* \
' # `quoted'
1
N/A
| \$+ [\
#^]? \S $index # special ($^Foo, $")
1
N/A
| [\$\@%&*]+ \
#? [:\'\w]+ $index # plain var or func
1
N/A
| [\$\@%&*]* [:\
'\w]+ (?: -> )? \(\s*[^\s,]\s*\) # 0/1-arg func call
1
N/A
| [+-]? ( \d[\d.]* | \.\d+ ) (?: [
eE
][+-]?\d+ )?
# a number
1
N/A
|
0
x [a-
fA
-F\d]+
# a hex constant
1
N/A
)
1
N/A
\s*\z
1
N/A
}
xo
&&
return
$_;
1
N/A
1
N/A
# If we didn't return, go ahead and quote the text.
1
N/A
return
$$
self
{
alt
} ?
"``$_''"
:
"$$self{LQUOTE}$_$$self{RQUOTE}"
;
1
N/A
}
1
N/A
1
N/A
# Handle links. Since this is plain text, we can't actually make any real
1
N/A
# links, so this is all to figure out what text we print out. Most of the
1
N/A
# work is done by Pod::ParseLink.
1
N/A
sub
seq_l
{
1
N/A
my
($
self
, $
link
, $
seq
) = @_;
1
N/A
my
($
text
, $
type
) = (
parselink
($
link
))[
1
,
4
];
1
N/A
my
($
file
, $
line
) = $
seq
->
file_line
;
1
N/A
$
text
= $
self
->
interpolate
($
text
, $
line
);
1
N/A
$
text
=
'<'
. $
text
.
'>'
if
$
type
eq
'url'
;
1
N/A
return
$
text
||
''
;
1
N/A
}
1
N/A
1
N/A
1
N/A
##############################################################################
1
N/A
# Header handling
1
N/A
##############################################################################
1
N/A
1
N/A
# The common code for handling all headers. Takes the interpolated header
1
N/A
# text, the line number, the indentation, and the surrounding marker for the
1
N/A
# alt formatting method.
1
N/A
sub
heading
{
1
N/A
my
($
self
, $
text
, $
line
, $
indent
, $
marker
) = @_;
1
N/A
$
self
->
item
(
"\n\n"
)
if
defined
$$
self
{
ITEM
};
1
N/A
$
text
=~ s/\s+$//;
1
N/A
$
text
= $
self
->
interpolate
($
text
, $
line
);
1
N/A
if
($$
self
{
alt
}) {
1
N/A
my
$
closemark
=
reverse
(
split
(//, $
marker
));
1
N/A
my
$
margin
=
' '
x $$
self
{
margin
};
1
N/A
$
self
->
output
(
"\n"
.
"$margin$marker $text $closemark"
.
"\n\n"
);
1
N/A
}
else
{
1
N/A
$
text
.=
"\n"
if
$$
self
{
loose
};
1
N/A
my
$
margin
=
' '
x ($$
self
{
margin
} + $
indent
);
1
N/A
$
self
->
output
($
margin
. $
text
.
"\n"
);
1
N/A
}
1
N/A
}
1
N/A
1
N/A
1
N/A
##############################################################################
1
N/A
# List handling
1
N/A
##############################################################################
1
N/A
1
N/A
# This method is called whenever an =item command is complete (in other words,
1
N/A
# we've seen its associated paragraph or know for certain that it doesn't have
1
N/A
# one). It gets the paragraph associated with the item as an argument. If
1
N/A
# that argument is empty, just output the item tag; if it contains a newline,
1
N/A
# output the item tag followed by the newline. Otherwise, see if there's
1
N/A
# enough room for us to output the item tag in the margin of the text or if we
1
N/A
# have to put it on a separate line.
1
N/A
sub
item
{
1
N/A
my
$
self
=
shift
;
1
N/A
local
$_ =
shift
;
1
N/A
my
$
tag
= $$
self
{
ITEM
};
1
N/A
unless
(
defined
$
tag
) {
1
N/A
carp
"Item called without tag"
;
1
N/A
return
;
1
N/A
}
1
N/A
undef
$$
self
{
ITEM
};
1
N/A
my
$
indent
= $$
self
{
INDENTS
}[-
1
];
1
N/A
unless
(
defined
$
indent
) { $
indent
= $$
self
{
indent
} }
1
N/A
my
$
margin
=
' '
x $$
self
{
margin
};
1
N/A
if
(!$_ || /^\s+$/ || ($$
self
{
MARGIN
} - $
indent
<
length
($
tag
) +
1
)) {
1
N/A
my
$
realindent
= $$
self
{
MARGIN
};
1
N/A
$$
self
{
MARGIN
} = $
indent
;
1
N/A
my
$
output
= $
self
->
reformat
($
tag
);
1
N/A
$
output
=~ s/^$
margin
/$
margin
:/
if
($$
self
{
alt
} && $
indent
>
0
);
1
N/A
$
output
=~ s/\n*$/\n/;
1
N/A
1
N/A
# If the text is just whitespace, we have an empty item paragraph;
1
N/A
# this can result from =over/=item/=back without any intermixed
1
N/A
# paragraphs. Insert some whitespace to keep the =item from merging
1
N/A
# into the next paragraph.
1
N/A
$
output
.=
"\n"
if
$_ && $_ =~ /^\s*$/;
1
N/A
1
N/A
$
self
->
output
($
output
);
1
N/A
$$
self
{
MARGIN
} = $
realindent
;
1
N/A
$
self
->
output
($
self
->
reformat
($_))
if
$_ && /\S/;
1
N/A
}
else
{
1
N/A
my
$
space
=
' '
x $
indent
;
1
N/A
$
space
=~ s/^$
margin
/$
margin
:/
if
$$
self
{
alt
};
1
N/A
$_ = $
self
->
reformat
($_);
1
N/A
s/^$
margin
/$
margin
:/
if
($$
self
{
alt
} && $
indent
>
0
);
1
N/A
my
$
tagspace
=
' '
x
length
$
tag
;
1
N/A
s/^($
space
)$
tagspace
/$
1
$
tag
/
or
warn
"Bizarre space in item"
;
1
N/A
$
self
->
output
($_);
1
N/A
}
1
N/A
}
1
N/A
1
N/A
1
N/A
##############################################################################
1
N/A
# Output formatting
1
N/A
##############################################################################
1
N/A
1
N/A
# Wrap a line, indenting by the current left margin. We can't use Text::Wrap
1
N/A
# because it plays games with tabs. We can't use formline, even though we'd
1
N/A
# really like to, because it screws up non-printing characters. So we have to
1
N/A
# do the wrapping ourselves.
1
N/A
sub
wrap
{
1
N/A
my
$
self
=
shift
;
1
N/A
local
$_ =
shift
;
1
N/A
my
$
output
=
''
;
1
N/A
my
$
spaces
=
' '
x $$
self
{
MARGIN
};
1
N/A
my
$
width
= $$
self
{
width
} - $$
self
{
MARGIN
};
1
N/A
while
(
length
> $
width
) {
1
N/A
if
(s/^([^\n]{
0
,$
width
})\s+// || s/^([^\n]{$
width
})//) {
1
N/A
$
output
.= $
spaces
. $
1
.
"\n"
;
1
N/A
}
else
{
1
N/A
last
;
1
N/A
}
1
N/A
}
1
N/A
$
output
.= $
spaces
. $_;
1
N/A
$
output
=~ s/\s+$/\n\n/;
1
N/A
$
output
;
1
N/A
}
1
N/A
1
N/A
# Reformat a paragraph of text for the current margin. Takes the text to
1
N/A
# reformat and returns the formatted text.
1
N/A
sub
reformat
{
1
N/A
my
$
self
=
shift
;
1
N/A
local
$_ =
shift
;
1
N/A
1
N/A
# If we're trying to preserve two spaces after sentences, do some munging
1
N/A
# to support that. Otherwise, smash all repeated whitespace.
1
N/A
if
($$
self
{
sentence
}) {
1
N/A
s/ +$//
mg
;
1
N/A
s/\.\n/. \n/g;
1
N/A
s/\n/ /g;
1
N/A
s/ +/ /g;
1
N/A
}
else
{
1
N/A
s/\s+/ /g;
1
N/A
}
1
N/A
$
self
->
wrap
($_);
1
N/A
}
1
N/A
1
N/A
# Output text to the output device.
1
N/A
sub
output
{ $_[
1
] =~
tr
/\
01
/ /;
print
{ $_[
0
]->
output_handle
} $_[
1
] }
1
N/A
1
N/A
# Output a block of code (something that isn't part of the POD text). Called
1
N/A
# by preprocess_paragraph only if we were given the code option. Exists here
1
N/A
# only so that it can be overridden by subclasses.
1
N/A
sub
output_code
{ $_[
0
]->
output
($_[
1
]) }
1
N/A
1
N/A
1
N/A
##############################################################################
1
N/A
# Backwards compatibility
1
N/A
##############################################################################
1
N/A
1
N/A
# The old Pod::Text module did everything in a pod2text() function. This
1
N/A
# tries to provide the same interface for legacy applications.
1
N/A
sub
pod2text
{
1
N/A
my
@
args
;
1
N/A
1
N/A
# This is really ugly; I hate doing option parsing in the middle of a
1
N/A
# module. But the old Pod::Text module supported passing flags to its
1
N/A
# entry function, so handle -a and -<number>.
1
N/A
while
($_[
0
] =~ /^-/) {
1
N/A
my
$
flag
=
shift
;
1
N/A
if
($
flag
eq
'-a'
) {
push
(@
args
,
alt
=>
1
) }
1
N/A
elsif
($
flag
=~ /^-(\d+)$/) {
push
(@
args
,
width
=> $
1
) }
1
N/A
else
{
1
N/A
unshift
(@_, $
flag
);
1
N/A
last
;
1
N/A
}
1
N/A
}
1
N/A
1
N/A
# Now that we know what arguments we're using, create the parser.
1
N/A
my
$
parser
=
Pod
::
Text
->
new
(@
args
);
1
N/A
1
N/A
# If two arguments were given, the second argument is going to be a file
1
N/A
# handle. That means we want to call parse_from_filehandle(), which means
1
N/A
# we need to turn the first argument into a file handle. Magic open will
1
N/A
# handle the <&STDIN case automagically.
1
N/A
if
(
defined
$_[
1
]) {
1
N/A
my
@
fhs
= @_;
1
N/A
local
*
IN
;
1
N/A
unless
(
open
(
IN
, $
fhs
[
0
])) {
1
N/A
croak
(
"Can't open $fhs[0] for reading: $!\n"
);
1
N/A
return
;
1
N/A
}
1
N/A
$
fhs
[
0
] = \*
IN
;
1
N/A
return
$
parser
->
parse_from_filehandle
(@
fhs
);
1
N/A
}
else
{
1
N/A
return
$
parser
->
parse_from_file
(@_);
1
N/A
}
1
N/A
}
1
N/A
1
N/A
1
N/A
##############################################################################
1
N/A
# Module return value and documentation
1
N/A
##############################################################################
1
N/A
1
N/A
1
;
1
N/A
__END__
1
N/A
1
N/A
=head1 NAME
1
N/A
1
N/A
Pod::Text - Convert POD data to formatted ASCII text
1
N/A
1
N/A
=head1 SYNOPSIS
1
N/A
1
N/A
use Pod::Text;
1
N/A
my $parser = Pod::Text->new (sentence => 0, width => 78);
1
N/A
1
N/A
# Read POD from STDIN and write to STDOUT.
1
N/A
$parser->parse_from_filehandle;
1
N/A
1
N/A
# Read POD from file.pod and write to
file.txt
.
1
N/A
$parser->parse_from_file ('file.pod', '
file.txt
');
1
N/A
1
N/A
=head1 DESCRIPTION
1
N/A
1
N/A
Pod::Text is a module that can convert documentation in the POD format (the
1
N/A
preferred language for documenting Perl) into formatted ASCII. It uses no
1
N/A
special formatting controls or codes whatsoever, and its output is therefore
1
N/A
suitable for nearly any device.
1
N/A
1
N/A
As a derived class from Pod::Parser, Pod::Text supports the same methods and
1
N/A
interfaces. See L<Pod::Parser> for all the details; briefly, one creates a
1
N/A
new parser with C<< Pod::Text->new() >> and then calls either
1
N/A
parse_from_filehandle() or parse_from_file().
1
N/A
1
N/A
new() can take options, in the form of
key
/
value
pairs, that control the
1
N/A
behavior of the parser. The currently recognized options are:
1
N/A
1
N/A
=over 4
1
N/A
1
N/A
=item alt
1
N/A
1
N/A
If set to a true value, selects an alternate output format that, among other
1
N/A
things, uses a different heading style and marks C<=item> entries with a
1
N/A
colon in the left margin. Defaults to false.
1
N/A
1
N/A
=item code
1
N/A
1
N/A
If set to a true value, the non-POD parts of the input file will be included
1
N/A
in the output. Useful for viewing code documented with POD blocks with the
1
N/A
POD rendered and the code left intact.
1
N/A
1
N/A
=item indent
1
N/A
1
N/A
The number of spaces to indent regular text, and the default indentation for
1
N/A
C<=over> blocks. Defaults to 4.
1
N/A
1
N/A
=item loose
1
N/A
1
N/A
If set to a true value, a blank line is printed after a C<=head1> heading.
1
N/A
If set to false (the default), no blank line is printed after C<=head1>,
1
N/A
although one is still printed after C<=head2>. This is the default because
1
N/A
it's the expected formatting for manual pages; if you're formatting
1
N/A
arbitrary text documents, setting this to true may result in more pleasing
1
N/A
output.
1
N/A
1
N/A
=item margin
1
N/A
1
N/A
The width of the left margin in spaces. Defaults to 0. This is the margin
1
N/A
for all text, including headings, not the amount by which regular text is
1
N/A
indented; for the latter, see the I<indent> option. To set the right
1
N/A
margin, see the I<width> option.
1
N/A
1
N/A
=item quotes
1
N/A
1
N/A
Sets the quote marks used to surround CE<lt>> text. If the value is a
1
N/A
single character, it is used as both the left and right quote; if it is two
1
N/A
characters, the first character is used as the left quote and the second as
1
N/A
the right quoted; and if it is four characters, the first two are used as
1
N/A
the left quote and the second two as the right quote.
1
N/A
1
N/A
This may also be set to the special value C<none>, in which case no quote
1
N/A
marks are added around CE<lt>> text.
1
N/A
1
N/A
=item sentence
1
N/A
1
N/A
If set to a true value, Pod::Text will assume that each sentence ends in two
1
N/A
spaces, and will try to preserve that spacing. If set to false, all
1
N/A
consecutive whitespace in non-verbatim paragraphs is compressed into a
1
N/A
single space. Defaults to true.
1
N/A
1
N/A
=item width
1
N/A
1
N/A
The column at which to wrap text on the right-hand side. Defaults to 76.
1
N/A
1
N/A
=back
1
N/A
1
N/A
The standard Pod::Parser method parse_from_filehandle() takes up to two
1
N/A
arguments, the first being the file handle to read POD from and the second
1
N/A
being the file handle to write the formatted output to. The first defaults
1
N/A
to STDIN if not given, and the second defaults to STDOUT. The method
1
N/A
parse_from_file() is almost identical, except that its two arguments are the
1
N/A
input and output disk files instead. See L<Pod::Parser> for the specific
1
N/A
details.
1
N/A
1
N/A
=head1 DIAGNOSTICS
1
N/A
1
N/A
=over 4
1
N/A
1
N/A
=item Bizarre space in item
1
N/A
1
N/A
=item Item called without tag
1
N/A
1
N/A
(W) Something has gone wrong in internal C<=item> processing. These
1
N/A
messages indicate a bug in Pod::Text; you should never see them.
1
N/A
1
N/A
=item Can't open %s for reading: %s
1
N/A
1
N/A
(F) Pod::Text was invoked via the compatibility mode pod2text() interface
1
N/A
and the input file it was given could not be opened.
1
N/A
1
N/A
=item Invalid quote specification "%s"
1
N/A
1
N/A
(F) The quote specification given (the quotes option to the constructor) was
1
N/A
invalid. A quote specification must be one, two, or four characters long.
1
N/A
1
N/A
=item %s:%d: Unknown command paragraph: %s
1
N/A
1
N/A
(W) The POD source contained a non-standard command paragraph (something of
1
N/A
the form C<=command args>) that Pod::Man didn't know about. It was ignored.
1
N/A
1
N/A
=item %s:%d: Unknown escape: %s
1
N/A
1
N/A
(W) The POD source contained an C<EE<lt>E<gt>> escape that Pod::Text didn't
1
N/A
know about.
1
N/A
1
N/A
=item %s:%d: Unknown formatting code: %s
1
N/A
1
N/A
(W) The POD source contained a non-standard formatting code (something of
1
N/A
the form C<XE<lt>E<gt>>) that Pod::Text didn't know about.
1
N/A
1
N/A
=item %s:%d: Unmatched =back
1
N/A
1
N/A
(W) Pod::Text encountered a C<=back> command that didn't correspond to an
1
N/A
C<=over> command.
1
N/A
1
N/A
=back
1
N/A
1
N/A
=head1 RESTRICTIONS
1
N/A
1
N/A
Embedded Ctrl-As (octal 001) in the input will be mapped to spaces on
1
N/A
output, due to an internal implementation detail.
1
N/A
1
N/A
=head1 NOTES
1
N/A
1
N/A
This is a replacement for an earlier Pod::Text module written by Tom
1
N/A
Christiansen. It has a revamped interface, since it now uses Pod::Parser,
1
N/A
but an interface roughly compatible with the old Pod::Text::pod2text()
1
N/A
function is still available. Please change to the new calling convention,
1
N/A
though.
1
N/A
1
N/A
The original Pod::Text contained code to do formatting via termcap
1
N/A
sequences, although it wasn't turned on by default and it was problematic to
1
N/A
get it to work at all. This rewrite doesn't even try to do that, but a
1
N/A
subclass of it does. Look for L<Pod::Text::Termcap>.
1
N/A
1
N/A
=head1 SEE ALSO
1
N/A
1
N/A
L<Pod::Parser>, L<Pod::Text::Termcap>, L<pod2text(1)>
1
N/A
1
N/A
The current version of this module is always available from its web site at
1
N/A
L<
http://www.eyrie.org/~eagle/software/podlators/
>. It is also part of the
1
N/A
Perl core distribution as of 5.6.0.
1
N/A
1
N/A
=head1 AUTHOR
1
N/A
1
N/A
Russ Allbery <rra@stanford.edu>, based I<very> heavily on the original
1
N/A
Pod::Text by Tom Christiansen <tchrist@mox.perl.com> and its conversion to
1
N/A
Pod::Parser by Brad Appleton <bradapp@enteract.com>.
1
N/A
1
N/A
=head1 COPYRIGHT AND LICENSE
1
N/A
1
N/A
Copyright 1999, 2000, 2001, 2002 by Russ Allbery <rra@stanford.edu>.
1
N/A
1
N/A
This program is free software; you may redistribute it
and
/
or
modify it
1
N/A
under the same terms as Perl itself.
1
N/A
1
N/A
=cut