Cross Reference: PlainText.pm
xref
: /
osnet-11
/
usr
/
src
/
cmd
/
perl
/
5.8.4
/
distrib
/
lib
/
Pod
/
PlainText.pm
Home
History
Annotate
Line#
Navigate
Download
Search
only in
./
1
N/A
# Pod::PlainText -- Convert POD data to formatted ASCII text.
1
N/A
# $Id:
Text.pm
,v 2.1 1999/09/20 11:53:33 eagle Exp $
1
N/A
#
1
N/A
# Copyright 1999-2000 by Russ Allbery <rra@stanford.edu>
1
N/A
#
1
N/A
# This program is free software; you can redistribute it
and
/
or
modify it
1
N/A
# under the same terms as Perl itself.
1
N/A
#
1
N/A
# This module is intended to be a replacement for Pod::Text, and attempts to
1
N/A
# match its output except for some specific circumstances where other
1
N/A
# decisions seemed to produce better output. It uses Pod::Parser and is
1
N/A
# designed to be very easy to subclass.
1
N/A
1
N/A
############################################################################
1
N/A
# Modules and declarations
1
N/A
############################################################################
1
N/A
1
N/A
package
Pod
::
PlainText
;
1
N/A
1
N/A
require
5.005
;
1
N/A
1
N/A
use
Carp
qw
(
carp
croak
);
1
N/A
use
Pod
::
Select
();
1
N/A
1
N/A
use
strict
;
1
N/A
use
vars
qw
(@
ISA
%
ESCAPES
$
VERSION
);
1
N/A
1
N/A
# We inherit from Pod::Select instead of Pod::Parser so that we can be used
1
N/A
# by Pod::Usage.
1
N/A
@
ISA
=
qw
(
Pod
::
Select
);
1
N/A
1
N/A
$
VERSION
=
'2.02'
;
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,
1
N/A
# which got it near verbatim from the original Pod::Text. It is therefore
1
N/A
# credited to Tom Christiansen, and I'm glad I didn't have to write it. :)
1
N/A
%
ESCAPES
= (
1
N/A
'amp'
=>
'&'
,
# ampersand
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
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"
=>
"\xCD"
,
# capital I, grave accent
1
N/A
"igrave"
=>
"\xED"
,
# 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
"lchevron"
=>
"\xAB"
,
# left chevron (double less than)
1
N/A
"rchevron"
=>
"\xBB"
,
# right chevron (double greater than)
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
{
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
$$
self
{
INDENTS
} = [];
# Stack of indentations.
1
N/A
$$
self
{
MARGIN
} = $$
self
{
indent
};
# Current left margin in spaces.
1
N/A
1
N/A
$
self
->
SUPER
::
initialize
;
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
$
self
->
item
(
"\n"
)
if
defined
$$
self
{
ITEM
};
1
N/A
$
command
=
'cmd_'
. $
command
;
1
N/A
$
self
->$
command
(@_);
1
N/A
}
1
N/A
1
N/A
# Called for a verbatim paragraph. Gets the paragraph, the line number, and
1
N/A
# a Pod::Paragraph object. Just output it verbatim, but with tabs converted
1
N/A
# to 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
1
N/A
# 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
# Perform a little magic to collapse multiple L<> references. This is
1
N/A
# here mostly for backwards-compatibility. We'll just rewrite the whole
1
N/A
# thing into actual text at this part, bypassing the whole internal
1
N/A
# sequence parsing thing.
1
N/A
s{
1
N/A
(
1
N/A
L<
# A link of the form L</something>.
1
N/A
/
1
N/A
(
1
N/A
[:\w]+
# The item has to be a simple word...
1
N/A
(\(\))?
# ...or simple function.
1
N/A
)
1
N/A
>
1
N/A
(
1
N/A
,?\s+(
and
\s+)?
# Allow lots of them, conjuncted.
1
N/A
L<
1
N/A
/
1
N/A
(
1
N/A
[:\w]+
1
N/A
(\(\))?
1
N/A
)
1
N/A
>
1
N/A
)+
1
N/A
)
1
N/A
} {
1
N/A
local
$_ = $
1
;
1
N/A
s%L</([^>]+)>%$
1
%g;
1
N/A
my
@
items
=
split
/(?:,?\s+(?:
and
\s+)?)/;
1
N/A
my
$
string
=
"the "
;
1
N/A
my
$i;
1
N/A
for
($i =
0
; $i < @
items
; $i++) {
1
N/A
$
string
.= $
items
[$i];
1
N/A
$
string
.=
", "
if
@
items
>
2
&& $i != $
#items;
1
N/A
$
string
.=
" and "
if
($i == $
#items - 1);
1
N/A
}
1
N/A
$
string
.=
" entries elsewhere in this document"
;
1
N/A
$
string
;
1
N/A
}
gex
;
1
N/A
1
N/A
# Now actually 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 an interior sequence. Gets the command, argument, and a
1
N/A
# Pod::InteriorSequence object and is expected to return the resulting text.
1
N/A
# Calls code, bold, italic, file, and link to handle those types of
1
N/A
# sequences, and handles S<>, E<>, X<>, and Z<> directly.
1
N/A
sub
interior_sequence
{
1
N/A
my
$
self
=
shift
;
1
N/A
my
$
command
=
shift
;
1
N/A
local
$_ =
shift
;
1
N/A
return
''
if
($
command
eq
'X'
|| $
command
eq
'Z'
);
1
N/A
1
N/A
# Expand escapes into the actual character now, carping if invalid.
1
N/A
if
($
command
eq
'E'
) {
1
N/A
return
$
ESCAPES
{$_}
if
defined
$
ESCAPES
{$_};
1
N/A
carp
"Unknown escape: E<$_>"
;
1
N/A
return
"E<$_>"
;
1
N/A
}
1
N/A
1
N/A
# For all the other sequences, 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{
2
,}/ /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
($_) }
1
N/A
else
{
carp
"Unknown sequence $command<$_>"
}
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.
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
$_;
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
=
shift
;
1
N/A
local
$_ =
shift
;
1
N/A
s/\s+$//;
1
N/A
$_ = $
self
->
interpolate
($_,
shift
);
1
N/A
if
($$
self
{
alt
}) {
1
N/A
$
self
->
output
(
"\n==== $_ ====\n\n"
);
1
N/A
}
else
{
1
N/A
$_ .=
"\n"
if
$$
self
{
loose
};
1
N/A
$
self
->
output
($_ .
"\n"
);
1
N/A
}
1
N/A
}
1
N/A
1
N/A
# Second level heading.
1
N/A
sub
cmd_head2
{
1
N/A
my
$
self
=
shift
;
1
N/A
local
$_ =
shift
;
1
N/A
s/\s+$//;
1
N/A
$_ = $
self
->
interpolate
($_,
shift
);
1
N/A
if
($$
self
{
alt
}) {
1
N/A
$
self
->
output
(
"\n== $_ ==\n\n"
);
1
N/A
}
else
{
1
N/A
$
self
->
output
(
' '
x ($$
self
{
indent
} /
2
) . $_ .
"\n\n"
);
1
N/A
}
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
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
=
shift
;
1
N/A
$$
self
{
MARGIN
} =
pop
@{ $$
self
{
INDENTS
} };
1
N/A
unless
(
defined
$$
self
{
MARGIN
}) {
1
N/A
carp
"Unmatched =back"
;
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
# Interior sequences
1
N/A
############################################################################
1
N/A
1
N/A
# The simple formatting ones. These are here mostly so that subclasses can
1
N/A
# override them and do more complicated things.
1
N/A
sub
seq_b
{
return
$_[
0
]{
alt
} ?
"``$_[1]''"
: $_[
1
] }
1
N/A
sub
seq_c
{
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
# The complicated one. Handle links. Since this is plain text, we can't
1
N/A
# actually make any real links, so this is all to figure out what text we
1
N/A
# print out.
1
N/A
sub
seq_l
{
1
N/A
my
$
self
=
shift
;
1
N/A
local
$_ =
shift
;
1
N/A
1
N/A
# Smash whitespace in case we were split across multiple lines.
1
N/A
s/\s+/ /g;
1
N/A
1
N/A
# If we were given any explicit text, just output it.
1
N/A
if
(/^([^|]+)\|/) {
return
$
1
}
1
N/A
1
N/A
# Okay, leading and trailing whitespace isn't important; get rid of it.
1
N/A
s/^\s+//;
1
N/A
s/\s+$//;
1
N/A
1
N/A
# Default to using the whole content of the link entry as a section
1
N/A
# name. Note that L<manpage/> forces a manpage interpretation, as does
1
N/A
# something looking like L<manpage(section)>. The latter is an
1
N/A
# enhancement over the original Pod::Text.
1
N/A
my
($
manpage
, $
section
) = (
''
, $_);
1
N/A
if
(/^(?:
https
?|
ftp
|
news
):/) {
1
N/A
# a URL
1
N/A
return
$_;
1
N/A
}
elsif
(/^
"\s*(.*?)\s*"
$/) {
1
N/A
$
section
=
'"'
. $
1
.
'"'
;
1
N/A
}
elsif
(m/^[-:.\w]+(?:\(\S+\))?$/) {
1
N/A
($
manpage
, $
section
) = ($_,
''
);
1
N/A
}
elsif
(m%/%) {
1
N/A
($
manpage
, $
section
) =
split
(/\s*\/\s*/, $_,
2
);
1
N/A
}
1
N/A
1
N/A
my
$
text
=
''
;
1
N/A
# Now build the actual output text.
1
N/A
if
(!
length
$
section
) {
1
N/A
$
text
=
"the $manpage manpage"
if
length
$
manpage
;
1
N/A
}
elsif
($
section
=~ /^[:\w]+(?:\(\))?/) {
1
N/A
$
text
.=
'the '
. $
section
.
' entry'
;
1
N/A
$
text
.= (
length
$
manpage
) ?
" in the $manpage manpage"
1
N/A
:
" elsewhere in this document"
;
1
N/A
}
else
{
1
N/A
$
section
=~ s/^\
"\s*//;
1
N/A
$
section
=~ s/\s*\
"$//;
1
N/A
$
text
.=
'the section on "'
. $
section
.
'"'
;
1
N/A
$
text
.=
" in the $manpage manpage"
if
length
$
manpage
;
1
N/A
}
1
N/A
$
text
;
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
1
N/A
# words, we've seen its associated paragraph or know for certain that it
1
N/A
# doesn't have one). It gets the paragraph associated with the item as an
1
N/A
# argument. If that argument is empty, just output the item tag; if it
1
N/A
# contains a newline, output the item tag followed by the newline.
1
N/A
# Otherwise, see if there's enough room for us to output the item tag in the
1
N/A
# margin of the text or if we 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
$
space
=
' '
x $
indent
;
1
N/A
$
space
=~ s/^ /:/
if
$$
self
{
alt
};
1
N/A
if
(!$_ || /^\s+$/ || ($$
self
{
MARGIN
} - $
indent
<
length
($
tag
) +
1
)) {
1
N/A
my
$
margin
= $$
self
{
MARGIN
};
1
N/A
$$
self
{
MARGIN
} = $
indent
;
1
N/A
my
$
output
= $
self
->
reformat
($
tag
);
1
N/A
$
output
=~ s/\n*$/\n/;
1
N/A
$
self
->
output
($
output
);
1
N/A
$$
self
{
MARGIN
} = $
margin
;
1
N/A
$
self
->
output
($
self
->
reformat
($_))
if
/\S/;
1
N/A
}
else
{
1
N/A
$_ = $
self
->
reformat
($_);
1
N/A
s/^ /:/
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
1
N/A
# Text::Wrap because it plays games with tabs. We can't use formline, even
1
N/A
# though we'd really like to, because it screws up non-printing characters.
1
N/A
# So we have to 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
1
N/A
# munging 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
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
::
PlainText
->
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
1
N/A
# means we need to turn the first argument into a file handle. Magic
1
N/A
# open will handle the <&STDIN case automagically.
1
N/A
if
(
defined
$_[
1
]) {
1
N/A
local
*
IN
;
1
N/A
unless
(
open
(
IN
, $_[
0
])) {
1
N/A
croak
(
"Can't open $_[0] for reading: $!\n"
);
1
N/A
return
;
1
N/A
}
1
N/A
$_[
0
] = \*
IN
;
1
N/A
return
$
parser
->
parse_from_filehandle
(@_);
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::PlainText - Convert POD data to formatted ASCII text
1
N/A
1
N/A
=head1 SYNOPSIS
1
N/A
1
N/A
use Pod::PlainText;
1
N/A
my $parser = Pod::PlainText->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::PlainText 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::PlainText 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::PlainText-E<gt>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 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 sentence
1
N/A
1
N/A
If set to a true value, Pod::PlainText 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
(W) Something has gone wrong in internal C<=item> processing. This message
1
N/A
indicates a bug in Pod::PlainText; you should never see it.
1
N/A
1
N/A
=item Can't open %s for reading: %s
1
N/A
1
N/A
(F) Pod::PlainText 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 Unknown escape: %s
1
N/A
1
N/A
(W) The POD source contained an C<EE<lt>E<gt>> escape that Pod::PlainText didn't
1
N/A
know about.
1
N/A
1
N/A
=item Unknown sequence: %s
1
N/A
1
N/A
(W) The POD source contained a non-standard internal sequence (something of
1
N/A
the form C<XE<lt>E<gt>>) that Pod::PlainText didn't know about.
1
N/A
1
N/A
=item Unmatched =back
1
N/A
1
N/A
(W) Pod::PlainText 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|Pod::Text::Termcap>.
1
N/A
1
N/A
=head1 SEE ALSO
1
N/A
1
N/A
L<Pod::Parser|Pod::Parser>, L<Pod::Text::Termcap|Pod::Text::Termcap>,
1
N/A
pod2text(1)
1
N/A
1
N/A
=head1 AUTHOR
1
N/A
1
N/A
Please report bugs using L<
http://rt.cpan.org
>.
1
N/A
1
N/A
Russ Allbery E<lt>rra@stanford.eduE<gt>, based I<very> heavily on the
1
N/A
original Pod::Text by Tom Christiansen E<lt>tchrist@mox.perl.comE<gt> and
1
N/A
its conversion to Pod::Parser by Brad Appleton
1
N/A
E<lt>bradapp@enteract.comE<gt>.
1
N/A
1
N/A
=cut