Commit | Line | Data |
4633a7c4 |
1 | package Text::Wrap; |
2 | |
9b599b2a |
3 | use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION $columns $debug); |
4 | use strict; |
5 | use Exporter; |
4633a7c4 |
6 | |
9b599b2a |
7 | $VERSION = "97.02"; |
8 | @ISA = qw(Exporter); |
4633a7c4 |
9 | @EXPORT = qw(wrap); |
9b599b2a |
10 | @EXPORT_OK = qw($columns $tabstop fill); |
4633a7c4 |
11 | |
9b599b2a |
12 | use Text::Tabs qw(expand unexpand $tabstop); |
b1a524cb |
13 | |
b1a524cb |
14 | |
4633a7c4 |
15 | BEGIN { |
9b599b2a |
16 | $columns = 76; # <= screen width |
17 | $debug = 0; |
4633a7c4 |
18 | } |
19 | |
4633a7c4 |
20 | sub wrap |
21 | { |
9b599b2a |
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 | } |
b1a524cb |
54 | |
b1a524cb |
55 | |
9b599b2a |
56 | sub fill |
57 | { |
58 | my ($ip, $xp, @raw) = @_; |
59 | my @para; |
60 | my $pp; |
b1a524cb |
61 | |
9b599b2a |
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 | } |
b1a524cb |
67 | |
9b599b2a |
68 | # if paragraph_indent is the same as line_indent, |
69 | # separate paragraphs with blank lines |
b1a524cb |
70 | |
9b599b2a |
71 | return join ($ip eq $xp ? "\n\n" : "\n", @para); |
4633a7c4 |
72 | } |
73 | |
74 | 1; |
68e56a55 |
75 | __END__ |
b1a524cb |
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 | |
9b599b2a |
87 | use Text::Wrap qw(wrap $columns $tabstop fill); |
b1a524cb |
88 | |
89 | $columns = 132; |
9b599b2a |
90 | $tabstop = 4; |
91 | |
92 | print fill($initial_tab, $subsequent_tab, @text); |
93 | print fill("", "", `cat book`); |
b1a524cb |
94 | |
95 | =head1 DESCRIPTION |
96 | |
4fc6b8d8 |
97 | Text::Wrap::wrap() is a very simple paragraph formatter. It formats a |
b1a524cb |
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 | |
9b599b2a |
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 | |
b1a524cb |
109 | =head1 EXAMPLE |
110 | |
111 | print wrap("\t","","This is a bit of text that forms |
112 | a normal book-style paragraph"); |
113 | |
68e56a55 |
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 |
9b599b2a |
118 | behavior was to die. Now the word is now split at line-length. |
68e56a55 |
119 | |
b1a524cb |
120 | =head1 AUTHOR |
121 | |
4fc6b8d8 |
122 | David Muir Sharnoff <muir@idiom.com> with help from Tim Pierce and |
9b599b2a |
123 | others. Updated by Jacqui Caren. |
b1a524cb |
124 | |
125 | =cut |