Why was this so hard!?
I just coded a little python script to help automate the process of posting via Jekyll.
In case you don’t know what Jekyll is, it’s a ruby application that converts a hand-full of markdown documents into a static website ready for hosting… so you can write posts in simple markdown-formatted files, throw them in a directory, and let Jekyll turn them into nice webpages.
Markdown is a highly-readable way of writing pages that can later be translated into the more cumbersome HTML or other markup language. You see it used a lot on Reddit and Github.
I wanted to code a little python script that would:
- Ask me for the title, categories, and tags
- Create a properly-named markdown file in the right directory
- Add the required front-material to the top of the file
- Open up the file in the windows version of VIM for me to edit
I thought this would be easy, and for the most part it was.
The problem was that I wanted this script to run on Windows… which I use for work. And Windows use of the backslash character between directories really screwed things up.
Sidenote: I switched to linux back in about 1995 after I was horribly disappointed by Windows 95… I’ve sort-of been holding a grudge ever since, but I’ll admit that Windows is actually pretty good these days. (They’ve borrowed a lot from the ‘nix world)
These backslashes in the pathways suck big donkey butts because the backslash is a common escape character… and one that’s used in python.
Turns out you can type the path with forward-slashes (like a sane person) and then use os.path.normpath(string)
to turn the path into what Windows likes… so problem solved, right? Not quite.
I don’t remember why MS went with the backslash, but it’s been a thorn in a lot of sides ever since. The worst thing about it is probably that the backslash isn’t always in the same place on the keyboard so if you switch machines a lot, it slows you down.
The next problem was that damn space that Microsoft put in the Program Files directory.
That space causes the Windows command line to think you’ve put in two command arguments; C:\Program and Files\whatever_else. So you try to run an .exe in the Program Files directory and you get this message: “Sorry, I couldn’t find C:\Program”.
Now the standard way around this is to put the path of the executable in double quotes… or even to put that space in double quotes. But somehow, adding those quotes to the string and calling os.system(command) just wasn’t working. It was like the double quotes were getting dropped.
command = '"' + directory + exe + '"'
os.system(command)
...
Sorry, can't find 'C:\Program'
What did work was this: os.system('"' + command + '"')
.
I’m not sure why I had to do that, but hey, this thing is working now.
If you’re interested, here’s the code:
import time, os
postdir = "Z:/websites/arete/_posts/" #Include trailing slash!
vim = os.path.normpath("C:/Program Files (x86)/Vim/vim74/vim.exe")
d = time.strftime('%Y-%m-%d')
print(d)
title = raw_input("Post Title: ").title()
cats = raw_input("[Categories]: ").lower()
tags = raw_input("[Tags]: ").lower()
ts = title.replace(" ", "-").lower()
fname = d + "-" + ts + ".md"
f = os.path.normpath(postdir + fname)
head = "---"
head += "\ntype: post"
head += "\ntitle: %s" % title
if cats: head += "\ncategories: %s" % cats
if tags: head += "\ntags: %s" % tags
head += "\n---\n"
print(f)
print(head)
with open(f, 'w') as postfile:
postfile.write(head)
command='"%s" -c "set spell spelllang=en_us" -c "setf markdown" + "%s"' % (vim, f)
print(command)
os.system('"' + command + '"')
Key to the Vim command
- set spell spelllange=en_us - turns on spell checking
- setf markdown - sets syntax highlighting to markdown
- + moves cursor to the last line
Note: As you can probably tell, I’m not an expert in python, or any other programming language. I’m pretty much entirely self-taught and I rarely program for Windows anymore, so don’t expect that this will the best, cleanest, most elegant way to accomplish this.