Posting to Wordpress from Vim with tags and Markdown

[tags]wordpress,vim,tagging,markdown,python[/tags] Only a certain kind of geek will delight in the following post.

I favor Vim for most of my text editing. I wanted to compose my WordPress entries with Vim, especially after finding a Vim syntax file for Markdown. Great! But how to post those entries into WordPress?

I found a way last night. A Berkeley student with the same itch, Kesava Yerra, wrote a Python Vim script that posts to WordPress. Supposing you've installed the Ultimate Tag Warrior plug-in for WordPress, his script even lets you tag your post. (An earlier version didn't have tagging.)

So to make this work, you first enable Ultimate Tag Warrior's "embedded tag support" (it's in WordPress's Options screen). Then open your Vim configuration file (.vimrc) and add the following code:

PYTHON:
  1. python <<EOF
  2.     import urllib
  3.     import urllib2
  4.     import vim
  5.     import xml.dom.minidom
  6.     import xmlrpclib
  7.  
  8.     blog_username = 'your_username'
  9.     blog_password = 'your_password'
  10.     blog_url = 'http://path.to.your.weblog/xmlrpc.php'
  11.  
  12.  
  13.     def make_tags(text, numtags=5):
  14.         params = urllib.urlencode({
  15.            'appid': 'upbylunch',
  16.            'context': text})
  17.            
  18.         u = urllib2.urlopen("http://api.search.yahoo.com/"
  19.             + "ContentAnalysisService/V1/termExtraction",
  20.             params)
  21.         response = u.read()
  22.        
  23.         doc = xml.dom.minidom.parseString(response)
  24.         tags = [str(i.childNodes[0].nodeValue)
  25.             for i in doc.getElementsByTagName('Result')]
  26.  
  27.         return tags[:numtags]
  28.  
  29.     def post_blog(numtags=5):
  30.         strid = ''
  31.         offsetline = 0
  32.  
  33.         if vim.current.buffer[0].find('StrID:') != -1:
  34.             strid = vim.current.buffer[0].split(':')[1]
  35.             offsetline = 1
  36.            
  37.         title = vim.current.buffer[offsetline + 0]
  38.         tags = vim.current.buffer[offsetline + 1]
  39.         text = '\n'.join(vim.current.buffer[offsetline + 2:])
  40.  
  41.         # Tag condition
  42.         if tags == '':
  43.             tags = '[ tags\]' + ','.join(make_tags(text,
  44.                 numtags)) + '[ /tags]\n'
  45.         else:
  46.             tags = '[ tags]' + tags + '[ /tags]\n'
  47.  
  48.         content = tags + text
  49.  
  50.         wp = xmlrpclib.ServerProxy(blog_url)
  51.         post = {
  52.             'title': title,
  53.             'description': content
  54.         }
  55.  
  56.         if strid == '':
  57.             strid = wp.metaWeblog.newPost('', blog_username,
  58.                 blog_password, post, 1)
  59.  
  60.             vim.current.buffer.append('\n')
  61.             vim.current.buffer[:] = ['StrID:' + strid] + [i
  62.                 for i in vim.current.buffer[:]]
  63.         else:
  64.             wp.metaWeblog.editPost(strid, blog_username,
  65.                 blog_password, post, 1)
  66.  
  67.         vim.command('set nomodified')
  68.     EOF


(Note: remove the whitespace from '[ tags]' and '[ \tags']) All set? Now you can compose in Vim, using the following format:

Title
your,tags,separated,by,commas (to auto-tag, leave line blank)
The content of your post...

When you're ready, issue this Vim command:

:py post_blog()

After a heartbeat, your entry is posted to WordPress (or to any blog that supports the Metaweblog API).

Oh, before I forget, you are going to have to assign Categories to your post manually in WordPress's Manage Posts panel.

I write using Markdown and, for syntax highlighting in Vim, I find Markdown Vim Mode to be indispensible. (I'll cover installation in a later post, as I had some trouble with it.) So to activate that, I save my entries with an *.mkd extension. The Markdown looks pretty. I write my post. Then I type :py post_blog() to publish.

I'm delighted.

22 Comments »

Leave a comment:

Powered by WordPress, the ltlblg theme and the written word.