all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 505003ccc2d0a9c0d4770676dfc9fcab6009ebd3 11208 bytes (raw)
name: contrib/lisp/org-mime.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
 
;;; org-mime.el --- org html export for text/html MIME emails

;; Copyright (C) 2010 Eric Schulte

;; Author: Eric Schulte
;; Keywords: mime, mail, email, html
;; Homepage: http://orgmode.org/worg/org-contrib/org-mime.php
;; Version: 0.01

;;; License:

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; WYSWYG, html mime composition using org-mode
;;
;; For mail composed using the orgstruct-mode minor mode, this
;; provides a function for converting all or part of your mail buffer
;; to embedded html as exported by org-mode.  Call `org-mime-htmlize'
;; in a message buffer to convert either the active region or the
;; entire buffer to html.
;;
;; Similarly the `org-mime-org-buffer-htmlize' function can be called
;; from within an org-mode buffer to convert the buffer to html, and
;; package the results into an email handling with appropriate MIME
;; encoding.
;;
;; you might want to bind this to a key with something like the
;; following message-mode binding
;; 
;;   (add-hook 'message-mode-hook
;;             (lambda ()
;;               (local-set-key "\C-c\M-o" 'org-mime-htmlize)))
;;
;; and the following org-mode binding
;; 
;;   (add-hook 'org-mode-hook
;;             (lambda ()
;;               (local-set-key "\C-c\M-o" 'org-mime-org-buffer-htmlize)))

;;; Code:
(require 'cl)

(defcustom org-mime-default-header
  "#+OPTIONS: latex:t\n"
  "Default header to control html export options, and ensure
  first line isn't assumed to be a title line."
  :group 'org-mime
  :type 'string)

(defcustom org-mime-library 'mml
  "Library to use for marking up MIME elements."
  :group 'org-mime
  :type '(choice 'mml 'semi 'vm))

(defcustom org-mime-preserve-breaks t
  "Used as temporary value of `org-export-preserve-breaks' during
  mime encoding."
  :group 'org-mime
  :type 'boolean)

(defcustom org-mime-fixedwith-wrap
  "<pre style=\"font-family: courier, monospace;\">\n%s</pre>\n"
  "Format string used to wrap a fixedwidth HTML email."
  :group 'org-mime
  :type 'string)

(defcustom org-mime-html-hook nil
  "Hook to run over the html buffer before attachment to email.
  This could be used for example to post-process html elements."
  :group 'org-mime
  :type 'hook)

;; example hook, for setting a dark background in <pre style="background-color: #EEE;"> elements
(defun org-mime-change-element-style (element style)
  "Set new default htlm style for <ELEMENT> elements in exported html."
  (while (re-search-forward (format "<%s" element) nil t)
    (replace-match (format "<%s style=\"%s;\"" element style))))

;; ;; example addition to `org-mime-html-hook' adding a dark background
;; ;; color to <pre> elements
;; (add-hook 'org-mime-html-hook
;;           (lambda ()
;;             (org-mime-change-element-style
;;              "pre" (format "color: %s; background-color: %s"
;;                            "#E6E1DC" "#232323"))))

(defun org-mime-file (ext path id)
  "Markup a file for attachment."
  (case org-mime-library
    ('mml (format
           "<#part type=\"%s\" filename=\"%s\" id=\"<%s>\">\n<#/part>\n"
           ext path id))
    ('semi (concat
            (format
             "-- xx [[%s\nContent-Disposition: inline;\nContent-ID: <%s>][base64]]\n"
             ext id)
            (base64-encode-string
             (with-temp-buffer
               (set-buffer-multibyte nil)
               (binary-insert-encoded-file path)
               (buffer-string)))))
    ('vm "?")))

(defvar org-mime-multipart-alist
  '((mml  ((beg . "<#multipart type=alternative>\n<#part type=text/plain>\n")
           (mid . "<#part type=text/html>")
           (end . "\n<#/multipart>\n")))
    (semi ((beg . "--<<alternative>>-{\n--[[text/plain]]\n")
           (mid . "--[[text/html]]\n")
           (end . "--}-<<alternative>>\n")))
    (vm   ((beg . "?")
           (mid . "?")
           (end . "?"))))
  "Text to wrap around plain and html strings.")

(defun org-mime-multipart-get (pos &optional mime-lib alist)
  (let ((alist (cadr (assoc (or mime-lib org-mime-library)
                            (or alist org-mime-multipart-alist)))))
    (cdr (assoc pos alist))))

(defun org-mime-multipart (plain html)
  "Markup a multipart/alternative with text/plain and text/html
  alternatives."
  (let ((begin (org-mime-multipart-get 'beg))
        (middle (org-mime-multipart-get 'mid))
        (end (org-mime-multipart-get 'end)))
    (concat begin plain middle html end)))

(defun org-mime-replace-images (str current-file)
  "Replace images in html files with cid links."
  (let (html-images)
    (cons
     (replace-regexp-in-string ;; replace images in html
      "src=\"\\([^\"]+\\)\""
      (lambda (text)
        (format
         "src=\"cid:%s\""
         (let* ((url (and (string-match "src=\"\\([^\"]+\\)\"" text)
                          (match-string 1 text)))
                (path (expand-file-name
                       url (file-name-directory current-file)))
                (ext (file-name-extension path))
                (id (replace-regexp-in-string "[\/\\\\]" "_" path)))
           (add-to-list 'html-images
                        (org-mime-file (concat "image/" ext) path id))
           id)))
      str)
     html-images)))

(defun org-mime-htmlize (arg)
  "Export a portion of an email body composed using `mml-mode' to
html using `org-mode'.  If called with an active region only
export that region, otherwise export the entire body."
  (interactive "P")
  (let* ((region-p (org-region-active-p))
         (html-start (or (and region-p (region-beginning))
                         (save-excursion
                           (goto-char (point-min))
                           (search-forward mail-header-separator)
                           (+ (point) 1))))
         (html-end (or (and region-p (region-end))
                       ;; TODO: should catch signature...
                       (point-max)))
         (raw-body (buffer-substring html-start html-end))
         (tmp-file (make-temp-name (expand-file-name "mail" "/tmp/")))
         (body (org-mime-org-export "org" raw-body tmp-file))
         ;; because we probably don't want to skip part of our mail
         (org-export-skip-text-before-1st-heading nil)
         ;; because we probably don't want to export a huge style file
         (org-export-htmlize-output-type 'inline-css)
         ;; makes the replies with ">"s look nicer
         (org-export-preserve-breaks org-mime-preserve-breaks)
         ;; to hold attachments for inline html images
         (html-and-images
          (org-mime-replace-images
           (org-mime-org-export "html" raw-body tmp-file)
           tmp-file))
         (html-images (unless arg (cdr html-and-images)))
         (html (org-mime-apply-html-hook
                (if arg
                    (format org-mime-fixedwith-wrap body)
                  (car html-and-images)))))
    (delete-region html-start html-end)
    (save-excursion
      (goto-char html-start)
      (insert (org-mime-multipart body html)
              (mapconcat 'identity html-images "\n")))))

(defun org-mime-unhtmlize (arg)
  "Delete mime-related text and revert buffer to pure plaintext state."
  (interactive "P")
  (let ((body-start (save-excursion
                      (goto-char (point-min))
                      (search-forward mail-header-separator)
                      (+ (point) 1)))
        (plaintext-start (org-mime-multipart-get 'beg))
        (plaintext-end (org-mime-multipart-get 'mid)))
    (condition-case nil
        (when (org-mime-buffer-html-p)
          (goto-char body-start)
          (search-forward plaintext-start)
          (delete-region body-start (point))
          (search-forward plaintext-end)
          (delete-region (- (point) (length plaintext-end)) (point-max))
          (goto-char body-start))
      (error nil))))

(defun org-mime-buffer-html-p ()
  "Return true if buffer has already been htmlized."
  (condition-case nil
      (save-excursion
        (goto-char (point-min))
        (search-forward (org-mime-multipart-get 'beg)))
    (error nil)))

(defun org-mime-toggle-html (arg)
  "If buffer hasn't been htmlized, do it. Otherwise revert."
  (interactive "P")
  (if (org-mime-buffer-html-p)
      (org-mime-unhtmlize arg)
      (org-mime-htmlize arg)))

(defun org-mime-org-export (fmt body tmp-file)
  "Org-Export BODY to format FMT with the file name set to
TMP-FILE during export."
  (save-excursion
    (with-temp-buffer
      (insert org-mime-default-header)
      (insert body)
      (write-file tmp-file)
      (org-load-modules-maybe)
      (unless org-local-vars
        (setq org-local-vars (org-get-local-variables)))
      (substring
       (eval ;; convert to fmt -- mimicing `org-run-like-in-org-mode'
        (list 'let org-local-vars 
              (list (intern (concat "org-export-as-" fmt))
                    nil nil nil ''string t)))
       (if (string= fmt "org") (length org-mime-default-header) 0)))))

(defun org-mime-apply-html-hook (html)
  (if org-mime-html-hook
      (with-temp-buffer
        (insert html)
        (goto-char (point-min))
        (run-hooks 'org-mime-html-hook)
        (buffer-string))
    html))

(defun org-mime-org-buffer-htmlize ()
  "Export the current org-mode buffer to HTML using
`org-export-as-html' and package the results into an email
handling with appropriate MIME encoding."
  (interactive)
  (require 'reporter)
  (let* ((region-p (org-region-active-p))
         (current-file (buffer-file-name (current-buffer)))
         (html-start (or (and region-p (region-beginning))
                         (save-excursion
                           (goto-char (point-min)))))
         (html-end (or (and region-p (region-end))
                       (point-max)))
         (body (org-export-as-org nil nil nil 'string t))
         (org-link-file-path-type 'absolute)
         ;; because we probably don't want to export a huge style file
         (org-export-htmlize-output-type 'inline-css)
         ;; to hold attachments for inline html images
         (html-and-images (org-mime-replace-images
                           (org-export-as-html nil nil nil 'string t)
                           current-file))
         (html-images (cdr html-and-images))
         (html (org-mime-apply-html-hook (car html-and-images))))
    ;; dump the exported html into a fresh message buffer
    (reporter-compose-outgoing)
    (goto-char (point-max))
    (insert (org-mime-multipart body html)
            (mapconcat 'identity html-images "\n"))))

(provide 'org-mime)

debug log:

solving 79a1789 ...
found 79a1789 in https://yhetil.org/emacs/87ochke6bd.fsf@in-ulm.de/
found 14a8ce3 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 14a8ce315e34858dc4599dcfd8968915bd2fdf37	contrib/lisp/org-mime.el

applying [1/1] https://yhetil.org/emacs/87ochke6bd.fsf@in-ulm.de/
diff --git a/contrib/lisp/org-mime.el b/contrib/lisp/org-mime.el
index 14a8ce3..79a1789 100644

Checking patch contrib/lisp/org-mime.el...
Applied patch contrib/lisp/org-mime.el cleanly.

index at:
100644 505003ccc2d0a9c0d4770676dfc9fcab6009ebd3	contrib/lisp/org-mime.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.