#!/bin/perl # # Usage: reform [-lNUM] [-rNUM] [-iNUM] [-sNUM] [files] # # Set default values for left margin, right margin, paragraph indent, # no. of lines between paragraphs, and spaces between sentences. # This script was taken from "Programming Perl," 1st edition, and has # been modified by Eric Pement. $l = 0; # Left margin $r = 0; # Right margin $i = 0; # Indentation of each new paragraph $n = 1; # No. of blank lines put between paragraphs $s = 1; # No. of spaces put between sentences # Process any switches. print() is used to send syntax message to stdout # instead of stderr, which is the default for die() messages. while ($ARGV[0] =~ /^-/) { $_ = shift; /^-(l|r|i|n|s)(\d+)/ && (eval "\$$1 = \$2", next); print "\nUnrecognized switch: $_\n\n", "usage: reform.pl [-lNUM] [-rNUM] [-iNUM] [-nNUM] [-sNUM] [files]\n\n", "Change defaults for Left margin (0), Right margin (70), Indentation\n", "for new paragraphs (0), Number of blank lines between paragraphs (1),\n", "and no. of Spaces allowed between sentences (1). Multiple spaces\n", "between words are reduced to 1.\n"; die "\n"; } # Calculate format strings. $r = $l + 70 unless $r; # default right margin is defined here $r -= $l; # make $r relative to $l die "Margins too close\n" if $l + $i >= $r; $LEFT = ' ' x $l; $INDENT = ' ' x $i; $RIGHT1 = '^' . '<' x ($r - 1 - $i); $RIGHT2 = '^' . '<' x ($r - 1); $SPACING = "\n" x $n; $SENTENCE = ' ' x $s; # Define a format at run time. $form = <<"End of Format Definition"; format STDOUT = $LEFT$INDENT$RIGHT1 \$_ $LEFT$RIGHT2~~ \$_ $SPACING. End of Format Definition print $form if $debugging; eval $form; # Set paragraph mode on input. $/ = ''; # For each paragraph... while (<>) { s/\s+/ /g; # Canonicalize white space. s/ $//; # Trim final space. s/([a-z0-9][.!?][)'"]*) ([A-Z"])/$1$SENTENCE$2/g; # Fix sentence ends. write; # Spit out new paragraph. } #-------end of file-----------------