From 4392a451e3d7e7c3b6a9a49c8f58cfb728b24d3e Mon Sep 17 00:00:00 2001 From: Solomon Peachy Date: Sun, 3 Jan 2016 10:34:37 -0500 Subject: [PATCH] Add another nanoblogger-to-nikola conversion script. I can't believe I'd forgotten that I already wrote one, but this newer one is simpler and is what I actually used to migrate things. --- nb2nikola-rev2.pl | 90 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100755 nb2nikola-rev2.pl diff --git a/nb2nikola-rev2.pl b/nb2nikola-rev2.pl new file mode 100755 index 0000000..f3c7cce --- /dev/null +++ b/nb2nikola-rev2.pl @@ -0,0 +1,90 @@ +#!/usr/bin/perl -w +use strict; +use Date::Manip; +use Unicode::Normalize; + +sub slugify($) { + my ($input) = @_; + + $input = NFKD($input); # Normalize the Unicode string + $input =~ tr/\000-\177//cd; # Strip non-ASCII characters (>127) + $input =~ s/[^\w\s-]//g; # Remove all characters that are not word characters (includes _), spaces, or hyphens + $input =~ s/^\s+|\s+$//g; # Trim whitespace from both ends + $input = lc($input); + $input =~ s/[-\s]+/-/g; # Replace all occurrences of spaces and hyphens with a single hyphen + + return $input; +} + +my %catlist = ( + 1 => "photos", + 2 => "free_software", + 3 => "old_advogato", + 4 => "old_diaryland", + 5 => "life_and_bs", + 6 => "writings", + 7 => "project365", + 8 => "food" +); + +Date_Init(); + +while () { + chomp; + + my ($fname, $cat) = split(/>/, $_); + + my @cats = split(/,/, $cat); + + for (my $i = 0; $i < scalar(@cats) ; $i++) { + $cats[$i] = $catlist{$cats[$i]}; + } + + my ($title, $date, $slug, $author, $desc, $format, $body); + + $body = ""; + + open INFILE, "<$fname"; + while () { + if ($_ =~ /TITLE:\s*(.*)/) { + $title = $1; + $slug = slugify($title); + } elsif ($_ =~ /DATE:\s*(.*)@(.*)/) { + $date = new Date::Manip::Date; + $date->parse("$1 $2"); + } elsif ($_ =~ /AUTHOR:\cs*(.*)/) { + $author = $1; + } elsif ($_ =~ /DESC:\s*(.*)/) { + $desc = $1; + } elsif ($_ =~ /FORMAT:\s*(.*)/) { + $format = $1; + } elsif ($_ =~ /BODY:\s*/) { + # SKIP + } elsif ($_ =~ /-----/) { + # SKIP + } elsif ($_ =~ /END-----/) { + last; + } else { + $body .= $_; + } + } + close INFILE; + + $fname = $date->printf("%q"); + open OUTFILE, ">$fname-$slug.md"; + print OUTFILE "\n"; + print OUTFILE $body; + close OUTFILE; + + + +}