emacsweblogs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Emacsweblogs] Maintaining the same blog on two servers


From: Mark A. Hershberger
Subject: Re: [Emacsweblogs] Maintaining the same blog on two servers
Date: Tue, 02 Mar 2010 16:33:42 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.90 (gnu/linux)

Ted Smith <address@hidden> writes:

> I've never really hacked emacs lisp seriously, nor do I have much
> expertise in other lisps, so I doubt I'd be able to do it. If you think
> it would be simple for a beginner and were willing to hold my hand a
> bit, I could give it a shot, though...

Sure.  So, you can see the basic structure of a defun from looking
around, but if you need a more information on that, see the intro to
emacs lisp.
http://www.gnu.org/software/emacs/emacs-lisp-intro/html_node/defun.html

For simplicity, we'll assume that all the weblogs we're publishing to
are WordPress.  There are some differences between how weblogger.el
interacts with different weblog engines, but we won't let that bother us
for the moment.

Let's create a new key binding to publish to all configured weblogs in
weblogger-config-alist.  (Later we may want to be able to set up a way
to only publish to some of those, but, for now, let's keep it simple.)

Find the line reading

    (define-key map "\C-c\C-e" 'weblogger-toggle-edit-body)

And add

    (define-key map (kbd "C-c C-m") 'weblogger-publish-to-all-weblogs)

(I'm switching to kbd here because it seems to provide a little better
cross-emacsen support.)

For information on key binding conventions, what is acceptable and what
isn't, see
http://www.gnu.org/software/emacs/elisp/html_node/Key-Binding-Conventions.html

Now we need to write the weblogger-publish-to-all-weblogs function:

(defun weblogger-publish-to-all-weblogs ()
  "Publish to all weblogs in weblog-config-alist."
  (interactive)
  (let ((entry (weblogger-entry-buffer-to-struct))
        weblogger-config-name weblogger-server-url
        weblogger-server-username weblogger-server-password
        weblogger-weblog-id)
    (mapc (lambda (config)
            (weblogger-switch-configuration (car config))
            (weblogger-api-new-entry entry t))
          weblogger-config-alist)))

If you're reading this email in Emacs, you can put the point right
after the last parenthesis and hit “C-x C-e” to evaluate and load it.
Otherwise, you can copy-paste the above function into your *scratch*
buffer and use “C-x C-e” to evaluate it.

Notes on this function:

1. (interactive)

   This is required to tell Emacs that this function is interactive an
   should be made available to keystrokes.

2. (let ((entry (weblogger-entry-buffer-to-struct)) …

   Call the function weblogger-entry-buffer-to-struct and store the
   result in the temporary variable named “entry”.

3. (let (…
         weblogger-config-name weblogger-server-url
         weblogger-server-username weblogger-server-password
         weblogger-weblog-id)

    Temporarily bind these variables to nil.  Their old values will be
    restored once we're out of let's scope.

4. (mapc …
       weblogger-config-alist)

   mapc will iterate over each item in the variable
   weblogger-config-alist and pass it as an argument to the lambda
   function I've elided here.  This variable looks like this on my
   system:

    (("blogger test" "http://www.blogger.com/api"; "address@hidden" "" "2462596")
     ("wp_test" "http://www.winkyfrown.com/wordpress/xmlrpc.php"; "hexmode" "" 
"1")
     ("movable_type" "http://www.winkyfrown.com/cgi-bin/mt/mt-xmlrpc.cgi"; 
"hexmode" "" "1")
     ("wordpress" "http://hexmode.wordpress.com/xmlrpc.php"; "hexmode" "" 
"1647614")
     ("default" "http://www.openweblog.com/interface/blogger/"; "hexmode" "" 
"hexmode"))

   On the first call, the lambda function will get

    ("blogger test" "http://www.blogger.com/api"; "address@hidden" "" "2462596")

   in its config argument.  On the second call, 

    ("wp_test" "http://www.winkyfrown.com/wordpress/xmlrpc.php"; "hexmode" "" 
"1")

   and so on.

5. (lambda (config)
      (weblogger-switch-configuration (car config))
      (weblogger-api-new-entry entry t))

   The lambda just creates an anonymous function and returns it to mapc
   for use.  This function is pretty simple, it just switches the
   configuration.  Note that (car config) just returns the first element
   of config ("blogger test", "wp_test", etc) which then gets passed to
   weblogger-switch-configuration.

   Behind the scenes, weblogger-switch-configuration is changing those
   variables we temporarily bound to nil back in step 3.

   weblogger-api-new-entry takes the entry we stored in step 2 and
   published it.  The t indicates that the entry should be published and
   not saved as a draft (t and nil are Emacs Lisp's booleans).

6. I've split out some functionality from weblogger-select-configuration
   (an interactive function) and put it in a utility function.  You'll
   need this as well:

   (defun weblogger-switch-configuration (config)
    "Switch the configuration."
    (let ((conf (cdr (assoc config weblogger-config-alist))))
      (setq weblogger-config-name config
            weblogger-server-url      (nth 0 conf)
            weblogger-server-username (nth 1 conf)
            weblogger-server-password (nth 2 conf)
            weblogger-weblog-id       (nth 3 conf))))

I've tested the above functions and they work.

Oh, if you look over this, you'll notice that I don't store my passwords
in weblogger-config-alist.  If you (or anyone else) is interested, I'll
show you how you can store them in a separate file that is optionally
GPG-encrypted.

> Thanks again! If all free software communities/developers were as
> inviting as you, we'd be a lot farther along the World Domination
> Roadmap :-)

Aw, gee…  Well, don't get too used to it.  I had a lot of
procrastinating to do today. ;)

Mark.

-- 
http://hexmode.com/

The only alternative to Tradition is bad tradition.
                          — Jaraslov Pelikan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]