From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id IAmVMuz4zF9efAAA0tVLHw (envelope-from ) for ; Sun, 06 Dec 2020 15:29:48 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id AJR0Luz4zF+kJAAA1q6Kng (envelope-from ) for ; Sun, 06 Dec 2020 15:29:48 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id ED0969401BF for ; Sun, 6 Dec 2020 15:29:47 +0000 (UTC) Received: from localhost ([::1]:52460 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1klvyq-0001RZ-Hg for larch@yhetil.org; Sun, 06 Dec 2020 10:29:44 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49778) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kldC6-0006nc-B0 for emacs-orgmode@gnu.org; Sat, 05 Dec 2020 14:26:10 -0500 Received: from mail.neuhalfen.name ([144.76.61.246]:54968) by eggs.gnu.org with esmtps (TLS1.2:DHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kldC1-0004IQ-M2 for emacs-orgmode@gnu.org; Sat, 05 Dec 2020 14:26:09 -0500 From: Jens Neuhalfen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=neuhalfen.name; s=2019; t=1607196359; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=untEwK+ZDW+R1Kcy0nWE7qTMepOo7JrVOkDrhq7lkGs=; b=RPv4RmxOtBcc1yViZfvCBBHFXTL36upFYx16sxVSEDPEmvDQ4p1JJymUSVaA8rMEFStkKG ntO5hzlsh5VLiZxNasRTWNHu12E8SUF9NrTIXCK7pKTvYtmRGl8LDN+kLyHkGWNmAMHNO2 utd4eiqY3Qg8cXQKgzpiKZd7dgzEUYtdlDd99Ub3j/y7/NrJbUNsOqyeU44TKsPu05qDCA 1z04hpdujPvzBj4U1oyP9BHM4FP7Vma8N4W7bUOC7lpi2VmHVUty2AMoQg23pQknTZTsJP bCuc7mkyajXvgB8/x9sAZuGQIgnvXnCsB8S7ZUELwSdFkOwP5i6L/ypd7lgDRg== To: emacs-orgmode@gnu.org Subject: [SUGGESTION] Allow EXPORT_FILE_NAME to use elisp Date: Sat, 05 Dec 2020 20:26:32 +0100 Message-ID: <874kl0836f.fsf@neuhalfen.name> MIME-Version: 1.0 Content-Type: text/plain Received-SPF: pass client-ip=144.76.61.246; envelope-from=jens@neuhalfen.name; helo=mail.neuhalfen.name X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Sun, 06 Dec 2020 10:26:15 -0500 X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -3.00 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=neuhalfen.name header.s=2019 header.b=RPv4RmxO; dmarc=pass (policy=none) header.from=neuhalfen.name; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Queue-Id: ED0969401BF X-Spam-Score: -3.00 X-Migadu-Scanner: ns3122888.ip-94-23-21.eu X-TUID: 9pUPNvMIJRXF 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.") + ;;; 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