#! /usr/bin/perl use strict; use Text::Autoformat; use Getopt::Std; use vars qw($opt_h $opt_n $opt_p $opt_w $opt_j $opt_s $opt_c %fmt); getopts("hn:j:sc:p:w:"); if ($opt_h) { exec('perldoc', $0); } # Process arguments. $fmt{justify} = $opt_j || "full"; $fmt{case} = $opt_c if $opt_c; $fmt{squeeze} = $opt_s || 0; $opt_n ||= 2; $opt_p ||= $=; $opt_w ||= 80; # Calculate layout information $fmt{left} = 0; $fmt{right} = int(($opt_w + 3) / $opt_n) - 5; $fmt{all} = 1; my $col_width = $fmt{right}; my $fmt_str = (join "|", map " %s ", 1..$opt_n)."\n"; # Read and process my $text = join '', <>; my @lines = split /\n/, autoformat($text, \%fmt); while (@lines) { my $col_len = int((@lines + $opt_n - 1) / $opt_n); $col_len = $opt_p - 2 if $col_len > $opt_p - 2; my @col; foreach (1..$opt_n) { push @col, [splice(@lines, 0, $col_len)]; } foreach my $i (0..$#{$col[0]}) { my @row = map $_->[$i], @col; $_ .= " " x ($col_width - length($_)) foreach @row; printf($fmt_str, @row); } print "\n" x ($opt_p - 2 - $col_len); print "\n\n"; } __END__ =head1 NAME reformat.pl - reformat text into columns =head1 SYNOPSIS B -[hs] [-c {lower,upper,sentence,title,highlight}] [-j {left,right,full,centre}] [-n cols] [-p page-length] [-w page-width] =head1 DESCRIPTION This script takes input and prints it out as formatted ASCII text in multi-column format. The majority of the formatting is done by Damian Conway's excellent I module. If no input file is specified on the command line it will default to reading from standard input. The options that are supported are: =over 4 =item h Print this help page. =item c argument Automatically adjust the case of text. =item j argument Change the justifications within each column. Defaults to using the full width. =item n argument Choose how many columns to display the text formatted in. Defaults to 2. =item p argument Choose how many lines there are in a page. Defaults to 60. =item s Choose to squeeze multiple spaces into one. =item w argument Choose how many characters wide a page is. Defaults to 80. =back =head1 AUTHOR AND COPYRIGHT Written by Ben Tilly. This may be copied and redistributed under the same terms as Perl.