nanoblogger to octopress notes

It turns out that converting from nanoblogger to octopress wasn't all that difficult with a little help from my friend perl. The only hiccup that I ran into was that the title line is kind of picky. It's a really good idea to enclose the title in quotes.

This perl script will process each txt file into a markdown file that should mostly work with octopress. One of the downsides of octopress is that if there is a problem, it does not tell you which file is causing the failure.

#!/usr/bin/perl

use strict;
use warnings;
use Carp;
use English qw( -no_match_vars );
use File::Glob;

# nanoblogger filename
# 2012-03-21T04_28_49.txt

# octopress filename
# 2012-11-18-octopress-instead-of-nanoblogger.markdown

# octopress header
# ---
# layout: post
# title: "Octopress instead of nanoblogger"
# date: 2012-11-18 14:37
# comments: true
# categories: [blog, software, shell]
# ---

my @files = <*.txt>;

for my $file ( @files ) {
    my ( $date, $time, $body, $title );

    if ( $file =~ m{([0-9-]+)T(\d{2}_\d{2})\S+[.]txt}xms ) {
        ( $date, $time ) = ( $1, $2 );
        $time =~ s{_}{:}g;
    }

    open my $input, '<', $file or croak qq{ERROR: $OS_ERROR};

    my $text_string = do { local $/; <$input> };
    my @text_array = split( /\n/, $text_string );

    close $input or croak qq{ERROR: $OS_ERROR};

    for my $line ( @text_array ) {
        if ( $line =~ m{TITLE: \s+ ([\S\s]+)}xms ) {
            $title = $1;
        }
    }

    if ( $text_string =~ m{BODY:\n(.*)END-----}xms ) {
        $body = $1;
    }

    if ( defined $date and defined $time and defined $body ) {
        my $file_title = $title;
        $file_title =~ s{\s+}{_}g;
        $file_title =~ s{[:]+}{_}g;
        $file_title =~ s{[,'".!;&?]}{}g;
        $file_title = lc( $file_title );

        my $filename = qq{$date-$file_title.markdown};
        print qq{$filename\n};
        open my $output, '>', $filename or croak qq{ERROR: $OS_ERROR};
        print {$output} qq{---\n};
        print {$output} qq{layout: post\n};
        print {$output} qq{title: $title\n};
        print {$output} qq{date: $date $time\n};
        print {$output} qq{comments: true\n};
        print {$output} qq{categories: \n};
        print {$output} qq{---\n};
        print {$output} qq{$body\n};
        close $output or croak qq{ERROR: $OS_ERROR};
    }
}