Blog image: 

I tend to keep a lot of notes around my ~/docs directory. They are formatted in Pandoc Markdown because it's easy to type and easy to read. However, sometimes the notes get fairly extensive, and it's nice to be able to read them as html in a web browser rather than in the terminal window. To that end, I wrote up this little tool that I call vah -- "View As HTML". All it does is orchestrate the conversion of the text file into html and then has a web browser open it.

For it to work, you first need a Markdown processor like Pandoc installed. On the system I'm working with at the moment, I already had Text::MultiMarkdown installed, so I used that.

If you don't have Text::MultiMarkdown already, you can install it from the CPAN:

sudo /opt/perl/bin/cpanm Text::MultiMarkdown

If you don't have the File::Slurp and Modern::Perl modules, you'll need those as well.

Then create the following script and name it "vah":

#!/opt/perl/bin/perl

use Modern::Perl;

use Text::MultiMarkdown 'markdown';
use File::Slurp 'slurp';
use File::Basename;

unless (@ARGV == 1) {
    die "Please pass exactly 1 arg: the name of the .txt file "
      . "you want to view. Quitting.\n";
}

my $txt_filename = $ARGV[0];

my $html_filename = $txt_filename;
$html_filename = '/tmp/' . basename($txt_filename);
$html_filename =~ s/\.txt$/.html/;

my $html_header = <<"EOT";

<!doctype html>
<html>
<head>
<style type="text/css">
body {
    background: #aaa;
    font-family: "Linux Libertine O";
}
#main {
    background: #eee;
    width: 46em;
    border: 2px solid #666;
    padding: 1em;
    font-family: 'Cantarell';
}
pre {
    background: #ccc;
    padding: 3px;
    margin-left: 1em;
    border: 1px solid #aaa;
}
</style>
</head>
<body>
<div id="main">

EOT

my $html_footer = <<"EOT";
</div>
</body>
</html>
EOT

open(my $html_file, '>', $html_filename) or die $!;

my $txt_content = slurp $txt_filename;
my $html_content = markdown $txt_content;

print {$html_file} $html_header, $html_content, $html_footer;
close $html_file;

# On GNU/Linux, you can probably call your browser directly.
# "open" is specific to Mac OS X.
system("open $html_filename");

After you chmod +x vah, try it out:

vah path/to/filename.txt

Notice, the css above says to use the beautiful Linux Libertine font, which I find quite readable. (To install this font on Mac OS X: download, unpack, and copy the .otf files into your ~/Library/Fonts directory).

Old Greenwich, CT 06870 - (914) 275 5520