Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 277 lines (211 sloc) 9.567 kb
03e3b9af »
2012-02-08 add library commentary from README
1 ;;; posterous.el --- Emacs integration for posterous.com
d90ae9b6 »
2010-05-14 Initial commit.
2
e481fa8e »
2012-02-27 Tidied documentation which should now conform to emacs-wiki and GNU g…
3 (defvar posterous-version "0.2")
4
5 ;; Maintainer: Sarah Mount <snim2@snim2.org>
6 ;; Author: Sarah Mount <snim2@snim2.org>
7 ;; Jonas Bernoulli <jonasbernoulli@gmail.com>
8 ;; zanoni https://github.com/zanoni
9 ;; Tim Disney <tim.disney@gmail.com>
10 ;; Version: 0.2
d90ae9b6 »
2010-05-14 Initial commit.
11 ;; Created: 14 May 2010
03e3b9af »
2012-02-08 add library commentary from README
12 ;; Keywords: posterous blog
e481fa8e »
2012-02-27 Tidied documentation which should now conform to emacs-wiki and GNU g…
13 ;; Last-Updated: Mon Feb 27 08:50:54 GMT 2012
14 ;; By: Sarah Mount
15 ;; Update: Tidied documentation, fixed posterous-new-markdown-post
16 ;; URL: https://github.com/snim2/posterous.el
d90ae9b6 »
2010-05-14 Initial commit.
17
18 ;; This file is NOT part of GNU Emacs.
19
20 ;; This is free software; you can redistribute it and/or modify it under
21 ;; the terms of the GNU General Public License as published by the Free
22 ;; Software Foundation; either version 2, or (at your option) any later
23 ;; version.
24 ;;
25 ;; This is distributed in the hope that it will be useful, but WITHOUT
26 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
27 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 ;; for more details.
29 ;;
30 ;; You should have received a copy of the GNU General Public License
31 ;; along with GNU Emacs; see the file COPYING. If not, write to the
32 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 ;; MA 02111-1307, USA.
34
03e3b9af »
2012-02-08 add library commentary from README
35 ;;; Commentary:
36
37 ;; Emacs integration for posterous.com.
38 ;; It currently supports posting text (only) to posterous.
39
40 ;; Place the .el file somewhere on you load path (e.g. ~/.emacs.d/) and
41 ;; set the following variables in your .emacs or init.el file:
42
43 ;; posterous-email -- your email address or username on posterous.com
44 ;; posterous-password -- your password on posterous.com
45
46 ;; and you can interact with posterous.com using the functions:
47
48 ;; M-x posterous-getsites
49 ;; -- places a list of sites that you own on posterous.com.
50 ;; -- Includes siteids.
51 ;; -- Currently still formatted as XML.
52
53 ;; M-x posterous-region
54 ;; -- post a region of text to posterous
55
56 ;; M-x posterous-region-private
57 ;; -- post a region of text to posterous as a private post.
58
59 ;; M-x posterous-buffer
60 ;; -- post a buffer of text to posterous.
61
62 ;; M-x posterous-buffer-private
63 ;; -- post a buffer of text to posterous as a private post.
64
e481fa8e »
2012-02-27 Tidied documentation which should now conform to emacs-wiki and GNU g…
65 ;; M-x posterous-new-markdown-post
66 ;; -- Create a new post with a nice markdown template and put into
67 ;; markdown mode.
68
69
03e3b9af »
2012-02-08 add library commentary from README
70 ;; If you do not wish to post to your default posterous site, you can
71 ;; customise the variable:
72
73 ;; posterous-default-siteid
74
75 ;; to discover the siteids of all your sites, use M-x posterous-getsites.
76
77 ;; If you do not wish to autopost to your social networks connected to
78 ;; your posterous.com account, place the following in your Emacs
79 ;; configuration file:
80
81 ;; (setq posterous-suppress-autopost)
82
83 ;;; Code:
d90ae9b6 »
2010-05-14 Initial commit.
84
85 (require 'url)
86 (require 'xml)
87
88
89 (defcustom posterous-email nil
90 "Email address or username on posterous.com."
91 :type 'string
92 :group 'posterous)
93
94
95 (defcustom posterous-password nil
96 "User password for posterous.com."
97 :type 'string
98 :group 'posterous)
99
100
101 (defcustom posterous-default-siteid nil
e481fa8e »
2012-02-27 Tidied documentation which should now conform to emacs-wiki and GNU g…
102 "Siteid for default posterous blog to post to. If this variable is null the user's default site will be used."
d90ae9b6 »
2010-05-14 Initial commit.
103 :type 'string
104 :group 'posterous)
105
106
107 (defcustom posterous-suppress-autopost nil
108 "Do NOT automatically send posterous posts to social networks."
109 :type 'boolean
110 :group 'posterous)
111
112
113 (defconst posterous-base-url "http://posterous.com/api/"
114 "Base posterous URL.")
115
116
117 (defconst posterous-getsites-url (concat posterous-base-url "getsites")
118 "Posterous URL for retrieving a list of posterous blogs.")
119
120
121 (defconst posterous-post-url (concat posterous-base-url "newpost")
122 "Posterous URL for posting new content.")
123
124
125 (defconst posterous-update-url (concat posterous-base-url "updatepost")
126 "Posterous URL for updating an existing post.")
127
128
129 (defconst posterous-comment-url (concat posterous-base-url "newcomment")
130 "Posterous URL for posting a new comment on an existing post.")
131
132
133 (defconst posterous-result-buffer "*posterous*"
134 "Buffer name for copying responses from posterous.com.")
135
e481fa8e »
2012-02-27 Tidied documentation which should now conform to emacs-wiki and GNU g…
136 (defun posterous-new-markdown-post ()
137 "Create a new post with a nice markdown template and put into markdown mode."
138 (interactive)
139 (let (title)
140 (setq title (read-from-minibuffer "Post title: "))
141 (switch-to-buffer (get-buffer-create title))
142 (markdown-mode)
143 (insert "<markdown>\n\n</markdown>"))
144 )
7a159154 »
2010-10-06 adding fcn to create a new markdown-ready post
145
d90ae9b6 »
2010-05-14 Initial commit.
146
147 (defun posterous-getsites ()
148 "Get a list of sites owned by this user."
149 (interactive)
150 (let ((url-request-method "GET")
e481fa8e »
2012-02-27 Tidied documentation which should now conform to emacs-wiki and GNU g…
151 (url-request-extra-headers
152 `(("Authorization" . ,(base64-encode-string
153 (format "%s:%s" posterous-email posterous-password))))))
154 (save-excursion
155 (if (null posterous-email)
156 (setq posterous-email (read-from-minibuffer "Email address: ")))
157 (if (null posterous-password)
158 (setq posterous-password (read-from-minibuffer "Posterous password: ")))
159 (url-retrieve posterous-getsites-url 'posterous-getsites-callback))))
d90ae9b6 »
2010-05-14 Initial commit.
160
161
162 (defun posterous-post-base (post private media title tags autopost)
e481fa8e »
2012-02-27 Tidied documentation which should now conform to emacs-wiki and GNU g…
163 "Generic POST to posterous function. Called by posterous-post-generic."
164 (let ((url-request-method "POST")
165 (url-request-extra-headers
166 `(("Content-Type" . "application/x-www-form-urlencoded")
167 ("Authorization" . ,(base64-encode-string
168 (format "%s:%s" posterous-email posterous-password)))))
169 (url-request-data
170 (concat "site_id=" (url-hexify-string posterous-default-siteid)
171 "&title=" (url-hexify-string title) ;; title
172 "&body=" (url-hexify-string post) ;; body
173 "&autopost=" (url-hexify-string autopost) ;; autopost
174 "&private=" (url-hexify-string private) ;; private
175 "&tags=" (url-hexify-string tags) ;; tags
176 "&media=" (url-hexify-string media) ;; media
177 )))
178 (url-retrieve posterous-post-url 'posterous-post-callback)))
d90ae9b6 »
2010-05-14 Initial commit.
179
180
181 (defun posterous-post-generic (start end private media)
182 "Generic post function called by all interactive functions."
183 (let (post title tags autopost)
e481fa8e »
2012-02-27 Tidied documentation which should now conform to emacs-wiki and GNU g…
184 (setq post (buffer-substring start end))
185 (setq title (read-from-minibuffer "Post title: "))
186 (setq tags (read-from-minibuffer "Tags (comma-separated): "))
187 (if (null posterous-email)
188 (setq posterous-email (read-from-minibuffer "Email address: ")))
189 (if (null posterous-password)
190 (setq posterous-password (read-from-minibuffer "Posterous password: ")))
191 (if posterous-suppress-autopost
192 (setq autopost "1")
193 (setq autopost "0"))
194 (posterous-post-base post private media title tags autopost)))
d90ae9b6 »
2010-05-14 Initial commit.
195
196
197 (defun posterous-region (start end)
198 "Post a region of text to posterous."
199 (interactive "r")
200 (save-excursion
e481fa8e »
2012-02-27 Tidied documentation which should now conform to emacs-wiki and GNU g…
201 (posterous-post-generic start end "0" "")))
d90ae9b6 »
2010-05-14 Initial commit.
202
203
204 (defun posterous-region-private (start end)
205 "Post a region of text to posterous as a private post."
206 (interactive "r")
207 (save-excursion
e481fa8e »
2012-02-27 Tidied documentation which should now conform to emacs-wiki and GNU g…
208 (posterous-post-generic start end "1" "")))
d90ae9b6 »
2010-05-14 Initial commit.
209
210
211 (defun posterous-buffer ()
212 "Post a whole buffer to posterous."
213 (interactive)
214 (save-excursion
e481fa8e »
2012-02-27 Tidied documentation which should now conform to emacs-wiki and GNU g…
215 (posterous-region (point-min) (point-max))))
d90ae9b6 »
2010-05-14 Initial commit.
216
217
218 (defun posterous-buffer-private ()
219 "Post a whole buffer to posterous as a private post."
220 (interactive)
221 (save-excursion
e481fa8e »
2012-02-27 Tidied documentation which should now conform to emacs-wiki and GNU g…
222 (posterous-region (point-min) (point-max))))
d90ae9b6 »
2010-05-14 Initial commit.
223
224
225 (defun posterous-getsites-callback (response)
e481fa8e »
2012-02-27 Tidied documentation which should now conform to emacs-wiki and GNU g…
226 "Deal with an HTTP RESPONSE from posterous.com. Remove the HTTP header, parse the XML returned from posterous."
d90ae9b6 »
2010-05-14 Initial commit.
227 (rename-buffer posterous-result-buffer)
228 ;;; Delete HTTP header
229 (goto-char (point-min))
230 (search-forward-regexp "\n\n")
231 (delete-region (point-min) (point))
232 (set-buffer-modified-p nil)
233 (switch-to-buffer (current-buffer)))
234 ;;; Parse XML returned by posterous.com
235 ; (let* ((root (xml-parse-region (point-min) (point-max)))
236 ; (rsp (car root)))))
237
238
239 (defun posterous-post-callback (response)
240 "Deal with an HTTP response from posterous.com.
241 Remove the HTTP header, parse the XML returned from posterous.
242 "
243 (rename-buffer posterous-result-buffer)
244 ;;; Delete HTTP header
245 (goto-char (point-min))
246 (search-forward-regexp "\n\n")
247 (delete-region (point-min) (point))
248 (set-buffer-modified-p nil)
249 (switch-to-buffer (current-buffer))
250 ;;; Parse XML returned by posterous.com
251 (let* ((root (xml-parse-region (point-min) (point-max)))
e481fa8e »
2012-02-27 Tidied documentation which should now conform to emacs-wiki and GNU g…
252 (rsp (car root))
253 (attrs (xml-node-attributes rsp))
254 (stat (cdr (assq 'stat attrs))))
255 (cond ((null stat)
256 (message "No status returned from posterous.com. Unknown error."))
257 ;;; Posting was successful.
258 ((string-equal stat "ok")
259 (let* ((post (car (xml-get-children rsp 'post)))
260 (id (car (xml-get-children post 'id)))
261 (text-id (car (xml-node-children id)))
262 (url (car (xml-get-children post 'url)))
263 (text-url (car (xml-node-children url))))
264 (message "New post at has id %s at: %s" text-id text-url)))
265 ;;; Posting failed.
266 ((string-equal stat "fail")
267 (let* ((err (car (xml-get-children rsp 'err)))
268 (err-attrs (xml-node-attributes err))
269 (msg (cdr (assq 'msg err-attrs))))
270 (message "Posting to posterous failed: %s." msg)))))
d90ae9b6 »
2010-05-14 Initial commit.
271 (kill-buffer (current-buffer)))
272
273
274 (provide 'posterous)
275
e481fa8e »
2012-02-27 Tidied documentation which should now conform to emacs-wiki and GNU g…
276 ;;; posterous.el ends here
Something went wrong with that request. Please try again.