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