/usr/bin/reformat is in reformat 20040319-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | #!/usr/bin/perl -WT
# (C) Stephan Beyer, GPL
# reformat -h or see the pod (at the bottom of the file, or 'man reformat')
# default options
# Note: booleans are true if ($boolean % 2)
$width = 72; # -w
$justify = 0; # -j (boolean)
$lmargin = ""; # -l
$allpar = 0; # -p (boolean)
# default stuff
$version = "20040319";
$newline = 0;
$lines = 0;
# options
for ($i = 0; $i < @ARGV; $i++) {
$ARGV[$i] = $ARGV[$i];
if ($ARGV[$i] eq
"-j") {
$justify++;
} elsif ($ARGV[$i] eq
"-l") {
$i++;
$lmargin = "";
for (my $tmp = 0; $tmp < int($ARGV[$i]); $tmp++) {
$lmargin .= " ";
}
} elsif ($ARGV[$i] eq
"-p") {
$allpar++;
} elsif ($ARGV[$i] eq
"-w") {
$i++;
$width = int($ARGV[$i]);
} else {
if ($ARGV[$i] ne "-h") {
print STDERR "Unknown option: ".$ARGV[$i]."\n";
}
print <<STOP
reformat $version by sbeyer
(C) Stephan Beyer <s-beyer\@gmx.net>, GPL
http://www.s-beyer.de/
Usage: $0 [options] [<inputfile] [>outputfile]
"reformat" only handles with stdin and stdout (and sometimes stderr).
Pipe as much as you want :)
Options (alphabetically):
-h Displays this help.
-j Switches hyphenless justification. [$justify]
-l margin Sets a left margin (numeric). [$lmargin]
-p Also reformat lines beginning with a whitespace. [$allpar]
-w width Sets a width (numeric) for the text. [$width]
Have fun!
STOP
;
if ($ARGV[$i] eq "-h") {
exit(0);
} else {
exit(1);
}
}
}
# Usage: prepare (STRING) -> prepares a string (see source)
sub prepare {
my $ret = $_[0];
$ret =~ s/[\r\n\t]/ /g;
$ret =~ s/ +/ /g;
$ret =~ s/^ //g;
$ret =~ s/ $//g;
return $ret;
}
# Usage: round (FLOAT) -> rounded to integer
sub round {
my $ret;
if ($_[0] - int($_[0]) >= 0.5) {
$ret = int($_[0]) + 1;
} else {
$ret = int($_[0]);
}
return $ret;
}
# Usage: splen (STRING) -> returns length of STRING + 1 (added space)
sub splen {
return length($_[0])+1;
}
# Usage: mindiff (INTEGER, ARRAY OF INTEGERS)
# -> returns integer value (of the array) of
# smallest absolute difference betwwen INTEGER and the ARRAY
sub mindiff {
my $num = $_[0];
shift(@_);
my $min = -1;
my $ret;
for(@_) {
if ((abs($_-$num) < $min) or ($min < 0)) {
$min = abs($_-$num);
$ret = $_;
}
}
return $ret;
}
# Usage: newline; -> registers a new line
sub newline {
print "\n";
$newline = 0;
$lines++;
}
# almost MAIN:
# Usage: reformat (STRING) -> reformat string
sub reformat {
my $str = $_[0];
my @words = split(/ /, prepare($str));
my $wc = @words; # word count
my $lc = $width + 1; # length count
my $last = 0; # old $i, used later
for ($i = 0; $i <= $wc; $i++) {
if ($newline == 0) {
print $lmargin;
$newline = 1;
}
if ($i < $wc) {
# substract current word
$lc -= splen($words[$i]);
# print $i." : ".$words[$i]." : ".length($words[$i])." : ".$lc."\n";
# is our mission impossible?
if (length($words[$i]) > $width) {
print STDERR ("ERROR: word is too long to fit in width ($width).\n");
print STDERR ("(\"".$words[$i]."\")\n");
print STDERR ("Aborting.\n");
exit(2);
}
}
if ($lc < 0) {
# add the last substracted word
$lc += splen($words[$i]);
if ($justify % 2) {
# justify?
# j1. $lc is how many spaces we need to fill
# j2. now we should find <lc> landmarks
# LandMark Factor * {1..lc+1} = set of landmarks
$lmf = $width / ($lc + 1); #--> TODO not very correct because the length changes when adding a space
# find available spaces
$tmp = 0;
for ($ii = $last; $ii < $i; $ii++) {
$tmp += splen($words[$ii]);
push(@spaces, $tmp);
} pop(@spaces);
# print "DEBUG: ".@spaces." spaces available: @spaces\n";
# find landmarks
for ($ii = 1; $ii <= $lc+1; $ii++) {
$tmp = round($lmf*$ii);
push(@landmarks, mindiff($tmp, @spaces)); # positions of our landmark
} pop(@landmarks);
# print "DEBUG: $lc landmarks to set: @landmarks\n";
$tmp = 0;
$lmhc = 0; # landmark help counter
}
for ($ii = $last; $ii < $i; $ii++) {
print $words[$ii];
if ($justify % 2) {
$tmp += splen($words[$ii]);
while (($lmhc < @landmarks) and ($tmp == $landmarks[$lmhc])) {
$lmhc++;
print " ";
}
}
if ($ii < $i-1) {
print " ";
} else {
newline;
}
}
$last = $i;
$lc = $width - length($words[$i]);
}
# remove landmarks & spaces
if ($justify % 2) {
for(@landmarks) {
pop(@landmarks);
}
for(@spaces) {
pop(@spaces);
}
}
# end of paragraph - print as usual (word1 word2 word3.\n)
if ($i == $wc) {
for ($ii = $last; $ii < $i; $ii++) {
print $words[$ii];
if ($ii < $i-1) {
print " ";
} else {
newline;
}
}
}
}
}
# Usage: unformatted (STRING)
sub unformatted {
$_[0] =~ s/[\r\n]//;
print $_[0];
newline;
}
############
### MAIN ###
############
$cache = "";
while(<STDIN>) {
if ($allpar % 2) { # -p set
$_ =~ s/^[\t ]*//;
}
if ($_ =~ m/^[\r\n\t ]/) { # ignore those lines
# reformat cache
reformat($cache);
# print ignored line
unformatted($_);
# clear cache
$cache = "";
} else {
$cache .= $_;
}
}
reformat($cache);
exit(0);
=head1 NAME
reformat - tool to simple format plain ascii texts
=head1 SYNOPSIS
B<reformat> [B<-h>] [B<-j>] [B<-l> I<margin>] [B<-p>] [B<-w> I<width>]
=head1 DESCRIPTION
B<reformat> is a simple tool to reformat plain texts. reformat reads
from F<stdin> and writes to F<stdout>.
Available options are:
=over
=item B<-h>
prints usage information
=item B<-j>
switch justify mode: Each line of a paragraph will have the same width (see
B<-w> option). To reach this, spaces (' ') will be added between words.
Default: disabled
=item B<-l> I<left-margin>
Sets the left margin to I<left-margin>. The margin is produced by
I<left-margin> spaces (' '), no tabs will be used. Default: 0
=item B<-p>
Accept lines beginning with a whitespace as usual paragraphs, too.
=item B<-w> I<width>
Sets the paragraph width to I<width>. No reformatted line will be longer
than I<width> (plus defined margins) then. Default: 72
=back
=head1 LIMITATIONS
=over
=item
B<reformat> isn't an intelligent program. It just reads a whole paragraph
into a buffer and then reformats it. The end of a paragraph is indicated by
an empty line (may also contain spaces or tabs) or at a line beginning
with whitespaces (if you don't use B<-p> option).
Lines beginning with whitespaces are lines to keep untouched. Nothing happens
with them, unless you use B<-p> option (as just mentioned).
=item
B<reformat> doesn't look for hyphenation and hyphens at all. It won't
make new lines when reached a hyphen. B<reformat> works word-by-word.
=item
B<reformat> doesn't detect 'small paragraphs' (paragraphs without an
empty line).
=item
Check for errors! If B<reformat> detects a word with a length greater
than the specified width, it will abort.
=item
B<reformat> has problems with control characters. Some text
documents contain the B<^L> character (0x0c), for example.
=back
=head1 TODO
Planned features are:
=over
=item
Fix I<some> problems, see L</LIMITATIONS>.
=item
Add an option to declare a string that indicates "don't reformat" in
the text. Would be nice on reformatting emails, and don't touch the quoteas
('> '-lines).
=item
Add an option (e.g. B<-i>) to keep indenting.
=back
=head1 EXAMPLES
=over
=item reformat < foo > bar
Reads text from F<foo>, reformats and writes to F<bar>.
=item reformat -l 15 -j -w 50 < foo
Nice justified, centered text from file F<foo> on an 80x25 terminal.
=back
=head1 SEE ALSO
L<fold(1)>
=head1 AUTHOR AND COPYRIGHT
(C) Stephan Beyer E<lt>s-beyer@gmx.netE<gt>, 2003-2004, GPL
=cut
|