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.

175 lines
4.6 KiB

  1. package Text::Wrap;
  2. require Exporter;
  3. @ISA = qw(Exporter);
  4. @EXPORT = qw(wrap fill);
  5. @EXPORT_OK = qw($columns $break $huge);
  6. $VERSION = 2001.0131;
  7. use vars qw($VERSION $columns $debug $break $huge);
  8. use strict;
  9. BEGIN {
  10. $columns = 76; # <= screen width
  11. $debug = 0;
  12. $break = '\s';
  13. $huge = 'wrap'; # alternatively: 'die' or 'overflow'
  14. }
  15. use Text::Tabs qw(expand unexpand);
  16. sub wrap
  17. {
  18. my ($ip, $xp, @t) = @_;
  19. my $r = "";
  20. my $tail = pop(@t);
  21. my $t = expand(join("", (map { /\s+\Z/ ? ( $_ ) : ($_, ' ') } @t), $tail));
  22. my $lead = $ip;
  23. my $ll = $columns - length(expand($ip)) - 1;
  24. my $nll = $columns - length(expand($xp)) - 1;
  25. my $nl = "";
  26. my $remainder = "";
  27. pos($t) = 0;
  28. while ($t !~ /\G\s*\Z/gc) {
  29. if ($t =~ /\G([^\n]{0,$ll})($break|\Z(?!\n))/xmgc) {
  30. $r .= unexpand($nl . $lead . $1);
  31. $remainder = $2;
  32. } elsif ($huge eq 'wrap' && $t =~ /\G([^\n]{$ll})/gc) {
  33. $r .= unexpand($nl . $lead . $1);
  34. $remainder = "\n";
  35. } elsif ($huge eq 'overflow' && $t =~ /\G([^\n]*?)($break|\Z(?!\n))/xmgc) {
  36. $r .= unexpand($nl . $lead . $1);
  37. $remainder = $2;
  38. } elsif ($huge eq 'die') {
  39. die "couldn't wrap '$t'";
  40. } else {
  41. die "This shouldn't happen";
  42. }
  43. $lead = $xp;
  44. $ll = $nll;
  45. $nl = "\n";
  46. }
  47. $r .= $remainder;
  48. print "-----------$r---------\n" if $debug;
  49. print "Finish up with '$lead'\n" if $debug;
  50. $r .= $lead . substr($t, pos($t), length($t)-pos($t))
  51. if pos($t) ne length($t);
  52. print "-----------$r---------\n" if $debug;;
  53. return $r;
  54. }
  55. sub fill
  56. {
  57. my ($ip, $xp, @raw) = @_;
  58. my @para;
  59. my $pp;
  60. for $pp (split(/\n\s+/, join("\n",@raw))) {
  61. $pp =~ s/\s+/ /g;
  62. my $x = wrap($ip, $xp, $pp);
  63. push(@para, $x);
  64. }
  65. # if paragraph_indent is the same as line_indent,
  66. # separate paragraphs with blank lines
  67. my $ps = ($ip eq $xp) ? "\n\n" : "\n";
  68. return join ($ps, @para);
  69. }
  70. 1;
  71. __END__
  72. =head1 NAME
  73. Text::Wrap - line wrapping to form simple paragraphs
  74. =head1 SYNOPSIS
  75. B<Example 1>
  76. use Text::Wrap
  77. $initial_tab = "\t"; # Tab before first line
  78. $subsequent_tab = ""; # All other lines flush left
  79. print wrap($initial_tab, $subsequent_tab, @text);
  80. print fill($initial_tab, $subsequent_tab, @text);
  81. @lines = wrap($initial_tab, $subsequent_tab, @text);
  82. @paragraphs = fill($initial_tab, $subsequent_tab, @text);
  83. B<Example 2>
  84. use Text::Wrap qw(wrap $columns $huge);
  85. $columns = 132; # Wrap at 132 characters
  86. $huge = 'die';
  87. $huge = 'wrap';
  88. $huge = 'overflow';
  89. B<Example 3>
  90. use Text::Wrap
  91. $Text::Wrap::columns = 72;
  92. print wrap('', '', @text);
  93. =head1 DESCRIPTION
  94. Text::Wrap::wrap() is a very simple paragraph formatter. It formats a
  95. single paragraph at a time by breaking lines at word boundries.
  96. Indentation is controlled for the first line (C<$initial_tab>) and
  97. all subsquent lines (C<$subsequent_tab>) independently. Please note:
  98. C<$initial_tab> and C<$subsequent_tab> are the literal strings that will
  99. be used: it is unlikley you would want to pass in a number.
  100. Lines are wrapped at C<$Text::Wrap::columns> columns. C<$Text::Wrap::columns>
  101. should be set to the full width of your output device. In fact,
  102. every resulting line will have length of no more than C<$columns - 1>.
  103. Beginner note: In example 2, above C<$columns> is imported into
  104. the local namespace, and set locally. In example 3,
  105. C<$Text::Wrap::columns> is set in its own namespace without importing it.
  106. When words that are longer than C<$columns> are encountered, they
  107. are broken up. C<wrap()> adds a C<"\n"> at column C<$columns>.
  108. This behavior can be overridden by setting C<$huge> to
  109. 'die' or to 'overflow'. When set to 'die', large words will cause
  110. C<die()> to be called. When set to 'overflow', large words will be
  111. left intact.
  112. Text::Wrap::fill() is a simple multi-paragraph formatter. It formats
  113. each paragraph separately and then joins them together when it's done. It
  114. will destory any whitespace in the original text. It breaks text into
  115. paragraphs by looking for whitespace after a newline. In other respects
  116. it acts like wrap().
  117. When called in list context, C<wrap()> will return a list of lines and
  118. C<fill()> will return a list of paragraphs.
  119. Historical notes: Older versions of C<wrap()> and C<fill()> always
  120. returned strings. Also, 'die' used to be the default value of
  121. C<$huge>. Now, 'wrap' is the default value.
  122. =head1 EXAMPLE
  123. print wrap("\t","","This is a bit of text that forms
  124. a normal book-style paragraph");
  125. =head1 AUTHOR
  126. David Muir Sharnoff <[email protected]> with help from Tim Pierce and
  127. many many others.