Commit | Line | Data |
4633a7c4 |
1 | package Text::Wrap; |
2 | |
9a09eeb5 |
3 | require Exporter; |
4633a7c4 |
4 | |
0c5a43b5 |
5 | @ISA = qw(Exporter); |
6 | @EXPORT = qw(wrap fill); |
7 | @EXPORT_OK = qw($columns $break $huge); |
4633a7c4 |
8 | |
f93bb41e |
9 | $VERSION = 2001.09291; |
b1a524cb |
10 | |
37a581db |
11 | use vars qw($VERSION $columns $debug $break $huge $unexpand $tabstop |
12 | $separator); |
9a09eeb5 |
13 | use strict; |
b1a524cb |
14 | |
4633a7c4 |
15 | BEGIN { |
9a09eeb5 |
16 | $columns = 76; # <= screen width |
17 | $debug = 0; |
18 | $break = '\s'; |
3e2ea5df |
19 | $huge = 'wrap'; # alternatively: 'die' or 'overflow' |
37a581db |
20 | $unexpand = 1; |
21 | $tabstop = 8; |
22 | $separator = "\n"; |
4633a7c4 |
23 | } |
24 | |
9a09eeb5 |
25 | use Text::Tabs qw(expand unexpand); |
26 | |
4633a7c4 |
27 | sub wrap |
28 | { |
9a09eeb5 |
29 | my ($ip, $xp, @t) = @_; |
30 | |
37a581db |
31 | local($Text::Tabs::tabstop) = $tabstop; |
9a09eeb5 |
32 | my $r = ""; |
3e2ea5df |
33 | my $tail = pop(@t); |
37a581db |
34 | my $t = expand(join("", (map { /\s+\z/ ? ( $_ ) : ($_, ' ') } @t), $tail)); |
9a09eeb5 |
35 | my $lead = $ip; |
36 | my $ll = $columns - length(expand($ip)) - 1; |
718842b0 |
37 | $ll = 0 if $ll < 0; |
9a09eeb5 |
38 | my $nll = $columns - length(expand($xp)) - 1; |
39 | my $nl = ""; |
40 | my $remainder = ""; |
41 | |
37a581db |
42 | use re 'taint'; |
43 | |
3e2ea5df |
44 | pos($t) = 0; |
45 | while ($t !~ /\G\s*\Z/gc) { |
37a581db |
46 | if ($t =~ /\G([^\n]{0,$ll})($break|\z)/xmgc) { |
47 | $r .= $unexpand |
48 | ? unexpand($nl . $lead . $1) |
49 | : $nl . $lead . $1; |
9a09eeb5 |
50 | $remainder = $2; |
3e2ea5df |
51 | } elsif ($huge eq 'wrap' && $t =~ /\G([^\n]{$ll})/gc) { |
37a581db |
52 | $r .= $unexpand |
53 | ? unexpand($nl . $lead . $1) |
54 | : $nl . $lead . $1; |
55 | $remainder = $separator; |
56 | } elsif ($huge eq 'overflow' && $t =~ /\G([^\n]*?)($break|\z)/xmgc) { |
57 | $r .= $unexpand |
58 | ? unexpand($nl . $lead . $1) |
59 | : $nl . $lead . $1; |
3e2ea5df |
60 | $remainder = $2; |
9a09eeb5 |
61 | } elsif ($huge eq 'die') { |
62 | die "couldn't wrap '$t'"; |
63 | } else { |
64 | die "This shouldn't happen"; |
65 | } |
66 | |
67 | $lead = $xp; |
68 | $ll = $nll; |
37a581db |
69 | $nl = $separator; |
9b599b2a |
70 | } |
9a09eeb5 |
71 | $r .= $remainder; |
b1a524cb |
72 | |
9a09eeb5 |
73 | print "-----------$r---------\n" if $debug; |
b1a524cb |
74 | |
3e2ea5df |
75 | print "Finish up with '$lead'\n" if $debug; |
b1a524cb |
76 | |
3e2ea5df |
77 | $r .= $lead . substr($t, pos($t), length($t)-pos($t)) |
78 | if pos($t) ne length($t); |
b1a524cb |
79 | |
9a09eeb5 |
80 | print "-----------$r---------\n" if $debug;; |
3e2ea5df |
81 | |
9a09eeb5 |
82 | return $r; |
4633a7c4 |
83 | } |
84 | |
0c5a43b5 |
85 | sub fill |
86 | { |
87 | my ($ip, $xp, @raw) = @_; |
88 | my @para; |
89 | my $pp; |
90 | |
91 | for $pp (split(/\n\s+/, join("\n",@raw))) { |
92 | $pp =~ s/\s+/ /g; |
93 | my $x = wrap($ip, $xp, $pp); |
94 | push(@para, $x); |
95 | } |
96 | |
97 | # if paragraph_indent is the same as line_indent, |
98 | # separate paragraphs with blank lines |
99 | |
3e2ea5df |
100 | my $ps = ($ip eq $xp) ? "\n\n" : "\n"; |
101 | return join ($ps, @para); |
0c5a43b5 |
102 | } |
103 | |
4633a7c4 |
104 | 1; |
68e56a55 |
105 | __END__ |
b1a524cb |
106 | |
107 | =head1 NAME |
108 | |
109 | Text::Wrap - line wrapping to form simple paragraphs |
110 | |
111 | =head1 SYNOPSIS |
112 | |
3e2ea5df |
113 | B<Example 1> |
114 | |
b1a524cb |
115 | use Text::Wrap |
116 | |
3e2ea5df |
117 | $initial_tab = "\t"; # Tab before first line |
118 | $subsequent_tab = ""; # All other lines flush left |
119 | |
b1a524cb |
120 | print wrap($initial_tab, $subsequent_tab, @text); |
0c5a43b5 |
121 | print fill($initial_tab, $subsequent_tab, @text); |
b1a524cb |
122 | |
7b614c55 |
123 | $lines = wrap($initial_tab, $subsequent_tab, @text); |
3e2ea5df |
124 | |
125 | @paragraphs = fill($initial_tab, $subsequent_tab, @text); |
126 | |
127 | B<Example 2> |
128 | |
9a09eeb5 |
129 | use Text::Wrap qw(wrap $columns $huge); |
b1a524cb |
130 | |
3e2ea5df |
131 | $columns = 132; # Wrap at 132 characters |
9a09eeb5 |
132 | $huge = 'die'; |
133 | $huge = 'wrap'; |
3e2ea5df |
134 | $huge = 'overflow'; |
b1a524cb |
135 | |
3e2ea5df |
136 | B<Example 3> |
bbc7dcd2 |
137 | |
3e2ea5df |
138 | use Text::Wrap |
b1a524cb |
139 | |
3e2ea5df |
140 | $Text::Wrap::columns = 72; |
141 | print wrap('', '', @text); |
9a09eeb5 |
142 | |
3e2ea5df |
143 | =head1 DESCRIPTION |
9a09eeb5 |
144 | |
37a581db |
145 | C<Text::Wrap::wrap()> is a very simple paragraph formatter. It formats a |
3e2ea5df |
146 | single paragraph at a time by breaking lines at word boundries. |
147 | Indentation is controlled for the first line (C<$initial_tab>) and |
718842b0 |
148 | all subsequent lines (C<$subsequent_tab>) independently. Please note: |
3e2ea5df |
149 | C<$initial_tab> and C<$subsequent_tab> are the literal strings that will |
150 | be used: it is unlikley you would want to pass in a number. |
151 | |
37a581db |
152 | Text::Wrap::fill() is a simple multi-paragraph formatter. It formats |
153 | each paragraph separately and then joins them together when it's done. It |
154 | will destory any whitespace in the original text. It breaks text into |
155 | paragraphs by looking for whitespace after a newline. In other respects |
156 | it acts like wrap(). |
157 | |
158 | =head1 OVERRIDES |
159 | |
160 | C<Text::Wrap::wrap()> has a number of variables that control its behavior. |
161 | Because other modules might be using C<Text::Wrap::wrap()> it is suggested |
162 | that you leave these variables alone! If you can't do that, then |
163 | use C<local($Text::Wrap::VARIABLE) = YOURVALUE> when you change the |
164 | values so that the original value is restored. This C<local()> trick |
165 | will not work if you import the variable into your own namespace. |
166 | |
3e2ea5df |
167 | Lines are wrapped at C<$Text::Wrap::columns> columns. C<$Text::Wrap::columns> |
168 | should be set to the full width of your output device. In fact, |
169 | every resulting line will have length of no more than C<$columns - 1>. |
170 | |
37a581db |
171 | It is possible to control which characters terminate words by |
172 | modifying C<$Text::Wrap::break>. Set this to a string such as |
173 | C<'[\s:]'> (to break before spaces or colons) or a pre-compiled regexp |
174 | such as C<qr/[\s']/> (to break before spaces or apostrophes). The |
175 | default is simply C<'\s'>; that is, words are terminated by spaces. |
176 | (This means, among other things, that trailing punctuation such as |
177 | full stops or commas stay with the word they are "attached" to.) |
178 | |
3e2ea5df |
179 | Beginner note: In example 2, above C<$columns> is imported into |
180 | the local namespace, and set locally. In example 3, |
181 | C<$Text::Wrap::columns> is set in its own namespace without importing it. |
182 | |
37a581db |
183 | C<Text::Wrap::wrap()> starts its work by expanding all the tabs in its |
184 | input into spaces. The last thing it does it to turn spaces back |
185 | into tabs. If you do not want tabs in your results, set |
186 | C<$Text::Wrap::unexapand> to a false value. Likewise if you do not |
187 | want to use 8-character tabstops, set C<$Text::Wrap::tabstop> to |
188 | the number of characters you do want for your tabstops. |
189 | |
190 | If you want to separate your lines with something other than C<\n> |
191 | then set C<$Text::Wrap::seporator> to your preference. |
192 | |
3e2ea5df |
193 | When words that are longer than C<$columns> are encountered, they |
194 | are broken up. C<wrap()> adds a C<"\n"> at column C<$columns>. |
195 | This behavior can be overridden by setting C<$huge> to |
196 | 'die' or to 'overflow'. When set to 'die', large words will cause |
197 | C<die()> to be called. When set to 'overflow', large words will be |
198 | left intact. |
9b599b2a |
199 | |
37a581db |
200 | Historical notes: 'die' used to be the default value of |
3e2ea5df |
201 | C<$huge>. Now, 'wrap' is the default value. |
202 | |
b1a524cb |
203 | =head1 EXAMPLE |
204 | |
205 | print wrap("\t","","This is a bit of text that forms |
206 | a normal book-style paragraph"); |
207 | |
208 | =head1 AUTHOR |
209 | |
4fc6b8d8 |
210 | David Muir Sharnoff <muir@idiom.com> with help from Tim Pierce and |
0c5a43b5 |
211 | many many others. |
b1a524cb |
212 | |