Bastien <b...@gnu.org> writes:

> Hi Eric,
>
> sorry for the late reply -- I don't use attachments that much
> but I see how this could be useful once correctly advertized and
> documented.
>
> Eric Abrahamsen <e...@ericabrahamsen.net> writes:
>
>> Would something along these lines be considered for inclusion?
>
> Yes, sure, for after 9.4 -- please go ahead with a proper patch.

How does this look? It occurred to me that having a global
`org-export-directory' option would be a good thing -- it makes sense to
me that a user might want all their exported files put in one place --
but in the interest of one-thing-at-a-time I didn't do that.

This additional property doesn't actually have anything to do with
org-attach. I'd originally thought that, with this option, I could write
an export filter that would check for:

:EXPORT_DIRECTORY: <ATTACH_DIR>

and replace it to point at whatever the attach dir was/should be for
that heading.

If that's something that many users would like to have, we could look at
adding it in org-attach.el. But in the meantime, the attached patch
makes it possible to do it yourself.

Thanks,
Eric

>From 0e80a6ed7ad955259c4e11ea74ed249696ba9b46 Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen <e...@ericabrahamsen.net>
Date: Sun, 6 Sep 2020 10:36:34 -0700
Subject: [PATCH] Add EXPORT_DIRECTORY property

* lisp/ox.el (org-export-output-file-name): Check for this property
and place the exported output file there.
* doc/org-manual.org: Document.
---
 doc/org-manual.org |  8 +++++++-
 lisp/ox.el         | 14 ++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index 46498bd22..6f256a569 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -11479,7 +11479,7 @@ global variables, include:
 - =CREATOR= ::
 
   #+cindex: @samp{CREATOR}, keyword
-  #+vindex: org-expot-creator-string
+  #+vindex: org-export-creator-string
   Entity responsible for output generation
   (~org-export-creator-string~).
 
@@ -11539,6 +11539,12 @@ global variables, include:
   generates the file name based on the buffer name and the extension
   based on the back-end format.
 
+- =EXPORT_DIRECTORY= ::
+
+  #+cindex: @samp{EXPORT_DIRECTORY}, keyword
+  An alternate directory in which to place the output file.
+  Otherwise, output files are placed alongside the original Org file.
+
 The =OPTIONS= keyword is a compact form.  To configure multiple
 options, use several =OPTIONS= lines.  =OPTIONS= recognizes the
 following arguments.
diff --git a/lisp/ox.el b/lisp/ox.el
index 9852cfd21..2d2c1ddb0 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -6428,6 +6428,20 @@ Return file name as a string."
 	      "Output file: " pub-dir nil nil nil
 	      (lambda (n) (string= extension (file-name-extension n t))))))
 	   extension))
+	 (pub-dir (or pub-dir
+		      (and subtreep (org-entry-get
+				     nil "EXPORT_DIRECTORY" 'selective))
+		      (org-with-point-at (point-min)
+			(catch :found
+			  (let ((case-fold-search t))
+			    (while (re-search-forward
+				    "^[ \t]*#\\+EXPORT_DIRECTORY:[ \t]+\\S-"
+				    nil t)
+			      (let ((element (org-element-at-point)))
+				(when (eq 'keyword (org-element-type element))
+				  (throw :found
+					 (org-element-property
+					  :value element))))))))))
 	 (output-file
 	  ;; Build file name.  Enforce EXTENSION over whatever user
 	  ;; may have come up with.  PUB-DIR, if defined, always has
-- 
2.28.0

Reply via email to