Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

827 lines
29 KiB

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