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