#!/usr/bin/perl
# nb-html2md - Converts nanoblogger's metafiles from HTML to Markdown
# Copyright (C) 2006, Caustiq
#
# Requirements:
# nanoblogger
# html2text.py
#
# Assumption:
# METADATA_CLOSETAG="END-----" in nanoblogger's blog conf file.
#
# *WARNING*:
# BACKUP YOUR NANOBLOGGER METADATA FILES BEFORE USING THIS PROGRAM!
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
use strict;
use Readonly;
if (@ARGV != 1) {
print (
"nb-html2md 1.0 - Converts nanoblogger's metafiles from HTML to ",
"Markdown\n\n",
"Usage: nb-html2md [NB DATA DIRECTORY]\n\n",
"Example:\n",
"\t$0 /home/caustiq/public_html/nb/data\n",
);
exit;
}
Readonly my $NB_DATA_DIR => shift @ARGV;
Readonly my $HTML2MD => "html2text.py";
chdir $NB_DATA_DIR or die "Cannot chdir to $NB_DATA_DIR: $!";
my @txt_filenames = glob "*.txt";
foreach my $filename (@txt_filenames) {
open TEXT, "<", $filename
or warn "Cannot open nb text file: $!";
my @name_and_ext = split /\./, $filename;
my $name_without_ext = shift @name_and_ext;
open HTML, ">", "$name_without_ext.html"
or warn "Cannot create nb html file: $!";
open MARKDOWN, ">", "$name_without_ext.md"
or warn "Cannot create nb markdown file: $!";
print "Converting $filename to use Markdown formatting... ";
while () {
if ($_ =~ /^DESC:/) {
print MARKDOWN (
$_,
"FORMAT: markdown\n",
);
next;
}
if ($_ =~ /^FORMAT:/) {
next;
}
print MARKDOWN $_;
if ($_ =~ /^BODY:/) {
while () {
print HTML $_ unless $_ =~ /^END-----/;
}
}
}
close TEXT;
close HTML;
close MARKDOWN;
system("python $HTML2MD <$name_without_ext.html >>$name_without_ext.md");
open MARKDOWN, ">>", "$name_without_ext.md"
or warn "Cannot open nb markdown file: $!";
print MARKDOWN "END-----\n";
close MARKDOWN;
rename "$name_without_ext.md", $filename
or warn "Cannot rename $name_without_ext.md: $!";
unlink "$name_without_ext.html"
or warn "Cannot delete $name_without_ext.html: $!";
print "done.\n";
}
print "\nConversion complete.\n";