Create File Names for Jekyll

Create File Names for Jekyll

As part of the migration to Jekyll, I’m trying to automate as many of the pain points as possible. YAML is one of them, as is the file names Jekyll expects.

To handle the YAML end, I put together this little TextExpander macro:

---
layout: post
title: %filltext:name=Title%
description: RUN bin/build-descrip.pl
author: hargrove
email: hargrove@spiritofnine.com
date: %Y-%m-%d %H:%M
copyright: © Stephen Hargrove %Y
category: %fillpopup:name=Category:default=misc:music:philosophy:technology:programming%
tags: [%filltext:name=Tags (comma separated)%]
comments: false
image:
 feature: %fillpopup:name=Image:default=misc:music:philosophy:politics:technology:programming%.jpg
---

I’ve assigned an abbreviation of ,new, so when I type that, the following pops up:

From here I can fill out the title, category, tags, and associated image. TextExpander auto fills the date. The description field reminds me to run a script that builds the description based on the first 255 or so characters of the post. Once TextExpander finished, I can toggle comments to true or false as appropriate.

So far so good, and now I can write the article. Once done, however, I have to save it, and Jekyll has requirements regarding the filename. The standard format is:

year-month-date-title.md

For example, 2013-11-06-woot.md would suffice. But I’m a little (too) pedantic about this, and want title to actually contain the title of the post. To wit: 2013-11-06-create-file-names-for-jekyll.md would be the filename for this post.

So I needed a couple of scripts. The first is an AppleScript to read the clipboard and pass that to a Perl script. It then takes the output of the Perl script and shoves it into to clipboard.

set clippy to the clipboard
set p to "echo '" & clippy & "' | /path/to/build-title.pl"
set the clipboard to (do shell script p)

Easy enough, although this was the longest part of the process for me because I suck at AppleScript. Here’s the Perl script:

#!/usr/bin/perl -w
use strict;
use Time::Piece;

my $t = localtime;

my $y = addZero($t->year);
my $m = addZero($t->mon);
my $d = addZero($t->mday);

my $date = $y . '-' . $m . '-' . $d;

my $line = <>;
chomp $line;

$line  =~ s/(\,|\.|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\=|\+|\'|\"|\{|\}|\[|\]|\||\?|\/|\<|\>)//g;
$line  =~ s/ +/\-/g;
my $tmp = lc($line);
$tmp .= '.md';

print "$date-$tmp";

sub addZero
{
    my $x = shift;

    if ($x < 10)
    {
        return '0' . $x;
    }
    else
    {
        return $x;
    }
}

This does several things:

  1. Gets the date and makes sure each part is 2 digits (e.g., January is 01 rather than 1, etc.);
  2. Read STDIN (which is the clipboard being passed from the AppleScript);
  3. Strips out any special characters that I might use in a title. Ex: I'm Awesome becomes Im Awesome;
  4. Converts one or more spaces to dashes. Im Awesome becomes Im-Awesome;
  5. Converts the input to lowercase (Im-Awesome becomes im-awesome);
  6. Shoves .md to the end of the title (im-awesome becomes im-awesome.md);
  7. It then prints (or returns) the date (2013-11-06), a dash, and the filename. Ex: 2013-11-06-im-awesome.md.

I dropped the AppleScript into ~/Library/Scripts so FastScripts will see it. Now, once I’ve finished writing an article, I highlight the title, ⌘c, fire off the AppleScript via Fastscripts, hit ⌘-shift-s, then ⌘v to paste the filename into the Save As dialog.

It’s not perfect, but it beats the hell out of the alternative. And it works for me. So, win?