From 6fa2efadd229a667fba1b18aecc9d1ead5f284ac Mon Sep 17 00:00:00 2001 Message-ID: <6fa2efadd229a667fba1b18aecc9d1ead5f284ac.1721815865.git.yantar92@posteo.net> In-Reply-To: <540c8ef21c26df79cf48f58afb4e88130985e2f7.1721815865.git.yantar92@posteo.net> References: <540c8ef21c26df79cf48f58afb4e88130985e2f7.1721815865.git.yantar92@posteo.net> From: Ihor Radchenko Date: Wed, 24 Jul 2024 12:09:36 +0200 Subject: [PATCH 3/3] org-export-to-file: Derive file name to write from export output * lisp/ox.el (org-export--write-output): New helper function performing writing an export output or a list of outputs to file. It derives the file name from :output-file property in the output string or INFO plist stored in the output string. (org-export-to-file): Handle export output being a list of strings. Use `org-export--write-output'. --- lisp/ox.el | 61 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/lisp/ox.el b/lisp/ox.el index a76b3b353..d78c04998 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -6830,6 +6830,31 @@ (defun org-latex-export-as-latex (switch-to-buffer-other-window buffer)) buffer))) +(defun org-export--write-output (output encoding) + "Write OUTPUT to file with ENCODING. +OUTPUT may be a string or a list of strings. +The target file is retrieved from :output-file OUTPUT property or +:output-file property in plist stored in `org-export-info' property of +each string. + +Return the file name or a list of file names." + (if (listp output) (mapcar #'org-export--write-output output) + (let ((file (or + (get-text-property 0 :output-file output) + (plist-get + (get-text-property 0 'org-export-info output) + :output-file)))) + (with-temp-buffer + (insert output) + ;; Ensure final newline. This is what was done + ;; historically, when we used `write-file'. + ;; Note that adding a newline is only safe for + ;; non-binary data. + (unless (bolp) (insert "\n")) + (let ((coding-system-for-write encoding)) + (write-region nil nil file)) + file)))) + ;;;###autoload (defun org-export-to-file (backend file &optional async subtreep visible-only body-only ext-plist @@ -6878,33 +6903,23 @@ (defun org-latex-export-to-latex `(let ((output (org-export-as ',backend ,subtreep ,visible-only ,body-only - ',ext-plist))) - (with-temp-buffer - (insert output) - ;; Ensure final newline. This is what was done - ;; historically, when we used `write-file'. - ;; Note that adding a newline is only safe for - ;; non-binary data. - (unless (bolp) (insert "\n")) - (let ((coding-system-for-write ',encoding)) - (write-region nil nil ,file))) - (or (ignore-errors (funcall ',post-process ,file)) ,file))) + ',ext-plist)) + file) + (setq file (org-export--write-output output ',encoding)) + (let ((post (lambda (f) (or (ignore-errors (funcall ',post-process f)) f)))) + (if (listp file) (mapcar post file) (funcall post file))))) (let ((output (org-export-as - backend subtreep visible-only body-only ext-plist))) - (with-temp-buffer - (insert output) - ;; Ensure final newline. This is what was done - ;; historically, when we used `write-file'. - ;; Note that adding a newline is only safe for - ;; non-binary data. - (unless (bolp) (insert "\n")) - (let ((coding-system-for-write encoding)) - (write-region nil nil file))) + backend subtreep visible-only body-only ext-plist)) + file) + (setq file (org-export--write-output output encoding)) (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output)) (org-kill-new output)) ;; Get proper return value. - (or (and (functionp post-process) (funcall post-process file)) - file)))))) + (let ((post (lambda (f) + (or (and (functionp post-process) + (funcall post-process f)) + f)))) + (if (listp file) (mapcar post file) (funcall post file)))))))) (defun org-export-output-file-name (extension &optional subtreep pub-dir) "Return output file's name according to buffer specifications. -- 2.45.2