Wednesday, June 25, 2008

I Like That You're Easily Impressed

When you get a book in text format to read on your Sony e-Reader, for example from Project Gutenberg, it will usually be formatted to a fixed width with line breaks at the end of each line. Why on Earth anyone would do this is not something I can explain, but they do.

Since your e-Reader will most likely have a screen whose display width does not conveniently align with these line breaks, text will be brutally choppy and difficult to read, appearing like this:

So
then the guy went to the place with the

guy
and some more stuff happened. Good times,
man.
Good times.


Behold the power of Perl!

#!/usr/local/bin/perl

my $parsedFile = $ARGV[0];
my $outputFile = $ARGV[1];
my @outContents;

open FD, $parsedFile or die "Can't open input file: $!";

LOOP: while ([FD]) {

my $line = $_;

if (/^$/) {
push(@outContents, $line);
} elsif (length($line) > 66) {
$line =~ chomp($line);
$line = $line . " ";
} else {
$line = $line . "\n";
}
push(@outContents, $line);
}

open FD, ">$outputFile" or die "Can't open output file: $!";
foreach $line (@outContents) {
print FD $line;
}
close FD;

Note: the [FD] in the while loop should actually be enclosed in angle brackets, stupid Blogger.

You're welcome.

1 comment:

You Look Like A Nail said...

I shouldn't be using ARGV for the input to this program. That's a C-ism. Actual perl people would use shift or @_ or such.

But, y'know, I'm a C kinda guy.