emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [SUGGESTION] Allow EXPORT_FILE_NAME to use elisp
@ 2020-12-05 19:26 Jens Neuhalfen
  0 siblings, 0 replies; only message in thread
From: Jens Neuhalfen @ 2020-12-05 19:26 UTC (permalink / raw)
  To: emacs-orgmode

Dear all,

I wrote a patch that allows the users to generate
the exported filename via elisp in EXPORT_FILE_NAME.

This suggestion encourages to reuse configuration (e.g. the document version
or title) in the export filename. A common use case when writing
documentation is that different versions of the document will be
exported over time. These are then distributed, e.g. via e-mail or
archived. By allowing the user to derive descriptive names during export
exchange of the document is greatly simplified.

E.g. the following excerpt will create a filename that contains the date
of export, the document title and the document version:

#+TITLE: My wonderful document
#+X-DOCUMENT-VERSION: 0.2-snapshot
#+EXPORT_FILE_NAME: `(format "%s-%s (v%s)" (org-format-time-string "%Y-%m-%d_%Hh%Mm" (org-current-time)) (org-macro--find-keyword-value  "TITLE") (org-macro--find-keyword-value  "X-DOCUMENT-VERSION"))`

Including the patch would make the life of "document writers" easier by
improving the consistency of their work. Just including the document
version in the filename would make it easier to exchange documents with
others. Allowing pure lisp is just the next step.

The gist of the patch is attached below. The whole patch is still
on GitHub.

As soon as I have the papers with the fsf signed, I can send the
whole patch.


SECURITY CONSIDERATION: Since running elisp on export is a security
risk I am looking for advice on how to make sure the user knows.
Any suggestions on how to achieve that?


https://github.com/neuhalje/org-mode/compare/master...eval_export_filename

diff --git a/lisp/ox.el b/lisp/ox.el
index 6dd2cd4a0..b79880666 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -6147,6 +6147,14 @@ to `:default' encoding.  If it fails, return S."
 	(plist-get translations :default)
 	s)))

+(defconst org-export--backquote-lisp-expression-regexp
+  "^[[:space:]]*`\\(.*\\)`[[:space:]]*$"
+  "A regexp to recognize an anchored lisp-expression between backticks.
+
+Spaces before and after the backticks are ignored.
+
+The syntax of the expression is not validated.")
+

 \f
 ;;; Asynchronous Export
@@ -6390,6 +6398,16 @@ or FILE."
           (or (and (functionp post-process) (funcall post-process file))
 	      file))))))

+(defun org-export-output-file-name--expand-name (file-name)
+  "If the filename is surrounded by `EXPR` then EXPR is evaluated and returned.
+   Otherwise return FILE-NAME"
+  (if (string-match org-export--backquote-lisp-expression-regexp file-name)
+      (let ((expression (match-string 1 file-name)))
+	(eval (car (read-from-string expression)))
+	)
+    file-name)
+)
+
 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
   "Return output file's name according to buffer specifications.

@@ -6400,6 +6418,16 @@ With a non-nil optional argument SUBTREEP, try to determine
 output file's name by looking for \"EXPORT_FILE_NAME\" property
 of subtree at point.

+If the filename (without extension) is enclosed in backticks, then
+the text between the backticks is evaluated and the result is taken
+as file name.
+
+E.g.
+
+#+EXPORT_FILE_NAME: `(format \"This is a %s\" \"test\")`
+
+will evaluate to \"This is a test\".
+
 When optional argument PUB-DIR is set, use it as the publishing
 directory.

@@ -6407,7 +6435,7 @@ Return file name as a string."
   (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
 	 (base-name
 	  (concat
-	   (file-name-sans-extension
+	   (org-export-output-file-name--expand-name (file-name-sans-extension
 	    (or
 	     ;; Check EXPORT_FILE_NAME subtree property.
 	     (and subtreep (org-entry-get nil "EXPORT_FILE_NAME" 'selective))
@@ -6426,7 +6454,7 @@ Return file name as a string."
 	     ;; Can't determine file name on our own: ask user.
 	     (read-file-name
 	      "Output file: " pub-dir nil nil nil
-	      (lambda (n) (string= extension (file-name-extension n t))))))
+	      (lambda (n) (string= extension (file-name-extension n t)))))))
 	   extension))
 	 (output-file
 	  ;; Build file name.  Enforce EXTENSION over whatever user


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-12-06 15:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-05 19:26 [SUGGESTION] Allow EXPORT_FILE_NAME to use elisp Jens Neuhalfen

Code repositories for project(s) associated with this 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).