Posting to Wordpress from Vim with tags and Markdown
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 <<EOF
- import urllib
- import urllib2
- import vim
- import xml.dom.minidom
- import xmlrpclib
- blog_username = 'your_username'
- blog_password = 'your_password'
- blog_url = 'http://path.to.your.weblog/xmlrpc.php'
- def make_tags(text, numtags=5):
- params = urllib.urlencode({
- 'appid': 'upbylunch',
- 'context': text})
- u = urllib2.urlopen("http://api.search.yahoo.com/"
- + "ContentAnalysisService/V1/termExtraction",
- params)
- response = u.read()
- doc = xml.dom.minidom.parseString(response)
- tags = [str(i.childNodes[0].nodeValue)
- for i in doc.getElementsByTagName('Result')]
- return tags[:numtags]
- def post_blog(numtags=5):
- strid = ''
- offsetline = 0
- if vim.current.buffer[0].find('StrID:') != -1:
- strid = vim.current.buffer[0].split(':')[1]
- offsetline = 1
- title = vim.current.buffer[offsetline + 0]
- tags = vim.current.buffer[offsetline + 1]
- text = '\n'.join(vim.current.buffer[offsetline + 2:])
- # Tag condition
- if tags == '':
- tags = '[ tags\]' + ','.join(make_tags(text,
- numtags)) + '[ /tags]\n'
- else:
- tags = '[ tags]' + tags + '[ /tags]\n'
- content = tags + text
- wp = xmlrpclib.ServerProxy(blog_url)
- post = {
- 'title': title,
- 'description': content
- }
- if strid == '':
- strid = wp.metaWeblog.newPost('', blog_username,
- blog_password, post, 1)
- vim.current.buffer.append('\n')
- vim.current.buffer[:] = ['StrID:' + strid] + [i
- for i in vim.current.buffer[:]]
- else:
- wp.metaWeblog.editPost(strid, blog_username,
- blog_password, post, 1)
- vim.command('set nomodified')
- 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.
Tags:blog markdown python tagging vim wordpress
June 10th, 2006 at 3:04 am
June 10th, 2006 at 7:13 am
June 16th, 2006 at 1:25 am
Glad you found it useful!
August 15th, 2006 at 5:26 am
Using vim for editing Posts was my number 1 reason to use (py)blosxom, but what you are presenting me here is the number 1 reason to use Wordpress.
August 15th, 2006 at 10:45 pm
August 26th, 2006 at 8:34 am
Um… this is sweet. I wasnt aware of such a feature in vim
January 12th, 2007 at 10:31 am
[…] Tonight I think I stumbled on a potential solution. Tri Nguyen has a nice post about how to use a Python enabled Vim to post directly to WordPress. Although I cannot seem to dial in the Markdown part of her scheme, the Vim-WordPress post capability is scratching an itch for me. Filed under: Vim — serge @ 9:17 pm […]
March 15th, 2007 at 10:49 am
Excellent! Thanks a lot for posting this—I’m going to try writing a similar integration for Django.
March 19th, 2007 at 6:27 pm
[…] Posting to Wordpress from Vim with tags and Markdown » Tri Nguyen adding this to the todo list (tags: vim wordpress) […]
March 28th, 2007 at 9:54 pm
April 16th, 2007 at 8:07 pm
Bless you, vim nerd.
May 6th, 2007 at 5:47 am
June 12th, 2007 at 4:38 am
July 13th, 2007 at 8:15 am
[…] Ceci est assez experimental, je suis en train d’écrire cet article dans mon éditeur de texte favori1. J’ai pour cela écrit un plugin, largement inspiré de ceci2. […]
July 13th, 2007 at 8:27 am
Hi ! I just discovered this handy little tool a few days ago, and decided to make a more complex version of it.
By more “complex” version, I mean that it allows to list articles posted on the blog, edit articles, and of course send new articles. It supports tags by using a plugin, but you can of course disable those.
It (should) work correctly.. You can find it here : http://www.friggeri.net/blog/2007/07/13/vimpress/
Adrien
August 10th, 2007 at 9:47 pm
[…] Posting to Wordpress from Vim with tags and Markdown » Tri Nguyen Says: June 15th, 2006 at 11:48 am […]
March 10th, 2008 at 1:15 am
[…] This is quite experimental (and unmaintained): I’m currently writing this article using the only decent text editor: Vim. That’s why I wrote a plugin inspired bye this one […]
March 11th, 2008 at 10:08 pm
[…] This is quite experimental (and unmaintained): I’m currently writing this article using the only decent text editor: Vim. That’s why I wrote a plugin inspired by this one […]
April 5th, 2008 at 6:16 pm
[…] Your page is on StumbleUpon […]
April 28th, 2008 at 2:18 am
August 27th, 2008 at 7:36 am
[…] As a dedicated Vim user, particularly after MacVim received some much-deserved attention, I was happy to read this post by Tri Nguyen: Posting to Wordpress from Vim with tags and Markdown”. […]