(Retracted by #8573)
[p5sagit/p5-mst-13.2.git] / lib / Text / Wrap.pm
1 package Text::Wrap;
2
3 require Exporter;
4
5 @ISA = qw(Exporter);
6 @EXPORT = qw(wrap fill);
7 @EXPORT_OK = qw($columns $break $huge);
8
9 $VERSION = 2000.06292219; #GMT
10
11 use vars qw($VERSION $columns $debug $break $huge);
12 use strict;
13
14 BEGIN   {
15         $columns = 76;  # <= screen width
16         $debug = 0;
17         $break = '\s';
18         $huge = 'wrap'; # alternatively: 'die'
19 }
20
21 use Text::Tabs qw(expand unexpand);
22
23 sub wrap
24 {
25         my ($ip, $xp, @t) = @_;
26
27         my $r = "";
28
29         my $t = _linearize(@t);
30
31         my $lead = $ip;
32         my $ll = $columns - length(expand($ip)) - 1;
33         my $nll = $columns - length(expand($xp)) - 1;
34         my $nl = "";
35         my $remainder = "";
36
37         while ($t !~ /^\s*$/) {
38                 if ($t =~ s/^([^\n]{0,$ll})($break|\Z(?!\n))//x) {
39                         $r .= unexpand($nl . $lead . $1);
40                         $remainder = $2;
41                 } elsif ($huge eq 'wrap' && $t =~ s/^([^\n]{$ll})//) {
42                         $r .= unexpand($nl . $lead . $1);
43                         $remainder = "\n";
44                 } elsif ($huge eq 'die') {
45                         die "couldn't wrap '$t'";
46                 } else {
47                         die "This shouldn't happen";
48                 }
49                         
50                 $lead = $xp;
51                 $ll = $nll;
52                 $nl = "\n";
53         }
54         $r .= $remainder;
55
56         print "-----------$r---------\n" if $debug;
57
58         print "Finish up with '$lead', '$t'\n" if $debug;
59
60         $r .= $lead . $t if $t ne "";
61
62         print "-----------$r---------\n" if $debug;;
63         return $r;
64 }
65
66 sub _linearize {
67     my @lines = expand(@_);
68
69     # Join the lines together, adding in extra whitespace only where needed
70     # to keep words seperated.
71     my $text = join "", map { /\s+\Z/ ? $_ : $_.' ' } @lines[0..$#lines-1];
72     $text .= $lines[-1];
73
74     return $text;
75 }
76
77
78 sub fill 
79 {
80         my ($ip, $xp, @raw) = @_;
81         my @para;
82         my $pp;
83
84         for $pp (split(/\n\s+/, join("\n",@raw))) {
85                 $pp =~ s/\s+/ /g;
86                 my $x = wrap($ip, $xp, $pp);
87                 push(@para, $x);
88         }
89
90         # if paragraph_indent is the same as line_indent, 
91         # separate paragraphs with blank lines
92
93         return join ($ip eq $xp ? "\n\n" : "\n", @para);
94 }
95
96 1;
97 __END__
98
99 =head1 NAME
100
101 Text::Wrap - line wrapping to form simple paragraphs
102
103 =head1 SYNOPSIS 
104
105         use Text::Wrap
106
107         print wrap($initial_tab, $subsequent_tab, @text);
108         print fill($initial_tab, $subsequent_tab, @text);
109
110         use Text::Wrap qw(wrap $columns $huge);
111
112         $columns = 132;
113         $huge = 'die';
114         $huge = 'wrap';
115
116 =head1 DESCRIPTION
117
118 Text::Wrap::wrap() is a very simple paragraph formatter.  It formats a
119 single paragraph at a time by breaking lines at word boundaries.
120 Indentation is controlled for the first line ($initial_tab) and
121 all subsequent lines ($subsequent_tab) independently.  
122
123 Lines are wrapped at $Text::Wrap::columns columns.  
124 $Text::Wrap::columns should be set to the full width of your output device.
125
126 When words that are longer than $columns are encountered, they
127 are broken up.  Previous versions of wrap() die()ed instead.
128 To restore the old (dying) behavior, set $Text::Wrap::huge to
129 'die'.
130
131 Text::Wrap::fill() is a simple multi-paragraph formatter.  It formats
132 each paragraph separately and then joins them together when it's done.  It
133 will destroy any whitespace in the original text.  It breaks text into
134 paragraphs by looking for whitespace after a newline.  In other respects
135 it acts like wrap().
136
137 =head1 EXAMPLE
138
139         print wrap("\t","","This is a bit of text that forms 
140                 a normal book-style paragraph");
141
142 =head1 AUTHOR
143
144 David Muir Sharnoff <muir@idiom.com> with help from Tim Pierce and
145 many many others.  
146