Commit | Line | Data |
4633a7c4 |
1 | package Text::Wrap; |
2 | |
4633a7c4 |
3 | require Exporter; |
4 | |
5 | @ISA = (Exporter); |
6 | @EXPORT = qw(wrap); |
7 | @EXPORT_OK = qw($columns); |
8 | |
b1a524cb |
9 | $VERSION = 96.041801; |
10 | |
11 | use vars qw($VERSION $columns $debug); |
12 | use strict; |
13 | |
4633a7c4 |
14 | BEGIN { |
b1a524cb |
15 | $columns = 76; # <= screen width |
16 | $debug = 0; |
4633a7c4 |
17 | } |
18 | |
19 | use Text::Tabs; |
20 | use strict; |
21 | |
22 | sub wrap |
23 | { |
24 | my ($ip, $xp, @t) = @_; |
25 | |
b1a524cb |
26 | my $r = ""; |
4633a7c4 |
27 | my $t = expand(join(" ",@t)); |
28 | my $lead = $ip; |
b1a524cb |
29 | my $ll = $columns - length(expand($lead)) - 1; |
30 | my $nl = ""; |
31 | |
32 | # remove up to a line length of things that aren't |
33 | # new lines and tabs. |
34 | |
35 | if ($t =~ s/^([^\n]{0,$ll})(\s|\Z(?!\n))//xm) { |
36 | |
37 | # accept it. |
38 | $r .= unexpand($lead . $1); |
39 | |
40 | # recompute the leader |
4633a7c4 |
41 | $lead = $xp; |
b1a524cb |
42 | $ll = $columns - length(expand($lead)) - 1; |
43 | $nl = $2; |
44 | |
45 | # repeat the above until there's none left |
46 | while ($t and $t =~ s/^([^\n]{0,$ll})(\s|\Z(?!\n))//xm) { |
47 | print "\$2 is '$2'\n" if $debug; |
48 | $nl = $2; |
49 | $r .= unexpand("\n" . $lead . $1); |
4633a7c4 |
50 | } |
b1a524cb |
51 | $r .= $nl; |
4633a7c4 |
52 | } |
b1a524cb |
53 | |
4633a7c4 |
54 | die "couldn't wrap '$t'" |
55 | if length($t) > $ll; |
b1a524cb |
56 | |
57 | print "-----------$r---------\n" if $debug; |
58 | |
59 | print "Finish up with '$lead', '$t'\n" if $debug; |
60 | |
61 | $r .= $lead . $t if $t ne ""; |
62 | |
63 | print "-----------$r---------\n" if $debug;; |
4633a7c4 |
64 | return $r; |
65 | } |
66 | |
67 | 1; |
b1a524cb |
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 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> |
100 | |
101 | =cut |