0fe7fb93c21557e0dde6483e6133543e40ff4fe3
[p5sagit/p5-mst-13.2.git] / lib / Text / Wrap.pm
1 package Text::Wrap;
2
3 use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION $columns $debug);
4 use strict;
5 use Exporter;
6
7 $VERSION = "97.02";
8 @ISA = qw(Exporter);
9 @EXPORT = qw(wrap);
10 @EXPORT_OK = qw($columns $tabstop fill);
11
12 use Text::Tabs qw(expand unexpand $tabstop);
13
14
15 BEGIN   {
16     $columns = 76;  # <= screen width
17     $debug = 0;
18 }
19
20 sub wrap
21 {
22     my ($ip, $xp, @t) = @_;
23
24     my @rv;
25     my $t = expand(join(" ",@t));
26
27     my $lead = $ip;
28     my $ll = $columns - length(expand($lead)) - 1;
29     my $nl = "";
30
31     $t =~ s/^\s+//;
32     while(length($t) > $ll) {
33         # remove up to a line length of things that
34         # aren't new lines and tabs.
35         if ($t =~ s/^([^\n]{0,$ll})(\s|\Z(?!\n))//) {
36             my ($l,$r) = ($1,$2);
37             $l =~ s/\s+$//;
38             print "WRAP  $lead$l..($r)\n" if $debug;
39             push @rv, unexpand($lead . $l), "\n";
40                 
41         } elsif ($t =~ s/^([^\n]{$ll})//) {
42             print "SPLIT $lead$1..\n" if $debug;
43             push @rv, unexpand($lead . $1),"\n";
44         }
45         # recompute the leader
46         $lead = $xp;
47         $ll = $columns - length(expand($lead)) - 1;
48         $t =~ s/^\s+//;
49     } 
50     print "TAIL  $lead$t\n" if $debug;
51     push @rv, $lead.$t if $t ne "";
52     return join '', @rv;
53 }
54
55
56 sub fill 
57 {
58         my ($ip, $xp, @raw) = @_;
59         my @para;
60         my $pp;
61
62         for $pp (split(/\n\s+/, join("\n",@raw))) {
63                 $pp =~ s/\s+/ /g;
64                 my $x = wrap($ip, $xp, $pp);
65                 push(@para, $x);
66         }
67
68         # if paragraph_indent is the same as line_indent, 
69         # separate paragraphs with blank lines
70
71         return join ($ip eq $xp ? "\n\n" : "\n", @para);
72 }
73
74 1;
75 __END__
76
77 =head1 NAME
78
79 Text::Wrap - line wrapping to form simple paragraphs
80
81 =head1 SYNOPSIS 
82
83         use Text::Wrap
84
85         print wrap($initial_tab, $subsequent_tab, @text);
86
87         use Text::Wrap qw(wrap $columns $tabstop fill);
88
89         $columns = 132;
90         $tabstop = 4;
91
92         print fill($initial_tab, $subsequent_tab, @text);
93         print fill("", "", `cat book`);
94
95 =head1 DESCRIPTION
96
97 Text::Wrap::wrap() is a very simple paragraph formatter.  It formats a
98 single paragraph at a time by breaking lines at word boundries.
99 Indentation is controlled for the first line ($initial_tab) and
100 all subsquent lines ($subsequent_tab) independently.  $Text::Wrap::columns
101 should be set to the full width of your output device.
102
103 Text::Wrap::fill() is a simple multi-paragraph formatter.  It formats
104 each paragraph separately and then joins them together when it's done.  It
105 will destory any whitespace in the original text.  It breaks text into
106 paragraphs by looking for whitespace after a newline.  In other respects
107 it acts like wrap().
108
109 =head1 EXAMPLE
110
111         print wrap("\t","","This is a bit of text that forms 
112                 a normal book-style paragraph");
113
114 =head1 BUGS
115
116 It's not clear what the correct behavior should be when Wrap() is
117 presented with a word that is longer than a line.  The previous 
118 behavior was to die.  Now the word is now split at line-length.
119
120 =head1 AUTHOR
121
122 David Muir Sharnoff <muir@idiom.com> with help from Tim Pierce and
123 others. Updated by Jacqui Caren.
124
125 =cut