Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

(too) big round of changes. Allow starting new post in current buffer…

…. Add function to check whether this post is properly initialized before sendingg - not used yet! Add unit tests using el-expectations, coverage low so far.
  • Loading branch information...
commit 25e342a75292bfada0e8e6d324312b6db63d8f56 1 parent 41969c1
Thomas Kappler authored

Showing 1 changed file with 113 additions and 21 deletions. Show diff stats Hide diff stats

  1. +113 21 wpmail.el
134 wpmail.el
@@ -37,8 +37,8 @@
37 37 ;; functions for details.
38 38
39 39 ;;; Dependencies:
40   -;; Message from Gnus. It is included in Emacs, at least in version 23
41   -;; which I'm using. Tested with Gnus v5.13.
  40 +;; Message from Gnus. It is included in Emacs, at least in version
  41 +;; 23. Tested with Gnus v5.13.
42 42
43 43 ;;; Installation:
44 44 ;; Customize the variables at the top of the code section, and
@@ -47,6 +47,17 @@
47 47 ;;; History:
48 48 ;; 2009-07: First release, git hash 4005549c87a95b7f0817d0cb9531118b505e7ad7
49 49
  50 +;;; TODO
  51 +
  52 +;; When proposing the file name for a title, remove suffixes.
  53 +
  54 +;; Function or buffer-local variable "initialized-p" that records if
  55 +;; current buffer was initialized using wpmail-new-post, so it has a
  56 +;; title etc. If not, ask for these things before sending. On second
  57 +;; thought, do we even need wpmail-new-post? Just let the user work,
  58 +;; and ask when sending. This stuff is not stored across sessions
  59 +;; anyway.
  60 +
50 61 ;;; Code:
51 62 (require 'message)
52 63
@@ -58,13 +69,21 @@ there. You don't need to save the files at all, however.")
58 69 (defconst wpmail-post-email "FOO@post.wordpress.com"
59 70 "The e-mail address you got from wordpress.com to send posts to.")
60 71
61   -(defvar wpmail-categories '("Own Code" "Stuff" "Weekly Links" "Weekly Reading")
  72 +(defvar wpmail-categories '("Academia"
  73 + "Best Practices"
  74 + "Links"
  75 + "Musings"
  76 + "Nitty Gritty"
  77 + "Own Code"
  78 + "Stuff"
  79 + "Theory")
62 80 "A list of the categories you use for blog posts.
63   -When starting a new post, wpmail will ask you for the
64   -category. These will be available for tab completion. However,
65   -you can also give a category that is not in this list.")
  81 +When starting a new post, wpmail will ask you for the category.
  82 +These will be available for tab completion. You can also give a
  83 +category that is not in this list, but your wordpress must know
  84 +it.")
66 85
67   -(defvar wpmail-default-tags "code,programming,testing"
  86 +(defvar wpmail-default-tags "programming"
68 87 "A list of post tags that will appear whenever you start a new post.")
69 88
70 89 (defconst wpmail-category-is-also-tag t
@@ -79,7 +98,7 @@ you can also give a category that is not in this list.")
79 98 From http://www.math.umd.edu/~halbert/dotemacs.html."
80 99 (replace-regexp-in-string "\\(^[ \t\n]*\\|[ \t\n]*$\\)" "" string))
81 100
82   -(defun wpmail-options-for-post-title ()
  101 +(defun wpmail-possible-titles ()
83 102 "Make a list of suggestions for a blog post title.
84 103 The list contains at least the buffer name. It also contains
85 104 some text around point, if it's not empty and not too long."
@@ -102,11 +121,11 @@ some text around point, if it's not empty and not too long."
102 121 (buffer-substring (region-beginning) (region-end))
103 122 (buffer-substring (point-min) (point-max))))
104 123
105   -;; End helpers -------------------------------------------
  124 +;; End helpers ---------------------------------------------
106 125
107 126 (defvar wpmail-post-title "wpmail.el post"
108 127 "The post's title when sending it off.
109   -Should be set via `wpmail-new-post'.")
  128 +Will be set by `wpmail-new-post' or `wpmail-new-post-here'.")
110 129
111 130 (defun wpmail-new-post (title category init-content)
112 131 "Start a new wordpress blog post.
@@ -137,35 +156,52 @@ change the post tags or the status. See
137 156 <http://support.wordpress.com/post-by-email/> for documentation
138 157 about shortcodes."
139 158 (interactive (list
140   - (read-string "Title: " nil nil (wpmail-options-for-post-title) nil)
  159 + (read-string "Title: " nil nil (wpmail-possible-titles) nil)
141 160 (completing-read "Category: " wpmail-categories)
142 161 current-prefix-arg))
143 162 (let ((content (if init-content (wpmail-buffer-or-region) nil)))
144   - (wpmail-initialize-new-post title category content)))
  163 + (wpmail-initialize-new-file title category content)))
  164 +
  165 +(defun wpmail-new-post-here (title category)
  166 + "Start a new wordpress blog post in the current buffer.
  167 +The buffer's content will be left untouched, and the wordpress
  168 +shortcodes will be inserted at the end."
  169 + (interactive (list
  170 + (read-string "Title: " nil nil (wpmail-possible-titles) nil)
  171 + (completing-read "Category: " wpmail-categories)))
  172 + (wpmail-initialize-this-buffer title category (point)))
145 173
146   -(defun wpmail-initialize-new-post (title category content)
  174 +(defun wpmail-initialize-new-file (title category content)
147 175 "Does the actual work after wpmail-new-post got the user's input."
148 176 (unless content (setq content ""))
149 177 (wpmail-create-and-show-new-post-buffer title category content)
150   - (set-visited-file-name (concat wpmail-posts-dir "/" title ".wordpress"))
151   - (set (make-local-variable 'wpmail-post-title) title))
  178 + (set-visited-file-name (wpmail-path-to-post-file title)))
  179 +
  180 +(defun wpmail-path-to-post-file (title)
  181 + "Find the path to a file with blog post TITLE.
  182 +The file will be in wpmail-posts-dir if non-nil, in the current
  183 +directory otherwise. The suffix depends on wpmail-use-markdown."
  184 + (let ((dir (if wpmail-posts-dir wpmail-posts-dir ".")))
  185 + (concat dir "/" title ".wp")))
152 186
153 187 (defun wpmail-create-and-show-new-post-buffer (title category content)
154 188 "Create a new buffer named TITLE and initialize it."
155 189 (let ((post-buffer (get-buffer-create title)))
156 190 (set-buffer post-buffer)
157   - (goto-char (point-max))
158   - (insert "\n")
159   - (insert content)
160   - (insert (wpmail-initial-shortcodes category wpmail-default-tags))
161   - (goto-char (point-min))
  191 + (wpmail-initialize-this-buffer title category (point-min))
162 192 (switch-to-buffer post-buffer)))
163 193
  194 +(defun wpmail-initialize-this-buffer (title category restore-point)
  195 + (set (make-local-variable 'wpmail-post-title) title)
  196 + (goto-char (point-max))
  197 + (insert "\n\n"
  198 + (wpmail-initial-shortcodes category wpmail-default-tags))
  199 + (goto-char restore-point))
  200 +
164 201 (defun wpmail-initial-shortcodes (category tags)
165 202 "Return the wordpress shortcodes as a string; see wpmail-new-post."
166 203 (mapconcat 'identity
167 204 (list
168   - "\n"
169 205 (concat "[category " category "]")
170 206 (concat "[tags " tags
171 207 (if wpmail-category-is-also-tag (concat "," category) "")
@@ -192,6 +228,62 @@ Partly copied from Trey Jackson
192 228 (insert content)
193 229 (message-send-and-exit)))
194 230
  231 +(defun wpmail-post-configured-p ()
  232 + "Determine whether we're ready to send the current buffer."
  233 + (and (boundp 'wpmail-post-title)
  234 + (save-excursion
  235 + (goto-char (point-min))
  236 + (search-forward "[status " nil t))))
  237 +
  238 +
  239 +;; Unit tests, using el-expectations by rubikitch,
  240 +;; <http://www.emacswiki.org/emacs/EmacsLispExpectations>.
  241 +;; ---------------------------------------------------------
  242 +
  243 +(eval-when-compile
  244 + (when (fboundp 'expectations)
  245 + (expectations
  246 +
  247 + ;; helpers
  248 +
  249 + (desc "trim")
  250 + (expect "foo"
  251 + (wpmail-trim "foo"))
  252 + (expect "foo"
  253 + (wpmail-trim "foo "))
  254 + (expect "foo"
  255 + (wpmail-trim " foo "))
  256 + (expect "foo bar"
  257 + (wpmail-trim " foo bar "))
  258 +
  259 + ; That'd be nice, but doesn't work with el-expectations.
  260 + ; (dolist foo '("foo" " foo" "foo " " foo ")
  261 + ; (expect "foo" (wpmail-trim foo)))
  262 +
  263 + (desc "possible-titles contains buffer name")
  264 + (expect (non-nil)
  265 + (memq (buffer-name) (wpmail-possible-titles)))
  266 +
  267 + ;; wpmail
  268 +
  269 + (desc "post-configured-p")
  270 + (expect nil
  271 + (with-temp-buffer
  272 + (wpmail-post-configured-p)))
  273 + (expect (non-nil)
  274 + (with-temp-buffer
  275 + (set (make-local-variable 'wpmail-post-title) "title")
  276 + (insert "[status draft]")
  277 + (wpmail-post-configured-p)))
  278 +
  279 + (desc "initialize-this-buffer")
  280 + (expect (non-nil)
  281 + (with-temp-buffer
  282 + (wpmail-initialize-this-buffer "title" "category" (point-min))
  283 + (wpmail-post-configured-p))))))
  284 +
  285 +;; End unit tests. -----------------------------------------
  286 +
195 287
196 288 (provide 'wpmail)
197 289 ;;; wpmail.el ends here

0 comments on commit 25e342a

Please sign in to comment.
Something went wrong with that request. Please try again.