Posting blog entry to Drupal from within Vim

Some people may wonder why would anyone want to do this? Well, there are at least two benefits. One, I can write posts offline with my favorite editor, whenever I feel like it. Then post them from within vim once I got an Internet connection. Two, I get to use niceties of vim such as spellchecker, autocompletion and syntax highlighting for html code. I found this solution on Peter Wilkinson's Blog. I did a few minor changes to make his script to work with the latest version of python. A step further, I combined it with his another post, now I can even use the regular vim syntax to read/post blog entry just like a regular file, e.g. :e blog/4 to edit blog entry number 4, :w to post the edited version. Very cool. See the following .vimrc code for details:


" post blog entry to my Drupal site
" Use :e blog/nodeID_which_is_digits to open an existing entry for editting;
"     For example :e blog/12
" Use :e blog/anything_other_than_digits to open a new entry for editing
"     For example :e blog/blah
" Use :w to post it.
" Use :w blog/anything to post a file as a new blog entry

python << EOF

strUserName = 'your_username'
strPassword = 'your_password'
strDrupal = 'http://your.domain.name'

import vim
import xmlrpclib
import re

def PostBlog():

  #
  # If first line contains a blog entry ID then edit existing post,
  # otherwise write a new one.
  #
  nFirstLine = 0
  strID = vim.current.buffer[0]
  if not re.match( '^\d+$', strID):
    strID = ''
  else:
    nFirstLine = 1

  strTitle = vim.current.buffer[nFirstLine]
  strText = "\n".join( vim.current.buffer[nFirstLine+1:])

  oDrupal = xmlrpclib.ServerProxy( strDrupal + '/xmlrpc.php')

  oPost = { 'title': strTitle, 'description': strText}

  if strID == '':
    strID = oDrupal.metaWeblog.newPost( 'blog', strUserName, strPassword, oPost, True)
  else:
    bSuccess = oDrupal.metaWeblog.editPost( strID, strUserName, strPassword, oPost, True)

  print "Posted entry %s" % strID

  #
  # Don't intend to write posts to disk so unmodify the buffer and
  # allow easy quit from VIM.
  #
  vim.command( 'set nomodified')

def ReadBlog( strID ):
  
  #
  # So html plugin is automatically enabled for editing the post
  # with auto-completion and syntax highlighting
  #
  vim.command('setfiletype html')

  if not strID.isdigit():
    print "New blog entry"
    return

  oDrupal = xmlrpclib.ServerProxy( strDrupal + '/xmlrpc.php')

  oBlog = oDrupal.metaWeblog.getPost( strID, strUserName, strPassword )

  vim.current.buffer[:] = []
  vim.current.buffer[0] = strID
  vim.current.buffer.append( oBlog['title'])
  vim.current.buffer.append( '')
  for strLine in oBlog['description'].split('\n'):
    vim.current.buffer.append( strLine)

EOF

:au BufWriteCmd blog/* py PostBlog() 
:au BufReadCmd blog/* py ReadBlog(vim.eval("expand('<afile>:t')"))

syntax on

Comments

Johnebyne's picture

Hello world

Hi

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <div> <h1><h2><h3><sub><sup><b><i><u><font><img>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options

To prevent automated spam submissions leave this field empty.
Nice place