PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/lisp/org-mime.el

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