This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
update to Text::Wrap 98.112901 from David Muir Sharnoff
[perl5.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 = 98.112901;
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         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";
51         }
52         $r .= $remainder;
53
54         print "-----------$r---------\n" if $debug;
55
56         print "Finish up with '$lead', '$t'\n" if $debug;
57
58         $r .= $lead . $t if $t ne "";
59
60         print "-----------$r---------\n" if $debug;;
61         return $r;
62 }
63
64 sub 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
82 1;
83 __END__
84
85 =head1 NAME
86
87 Text::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);
94         print fill($initial_tab, $subsequent_tab, @text);
95
96         use Text::Wrap qw(wrap $columns $huge);
97
98         $columns = 132;
99         $huge = 'die';
100         $huge = 'wrap';
101
102 =head1 DESCRIPTION
103
104 Text::Wrap::wrap() is a very simple paragraph formatter.  It formats a
105 single paragraph at a time by breaking lines at word boundries.
106 Indentation is controlled for the first line ($initial_tab) and
107 all subsquent lines ($subsequent_tab) independently.  
108
109 Lines are wrapped at $Text::Wrap::columns columns.  
110 $Text::Wrap::columns should be set to the full width of your output device.
111
112 When words that are longer than $columns are encountered, they
113 are broken up.  Previous versions of wrap() die()ed instead.
114 To restore the old (dying) behavior, set $Text::Wrap::huge to
115 'die'.
116
117 Text::Wrap::fill() is a simple multi-paragraph formatter.  It formats
118 each paragraph separately and then joins them together when it's done.  It
119 will destory any whitespace in the original text.  It breaks text into
120 paragraphs by looking for whitespace after a newline.  In other respects
121 it acts like wrap().
122
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
130 David Muir Sharnoff <muir@idiom.com> with help from Tim Pierce and
131 many many others.  
132