Source code of Windows XP (NT5)
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.

132 lines
2.9 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 = 98.112902;
  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'
  14. }
  15. use Text::Tabs qw(expand unexpand);
  16. sub wrap
  17. {
  18. my ($ip, $xp, @t) = @_;
  19. my $r = "";
  20. my $t = expand(join(" ",@t));
  21. my $lead = $ip;
  22. my $ll = $columns - length(expand($ip)) - 1;
  23. my $nll = $columns - length(expand($xp)) - 1;
  24. my $nl = "";
  25. my $remainder = "";
  26. while ($t !~ /^\s*$/) {
  27. if ($t =~ s/^([^\n]{0,$ll})($break|\Z(?!\n))//xm) {
  28. $r .= unexpand($nl . $lead . $1);
  29. $remainder = $2;
  30. } elsif ($huge eq 'wrap' && $t =~ s/^([^\n]{$ll})//) {
  31. $r .= unexpand($nl . $lead . $1);
  32. $remainder = "\n";
  33. } elsif ($huge eq 'die') {
  34. die "couldn't wrap '$t'";
  35. } else {
  36. die "This shouldn't happen";
  37. }
  38. $lead = $xp;
  39. $ll = $nll;
  40. $nl = "\n";
  41. }
  42. $r .= $remainder;
  43. print "-----------$r---------\n" if $debug;
  44. print "Finish up with '$lead', '$t'\n" if $debug;
  45. $r .= $lead . $t if $t ne "";
  46. print "-----------$r---------\n" if $debug;;
  47. return $r;
  48. }
  49. sub fill
  50. {
  51. my ($ip, $xp, @raw) = @_;
  52. my @para;
  53. my $pp;
  54. for $pp (split(/\n\s+/, join("\n",@raw))) {
  55. $pp =~ s/\s+/ /g;
  56. my $x = wrap($ip, $xp, $pp);
  57. push(@para, $x);
  58. }
  59. # if paragraph_indent is the same as line_indent,
  60. # separate paragraphs with blank lines
  61. return join ($ip eq $xp ? "\n\n" : "\n", @para);
  62. }
  63. 1;
  64. __END__
  65. =head1 NAME
  66. Text::Wrap - line wrapping to form simple paragraphs
  67. =head1 SYNOPSIS
  68. use Text::Wrap
  69. print wrap($initial_tab, $subsequent_tab, @text);
  70. print fill($initial_tab, $subsequent_tab, @text);
  71. use Text::Wrap qw(wrap $columns $huge);
  72. $columns = 132;
  73. $huge = 'die';
  74. $huge = 'wrap';
  75. =head1 DESCRIPTION
  76. Text::Wrap::wrap() is a very simple paragraph formatter. It formats a
  77. single paragraph at a time by breaking lines at word boundaries.
  78. Indentation is controlled for the first line ($initial_tab) and
  79. all subsequent lines ($subsequent_tab) independently.
  80. Lines are wrapped at $Text::Wrap::columns columns.
  81. $Text::Wrap::columns should be set to the full width of your output device.
  82. When words that are longer than $columns are encountered, they
  83. are broken up. Previous versions of wrap() die()ed instead.
  84. To restore the old (dying) behavior, set $Text::Wrap::huge to
  85. 'die'.
  86. Text::Wrap::fill() is a simple multi-paragraph formatter. It formats
  87. each paragraph separately and then joins them together when it's done. It
  88. will destroy any whitespace in the original text. It breaks text into
  89. paragraphs by looking for whitespace after a newline. In other respects
  90. it acts like wrap().
  91. =head1 EXAMPLE
  92. print wrap("\t","","This is a bit of text that forms
  93. a normal book-style paragraph");
  94. =head1 AUTHOR
  95. David Muir Sharnoff <[email protected]> with help from Tim Pierce and
  96. many many others.