Utility to format text files with word-wrap
Posted in 1996
As noted in an earlier posting, I have placed a shell/awk script called "ww" in the IIUG software repository. This script wraps (folds) long lines of text without splitting words. I generally need to do this type of reformatting to solve two kinds of problems: e-mail or news with lines wider than 80 characters, and text files created by word processors that need to be converted to HTML. The e-mail/news problem usually occurs when the sender has a workstation window wider than 80 characters or uses client software that inserts a hard RETURN or newline only after each paragraph. The (-m) option is particularly good for processing these kinds of files. While ww doesn't do anything specifically related to Informix, I thought I'd make it available to readers of this forum since I've noticed a growing number of articles with this problem. Very often, piping such an article through "| ww -m | more" will make it much more readable. The other main use I have for ww is to help in converting word processing text files to HTML for Web pages. I periodically need to do this for an end user since we have yet to move to WYSIWYG Web page editing here. Invariably, when such files are saved as text, the lines are much longer than 80 characters. While I could just leave them that way, I find that having them fit correctly in a regular window makes them easier to work with. The (-H) option will add rudimentary HTML tags to the output, in my own idiosyncratic style. You can also use (-s) and embed "-w" and "+w" lines in the file if you have sections that shouldn't be wrapped. I've tried to keep only the essential functions in this script, so you're welcome to hack on it to make it suit your needs. If you make it do anything particularly clever, I'd like to hear about it. The script was developed in the SunOS/Solaris environment. The only thing that you might have to fiddle with is the way that the nawk arguments are set, but that should be pretty easy. Enclosed below are some comments from the front of the script that are something between a README file and a bona fide man page. You can access the file from "http://www.iiug.org/software/software_index.html" on the Web, or download it via FTP from "ftp://ftp.iiug.org/pub/informix/pub/ww.Z". Enjoy, Walt. ------------------------------------------------------------------------------ ww (wordwrap) Wrap long lines without splitting words Usage: ww [-bhmstH] [-f chars] [-i n] [-l length] [files] Options: -b break each output line at the end of each input line -f don't wrap lines beginning with flag characters "chars" -h don't wrap mail or news header lines -i indent first line of each output paragraph by n spaces -l specifies maximum output line length (default = 79) -m process as mail/news message/article (==> -h -f'>}:') -s process file in sections marked with "-w" and "+w" -t input lines indented with tabs or blanks also start new paragraphs -H add rudimentary HTML formatting to output Notes: -h Mail/News header lines are all non-blank lines before the first blank line. -f Multiple characters may be specified with (-f). Only the first character position of each input line is inspected for the presence of one of these flag characters. -m In addition to implying the arguments "-h -f'>}:'", (-m) will also cause WW to attempt not to wrap lines in a signature block at the end of the message. The convention for indicating such a block is that it is preceded by a line consisting of two dashes and a blank ("-- "). Unfortunately, this convention is followed much less than it once was, and there is no other widely used consistent way of flagging signature blocks. -l If a maximum output line length of zero (0) is explicitly specified, WW will use one less than the current terminal or window width as reported by "/usr/5bin/tput cols". -s This option will cause WW to recognize lines consisting of "-w" and "+w" as turning wrapping off and on respectively. "-W" and "+W" are also allowed. These strings must be the only characters on the flagging lines, which are not printed themselves. Wrapping is initially turned on; i. e., an implied "+w" precedes the first line of the file. -t Using (-t) will cause WW to recognize any line starting with tabs or blanks (whitespace) as starting a new paragraph. Blank lines always mark the start of new paragraphs, regardless of whether (-t) is used. All paragraphs in the output are set off by at least one blank line. If a new paragraph is started due to beginning-of-line whitespace and the immediately previous output line was not blank, WW will print a blank line before starting the new paragraph. -i If (-i) is not used, all paragraphs will be started flush left, regardless of whether they are started due to blank lines or whitespace. If an indentation is specified, the first line of each paragraph will be indented using the requested number of blanks. -H If (-H) is specified, WW will add basic HTML formatting tags to the output: o <HTML> and <BODY> will be added to the front of the file, and </BODY> and </HTML> will be added to the end. A comment tag will also be included at the front that lists the WW options that were used to create the file. o If wrap/no-wrap sections are used (-s), a <PRE> tag will be output when wrapping is turned off with "-w", and a </PRE> tag will be output when wrapping is turned on with "+w". o If (-f) is used to cause lines flagged with certain characters to not be wrapped, a <BR> tag will be appended to each such line. o If a mail/news header is being recognized (-h, -m), it will be enclosed in <PRE></PRE> tags. Likewise, a <PRE> tag will be output before a signature block indicated by a "-- " line if (-m) is used. o Each paragraph will be followed by a "<P>" tag on a separate line. Other WW attempts to recognize end-of-sentence and other punctuation marks and append the correct number of blanks. An array holds common titles such as "Dr." and "Ms." that should have only one blank following the period ("."). ------------------------------------------------------------------------------ -- Walt Hultgren Internet: walt@rmy.emory.edu (IP 170.140.97.4) Emory University UUC