| 1 |
# Pod::PlainText -- Convert POD data to formatted ASCII text. |
| 2 |
# $Id: Text.pm,v 2.1 1999/09/20 11:53:33 eagle Exp $ |
| 3 |
# |
| 4 |
# Copyright 1999-2000 by Russ Allbery <[email protected]> |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify it |
| 7 |
# under the same terms as Perl itself. |
| 8 |
# |
| 9 |
# This module is intended to be a replacement for Pod::Text, and attempts to |
| 10 |
# match its output except for some specific circumstances where other |
| 11 |
# decisions seemed to produce better output. It uses Pod::Parser and is |
| 12 |
# designed to be very easy to subclass. |
| 13 |
|
| 14 |
############################################################################ |
| 15 |
# Modules and declarations |
| 16 |
############################################################################ |
| 17 |
|
| 18 |
package Pod::PlainText; |
| 19 |
use strict; |
| 20 |
use warnings; |
| 21 |
|
| 22 |
require 5.005; |
| 23 |
|
| 24 |
use Carp qw(carp croak); |
| 25 |
use Pod::Select (); |
| 26 |
|
| 27 |
use vars qw(@ISA %ESCAPES $VERSION); |
| 28 |
|
| 29 |
# We inherit from Pod::Select instead of Pod::Parser so that we can be used |
| 30 |
# by Pod::Usage. |
| 31 |
@ISA = qw(Pod::Select); |
| 32 |
|
| 33 |
$VERSION = '2.06'; |
| 34 |
|
| 35 |
BEGIN { |
| 36 |
if ($] < 5.006) { |
| 37 |
require Symbol; |
| 38 |
Symbol->import; |
| 39 |
} |
| 40 |
if ($] < 5.008 || ord "A" == 65) { |
| 41 |
*to_native = sub { return chr shift; }; |
| 42 |
} |
| 43 |
else { |
| 44 |
*to_native = sub { return chr utf8::unicode_to_native(shift); }; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
############################################################################ |
| 49 |
# Table of supported E<> escapes |
| 50 |
############################################################################ |
| 51 |
|
| 52 |
# This table is taken near verbatim from Pod::PlainText in Pod::Parser, |
| 53 |
# which got it near verbatim from the original Pod::Text. It is therefore |
| 54 |
# credited to Tom Christiansen, and I'm glad I didn't have to write it. :) |
| 55 |
%ESCAPES = ( |
| 56 |
'amp' => '&', # ampersand |
| 57 |
'lt' => '<', # left chevron, less-than |
| 58 |
'gt' => '>', # right chevron, greater-than |
| 59 |
'quot' => '"', # double quote |
| 60 |
|
| 61 |
"Aacute" => to_native(0xC1), # capital A, acute accent |
| 62 |
"aacute" => to_native(0xE1), # small a, acute accent |
| 63 |
"Acirc" => to_native(0xC2), # capital A, circumflex accent |
| 64 |
"acirc" => to_native(0xE2), # small a, circumflex accent |
| 65 |
"AElig" => to_native(0xC6), # capital AE diphthong (ligature) |
| 66 |
"aelig" => to_native(0xE6), # small ae diphthong (ligature) |
| 67 |
"Agrave" => to_native(0xC0), # capital A, grave accent |
| 68 |
"agrave" => to_native(0xE0), # small a, grave accent |
| 69 |
"Aring" => to_native(0xC5), # capital A, ring |
| 70 |
"aring" => to_native(0xE5), # small a, ring |
| 71 |
"Atilde" => to_native(0xC3), # capital A, tilde |
| 72 |
"atilde" => to_native(0xE3), # small a, tilde |
| 73 |
"Auml" => to_native(0xC4), # capital A, dieresis or umlaut mark |
| 74 |
"auml" => to_native(0xE4), # small a, dieresis or umlaut mark |
| 75 |
"Ccedil" => to_native(0xC7), # capital C, cedilla |
| 76 |
"ccedil" => to_native(0xE7), # small c, cedilla |
| 77 |
"Eacute" => to_native(0xC9), # capital E, acute accent |
| 78 |
"eacute" => to_native(0xE9), # small e, acute accent |
| 79 |
"Ecirc" => to_native(0xCA), # capital E, circumflex accent |
| 80 |
"ecirc" => to_native(0xEA), # small e, circumflex accent |
| 81 |
"Egrave" => to_native(0xC8), # capital E, grave accent |
| 82 |
"egrave" => to_native(0xE8), # small e, grave accent |
| 83 |
"ETH" => to_native(0xD0), # capital Eth, Icelandic |
| 84 |
"eth" => to_native(0xF0), # small eth, Icelandic |
| 85 |
"Euml" => to_native(0xCB), # capital E, dieresis or umlaut mark |
| 86 |
"euml" => to_native(0xEB), # small e, dieresis or umlaut mark |
| 87 |
"Iacute" => to_native(0xCD), # capital I, acute accent |
| 88 |
"iacute" => to_native(0xED), # small i, acute accent |
| 89 |
"Icirc" => to_native(0xCE), # capital I, circumflex accent |
| 90 |
"icirc" => to_native(0xEE), # small i, circumflex accent |
| 91 |
"Igrave" => to_native(0xCD), # capital I, grave accent |
| 92 |
"igrave" => to_native(0xED), # small i, grave accent |
| 93 |
"Iuml" => to_native(0xCF), # capital I, dieresis or umlaut mark |
| 94 |
"iuml" => to_native(0xEF), # small i, dieresis or umlaut mark |
| 95 |
"Ntilde" => to_native(0xD1), # capital N, tilde |
| 96 |
"ntilde" => to_native(0xF1), # small n, tilde |
| 97 |
"Oacute" => to_native(0xD3), # capital O, acute accent |
| 98 |
"oacute" => to_native(0xF3), # small o, acute accent |
| 99 |
"Ocirc" => to_native(0xD4), # capital O, circumflex accent |
| 100 |
"ocirc" => to_native(0xF4), # small o, circumflex accent |
| 101 |
"Ograve" => to_native(0xD2), # capital O, grave accent |
| 102 |
"ograve" => to_native(0xF2), # small o, grave accent |
| 103 |
"Oslash" => to_native(0xD8), # capital O, slash |
| 104 |
"oslash" => to_native(0xF8), # small o, slash |
| 105 |
"Otilde" => to_native(0xD5), # capital O, tilde |
| 106 |
"otilde" => to_native(0xF5), # small o, tilde |
| 107 |
"Ouml" => to_native(0xD6), # capital O, dieresis or umlaut mark |
| 108 |
"ouml" => to_native(0xF6), # small o, dieresis or umlaut mark |
| 109 |
"szlig" => to_native(0xDF), # small sharp s, German (sz ligature) |
| 110 |
"THORN" => to_native(0xDE), # capital THORN, Icelandic |
| 111 |
"thorn" => to_native(0xFE), # small thorn, Icelandic |
| 112 |
"Uacute" => to_native(0xDA), # capital U, acute accent |
| 113 |
"uacute" => to_native(0xFA), # small u, acute accent |
| 114 |
"Ucirc" => to_native(0xDB), # capital U, circumflex accent |
| 115 |
"ucirc" => to_native(0xFB), # small u, circumflex accent |
| 116 |
"Ugrave" => to_native(0xD9), # capital U, grave accent |
| 117 |
"ugrave" => to_native(0xF9), # small u, grave accent |
| 118 |
"Uuml" => to_native(0xDC), # capital U, dieresis or umlaut mark |
| 119 |
"uuml" => to_native(0xFC), # small u, dieresis or umlaut mark |
| 120 |
"Yacute" => to_native(0xDD), # capital Y, acute accent |
| 121 |
"yacute" => to_native(0xFD), # small y, acute accent |
| 122 |
"yuml" => to_native(0xFF), # small y, dieresis or umlaut mark |
| 123 |
|
| 124 |
"lchevron" => to_native(0xAB), # left chevron (double less than) |
| 125 |
"rchevron" => to_native(0xBB), # right chevron (double greater than) |
| 126 |
); |
| 127 |
|
| 128 |
|
| 129 |
############################################################################ |
| 130 |
# Initialization |
| 131 |
############################################################################ |
| 132 |
|
| 133 |
# Initialize the object. Must be sure to call our parent initializer. |
| 134 |
sub initialize { |
| 135 |
my $self = shift; |
| 136 |
|
| 137 |
$$self{alt} = 0 unless defined $$self{alt}; |
| 138 |
$$self{indent} = 4 unless defined $$self{indent}; |
| 139 |
$$self{loose} = 0 unless defined $$self{loose}; |
| 140 |
$$self{sentence} = 0 unless defined $$self{sentence}; |
| 141 |
$$self{width} = 76 unless defined $$self{width}; |
| 142 |
|
| 143 |
$$self{INDENTS} = []; # Stack of indentations. |
| 144 |
$$self{MARGIN} = $$self{indent}; # Current left margin in spaces. |
| 145 |
|
| 146 |
return $self->SUPER::initialize; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
############################################################################ |
| 151 |
# Core overrides |
| 152 |
############################################################################ |
| 153 |
|
| 154 |
# Called for each command paragraph. Gets the command, the associated |
| 155 |
# paragraph, the line number, and a Pod::Paragraph object. Just dispatches |
| 156 |
# the command to a method named the same as the command. =cut is handled |
| 157 |
# internally by Pod::Parser. |
| 158 |
sub command { |
| 159 |
my $self = shift; |
| 160 |
my $command = shift; |
| 161 |
return if $command eq 'pod'; |
| 162 |
return if ($$self{EXCLUDE} && $command ne 'end'); |
| 163 |
if (defined $$self{ITEM}) { |
| 164 |
$self->item ("\n"); |
| 165 |
local $_ = "\n"; |
| 166 |
$self->output($_) if($command eq 'back'); |
| 167 |
} |
| 168 |
$command = 'cmd_' . $command; |
| 169 |
return $self->$command (@_); |
| 170 |
} |
| 171 |
|
| 172 |
# Called for a verbatim paragraph. Gets the paragraph, the line number, and |
| 173 |
# a Pod::Paragraph object. Just output it verbatim, but with tabs converted |
| 174 |
# to spaces. |
| 175 |
sub verbatim { |
| 176 |
my $self = shift; |
| 177 |
return if $$self{EXCLUDE}; |
| 178 |
$self->item if defined $$self{ITEM}; |
| 179 |
local $_ = shift; |
| 180 |
return if /^\s*$/; |
| 181 |
s/^(\s*\S+)/(' ' x $$self{MARGIN}) . $1/gme; |
| 182 |
return $self->output($_); |
| 183 |
} |
| 184 |
|
| 185 |
# Called for a regular text block. Gets the paragraph, the line number, and |
| 186 |
# a Pod::Paragraph object. Perform interpolation and output the results. |
| 187 |
sub textblock { |
| 188 |
my $self = shift; |
| 189 |
return if $$self{EXCLUDE}; |
| 190 |
if($$self{VERBATIM}) { |
| 191 |
$self->output($_[0]); |
| 192 |
return; |
| 193 |
} |
| 194 |
local $_ = shift; |
| 195 |
my $line = shift; |
| 196 |
|
| 197 |
# Perform a little magic to collapse multiple L<> references. This is |
| 198 |
# here mostly for backwards-compatibility. We'll just rewrite the whole |
| 199 |
# thing into actual text at this part, bypassing the whole internal |
| 200 |
# sequence parsing thing. |
| 201 |
s{ |
| 202 |
( |
| 203 |
L< # A link of the form L</something>. |
| 204 |
/ |
| 205 |
( |
| 206 |
[:\w]+ # The item has to be a simple word... |
| 207 |
(\(\))? # ...or simple function. |
| 208 |
) |
| 209 |
> |
| 210 |
( |
| 211 |
,?\s+(and\s+)? # Allow lots of them, conjuncted. |
| 212 |
L< |
| 213 |
/ |
| 214 |
( |
| 215 |
[:\w]+ |
| 216 |
(\(\))? |
| 217 |
) |
| 218 |
> |
| 219 |
)+ |
| 220 |
) |
| 221 |
} { |
| 222 |
local $_ = $1; |
| 223 |
s%L</([^>]+)>%$1%g; |
| 224 |
my @items = split /(?:,?\s+(?:and\s+)?)/; |
| 225 |
my $string = "the "; |
| 226 |
my $i; |
| 227 |
for ($i = 0; $i < @items; $i++) { |
| 228 |
$string .= $items[$i]; |
| 229 |
$string .= ", " if @items > 2 && $i != $#items; |
| 230 |
$string .= " and " if ($i == $#items - 1); |
| 231 |
} |
| 232 |
$string .= " entries elsewhere in this document"; |
| 233 |
$string; |
| 234 |
}gex; |
| 235 |
|
| 236 |
# Now actually interpolate and output the paragraph. |
| 237 |
$_ = $self->interpolate ($_, $line); |
| 238 |
s/\s*$/\n/s; |
| 239 |
if (defined $$self{ITEM}) { |
| 240 |
$self->item ($_ . "\n"); |
| 241 |
} else { |
| 242 |
$self->output ($self->reformat ($_ . "\n")); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
# Called for an interior sequence. Gets the command, argument, and a |
| 247 |
# Pod::InteriorSequence object and is expected to return the resulting text. |
| 248 |
# Calls code, bold, italic, file, and link to handle those types of |
| 249 |
# sequences, and handles S<>, E<>, X<>, and Z<> directly. |
| 250 |
sub interior_sequence { |
| 251 |
my $self = shift; |
| 252 |
my $command = shift; |
| 253 |
local $_ = shift; |
| 254 |
return '' if ($command eq 'X' || $command eq 'Z'); |
| 255 |
|
| 256 |
# Expand escapes into the actual character now, carping if invalid. |
| 257 |
if ($command eq 'E') { |
| 258 |
return $ESCAPES{$_} if defined $ESCAPES{$_}; |
| 259 |
carp "Unknown escape: E<$_>"; |
| 260 |
return "E<$_>"; |
| 261 |
} |
| 262 |
|
| 263 |
# For all the other sequences, empty content produces no output. |
| 264 |
return if $_ eq ''; |
| 265 |
|
| 266 |
# For S<>, compress all internal whitespace and then map spaces to \01. |
| 267 |
# When we output the text, we'll map this back. |
| 268 |
if ($command eq 'S') { |
| 269 |
s/\s{2,}/ /g; |
| 270 |
tr/ /\01/; |
| 271 |
return $_; |
| 272 |
} |
| 273 |
|
| 274 |
# Anything else needs to get dispatched to another method. |
| 275 |
if ($command eq 'B') { return $self->seq_b ($_) } |
| 276 |
elsif ($command eq 'C') { return $self->seq_c ($_) } |
| 277 |
elsif ($command eq 'F') { return $self->seq_f ($_) } |
| 278 |
elsif ($command eq 'I') { return $self->seq_i ($_) } |
| 279 |
elsif ($command eq 'L') { return $self->seq_l ($_) } |
| 280 |
else { carp "Unknown sequence $command<$_>" } |
| 281 |
} |
| 282 |
|
| 283 |
# Called for each paragraph that's actually part of the POD. We take |
| 284 |
# advantage of this opportunity to untabify the input. |
| 285 |
sub preprocess_paragraph { |
| 286 |
my $self = shift; |
| 287 |
local $_ = shift; |
| 288 |
1 while s/^(.*?)(\t+)/$1 . ' ' x (length ($2) * 8 - length ($1) % 8)/me; |
| 289 |
return $_; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
############################################################################ |
| 294 |
# Command paragraphs |
| 295 |
############################################################################ |
| 296 |
|
| 297 |
# All command paragraphs take the paragraph and the line number. |
| 298 |
|
| 299 |
# First level heading. |
| 300 |
sub cmd_head1 { |
| 301 |
my $self = shift; |
| 302 |
local $_ = shift; |
| 303 |
s/\s+$//s; |
| 304 |
$_ = $self->interpolate ($_, shift); |
| 305 |
if ($$self{alt}) { |
| 306 |
$self->output ("\n==== $_ ====\n\n"); |
| 307 |
} else { |
| 308 |
$_ .= "\n" if $$self{loose}; |
| 309 |
$self->output ($_ . "\n"); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
# Second level heading. |
| 314 |
sub cmd_head2 { |
| 315 |
my $self = shift; |
| 316 |
local $_ = shift; |
| 317 |
s/\s+$//s; |
| 318 |
$_ = $self->interpolate ($_, shift); |
| 319 |
if ($$self{alt}) { |
| 320 |
$self->output ("\n== $_ ==\n\n"); |
| 321 |
} else { |
| 322 |
$_ .= "\n" if $$self{loose}; |
| 323 |
$self->output (' ' x ($$self{indent} / 2) . $_ . "\n"); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
# third level heading - not strictly perlpodspec compliant |
| 328 |
sub cmd_head3 { |
| 329 |
my $self = shift; |
| 330 |
local $_ = shift; |
| 331 |
s/\s+$//s; |
| 332 |
$_ = $self->interpolate ($_, shift); |
| 333 |
if ($$self{alt}) { |
| 334 |
$self->output ("\n= $_ =\n"); |
| 335 |
} else { |
| 336 |
$_ .= "\n" if $$self{loose}; |
| 337 |
$self->output (' ' x ($$self{indent}) . $_ . "\n"); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
# fourth level heading - not strictly perlpodspec compliant |
| 342 |
# just like head3 |
| 343 |
*cmd_head4 = \&cmd_head3; |
| 344 |
|
| 345 |
# Start a list. |
| 346 |
sub cmd_over { |
| 347 |
my $self = shift; |
| 348 |
local $_ = shift; |
| 349 |
unless (/^[-+]?\d+\s+$/) { $_ = $$self{indent} } |
| 350 |
push (@{ $$self{INDENTS} }, $$self{MARGIN}); |
| 351 |
$$self{MARGIN} += ($_ + 0); |
| 352 |
} |
| 353 |
|
| 354 |
# End a list. |
| 355 |
sub cmd_back { |
| 356 |
my $self = shift; |
| 357 |
$$self{MARGIN} = pop @{ $$self{INDENTS} }; |
| 358 |
unless (defined $$self{MARGIN}) { |
| 359 |
carp 'Unmatched =back'; |
| 360 |
$$self{MARGIN} = $$self{indent}; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
# An individual list item. |
| 365 |
sub cmd_item { |
| 366 |
my $self = shift; |
| 367 |
if (defined $$self{ITEM}) { $self->item } |
| 368 |
local $_ = shift; |
| 369 |
s/\s+$//s; |
| 370 |
$$self{ITEM} = $self->interpolate ($_); |
| 371 |
} |
| 372 |
|
| 373 |
# Begin a block for a particular translator. Setting VERBATIM triggers |
| 374 |
# special handling in textblock(). |
| 375 |
sub cmd_begin { |
| 376 |
my $self = shift; |
| 377 |
local $_ = shift; |
| 378 |
my ($kind) = /^(\S+)/ or return; |
| 379 |
if ($kind eq 'text') { |
| 380 |
$$self{VERBATIM} = 1; |
| 381 |
} else { |
| 382 |
$$self{EXCLUDE} = 1; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
# End a block for a particular translator. We assume that all =begin/=end |
| 387 |
# pairs are properly closed. |
| 388 |
sub cmd_end { |
| 389 |
my $self = shift; |
| 390 |
$$self{EXCLUDE} = 0; |
| 391 |
$$self{VERBATIM} = 0; |
| 392 |
} |
| 393 |
|
| 394 |
# One paragraph for a particular translator. Ignore it unless it's intended |
| 395 |
# for text, in which case we treat it as a verbatim text block. |
| 396 |
sub cmd_for { |
| 397 |
my $self = shift; |
| 398 |
local $_ = shift; |
| 399 |
my $line = shift; |
| 400 |
return unless s/^text\b[ \t]*\r?\n?//; |
| 401 |
$self->verbatim ($_, $line); |
| 402 |
} |
| 403 |
|
| 404 |
# just a dummy method for the time being |
| 405 |
sub cmd_encoding { |
| 406 |
return; |
| 407 |
} |
| 408 |
|
| 409 |
############################################################################ |
| 410 |
# Interior sequences |
| 411 |
############################################################################ |
| 412 |
|
| 413 |
# The simple formatting ones. These are here mostly so that subclasses can |
| 414 |
# override them and do more complicated things. |
| 415 |
sub seq_b { return $_[0]{alt} ? "``$_[1]''" : $_[1] } |
| 416 |
sub seq_c { return $_[0]{alt} ? "``$_[1]''" : "`$_[1]'" } |
| 417 |
sub seq_f { return $_[0]{alt} ? "\"$_[1]\"" : $_[1] } |
| 418 |
sub seq_i { return '*' . $_[1] . '*' } |
| 419 |
|
| 420 |
# The complicated one. Handle links. Since this is plain text, we can't |
| 421 |
# actually make any real links, so this is all to figure out what text we |
| 422 |
# print out. |
| 423 |
sub seq_l { |
| 424 |
my $self = shift; |
| 425 |
local $_ = shift; |
| 426 |
|
| 427 |
# Smash whitespace in case we were split across multiple lines. |
| 428 |
s/\s+/ /g; |
| 429 |
|
| 430 |
# If we were given any explicit text, just output it. |
| 431 |
if (/^([^|]+)\|/) { return $1 } |
| 432 |
|
| 433 |
# Okay, leading and trailing whitespace isn't important; get rid of it. |
| 434 |
s/^\s+//; |
| 435 |
s/\s+$//; |
| 436 |
|
| 437 |
# Default to using the whole content of the link entry as a section |
| 438 |
# name. Note that L<manpage/> forces a manpage interpretation, as does |
| 439 |
# something looking like L<manpage(section)>. The latter is an |
| 440 |
# enhancement over the original Pod::Text. |
| 441 |
my ($manpage, $section) = ('', $_); |
| 442 |
if (/^(?:https?|ftp|news):/) { |
| 443 |
# a URL |
| 444 |
return $_; |
| 445 |
} elsif (/^"\s*(.*?)\s*"$/) { |
| 446 |
$section = '"' . $1 . '"'; |
| 447 |
} elsif (m/^[-:.\w]+(?:\(\S+\))?$/) { |
| 448 |
($manpage, $section) = ($_, ''); |
| 449 |
} elsif (m{/}) { |
| 450 |
($manpage, $section) = split (/\s*\/\s*/, $_, 2); |
| 451 |
} |
| 452 |
|
| 453 |
my $text = ''; |
| 454 |
# Now build the actual output text. |
| 455 |
if (!length $section) { |
| 456 |
$text = "the $manpage manpage" if length $manpage; |
| 457 |
} elsif ($section =~ /^[:\w]+(?:\(\))?/) { |
| 458 |
$text .= 'the ' . $section . ' entry'; |
| 459 |
$text .= (length $manpage) ? " in the $manpage manpage" |
| 460 |
: ' elsewhere in this document'; |
| 461 |
} else { |
| 462 |
$section =~ s/^\"\s*//; |
| 463 |
$section =~ s/\s*\"$//; |
| 464 |
$text .= 'the section on "' . $section . '"'; |
| 465 |
$text .= " in the $manpage manpage" if length $manpage; |
| 466 |
} |
| 467 |
return $text; |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
############################################################################ |
| 472 |
# List handling |
| 473 |
############################################################################ |
| 474 |
|
| 475 |
# This method is called whenever an =item command is complete (in other |
| 476 |
# words, we've seen its associated paragraph or know for certain that it |
| 477 |
# doesn't have one). It gets the paragraph associated with the item as an |
| 478 |
# argument. If that argument is empty, just output the item tag; if it |
| 479 |
# contains a newline, output the item tag followed by the newline. |
| 480 |
# Otherwise, see if there's enough room for us to output the item tag in the |
| 481 |
# margin of the text or if we have to put it on a separate line. |
| 482 |
sub item { |
| 483 |
my $self = shift; |
| 484 |
local $_ = shift; |
| 485 |
my $tag = $$self{ITEM}; |
| 486 |
unless (defined $tag) { |
| 487 |
carp 'item called without tag'; |
| 488 |
return; |
| 489 |
} |
| 490 |
undef $$self{ITEM}; |
| 491 |
my $indent = $$self{INDENTS}[-1]; |
| 492 |
unless (defined $indent) { $indent = $$self{indent} } |
| 493 |
my $space = ' ' x $indent; |
| 494 |
$space =~ s/^ /:/ if $$self{alt}; |
| 495 |
if (!$_ || /^\s+$/ || ($$self{MARGIN} - $indent < length ($tag) + 1)) { |
| 496 |
my $margin = $$self{MARGIN}; |
| 497 |
$$self{MARGIN} = $indent; |
| 498 |
my $output = $self->reformat ($tag); |
| 499 |
$output =~ s/[\r\n]*$/\n/; |
| 500 |
$self->output ($output); |
| 501 |
$$self{MARGIN} = $margin; |
| 502 |
$self->output ($self->reformat ($_)) if /\S/; |
| 503 |
} else { |
| 504 |
$_ = $self->reformat ($_); |
| 505 |
s/^ /:/ if ($$self{alt} && $indent > 0); |
| 506 |
my $tagspace = ' ' x length $tag; |
| 507 |
s/^($space)$tagspace/$1$tag/ or carp 'Bizarre space in item'; |
| 508 |
$self->output ($_); |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
############################################################################ |
| 514 |
# Output formatting |
| 515 |
############################################################################ |
| 516 |
|
| 517 |
# Wrap a line, indenting by the current left margin. We can't use |
| 518 |
# Text::Wrap because it plays games with tabs. We can't use formline, even |
| 519 |
# though we'd really like to, because it screws up non-printing characters. |
| 520 |
# So we have to do the wrapping ourselves. |
| 521 |
sub wrap { |
| 522 |
my $self = shift; |
| 523 |
local $_ = shift; |
| 524 |
my $output = ''; |
| 525 |
my $spaces = ' ' x $$self{MARGIN}; |
| 526 |
my $width = $$self{width} - $$self{MARGIN}; |
| 527 |
while (length > $width) { |
| 528 |
if (s/^([^\r\n]{0,$width})\s+// || s/^([^\r\n]{$width})//) { |
| 529 |
$output .= $spaces . $1 . "\n"; |
| 530 |
} else { |
| 531 |
last; |
| 532 |
} |
| 533 |
} |
| 534 |
$output .= $spaces . $_; |
| 535 |
$output =~ s/\s+$/\n\n/; |
| 536 |
return $output; |
| 537 |
} |
| 538 |
|
| 539 |
# Reformat a paragraph of text for the current margin. Takes the text to |
| 540 |
# reformat and returns the formatted text. |
| 541 |
sub reformat { |
| 542 |
my $self = shift; |
| 543 |
local $_ = shift; |
| 544 |
|
| 545 |
# If we're trying to preserve two spaces after sentences, do some |
| 546 |
# munging to support that. Otherwise, smash all repeated whitespace. |
| 547 |
if ($$self{sentence}) { |
| 548 |
s/ +$//mg; |
| 549 |
s/\.\r?\n/. \n/g; |
| 550 |
s/[\r\n]+/ /g; |
| 551 |
s/ +/ /g; |
| 552 |
} else { |
| 553 |
s/\s+/ /g; |
| 554 |
} |
| 555 |
return $self->wrap($_); |
| 556 |
} |
| 557 |
|
| 558 |
# Output text to the output device. |
| 559 |
sub output { $_[1] =~ tr/\01/ /; print { $_[0]->output_handle } $_[1] } |
| 560 |
|
| 561 |
|
| 562 |
############################################################################ |
| 563 |
# Backwards compatibility |
| 564 |
############################################################################ |
| 565 |
|
| 566 |
# The old Pod::Text module did everything in a pod2text() function. This |
| 567 |
# tries to provide the same interface for legacy applications. |
| 568 |
sub pod2text { |
| 569 |
my @args; |
| 570 |
|
| 571 |
# This is really ugly; I hate doing option parsing in the middle of a |
| 572 |
# module. But the old Pod::Text module supported passing flags to its |
| 573 |
# entry function, so handle -a and -<number>. |
| 574 |
while ($_[0] =~ /^-/) { |
| 575 |
my $flag = shift; |
| 576 |
if ($flag eq '-a') { push (@args, alt => 1) } |
| 577 |
elsif ($flag =~ /^-(\d+)$/) { push (@args, width => $1) } |
| 578 |
else { |
| 579 |
unshift (@_, $flag); |
| 580 |
last; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
# Now that we know what arguments we're using, create the parser. |
| 585 |
my $parser = Pod::PlainText->new (@args); |
| 586 |
|
| 587 |
# If two arguments were given, the second argument is going to be a file |
| 588 |
# handle. That means we want to call parse_from_filehandle(), which |
| 589 |
# means we need to turn the first argument into a file handle. Magic |
| 590 |
# open will handle the <&STDIN case automagically. |
| 591 |
if (defined $_[1]) { |
| 592 |
my $infh; |
| 593 |
if ($] < 5.006) { |
| 594 |
$infh = gensym(); |
| 595 |
} |
| 596 |
unless (open ($infh, $_[0])) { |
| 597 |
croak ("Can't open $_[0] for reading: $!\n"); |
| 598 |
} |
| 599 |
$_[0] = $infh; |
| 600 |
return $parser->parse_from_filehandle (@_); |
| 601 |
} else { |
| 602 |
return $parser->parse_from_file (@_); |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
############################################################################ |
| 608 |
# Module return value and documentation |
| 609 |
############################################################################ |
| 610 |
|
| 611 |
1; |
| 612 |
__END__ |
| 613 |
|
| 614 |
=head1 NAME |
| 615 |
|
| 616 |
Pod::PlainText - Convert POD data to formatted ASCII text |
| 617 |
|
| 618 |
=head1 SYNOPSIS |
| 619 |
|
| 620 |
use Pod::PlainText; |
| 621 |
my $parser = Pod::PlainText->new (sentence => 0, width => 78); |
| 622 |
|
| 623 |
# Read POD from STDIN and write to STDOUT. |
| 624 |
$parser->parse_from_filehandle; |
| 625 |
|
| 626 |
# Read POD from file.pod and write to file.txt. |
| 627 |
$parser->parse_from_file ('file.pod', 'file.txt'); |
| 628 |
|
| 629 |
=head1 DESCRIPTION |
| 630 |
|
| 631 |
Pod::PlainText is a module that can convert documentation in the POD format (the |
| 632 |
preferred language for documenting Perl) into formatted ASCII. It uses no |
| 633 |
special formatting controls or codes whatsoever, and its output is therefore |
| 634 |
suitable for nearly any device. |
| 635 |
|
| 636 |
As a derived class from Pod::Parser, Pod::PlainText supports the same methods and |
| 637 |
interfaces. See L<Pod::Parser> for all the details; briefly, one creates a |
| 638 |
new parser with C<Pod::PlainText-E<gt>new()> and then calls either |
| 639 |
parse_from_filehandle() or parse_from_file(). |
| 640 |
|
| 641 |
new() can take options, in the form of key/value pairs, that control the |
| 642 |
behavior of the parser. The currently recognized options are: |
| 643 |
|
| 644 |
=over 4 |
| 645 |
|
| 646 |
=item alt |
| 647 |
|
| 648 |
If set to a true value, selects an alternate output format that, among other |
| 649 |
things, uses a different heading style and marks C<=item> entries with a |
| 650 |
colon in the left margin. Defaults to false. |
| 651 |
|
| 652 |
=item indent |
| 653 |
|
| 654 |
The number of spaces to indent regular text, and the default indentation for |
| 655 |
C<=over> blocks. Defaults to 4. |
| 656 |
|
| 657 |
=item loose |
| 658 |
|
| 659 |
If set to a true value, a blank line is printed after a C<=headN> headings. |
| 660 |
If set to false (the default), no blank line is printed after C<=headN>. |
| 661 |
This is the default because it's the expected formatting for manual pages; |
| 662 |
if you're formatting arbitrary text documents, setting this to true may |
| 663 |
result in more pleasing output. |
| 664 |
|
| 665 |
=item sentence |
| 666 |
|
| 667 |
If set to a true value, Pod::PlainText will assume that each sentence ends in two |
| 668 |
spaces, and will try to preserve that spacing. If set to false, all |
| 669 |
consecutive whitespace in non-verbatim paragraphs is compressed into a |
| 670 |
single space. Defaults to true. |
| 671 |
|
| 672 |
=item width |
| 673 |
|
| 674 |
The column at which to wrap text on the right-hand side. Defaults to 76. |
| 675 |
|
| 676 |
=back |
| 677 |
|
| 678 |
The standard Pod::Parser method parse_from_filehandle() takes up to two |
| 679 |
arguments, the first being the file handle to read POD from and the second |
| 680 |
being the file handle to write the formatted output to. The first defaults |
| 681 |
to STDIN if not given, and the second defaults to STDOUT. The method |
| 682 |
parse_from_file() is almost identical, except that its two arguments are the |
| 683 |
input and output disk files instead. See L<Pod::Parser> for the specific |
| 684 |
details. |
| 685 |
|
| 686 |
=head1 DIAGNOSTICS |
| 687 |
|
| 688 |
=over 4 |
| 689 |
|
| 690 |
=item Bizarre space in item |
| 691 |
|
| 692 |
(W) Something has gone wrong in internal C<=item> processing. This message |
| 693 |
indicates a bug in Pod::PlainText; you should never see it. |
| 694 |
|
| 695 |
=item Can't open %s for reading: %s |
| 696 |
|
| 697 |
(F) Pod::PlainText was invoked via the compatibility mode pod2text() interface |
| 698 |
and the input file it was given could not be opened. |
| 699 |
|
| 700 |
=item Unknown escape: %s |
| 701 |
|
| 702 |
(W) The POD source contained an C<EE<lt>E<gt>> escape that Pod::PlainText didn't |
| 703 |
know about. |
| 704 |
|
| 705 |
=item Unknown sequence: %s |
| 706 |
|
| 707 |
(W) The POD source contained a non-standard internal sequence (something of |
| 708 |
the form C<XE<lt>E<gt>>) that Pod::PlainText didn't know about. |
| 709 |
|
| 710 |
=item Unmatched =back |
| 711 |
|
| 712 |
(W) Pod::PlainText encountered a C<=back> command that didn't correspond to an |
| 713 |
C<=over> command. |
| 714 |
|
| 715 |
=back |
| 716 |
|
| 717 |
=head1 RESTRICTIONS |
| 718 |
|
| 719 |
Embedded Ctrl-As (octal 001) in the input will be mapped to spaces on |
| 720 |
output, due to an internal implementation detail. |
| 721 |
|
| 722 |
=head1 NOTES |
| 723 |
|
| 724 |
This is a replacement for an earlier Pod::Text module written by Tom |
| 725 |
Christiansen. It has a revamped interface, since it now uses Pod::Parser, |
| 726 |
but an interface roughly compatible with the old Pod::Text::pod2text() |
| 727 |
function is still available. Please change to the new calling convention, |
| 728 |
though. |
| 729 |
|
| 730 |
The original Pod::Text contained code to do formatting via termcap |
| 731 |
sequences, although it wasn't turned on by default and it was problematic to |
| 732 |
get it to work at all. This rewrite doesn't even try to do that, but a |
| 733 |
subclass of it does. Look for L<Pod::Text::Termcap|Pod::Text::Termcap>. |
| 734 |
|
| 735 |
=head1 SEE ALSO |
| 736 |
|
| 737 |
B<Pod::PlainText> is part of the L<Pod::Parser> distribution. |
| 738 |
|
| 739 |
L<Pod::Parser|Pod::Parser>, L<Pod::Text::Termcap|Pod::Text::Termcap>, |
| 740 |
pod2text(1) |
| 741 |
|
| 742 |
=head1 AUTHOR |
| 743 |
|
| 744 |
Please report bugs using L<http://rt.cpan.org>. |
| 745 |
|
| 746 |
Russ Allbery E<lt>[email protected]<gt>, based I<very> heavily on the |
| 747 |
original Pod::Text by Tom Christiansen E<lt>[email protected]<gt> and |
| 748 |
its conversion to Pod::Parser by Brad Appleton |
| 749 |
E<lt>[email protected]<gt>. |
| 750 |
|
| 751 |
=cut |