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 | |
be3174d2 |
9 | $VERSION = 2000.06292219; #GMT |
b1a524cb |
10 | |
9a09eeb5 |
11 | use vars qw($VERSION $columns $debug $break $huge); |
12 | use strict; |
b1a524cb |
13 | |
4633a7c4 |
14 | BEGIN { |
9a09eeb5 |
15 | $columns = 76; # <= screen width |
16 | $debug = 0; |
17 | $break = '\s'; |
18 | $huge = 'wrap'; # alternatively: 'die' |
4633a7c4 |
19 | } |
20 | |
9a09eeb5 |
21 | use Text::Tabs qw(expand unexpand); |
22 | |
4633a7c4 |
23 | sub wrap |
24 | { |
9a09eeb5 |
25 | my ($ip, $xp, @t) = @_; |
26 | |
27 | my $r = ""; |
0110aa01 |
28 | |
29 | my $t = _linearize(@t); |
30 | |
9a09eeb5 |
31 | my $lead = $ip; |
32 | my $ll = $columns - length(expand($ip)) - 1; |
33 | my $nll = $columns - length(expand($xp)) - 1; |
34 | my $nl = ""; |
35 | my $remainder = ""; |
36 | |
37 | while ($t !~ /^\s*$/) { |
be3174d2 |
38 | if ($t =~ s/^([^\n]{0,$ll})($break|\Z(?!\n))//x) { |
9a09eeb5 |
39 | $r .= unexpand($nl . $lead . $1); |
40 | $remainder = $2; |
41 | } elsif ($huge eq 'wrap' && $t =~ s/^([^\n]{$ll})//) { |
42 | $r .= unexpand($nl . $lead . $1); |
43 | $remainder = "\n"; |
44 | } elsif ($huge eq 'die') { |
45 | die "couldn't wrap '$t'"; |
46 | } else { |
47 | die "This shouldn't happen"; |
48 | } |
49 | |
50 | $lead = $xp; |
51 | $ll = $nll; |
52 | $nl = "\n"; |
9b599b2a |
53 | } |
9a09eeb5 |
54 | $r .= $remainder; |
b1a524cb |
55 | |
9a09eeb5 |
56 | print "-----------$r---------\n" if $debug; |
b1a524cb |
57 | |
9a09eeb5 |
58 | print "Finish up with '$lead', '$t'\n" if $debug; |
b1a524cb |
59 | |
9a09eeb5 |
60 | $r .= $lead . $t if $t ne ""; |
b1a524cb |
61 | |
9a09eeb5 |
62 | print "-----------$r---------\n" if $debug;; |
63 | return $r; |
4633a7c4 |
64 | } |
65 | |
0110aa01 |
66 | sub _linearize { |
67 | my @lines = expand(@_); |
68 | |
69 | # Join the lines together, adding in extra whitespace only where needed |
70 | # to keep words seperated. |
71 | my $text = join "", map { /\s+\Z/ ? $_ : $_.' ' } @lines[0..$#lines-1]; |
72 | $text .= $lines[-1]; |
73 | |
74 | return $text; |
75 | } |
76 | |
77 | |
0c5a43b5 |
78 | sub fill |
79 | { |
80 | my ($ip, $xp, @raw) = @_; |
81 | my @para; |
82 | my $pp; |
83 | |
84 | for $pp (split(/\n\s+/, join("\n",@raw))) { |
85 | $pp =~ s/\s+/ /g; |
86 | my $x = wrap($ip, $xp, $pp); |
87 | push(@para, $x); |
88 | } |
89 | |
90 | # if paragraph_indent is the same as line_indent, |
91 | # separate paragraphs with blank lines |
92 | |
93 | return join ($ip eq $xp ? "\n\n" : "\n", @para); |
94 | } |
95 | |
4633a7c4 |
96 | 1; |
68e56a55 |
97 | __END__ |
b1a524cb |
98 | |
99 | =head1 NAME |
100 | |
101 | Text::Wrap - line wrapping to form simple paragraphs |
102 | |
103 | =head1 SYNOPSIS |
104 | |
105 | use Text::Wrap |
106 | |
107 | print wrap($initial_tab, $subsequent_tab, @text); |
0c5a43b5 |
108 | print fill($initial_tab, $subsequent_tab, @text); |
b1a524cb |
109 | |
9a09eeb5 |
110 | use Text::Wrap qw(wrap $columns $huge); |
b1a524cb |
111 | |
112 | $columns = 132; |
9a09eeb5 |
113 | $huge = 'die'; |
114 | $huge = 'wrap'; |
b1a524cb |
115 | |
116 | =head1 DESCRIPTION |
117 | |
4fc6b8d8 |
118 | Text::Wrap::wrap() is a very simple paragraph formatter. It formats a |
8dcee03e |
119 | single paragraph at a time by breaking lines at word boundaries. |
b1a524cb |
120 | Indentation is controlled for the first line ($initial_tab) and |
8dcee03e |
121 | all subsequent lines ($subsequent_tab) independently. |
9a09eeb5 |
122 | |
123 | Lines are wrapped at $Text::Wrap::columns columns. |
124 | $Text::Wrap::columns should be set to the full width of your output device. |
125 | |
126 | When words that are longer than $columns are encountered, they |
127 | are broken up. Previous versions of wrap() die()ed instead. |
128 | To restore the old (dying) behavior, set $Text::Wrap::huge to |
129 | 'die'. |
9b599b2a |
130 | |
0c5a43b5 |
131 | Text::Wrap::fill() is a simple multi-paragraph formatter. It formats |
132 | each paragraph separately and then joins them together when it's done. It |
8dcee03e |
133 | will destroy any whitespace in the original text. It breaks text into |
0c5a43b5 |
134 | paragraphs by looking for whitespace after a newline. In other respects |
135 | it acts like wrap(). |
136 | |
b1a524cb |
137 | =head1 EXAMPLE |
138 | |
139 | print wrap("\t","","This is a bit of text that forms |
140 | a normal book-style paragraph"); |
141 | |
142 | =head1 AUTHOR |
143 | |
4fc6b8d8 |
144 | David Muir Sharnoff <muir@idiom.com> with help from Tim Pierce and |
0c5a43b5 |
145 | many many others. |
b1a524cb |
146 | |