I’m a command-line person. If you can show me a command-line version of something I already do in a windowed environment, I’ll get stuff done faster. I often look for command-line solutions to tasks that become repetitive and feel as though time could be saved by doing them on the console.
A recent example of this is the posting of asides on my blog. Asides are often very short (one or two sentences at most — they appear on my blog without a title) and navigating the WordPress Administration interface in a web browser simply to post one or two sentences became very time consuming and distracting. Since I’m constantly editing files and code on the console using my favorite editor (vi), being able to quickly create and post an aside from the same environment would be awesome.
Before writing a tool that allowed me to post to my WordPress blog, I searched Google to see if someone else had already written something. Sure enough, I found blogpost, a script written in Python by Stuart Rackham:
blogpost is a WordPress command-line weblog client. It creates and updates weblog entries directly from AsciiDoc (or HTML) source documents. You can also delete and list weblog entries from the command-line.
It uses XML-RPC to post to WordPress blogs and also supports automatically uploading media files (images, videos, audio, documents) that are referenced within the AsciiDoc (or HTML) post file. Check the blogpost man page for full details.
Remember, my main goal here is to make posting short asides easier. I’m perfectly happy using the WordPress web interface to write longer posts. In fact, I prefer the web interface for longer posts because I get things like automatic spell checking (through OS X) and automatic draft saving (through WordPress).
After installing blogpost and modifying the configuration file to include my WordPress login details, I created a file called
post.txt using the vi editor and, after saving the file and closing vi, I published the aside using blogpost:
$ blogpost.py --title="My Test Aside Post" -U --doctype='html' create post.txt
creating published post 'My Test Aside Post'...
$ blogpost.py cat --categories="Asides, Blog Entries, General" post.txt
assigning categories: Asides,Blog Entries,General
Note that I only need really basic formatting (i.e., HTML for links), so I use the
--doctype='html' option. This allows me to type raw HTML in vi when I’m editing the post file, just as I do now in WordPress (I don’t use the Visual Editor).
While the options and flexibility provided by blogpost are great, the process of publishing an aside needed to be more automated to solve my problem. Creating a new file in vi, typing all those options, running two separate commands, and then deleting the file every time I wanted to post a few sentences on my blog didn’t make a whole lot of sense. So I whipped together this little shell script to help automate the steps above:
## aside.sh - automates publishing asides using blogpost.py
# Open a temporary file in the vi editor
# Display new aside before publishing
echo "New Aside:"
# Prompt for an aside title
echo "Enter a title for this Aside:"
# Using the temp file saved above, post the Aside
blogpost.py --title="$TITLE" -U --doctype='html' create aside.$$
blogpost.py cat --categories="Blog Entries, Asides" aside.$$
# Remove the temporary file
Now posting an aside to my blog is as simple as running
./aside.sh, typing the aside in vi, saving and quitting (
:wq), and then typing a title. The rest of the work, including cleanup, is taken care of by the script!
Stuart did an excellent job with blogpost and if you have a blog and use the console (and why shouldn’t you?!) I recommend you check it out. The blogpost README is a great place to start, as it includes prerequisites and installation information.