Editing Movable Type Entries With Vim

My journal used to run on the Movable Type weblog software, which is this ultra-slick way of typing words into a box and having them appear on a webpage. It seemed to work very well. However, since my site is hosted on a co-located server that I myself administer, there's no need for me to be limited to a little 66-column textarea in a web form when it comes to composing and editing entries. I usually edit everything with vim, so I looked for a way to use vim for journal entries. It's actually not too far-fetched.

In addition to the regular web form, Movable Type also offers an XMLRPC interface. This is in essence a way in which one can manipulate entries by sending form data, without the use of the browser-oriented web interface. It's like a command-line version of Movable Type, done over HTTP. You do need some sort of client to correctly format and send this data, though. Enter mtsend.py, a python script written for just this purpose. It's meant to send a file or STDIN into Movable Type using XMLRPC. With that, I can use vim to edit, and pipe the results into mtsend.py, which kicks it up to my site.

I start with an entry template, which is a very simple textfile with the following:



TITLE, CATEGORY, and BODY are pretty self-explanatory. I set CONVERT BREAKS to 0 so that my wrapped-lines are not applied literally into HTML with a <br /> tag every ~80 characters. This also kills the automatic <P> tags that would be placed around empty-line delimited paragraphs, so I write my own as I go, treating the text I'm editing as the HTML I expect to appear on the site.

To send a new entry up to the server, I use this command (from within vim):

:w !mtsend.py -N

That produces the following output, assuming all goes well:

Parsing post entry from standard input...
Saving new post entry...
Retrieve available categories...
Add categories "5" to post entry "116"...

To edit an existing journal entry, such as number 116, first this command to retrieve it:

:%!mtsend.py -q -G 116

The post will be in a format similar to the above template, with more header fields. This can be edited, and then sent back to the server with:

:w !mtsend.py -E 116

If an entry gets changed in this manner, it will need to be rebuilt by Movable Type, which can be initiated with this:

:!mtsend.py -R 116

These commands are a little clunky and hard to remember, though, so I made some vim macros to simplify things. They all begin with #mt, with the third letter denoting the relevant flag given to mtsend.py. In ~/.vimrc:

" Shortcuts for editing Movable Type journal using mtsend.py
" (T)emplate- reads in the template file
map #mtt :0r ~/docs/boilerplate/journal.html<CR>:set filetype=html<CR>A
" (N)ew- posts new entry
map #mtn :w !mtsend.py -N<CR>
" (G)et- retrieves latest post, enables HTML syntax highlighting
map #mtG :%!mtsend.py -q -G -<CR>:set filetype=html<CR>
" (g)et- retrieves specified post
map #mtg :%!mtsend.py -q -G
" (E)dit- uploads new revision to latest post
map #mte :w !mtsend.py -E -<CR>
" (R)ebuild- rebuild specified entry
map #mtr :!mtsend.py -R

MT requires a username and password, obviously, and these are stored in a mode-0600 config file on my home machine for use by mtsend.py. I take the additional step of restricting access to the mt-xmlrpc.cgi script itself with the following in a .htaccess file in the MT CGI directory:

<Files mt-xmlrpc.cgi>
    Order Deny,Allow
    Allow from my.home.ip.address/32
    Deny from all

More notes