ox-html: Support for customised latex image dir

* lisp/ox-html.el (org-html-latex-image, org-html-prepare-latex-images,
org-html-latex-image-options): Allow for customising the LaTex image
directory in `org-html-latex-image-options'.
This commit is contained in:
TEC 2024-01-08 18:04:12 +08:00
parent a7b576a567
commit bc2ef6c91d
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
1 changed files with 54 additions and 33 deletions

View File

@ -1174,15 +1174,19 @@ See `format-time-string' for more information on its components."
(defcustom org-html-latex-image-options (defcustom org-html-latex-image-options
'(:foreground "Black" :background "Transparent" '(:foreground "Black" :background "Transparent"
:page-width 1.0 :scale 1.0 :inline nil) :page-width 1.0 :scale 1.0 :image-dir "ltximg" :inline nil)
"LaTeX preview options that apply to generated images. "LaTeX preview options that apply to generated images.
This is a HTML-specific counterpart to `org-latex-preview-appearance-options', which see. This is a HTML-specific counterpart to
`org-latex-preview-appearance-options', which see.
This also supports the extra property \":inline\", which controls the This supports two extra properties,
inlining of images, it can be: :image-dir an html-export counterpart of `org-latex-preview-cache', and
- t, to inline all images :inline images that should not be saved according to :image-dir,
- a extension, or list of extensions, to inline those formats (e.g. \"svg\") but instead inlined in the generated HTML. This can be:
- nil, to never inline images" - t, to inline all images
- nil, to never inline images
- an extension or list of extensions, for images that
should be inline (e.g. \"svg\")"
:group 'org-export-html :group 'org-export-html
:package-version '(Org . "9.7") :package-version '(Org . "9.7")
:type 'plist) :type 'plist)
@ -3046,7 +3050,7 @@ CONTENTS is nil. INFO is a plist holding contextual information."
;;;; LaTeX Environment ;;;; LaTeX Environment
(defun org-html-prepare-latex-images (parse-tree _backend info) (defun org-html-prepare-latex-images (parse-tree _backend info)
"Make sure that appropriate preview images exist for all LaTeX "Make sure that appropriate preview images exist for all LaTeX.
TODO." TODO."
(when (assq (plist-get info :with-latex) org-latex-preview-process-alist) (when (assq (plist-get info :with-latex) org-latex-preview-process-alist)
(let* ((latex-preamble (let* ((latex-preamble
@ -3096,14 +3100,13 @@ TODO."
:key hash) :key hash)
fragment-info)) fragment-info))
(setq prev-fg fg prev-bg bg))) (setq prev-fg fg prev-bg bg)))
(let ((org-latex-preview-appearance-options (when fragment-info
(list (plist-member html-options :scale)))) (apply #'org-async-wait-for
(when fragment-info (org-latex-preview--create-image-async
(apply #'org-async-wait-for processing-type
(org-latex-preview--create-image-async (nreverse fragment-info)
processing-type :latex-preamble latex-preamble
(nreverse fragment-info) :appearance-options html-options)))
:latex-preamble latex-preamble))))
(plist-put info :html-latex-preview-hash-table element-hash-table) (plist-put info :html-latex-preview-hash-table element-hash-table)
nil))) nil)))
@ -3196,25 +3199,43 @@ CONTENTS is nil. INFO is a plist holding contextual information."
(org-html--as-latex latex-fragment info)) (org-html--as-latex latex-fragment info))
(defun org-html-latex-image (element info) (defun org-html-latex-image (element info)
"TODO" "Transcode the LaTeX fragment or environment ELEMENT from Org to HTML.
INFO is a plist holding contextual information, and it is assumed
that an image for ELEMENT already exists within it."
(let* ((hash (or (gethash element (plist-get info :html-latex-preview-hash-table)) (let* ((hash (or (gethash element (plist-get info :html-latex-preview-hash-table))
(error "Expected LaTeX preview hash to exist for element, but none found"))) (error "Expected LaTeX preview hash to exist for element, but none found")))
(path-info (or (org-latex-preview--get-cached hash) (path-info (or (org-latex-preview--get-cached hash)
(error "Expected LaTeX preview %S to exist in the cache" hash))) (error "Expected LaTeX preview %S to exist in the cache" hash)))
(inline-condition (plist-get (plist-get info :html-latex-image-options) :inline)) (image-options (plist-get info :html-latex-image-options))
(image-dir (plist-get image-options :image-dir))
(inline-condition (plist-get image-options :inline))
(rescale-factor (if (eq (plist-get (cdr path-info) :image-type) 'svg)
(plist-get image-options :scale)
1))
(image-source (image-source
(if (or (eq inline-condition 't) (cond
(member (file-name-extension (car path-info)) ((or (eq inline-condition 't)
(org-ensure-list inline-condition))) (member (file-name-extension (car path-info))
(let ((coding-system-for-read 'utf-8) (org-ensure-list inline-condition)))
(file-name-handler-alist nil)) (let ((coding-system-for-read 'utf-8)
(with-temp-buffer (file-name-handler-alist nil))
(insert-file-contents-literally (car path-info)) (with-temp-buffer
(base64-encode-region (point-min) (point-max)) (insert-file-contents-literally (car path-info))
(goto-char (point-min)) (base64-encode-region (point-min) (point-max))
(insert "data:image/svg+xml;base64,") (goto-char (point-min))
(buffer-string))) (insert "data:image/svg+xml;base64,")
(car path-info)))) (buffer-string))))
((stringp image-dir)
(let* ((image-dir (expand-file-name image-dir))
(image-path (file-name-with-extension
(file-name-concat image-dir (substring hash 0 11))
(file-name-extension (car path-info)))))
(unless (file-directory-p image-dir)
(mkdir image-dir t))
(unless (file-exists-p image-path)
(copy-file (car path-info) image-path))
image-path))
(t (car path-info)))))
(unless (and (plist-get (cdr path-info) :height) (unless (and (plist-get (cdr path-info) :height)
(plist-get (cdr path-info) :depth)) (plist-get (cdr path-info) :depth))
(error "Something went wrong during image generation")) (error "Something went wrong during image generation"))
@ -3226,10 +3247,10 @@ CONTENTS is nil. INFO is a plist holding contextual information."
(org-element-property :value element)) (org-element-property :value element))
:style (if (eq (org-element-type element) 'latex-environment) :style (if (eq (org-element-type element) 'latex-environment)
(format "height: %.4fem" (format "height: %.4fem"
(plist-get (cdr path-info) :height)) (* rescale-factor (plist-get (cdr path-info) :height)))
(format "height: %.4fem; vertical-align: -%.4fem; display: inline-block" (format "height: %.4fem; vertical-align: -%.4fem; display: inline-block"
(plist-get (cdr path-info) :height) (* rescale-factor (plist-get (cdr path-info) :height))
(plist-get (cdr path-info) :depth))) (* rescale-factor (plist-get (cdr path-info) :depth))))
:class (if (eq (org-element-type element) 'latex-environment) :class (if (eq (org-element-type element) 'latex-environment)
"org-latex org-latex-environment" "org-latex org-latex-environment"
"org-latex org-latex-fragment"))) "org-latex org-latex-fragment")))