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:
  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.

Tags:

21 Responses to “Posting to Wordpress from Vim with tags and Markdown”

  1. Markdown Says:

    links from Technorati Starting post:Posting to Wordpress from Vim with tags and Markdown by at Google Blog Search: markdown

  2. Markdown Says:

    links from Technorati Starting post:Posting to Wordpress from Vim with tags and Markdown by at Google Blog Search: markdown

  3. Kesava Yerra Says:

    Glad you found it useful!

  4. René Says:

    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. :)

  5. Polska Planeta WordPress’a Says:

    links from TechnoratiSandbox · plaintxt.orgEinloggen - WordDokuBluesome - Exec-PHP plugin for WordpressWarpspire » Hemingway for WordpressGoogle Analytics and Feedburner Reports plugin for WordPress : tan tan noodles - msg free since 2005Posting to Wordpress from Vim with tags and Markdown » Tri NguyenMySQL Connection Management in PHP - How (Not) To Do Things - Jay PipesCustom WordPress Database Error - 5ThirtyOne - Refreshing isn’t it?Wordpress: Ultimate Tag Warrior 3

  6. Paul Cline Says:

    Um… this is sweet. I wasnt aware of such a feature in vim

  7. In the twinkling of an eye » Vim and WordPress Says:
    Kramer auto Pingback

    […] 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 […]

  8. whiteinge Says:

    Excellent! Thanks a lot for posting this—I’m going to try writing a similar integration for Django.

  9. links for 2007-03-20 | christian wilcox dot com Says:

    […] Posting to Wordpress from Vim with tags and Markdown Â» Tri Nguyen adding this to the todo list (tags: vim wordpress) […]

  10. Blog - ~tigrou/pwet.fr Says:

    links from Technorati de Christophe Blaess et pour le moment, je trouve ce langage très élégant et plutôt agréable à utiliser… Bon j’ai juste pondu 18 lignes de code très inspirées d’un petit hack permettant de poster sur un blog Wordpress depuis Vim . Pour le moment mon code permet d’afficher dans un buffer vim les groupes de classes, les nom des classes et leurs attributs extraits du XML généré par l’extension eZ Publish SmileClasses (la même utilisée par le plugin Eclipse), c’est un début.

  11. Paul Smith Says:

    Bless you, vim nerd.

  12. lifebsideben Says:

    links from Technorati writing interface again… from my Mac anyhow. (Update it seems you can still get the main tagging stuff to work if you turn on ‘embedded tag support’ in UTW and then use the SimpleTag format. Cool! Thanks to this blog post about posting from vim of all things, for the hint!)

  13. Woxidu Says:

    links from Technorati By egwynn on June 12th, 2007. 0 Comments The greatest text editor in the world strikes again. A small vim plugin using python makes it possible for me to post to my wordpress without a web browser, and without a super-fancy blog-posting program. This is ideal for a geek like me who doesn’t need software packed with features to do simple blogging. Go Vim!

  14. Friggeri.net : Archive : Vimpress ! Says:

    […] 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. […]

  15. Adrien .F Says:

    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

  16. Wordpress Posting Vim Script « not.upbylunch Says:

    […] Posting to Wordpress from Vim with tags and Markdown » Tri Nguyen Says: June 15th, 2006 at 11:48 am […]

  17. Friggeri.net » Blog Archive » Vimpress ! Says:

    […] 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 […]

  18. Syntactic Sugar » Blog Archive » Vimpress ! Says:

    […] 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 […]

  19. Your page is now on StumbleUpon! Says:

    […] Your page is on StumbleUpon […]

  20. molok’s nest Says:

    links from Technorati, che viene poi successivamente convertito in HTML. A differenza dell’HTML, è molto più leggibile e non contiene centinaia di inutili tags. Insomma, fatto sta che mi sono ritrovato a leggere questovecchio postsu come utilizzare Markdown con Vim e magicamente anche con Wordpress. Il link allo script nel post sembra essere morto, ma grazie ai poteri della blogosfera e dell’antani, ho trovato una più che valida alternativa:

  21. Blog from Vim at De-escalate Says:

    […] 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”. […]

Leave a Reply

RSS icon On family, tools, and art

Close
Powered by ShareThis