emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ihor Radchenko <yantar92@posteo.net>
To: Ihor Radchenko <yantar92@gmail.com>
Cc: alain.cochard@unistra.fr,  emacs-orgmode@gnu.org
Subject: Re: [BUG] EXPORT_FILE_NAME keyword is used by `org-export-output-file-name' before macro expansion/INCLUDEs/removing COMMENTed trees (was: Confused about what the COMMENT keyword means)
Date: Thu, 10 Nov 2022 01:51:05 +0000	[thread overview]
Message-ID: <874jv7lgqe.fsf@localhost> (raw)
In-Reply-To: <87k05i25lq.fsf@localhost>

[-- Attachment #1: Type: text/plain, Size: 589 bytes --]

Ihor Radchenko <yantar92@gmail.com> writes:

> There should be no other buffer setting that is taken into account
> in COMMENTed subtrees. EXPORT_FILE_NAME is the only one.
>
> I think that we should not really change the manual unless we have to.
> It will be better to fix the bug instead.

I am attaching a tentative change to ox.el machinery that will allow
calculating EXPORT_FILE_NAME dynamically inside the processed export
buffer.

The change is not yet fixing the bug. I just want to hear feedback
on the approach I used.

Let me know if you see any issues or have better ideas.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-export-Allow-export-file-name-to-be-determined-a.patch --]
[-- Type: text/x-patch, Size: 6628 bytes --]

From 57aa366e9d5596741c256688c9b8ef07a9b24e17 Mon Sep 17 00:00:00 2001
Message-Id: <57aa366e9d5596741c256688c9b8ef07a9b24e17.1668044888.git.yantar92@posteo.net>
From: Ihor Radchenko <yantar92@posteo.net>
Date: Thu, 10 Nov 2022 09:39:20 +0800
Subject: [PATCH] org-export: Allow export file name to be determined after
 export processing

* lisp/ox.el (org-export-as): When :output-file info property is set
to a list, calculate :output-file after expanding macros, removing
uninterpreted data, and other processing.  Use the list value is used
as argument list for `org-export-output-file-name'.  Add new optional
argument WITH-INFO to return the info channel in addition to the
export string.
(org-export-to-file): Allow FILE argument to be an argument list to be
passed to `org-export-output-file-name' in the processed export
buffer.

This patch allows calculating the output file name after expanding all
the macros, cleaning up commented trees, and other export processing.
In particular, #+EXPORT_FILE_NAME keywords inside commented trees can
be ignored in contrast to what `org-export-output-file-name' returns
on the original Org file.

This patch is not changing any existing behavior and not fixing the
reported byg.  The callers of `org-export-to-file' still need to
change FILE argument to make use of the changes herein.

Reported-by: Alain.Cochard@unistra.fr
Link: https://orgmode.org/list/25422.27044.980916.495348@gargle.gargle.HOWL
---
 lisp/ox.el | 59 ++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 42 insertions(+), 17 deletions(-)

diff --git a/lisp/ox.el b/lisp/ox.el
index 770f86740..82cf0d166 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -2994,7 +2994,8 @@ (defun org-export--remove-uninterpreted-data (data info)
 
 ;;;###autoload
 (defun org-export-as
-    (backend &optional subtreep visible-only body-only ext-plist)
+    (backend
+     &optional subtreep visible-only body-only ext-plist with-info)
   "Transcode current Org buffer into BACKEND code.
 
 BACKEND is either an export back-end, as returned by, e.g.,
@@ -3020,7 +3021,10 @@ (defun org-export-as
 with external parameters overriding Org default settings, but
 still inferior to file-local settings.
 
-Return code as a string."
+Return code as a string.
+
+When optional argument WITH-INFO is non-nil, return a cons cell with car
+containing return code string and cdr containing the info channel."
   (when (symbolp backend) (setq backend (org-export-get-backend backend)))
   (org-export-barf-if-invalid-backend backend)
   (org-fold-core-ignore-modifications
@@ -3106,6 +3110,14 @@ (defun org-export-as
 			     p
 			     (org-export--remove-uninterpreted-data value info))))
 	       (_ nil)))
+           ;; Update `:output-file' option if needed.
+           (let ((output-file (plist-get info :output-file)))
+             (when (and output-file (listp output-file))
+               (plist-put
+                info :output-file
+                (apply #'org-export-output-file-name
+                       ;; `:output-file' contains argument list.
+                       output-file))))
 	   ;; Install user's and developer's filters.
 	   (setq info (org-export-install-filters info))
 	   ;; Call options filters and update export options.  We do not
@@ -3155,10 +3167,13 @@ (defun org-export-as
 	     ;; Remove all text properties since they cannot be
 	     ;; retrieved from an external process.  Finally call
 	     ;; final-output filter and return result.
-	     (org-no-properties
-	      (org-export-filter-apply-functions
-	       (plist-get info :filter-final-output)
-	       output info)))))))))
+             (let ((final-output
+	            (org-no-properties
+	             (org-export-filter-apply-functions
+	              (plist-get info :filter-final-output)
+	              output info))))
+               (if with-info (cons final-output info)
+                 final-output)))))))))
 
 ;;;###autoload
 (defun org-export-string-as (string backend &optional body-only ext-plist)
@@ -6637,7 +6652,10 @@ (defun org-export-to-file
 BACKEND is either an export back-end, as returned by, e.g.,
 `org-export-create-backend', or a symbol referring to
 a registered back-end.  FILE is the name of the output file, as
-a string.
+a string.  FILE can also be a list of options to determine file name
+automatically.  The options will be used as arguments of
+`org-export-output-file-name', which will be called right before
+:filter-options filter inside the cleaned buffer to be exported.
 
 A non-nil optional argument ASYNC means the process should happen
 asynchronously.  The resulting buffer will then be accessible
@@ -6664,26 +6682,33 @@   (defun org-latex-export-to-latex
 POST-PROCESS needs to be quoted.
 
 The function returns either a file name returned by POST-PROCESS,
-or FILE."
+or FILE written."
   (declare (indent 2))
-  (if (not (file-writable-p file)) (error "Output file not writable")
+  (if (and (stringp file) (not (file-writable-p file)))
+      (error "Output file not writable")
     (let ((ext-plist (org-combine-plists `(:output-file ,file) ext-plist))
 	  (encoding (or org-export-coding-system buffer-file-coding-system)))
       (if async
           (org-export-async-start
 	      (lambda (file)
 		(org-export-add-to-stack (expand-file-name file) backend))
-	    `(let ((output
-		    (org-export-as
-		     ',backend ,subtreep ,visible-only ,body-only
-		     ',ext-plist)))
+	    `(let* ((output
+		     (org-export-as
+		      ',backend ,subtreep ,visible-only ,body-only
+		      ',ext-plist ,(when (listp file) 'with-info)))
+                    (file ,(if (listp file) '(cdr output) file)))
+               (when (consp output) (setq output (car output)))
 	       (with-temp-buffer
 		 (insert output)
 		 (let ((coding-system-for-write ',encoding))
-		   (write-region (point-min) (point-max) ,file)))
-	       (or (ignore-errors (funcall ',post-process ,file)) ,file)))
-        (let ((output (org-export-as
-                       backend subtreep visible-only body-only ext-plist)))
+		   (write-region (point-min) (point-max) file)))
+	       (or (ignore-errors (funcall ',post-process file)) file)))
+        (let* ((output (org-export-as
+                        backend subtreep visible-only
+                        body-only ext-plist
+                        (when (listp file) 'with-info)))
+               (file (if (listp file) (cdr output) file)))
+          (when (consp output) (setq output (car output)))
           (with-temp-buffer
             (insert output)
             (let ((coding-system-for-write encoding))
-- 
2.35.1


[-- Attachment #3: Type: text/plain, Size: 224 bytes --]


-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

      parent reply	other threads:[~2022-11-10  1:51 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-26 18:28 Confused about what the COMMENT keyword means Alain.Cochard
2022-09-27  1:55 ` Ihor Radchenko
2022-09-27  7:00   ` Alain.Cochard
2022-09-27  7:40     ` Ihor Radchenko
2022-09-30  8:57       ` Alain.Cochard
2022-10-02  4:48         ` [BUG] EXPORT_FILE_NAME keyword is used by `org-export-output-file-name' before macro expansion/INCLUDEs/removing COMMENTed trees (was: Confused about what the COMMENT keyword means) Ihor Radchenko
2022-10-11 14:42           ` Alain.Cochard
2022-10-18  8:53             ` Alain.Cochard
2022-11-10  1:56               ` Ihor Radchenko
2022-11-10  2:03             ` [FR] Allow TITLE to be used as default export file name (was: [BUG] EXPORT_FILE_NAME keyword is used by `org-export-output-file-name' before macro expansion/INCLUDEs/removing COMMENTed trees (was: Confused about what the COMMENT keyword means)) Ihor Radchenko
2022-11-10  7:53               ` Alain.Cochard
2022-11-10  8:04                 ` Ihor Radchenko
2022-11-10  1:51           ` Ihor Radchenko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874jv7lgqe.fsf@localhost \
    --to=yantar92@posteo.net \
    --cc=alain.cochard@unistra.fr \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).