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