From mboxrd@z Thu Jan 1 00:00:00 1970 From: Greg Sexton Subject: Base 64 inline images in html export Date: Mon, 7 Dec 2015 12:15:54 +0000 Message-ID: Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:51128) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a5uiI-0000o4-Oj for emacs-orgmode@gnu.org; Mon, 07 Dec 2015 07:16:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a5uiF-0007MB-Co for emacs-orgmode@gnu.org; Mon, 07 Dec 2015 07:16:18 -0500 Received: from smtp-fw-4101.amazon.com ([72.21.198.25]:49375) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a5uiF-0007Lt-8C for emacs-orgmode@gnu.org; Mon, 07 Dec 2015 07:16:15 -0500 Received: from ex10-hub-7001.ant.amazon.com (pdx1-ws-svc-lb16-vlan3.amazon.com [10.239.138.214]) by email-inbound-relay-60014.pdx1.amazon.com (8.14.7/8.14.7) with ESMTP id tB7CG8N8002723 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=FAIL) for ; Mon, 7 Dec 2015 12:16:11 GMT List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Hi, I did find a post about this from around 2009 but I'm not sure it went anywhere. It's very useful for me to be able to export a single html file that I can distribute around. Base 64 encoding images directly in to the exported html makes this possible. The feature seems fairly well supported by browsers these days.[1] I've hacked up this ugly proof of concept. I guess it wouldn't take too much to productionize this and make the behavior configurable. Any thoughts? --8<---------------cut here---------------start------------->8--- (defun gs/b64-img (file-uri) (let ((file (s-replace "file://" "" file-uri))) (if (f-exists? file) ;; src="data:image/png;base64,iVBORw0KGgoAAAANSUh..." (s-concat "data:image/" (f-ext file) ";base64," (base64-encode-string (f-read-bytes file))) file-uri))) (defun org-html--format-image (source attributes info) "Return \"img\" tag with given SOURCE and ATTRIBUTES. SOURCE is a string specifying the location of the image. ATTRIBUTES is a plist, as returned by `org-export-read-attribute'. INFO is a plist used as a communication channel." (org-html-close-tag "img" (org-html--make-attribute-string (org-combine-plists (list :src (gs/b64-img source) ; <-- interesting line is here :alt (if (string-match-p "^ltxpng/" source) (org-html-encode-plain-text (org-find-text-property-in-string 'org-latex-src source)) (file-name-nondirectory source))) attributes)) info)) --8<---------------cut here---------------end--------------->8--- [1]: http://stackoverflow.com/questions/1207190/embedding-base64-images -- Greg Amazon Development Centre (London) Ltd. Registered in England and Wales with registration number 04543232 and which has its registered office at Leadenhall Court, One Leadenhall Street, London EC3V 1PP, United Kingdom.