This question https://emacs.stackexchange.com/a/27065/13217 explains perfectly how to embed base64 image inside of an HTML exported document, and it works great.
I wonder now how could I automate this process for each image file referenced in my document ?
Adding a pre-export task replacing all file references with the base64 encoded image seems the way to go, but I don't have any practice with elisp. Can anyone help me out ?
I found out some ideas in this documentation : http://orgmode.org/manual/Advanced-configuration.html which seems a good starting point. But I fail to manipulate correctly the input text.
This is what I came out with :
(defun html-base64-images (text backend info)
(when (org-export-derived-backend-p backend 'html)
(message "The text is: %S" text)
(setq filename (replace-regexp-in-string "\\\"\(.*\)+\\\"" "\1" text))
(message "The filename is: %S" filename)
(setq output-text (replace-regexp-in-string "img src=\".*\"" "img src=\"data:image/png;base64," text))
(concat output-text tob64(filename))
(message "The new text is %S" output-text)
output-text
)
)
(add-to-list 'org-export-filter-link-functions
'html-base64-images)
So in the Messages buffer I can see my messages, but I have problems to manipulate the TEXT string correctly. The output looks like :
The text is: "<img src=\"./GN_moissonnage2.png\" alt=\"GN_moissonnage2.png\" />"
The filename is: "<img src=\"./GN_moissonnage2.png\" alt=\"GN_moissonnage2.png\" />"
The new text is "<img src=\"data:image/png;base64, />"
I need to :
- get the filename
- replace it with my base64 URI
I'm not used to elisp at all ... maybe I'm doing it the wrong way ...