Update contributed slide output to use uppercase properties
[org-mode.git] / contrib / lisp / org-mime.el
blobb635bc14cd9cdd02c3ceebb1cd09ff41a6e40db7
1 ;;; org-mime.el --- org html export for text/html MIME emails
3 ;; Copyright (C) 2010-2013 Eric Schulte
5 ;; Author: Eric Schulte
6 ;; Keywords: mime, mail, email, html
7 ;; Homepage: http://orgmode.org/worg/org-contrib/org-mime.php
8 ;; Version: 0.01
10 ;; This file is not part of GNU Emacs.
12 ;;; License:
14 ;; This program is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; any later version.
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
29 ;;; Commentary:
31 ;; WYSWYG, html mime composition using org-mode
33 ;; For mail composed using the orgstruct-mode minor mode, this
34 ;; provides a function for converting all or part of your mail buffer
35 ;; to embedded html as exported by org-mode. Call `org-mime-htmlize'
36 ;; in a message buffer to convert either the active region or the
37 ;; entire buffer to html.
39 ;; Similarly the `org-mime-org-buffer-htmlize' function can be called
40 ;; from within an org-mode buffer to convert the buffer to html, and
41 ;; package the results into an email handling with appropriate MIME
42 ;; encoding.
44 ;; you might want to bind this to a key with something like the
45 ;; following message-mode binding
47 ;; (add-hook 'message-mode-hook
48 ;; (lambda ()
49 ;; (local-set-key "\C-c\M-o" 'org-mime-htmlize)))
51 ;; and the following org-mode binding
53 ;; (add-hook 'org-mode-hook
54 ;; (lambda ()
55 ;; (local-set-key "\C-c\M-o" 'org-mime-org-buffer-htmlize)))
57 ;;; Code:
58 (require 'cl)
60 (declare-function org-export-string-as "ox"
61 (string backend &optional body-only ext-plist))
63 (defcustom org-mime-use-property-inheritance nil
64 "Non-nil means al MAIL_ properties apply also for sublevels."
65 :group 'org-mime
66 :type 'boolean)
68 (defcustom org-mime-default-header
69 "#+OPTIONS: latex:t\n"
70 "Default header to control html export options, and ensure
71 first line isn't assumed to be a title line."
72 :group 'org-mime
73 :type 'string)
75 (defcustom org-mime-library 'mml
76 "Library to use for marking up MIME elements."
77 :group 'org-mime
78 :type '(choice 'mml 'semi 'vm))
80 (defcustom org-mime-preserve-breaks t
81 "Used as temporary value of `org-export-preserve-breaks' during
82 mime encoding."
83 :group 'org-mime
84 :type 'boolean)
86 (defcustom org-mime-fixedwith-wrap
87 "<pre style=\"font-family: courier, monospace;\">\n%s</pre>\n"
88 "Format string used to wrap a fixedwidth HTML email."
89 :group 'org-mime
90 :type 'string)
92 (defcustom org-mime-html-hook nil
93 "Hook to run over the html buffer before attachment to email.
94 This could be used for example to post-process html elements."
95 :group 'org-mime
96 :type 'hook)
98 (mapc (lambda (fmt)
99 (eval `(defcustom
100 ,(intern (concat "org-mime-pre-" fmt "-hook"))
102 (concat "Hook to run before " fmt " export.\nFunctions "
103 "should take no arguments and will be run in a "
104 "buffer holding\nthe text to be exported."))))
105 '("ascii" "org" "html"))
107 (defcustom org-mime-send-subtree-hook nil
108 "Hook to run in the subtree in the Org-mode file before export.")
110 (defcustom org-mime-send-buffer-hook nil
111 "Hook to run in the Org-mode file before export.")
113 ;; example hook, for setting a dark background in <pre style="background-color: #EEE;"> elements
114 (defun org-mime-change-element-style (element style)
115 "Set new default htlm style for <ELEMENT> elements in exported html."
116 (while (re-search-forward (format "<%s" element) nil t)
117 (replace-match (format "<%s style=\"%s\"" element style))))
119 (defun org-mime-change-class-style (class style)
120 "Set new default htlm style for objects with classs=CLASS in
121 exported html."
122 (while (re-search-forward (format "class=\"%s\"" class) nil t)
123 (replace-match (format "class=\"%s\" style=\"%s\"" class style))))
125 ;; ;; example addition to `org-mime-html-hook' adding a dark background
126 ;; ;; color to <pre> elements
127 ;; (add-hook 'org-mime-html-hook
128 ;; (lambda ()
129 ;; (org-mime-change-element-style
130 ;; "pre" (format "color: %s; background-color: %s;"
131 ;; "#E6E1DC" "#232323"))
132 ;; (org-mime-change-class-style
133 ;; "verse" "border-left: 2px solid gray; padding-left: 4px;")))
135 (defun org-mime-file (ext path id)
136 "Markup a file for attachment."
137 (case org-mime-library
138 ('mml (format (concat "<#part type=\"%s\" filename=\"%s\" "
139 "disposition=inline id=\"<%s>\">\n<#/part>\n")
140 ext path id))
141 ('semi (concat
142 (format (concat "--[[%s\nContent-Disposition: "
143 "inline;\nContent-ID: <%s>][base64]]\n")
144 ext id)
145 (base64-encode-string
146 (with-temp-buffer
147 (set-buffer-multibyte nil)
148 (binary-insert-encoded-file path)
149 (buffer-string)))))
150 ('vm "?")))
152 (defun org-mime-multipart (plain html &optional images)
153 "Markup a multipart/alternative with text/plain and text/html alternatives.
154 If the html portion of the message includes images wrap the html
155 and images in a multipart/related part."
156 (case org-mime-library
157 ('mml (concat "<#multipart type=alternative><#part type=text/plain>"
158 plain
159 (when images "<#multipart type=related>")
160 "<#part type=text/html>"
161 html
162 images
163 (when images "<#/multipart>\n")
164 "<#/multipart>\n"))
165 ('semi (concat
166 "--" "<<alternative>>-{\n"
167 "--" "[[text/plain]]\n" plain
168 (when images (concat "--" "<<alternative>>-{\n"))
169 "--" "[[text/html]]\n" html
170 images
171 (when images (concat "--" "}-<<alternative>>\n"))
172 "--" "}-<<alternative>>\n"))
173 ('vm "?")))
175 (defun org-mime-replace-images (str current-file)
176 "Replace images in html files with cid links."
177 (let (html-images)
178 (cons
179 (replace-regexp-in-string ;; replace images in html
180 "src=\"\\([^\"]+\\)\""
181 (lambda (text)
182 (format
183 "src=\"cid:%s\""
184 (let* ((url (and (string-match "src=\"\\([^\"]+\\)\"" text)
185 (match-string 1 text)))
186 (path (expand-file-name
187 url (file-name-directory current-file)))
188 (ext (file-name-extension path))
189 (id (replace-regexp-in-string "[\/\\\\]" "_" path)))
190 (add-to-list 'html-images
191 (org-mime-file (concat "image/" ext) path id))
192 id)))
193 str)
194 html-images)))
196 (defun org-mime-htmlize (arg)
197 "Export a portion of an email body composed using `mml-mode' to
198 html using `org-mode'. If called with an active region only
199 export that region, otherwise export the entire body."
200 (interactive "P")
201 (require 'ox-org)
202 (require 'ox-html)
203 (let* ((region-p (org-region-active-p))
204 (html-start (or (and region-p (region-beginning))
205 (save-excursion
206 (goto-char (point-min))
207 (search-forward mail-header-separator)
208 (+ (point) 1))))
209 (html-end (or (and region-p (region-end))
210 ;; TODO: should catch signature...
211 (point-max)))
212 (raw-body (concat org-mime-default-header
213 (buffer-substring html-start html-end)))
214 (tmp-file (make-temp-name (expand-file-name
215 "mail" temporary-file-directory)))
216 (body (org-export-string-as raw-body 'org t))
217 ;; because we probably don't want to skip part of our mail
218 (org-export-skip-text-before-1st-heading nil)
219 ;; because we probably don't want to export a huge style file
220 (org-export-htmlize-output-type 'inline-css)
221 ;; makes the replies with ">"s look nicer
222 (org-export-preserve-breaks org-mime-preserve-breaks)
223 ;; dvipng for inline latex because MathJax doesn't work in mail
224 (org-export-with-LaTeX-fragments 'dvipng)
225 ;; to hold attachments for inline html images
226 (html-and-images
227 (org-mime-replace-images
228 (org-export-string-as raw-body 'html t) tmp-file))
229 (html-images (unless arg (cdr html-and-images)))
230 (html (org-mime-apply-html-hook
231 (if arg
232 (format org-mime-fixedwith-wrap body)
233 (car html-and-images)))))
234 (delete-region html-start html-end)
235 (save-excursion
236 (goto-char html-start)
237 (insert (org-mime-multipart
238 body html (mapconcat 'identity html-images "\n"))))))
240 (defun org-mime-apply-html-hook (html)
241 (if org-mime-html-hook
242 (with-temp-buffer
243 (insert html)
244 (goto-char (point-min))
245 (run-hooks 'org-mime-html-hook)
246 (buffer-string))
247 html))
249 (defmacro org-mime-try (&rest body)
250 `(condition-case nil ,@body (error nil)))
252 (defun org-mime-send-subtree (&optional fmt)
253 (save-restriction
254 (org-narrow-to-subtree)
255 (run-hooks 'org-mime-send-subtree-hook)
256 (flet ((mp (p) (org-entry-get nil p org-mime-use-property-inheritance)))
257 (let* ((file (buffer-file-name (current-buffer)))
258 (subject (or (mp "MAIL_SUBJECT") (nth 4 (org-heading-components))))
259 (to (mp "MAIL_TO"))
260 (cc (mp "MAIL_CC"))
261 (bcc (mp "MAIL_BCC"))
262 (body (buffer-substring
263 (save-excursion (goto-char (point-min))
264 (forward-line 1)
265 (when (looking-at "[ \t]*:PROPERTIES:")
266 (re-search-forward ":END:" nil)
267 (forward-char))
268 (point))
269 (point-max))))
270 (org-mime-compose body (or fmt 'org) file to subject
271 `((cc . ,cc) (bcc . ,bcc)))))))
273 (defun org-mime-send-buffer (&optional fmt)
274 (run-hooks 'org-mime-send-buffer-hook)
275 (let* ((region-p (org-region-active-p))
276 (subject (org-export-grab-title-from-buffer))
277 (file (buffer-file-name (current-buffer)))
278 (body-start (or (and region-p (region-beginning))
279 (save-excursion (goto-char (point-min)))))
280 (body-end (or (and region-p (region-end)) (point-max)))
281 (temp-body-file (make-temp-file "org-mime-export"))
282 (body (buffer-substring body-start body-end)))
283 (org-mime-compose body (or fmt 'org) file nil subject)))
285 (defun org-mime-compose (body fmt file &optional to subject headers)
286 (require 'message)
287 (message-mail to subject headers nil)
288 (message-goto-body)
289 (flet ((bhook (body fmt)
290 (let ((hook (intern (concat "org-mime-pre-"
291 (symbol-name fmt)
292 "-hook"))))
293 (if (> (eval `(length ,hook)) 0)
294 (with-temp-buffer
295 (insert body)
296 (goto-char (point-min))
297 (eval `(run-hooks ',hook))
298 (buffer-string))
299 body))))
300 (let ((fmt (if (symbolp fmt) fmt (intern fmt))))
301 (cond
302 ((eq fmt 'org)
303 (require 'ox-org)
304 (insert (org-export-string-as
305 (org-babel-trim (bhook body 'org)) 'org t)))
306 ((eq fmt 'ascii)
307 (require 'ox-ascii)
308 (insert (org-export-string-as
309 (concat "#+Title:\n" (bhook body 'ascii)) 'ascii t)))
310 ((or (eq fmt 'html) (eq fmt 'html-ascii))
311 (require 'ox-ascii)
312 (require 'ox-org)
313 (let* ((org-link-file-path-type 'absolute)
314 ;; we probably don't want to export a huge style file
315 (org-export-htmlize-output-type 'inline-css)
316 (html-and-images
317 (org-mime-replace-images
318 (org-export-string-as (bhook body 'html) 'html t) file))
319 (images (cdr html-and-images))
320 (html (org-mime-apply-html-hook (car html-and-images))))
321 (insert (org-mime-multipart
322 (org-export-string-as
323 (org-babel-trim
324 (bhook body (if (eq fmt 'html) 'org 'ascii)))
325 (if (eq fmt 'html) 'org 'ascii) t)
326 html)
327 (mapconcat 'identity images "\n"))))))))
329 (defun org-mime-org-buffer-htmlize ()
330 "Create an email buffer containing the current org-mode file
331 exported to html and encoded in both html and in org formats as
332 mime alternatives."
333 (interactive)
334 (org-mime-send-buffer 'html))
336 (defun org-mime-subtree ()
337 "Create an email buffer containing the current org-mode subtree
338 exported to a org format or to the format specified by the
339 MAIL_FMT property of the subtree."
340 (interactive)
341 (org-mime-send-subtree
342 (or (org-entry-get nil "MAIL_FMT" org-mime-use-property-inheritance) 'org)))
344 (provide 'org-mime)