Peter's Blog

Redefining the Impossible

Posting to Drupal from VIM


It is possible to post blog entries to Drupal using VIM. The only caveat is that VIM should have the python extensions compiled into it. Also, Drupal should have the blogapi enabled to allow posts to be submitted or read via XMLRPC. The following code is put in the .vimrc file:

python << EOF

strUserName = 'me'
strPassword = 'bigsecret'
strDrupal = 'http://my_server.com'

def PostBlog():
    import xmlrpclib
    import re

    #
    # 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( '', strUserName, strPassword, oPost, 1)
    else:
        bSuccess = oDrupal.metaWeblog.editPost( strID, strUserName, strPassword, oPost, 1)

    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 = None):
    import xmlrpclib

    oDrupal = xmlrpclib.ServerProxy( strDrupal + '/xmlrpc.php')
    if strID:
        oBlog = oDrupal.metaWeblog.getPost( strID, strUserName, strPassword)
    else:
        oBlogs = oDrupal.metaWeblog.getRecentPosts( '', strUserName, strPassword, 1)
        oBlog = oBlogs[0]
        strID = oBlog['postid']

    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

To use this, type a blog entry into a buffer. The first line should be the title of the posting, the remaining lines are the body. For example:

Thoughts from a Blogger
I am a rampant blogger.
Heaven help me.

Then, from the VIM command line execute:

:py PostBlog()

The ID for the new posting will be displayed. A posting can be edited using:

:py ReadBlog( <ID>)

e.g.
:py ReadBlog( '451')

to read the posting and

:py PostBlog()

to post it again after editing it.

You can also edit the most recent posting by entering:

:py ReadBlog()

This could be expanded a lot more if I had the time, for example:

  • Show list of previous posts to open and edit
  • Map ':w' to post the blog entry
  • Use pure VIM code if that supports xmlrpc (?)

Filed under: blog drupal php python vim

6 Comments

Peter Says:

over 5 years ago

I use the MozEX extension to Firefox. Among other things, it can open up the contents of any textarea in VIM.

-Neil http://delocalizedham.com/

Peter Says:

over 5 years ago

Wonderful idea.

I installed this but I could not configure it to do anything in Firefox 0.93, probably because it is an old extension (18 months without an update) and is incompatible with the new way of doing extensions. I could:

  • figure out how to configure it and nurse it into life
  • figure out how to write extensions and fix the source code

but I don't have the time.

Peter

Peter Says:

over 5 years ago

Right, I did have to manually copy the perferences out of an older version that worked more. Type 'about:config' into your url bar to get a place to enter these or manually edit prefs.js when the browser is closed. Here is what grep turns up for me:

.mozilla/firefox/hovf37sd.user2/prefs.js:user_pref("mozex.command.mailer", "/usr/X11R6/bin/xterm -bg black -fg white -vb +sb -geometry 80x24 -e mutt -s %s %a -b %Z"); .mozilla/firefox/hovf37sd.user2/prefs.js:user_pref("mozex.command.source", "/usr/X11R6/bin/xterm -bg black -fg white -vb +sb -geometry 80x24 -e view %t"); .mozilla/firefox/hovf37sd.user2/prefs.js:user_pref("mozex.command.textarea", "/usr/X11R6/bin/xterm -bg black -fg white -vb +sb -geometry 80x24 -e vim %t");

Peter Says:

over 5 years ago

Yes this works nicely. I did the following:

  • Installed mozex
  • Opened about:config
  • right clicked and created a new string
  • called the string mozex.command.textarea
  • gave it the value c:\bin\vim\vim62\gvim.exe %t

Now I can right click on my textareas and edit them in VIM: like I am doing now.

I am still posting long entrys as I described defore but this will be very useful!

One oddity: after exiting VIM I have to click on the text area to see the changes.

Peter

Peter Says:

over 5 years ago

Update: it worked on windows XP but on Win2k it is necessary to create a directory called c:\Windows\Temp for the temporary files: it is assuming it is running on Windows 98 or somesuch.

If I had the time I'd really like a go at fixing these problems as this could be very useful to me and TextAreas are pretty horrible for editing. Unfortunately I am a total Firefox extension n00b and I would have to find the time.

Peter

beau Says:

about 1 year ago

what about editing or deleting a post?

Sorry but comments on this post are now closed.