Skip to content

Instantly share code, notes, and snippets.

@xjackk
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xjackk/f1deec120319111be5b5 to your computer and use it in GitHub Desktop.
Save xjackk/f1deec120319111be5b5 to your computer and use it in GitHub Desktop.
The Diary of Anne Frank

The Diary of Anne Frank

I am going to be doing some light reading this weekend. I wrote a script that generates a markdown copy of "The Diary of Anne Frank", a classic.

Install

To install the book simply run.

perl install.pl
perl annefrank.pl

This will generate a diary.md file in the same directory.


~ Enjoy!

#!/usr/bin/env perl
use warnings;
use strict;
use LWP::Simple;
use HTML::WikiConverter;
use HTML::WikiConverter::Markdown;
##
## One time download for reading.
##
## jaaacckz1@gmail.com
##
## - xjrK
##
## "The Diary of Anne Frank"
##
# grab pages and get to reading this weekend.
my ($page1, $page2, $page3, $page4, $page5);
$page1 = get("http://www.fullbooks.com/A-Young-Girl-s-Diary1.html");
$page2 = get("http://www.fullbooks.com/A-Young-Girl-s-Diary2.html");
$page3 = get("http://www.fullbooks.com/A-Young-Girl-s-Diary3.html");
$page4 = get("http://www.fullbooks.com/A-Young-Girl-s-Diary4.html");
$page5 = get("http://www.fullbooks.com/A-Young-Girl-s-Diary5.html");
my @pages = ($page1, $page2, $page3, $page4, $page5);
# open the diary.
# "The only girl I've ever loved was born with roses in her eyes"
my $diaryfile = "diary.md";
unless(open DIARY, '>>'.$diaryfile) { die "Can't write to it!"; }
foreach my $page (@pages) {
my $converter = new HTML::WikiConverter( dialect => 'Markdown' );
# Append to file
print DIARY $converter->html2wiki($page);
}
# close the diary
close DIARY;
# "But then they buried her alive one evening, 1945, with just her sister at her side
my $data = read_file($diaryfile);
# Clean everything up. (=
# "God is a place; You will wait for the rest of your life."
$data =~ s/\<br \/>//gi;
$data =~ s/\<table>//gi;
$data =~ s/\<\/table//gi;
$data =~ s/\<br \///gi;
$data =~ s/\<tr>\///gi;
$data =~ s/\<\/tr>\///gi;
$data =~ s/<tr>//gi;
$data =~ s/\<\/tr>//gi;
$data =~ s/<td>//gi;
$data =~ s/<\/td>//gi;
$data =~ s/\s\\_//gi;
$data =~ s/\\_\w//gi;
$data =~ s/\w\\_//gi;
$data =~ s/!\\_//gi;
write_file($diaryfile, $data);
exit; # all done.
# read some stuff
sub read_file {
my ($filename) = @_;
open my $in, '<:encoding(UTF-8)', $filename or die "Could not open '$filename' for reading $!";
# "In my dreams you're alive and you're crying"
local $/ = undef;
my $all = <$in>;
close $in;
return $all;
}
# write some stuff to the file
sub write_file {
my ($filename, $content) = @_;
open my $out, '>:encoding(UTF-8)', $filename or die "Could not open '$filename' for writing $!";
# "Rings of flowers around your eyes,
# And I'll love you for the rest of your life when you're ready
print $out $content;
close $out;
return;
}
#!/usr/bin/perl
# Install the goods.
use strict;
use warnings;
# quick and dirty for the linux box. Sorry about it.
system("cpan", "HTML::WikiConverter");
system("cpan", "HTML::WikiConverter::Markdown");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment