Debian Codegraph @

reflow

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/local/bin/perl -w
2 # Version 7.0
3 # Original script written by Michael Larsen, [email protected]
4 # Modified by Martin Ward, [email protected]
5 # Copyright 1994 Michael Larsen and Martin Ward
6 # Email: [email protected]
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1335 USA
21 #
22 # Usage: reflow [-skipto stuff] [-fast|-slow|-veryslow] [-skipindented] src>out
23 #
24 # Uses Knuth's paragraphing algorithm (as used in TeX) to "reflow"
25 # text files by picking "good" places to break the line.
26 #
27 # It takes an ascii text file (with paragraphs separated by blank lines)
28 # and reflows the paragraphs. If two or more lines in a row are "indented"
29 # then they are assumed to be a quoted poem and are passed through unchanged.
30 # (The -skipindented causes ALL indented lines to be passed unchanged)
31 #
32 # The "reflow" operation tries to keep the lines the same length but also
33 # tries to break at punctuation, and avoid breaking within a proper name
34 # or after certain "connectives" ("a", "the", etc.).
35 #
36 # The "-veryslow" option reflows each paragraph 16 times with different
37 # optimal line widths and picks the "best" result -- with this option
38 # the paragraphs are easier to read, but the line width may vary from
39 # one paragraph to the next.
40 #
41 #
42 # Recent version history:
43 # 5.1: Fixed a bug with hyphens at end of lines
44 # (caught by check-punct!)
45 # 5.2: Avoid turning ". . ." into ". . ."
46 # 5.3: Added an option "-skipto stuff"
47 # which skips lines up to and including a line starting with "stuff"
48 # 5.4: Added an option "-skipindented" which treats ALL indented lines
49 # as "poetry" (i.e. not reflow-ed), rather than requiring two
50 # indented lines.
51 # 5.5: Fixed a bug when a paragraph starts with "--"
52 # 5.6: Tweaked some parameters, and modified to break before rather
53 # than after a word which starts with an opening parenthesis
54 # 5.7: Delete spaces at the end of lines in output.
55 # 5.8: Added an option "-slow" which tries a list of "optimum" line lengths,
56 # picking the one which gives the least total penalties.
57 # 5.9: Tweaked the parameters a bit more!
58 # 6.0: Added a "-veryslow" option which uses a LONG list of "optimum" lengths,
59 # -veryslow is about 7 times slower than with no options (or -fast)
60 # 6.1: Added a penalty for paragraphs with 1 or 2 words on the last line.
61 # 6.2: Fixed a bug in calculating $bestsofar.
62 # 6.3: Modified to work with perl5beta1 (gaining a 20% speed improvement).
63 # 6.4: Added "-w" and fixed warnings and a bug with v long first word in para.
64 # 6.5: Fix 5.1 could leave @linewords empty
65 # -- check for hyphen at end of line before checking @linewords
66 # 6.6: Trim spaces from the ends of lines before printing.
67 # 6.7: Tweaked "optimum" parameters and reduced maximum to 75
68 # 6.8: Speed optimisations, including converting all penalties to integers
69 # -- now 37% faster on -veryslow
70 # 7.0: Converted to a module with an XSUB for reflow_trial
71 # -- Vast speed improvement!
72
73 use Text::Reflow (reflow_file);
74 use strict;
75
76 my @slow_optimum = (65, 70, 60); # List of best line lengths for -slow option
77 my @veryslow_optimum = (60..70); # ditto for -veryslow option
78 my @fast_optimum = (65); # The default
79
80 my $Usage = "reflow [-fast|-slow|-veryslow] [-skipto patt] [-skipindented] ...\n";
81
82 $| = 1; # unbuffer output
83 my @opts = ();
84
85 # Check for options -skipindented, -fast, -slow, -veryslow for backwards compatibility:
86 while (($#ARGV >= 0) && ($ARGV[0] =~ /^-/)) {
87 if ($ARGV[0] eq "-skipindented") {
88 shift;
89 if (@ARGV && ($ARGV[0] =~ /^[012]$/)) {
90 push(@opts, "skipindented", shift);
91 } else {
92 push(@opts, "skipindented", 1);
93 }
94 } elsif ($ARGV[0] eq "-fast") {
95 shift;
96 push(@opts, "optimum", \@fast_optimum);
97 } elsif ($ARGV[0] eq "-slow") {
98 shift;
99 push(@opts, "optimum", \@slow_optimum);
100 } elsif ($ARGV[0] eq "-veryslow") {
101 shift;
102 push(@opts, "optimum", \@veryslow_optimum);
103 } else {
104 my $key = shift;
105 $key =~ s/^-//;
106 my $value = shift;
107 push(@opts, $key, $value);
108 }
109 }
110
111 push(@ARGV, "") unless (@ARGV); # Read STDIN if no file given
112
113 foreach my $file (@ARGV) {
114 reflow_file($file, "", @opts);
115 }
116
117
118 __END__
119
120 =head1 NAME
121
122 reflow - Perl script for reflowing text files using Knuth's
123 paragraphing algorithm.
124
125 =head1 SYNOPSIS
126
127 reflow < input > output
128
129 reflow file1 file2 ... > output
130
131 =head1 DESCRIPTION
132
133 Uses Knuth's paragraphing algorithm (as used in TeX) to "reflow"
134 text files by picking "good" places to break the line.
135
136 It takes an ascii text file (with paragraphs separated by blank lines)
137 and reflows the paragraphs. If two or more lines in a row are "indented"
138 then they are assumed to be a quoted poem and are passed through unchanged.
139 (The -skipindented causes ALL indented lines to be passed unchanged)
140
141 The algorithm tries to keep the lines the same length but also
142 tries to break at punctuation, and avoid breaking within a proper name
143 or after certain "connectives" ("a", "the", etc.).
144
145 =head2 OPTIONS
146
147 =over 5
148
149 =item B<-skipindented>
150
151 Cause ALL indented lines to be passed unchanged. Normally a single indented
152 line is included in the paragraph, while two or more indented lines
153 are considered to be a quotation or poem and are passed through unchanged.
154
155 =item B<-fast>
156
157 Sets the optimum line width to 65 characters. This is the default.
158
159 =item B<-slow>
160
161 Tries line widths of 60, 65 and 70 characters and picks the best one.
162
163 =item B<-veryslow>
164
165 Tries all line widths between 60 and 70 characters inclusive,
166 and picks the best one. With the B<-slow> and B<-veryslow> options
167 the line widths within a paragraph will vary less,
168 but the line widths may vary from one paragraph to the next.
169
170 =item B<-skipto pattern>
171
172 Skip to the first line starting with the given pattern before starting
173 to reflow. This is useful for skipping Project Gutenberg headers
174 or contents tables.
175
176 See Text::Reflow(3) for more options: all the keyword options
177 defined in Text::Reflow are also available via the reflow script.
178
179 =back
180
181 =head1 AUTHOR
182
183 Original C<reflow> perl script written by Michael Larsen, [email protected].
184
185 Modified, enhanced and converted to a perl module with XSUB
186 by Martin Ward, [email protected]
187
188 =head1 SEE ALSO
189
190 Text::Reflow(3).
191
192 perl(1).
193
194 See "TeX the Program" by Donald Knuth for a description of the algorithm used.
195
196 =cut