| Reflow(3pm) | User Contributed Perl Documentation | Reflow(3pm) |
Text::Reflow - Perl module for reflowing text files using Knuth's paragraphing algorithm.
use Text::Reflow qw(reflow_file reflow_string reflow_array);
reflow_file($infile, $outfile, key => value, ...);
$output = reflow_string($input, key => value, ...);
$output = reflow_array(\@input, key => value, ...);
These routines will reflow the paragraphs in the given file, filehandle, string or array using Knuth's paragraphing algorithm (as used in TeX) to pick "good" places to break the lines.
Each routine takes ascii text data with paragraphs separated by blank lines and reflows the paragraphs. If two or more lines in a row are "indented" then they are assumed to be a quoted poem and are passed through unchanged (but see below)
The reflow algorithm tries to keep the lines the same length but also tries to break at punctuation, and avoid breaking within a proper name or after certain connectives ("a", "the", etc.). The result is a file with a more "ragged" right margin than is produced by "fmt" or "Text::Wrap" but it is easier to read since fewer phrases are broken across line breaks.
For "reflow_file", if $infile is the empty string, then the input is taken from STDIN and if $outfile is the empty string, the output is written to STDOUT. Otherwise, $infile and $outfile may be a string, a FileHandle reference or a FileHandle glob.
A typical invocation is:
reflow_file("myfile", "");
which reflows the whole of myfile and prints the result to STDOUT.
The behaviour of Reflow can be adjusted by setting various keyword options. These can be set globally by referencing the appropriate variable in the Text::Reflow package, for example:
$Text::Reflow::maximum = 80;
$Text::Reflow::optimum = 75;
will set the maximum line length to 80 characters and the optimum line length to 75 characters for all subsequent reflow operations. Or they can be passed to a reflow_ function as a keyword parameter, for example:
$out = reflow_string($in, maximum => 80, optimum => 75);
in which case the new options only apply to this call.
The following options are currently implemented, with their default values:
quote => "> ", indent => "> "
noreflow => '(\.\s*){4}\.'
will not reflow any lines containing five or more consecutive dots.
None by default.
Original "reflow" perl script written by Michael Larsen, larsen@edu.upenn.math.
Modified, enhanced and converted to a perl module with XSUB by Martin Ward, martin@gkc.org.uk
See "TeX the Program" by Donald Knuth for a description of the algorithm used.
| 2022-10-19 | perl v5.36.0 |