Re-formatting badly formatted text

You’ll find lots of badly formatted text in (pdf) ebooks and such. I used this script to quickly reformat most of the text:

#!/usr/bin/perl

while(<>) {

# remove stray newlines
s/(\w|\,|\;|\:)\s*\n/$1 /gm;

# remove hypenation
s/(\w)\-\s*\n/$1/gm;

# print the row
print;

}

Invoke the script like this:

$ ebookfmt.pl < unformatted.txt > formatted.txt

Then you can import it in Libreoffice and export again as a PDF or save the odt and create an epub or mobi with Calibre.

Advertisements