diff options
author | Ihor Radchenko <yantar92@posteo.net> | 2024-06-06 14:17:54 +0200 |
---|---|---|
committer | Ihor Radchenko <yantar92@posteo.net> | 2024-06-06 14:44:01 +0200 |
commit | e6c5f25c13dffa8f9b7b7bf61644cee272ec6a57 (patch) | |
tree | f62ecee3e7c2ae90e514bf77943a69932d465c67 | |
parent | 51aea4946252d358d827ac9d197eb5a6155dd9fb (diff) |
org-babel-exp-code: Fix exporting src blocks with :var arguments
* lisp/ob-exp.el (org-babel-exp-code): Do not use resolved argument
values when formatting the code block. Resolved argument values may
contain awkward data like full (long) table contents, various Elisp
data (including non-printable), etc. Simply using verbatim src block
parameters as they appear in the original buffer is more reliable.
(org-babel-exp-inline-code-template):
(org-babel-exp-code-template): Update docstrings, drop %flags
placeholder, which is no longer supported using the current src block
syntax.
* etc/ORG-NEWS (=ox-org= preserves header arguments in src blocks):
Drop "non-default" when explaining how header arguments are formatted.
We cannot know which values are default and which not without
resolving the values - something we cannot do as it turned out.
* testing/lisp/test-ob-exp.el (ob-exp/exports-inline-code):
(ob-export/export-src-block-with-flags):
(ob-export/body-with-coderef): Update the tests.
-rw-r--r-- | etc/ORG-NEWS | 5 | ||||
-rw-r--r-- | lisp/ob-exp.el | 46 | ||||
-rw-r--r-- | testing/lisp/test-ob-exp.el | 12 |
3 files changed, 15 insertions, 48 deletions
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 43b302254..26f927d42 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -1592,11 +1592,10 @@ setting the ~STYLE~ property for each sub-task. The change is breaking when ~org-use-property-inheritance~ is set to ~t~. -*** =ox-org= preserves header non-default arguments in src blocks +*** =ox-org= preserves header arguments in src blocks Previously, all the header arguments where stripped from src blocks -during export. Now, header arguments are preserved as long as their -values are not equal to the default header argument values. +during export. Now, header arguments are preserved. *** =ox-org= now exports special table rows by default diff --git a/lisp/ob-exp.el b/lisp/ob-exp.el index 34f12fdcc..657cb2b52 100644 --- a/lisp/ob-exp.el +++ b/lisp/ob-exp.el @@ -370,7 +370,7 @@ The function respects the value of the :exports header argument." nil)))) (defcustom org-babel-exp-code-template - "#+begin_src %lang%switches%flags%header-args\n%body\n#+end_src" + "#+begin_src %lang%switches%header-args\n%body\n#+end_src" "Template used to export the body of code blocks. This template may be customized to include additional information such as the code block name, or the values of particular header @@ -381,8 +381,7 @@ and the following %keys may be used. name ------ the name of the code block body ------ the body of the code block switches -- the switches associated to the code block - flags ----- the flags passed to the code block - header-args the non-default header arguments of the code block + header-args the header arguments of the code block In addition to the keys mentioned above, every header argument defined for the code block may be used as a key and will be @@ -392,7 +391,7 @@ replaced with its value." :package-version '(Org . "9.7")) (defcustom org-babel-exp-inline-code-template - "src_%lang[%switches%flags%header-args]{%body}" + "src_%lang[%switches%header-args]{%body}" "Template used to export the body of inline code blocks. This template may be customized to include additional information such as the code block name, or the values of particular header @@ -403,8 +402,7 @@ and the following %keys may be used. name ------ the name of the code block body ------ the body of the code block switches -- the switches associated to the code block - flags ----- the flags passed to the code block - header-args the non-default header arguments of the code block + header-args the header arguments of the code block In addition to the keys mentioned above, every header argument defined for the code block may be used as a key and will be @@ -438,39 +436,9 @@ replaced with its value." (and f (concat " " (cdr f))))) ("header-args" . - ,(let* ((header-args - (mapcar - (lambda (pair) - ;; Do no include special parameters, parameters with - ;; their values equal to defaults. - (unless (or - ;; Special parameters that are not real header - ;; arguments. - (memq (car pair) - '( :result-params :result-type - ;; This is an obsolete parameter still - ;; used in some tests. - :flags)) - ;; Global defaults. - (equal (cdr pair) - (alist-get - (car pair) - (if (eq type 'inline) org-babel-default-inline-header-args - org-babel-default-header-args))) - ;; Per-language defaults. - (let ((lang-headers - (intern - (concat "org-babel-default-header-args:" - (nth 0 info))))) - (and (boundp lang-headers) - (equal (cdr pair) - (alist-get (car pair) - (eval lang-headers t)))))) - (format "%s %s" (car pair) (cdr pair)))) - (nth 2 info))) - (header-arg-string - (mapconcat #'identity (delq nil header-args) " "))) - (unless (seq-empty-p header-arg-string) (concat " " header-arg-string)))) + ,(org-babel-exp--at-source + (when-let ((params (org-element-property :parameters (org-element-context)))) + (concat " " params)))) ,@(mapcar (lambda (pair) (cons (substring (symbol-name (car pair)) 1) (format "%S" (cdr pair)))) diff --git a/testing/lisp/test-ob-exp.el b/testing/lisp/test-ob-exp.el index d029dadfb..6749284d7 100644 --- a/testing/lisp/test-ob-exp.el +++ b/testing/lisp/test-ob-exp.el @@ -262,9 +262,9 @@ Here is one at the end of a line. {{{results(=2=)}}} (string-match (replace-regexp-in-string "\\\\\\[]{" "\\(?:\\[]\\)?{" ;accept both src_sh[]{...} or src_sh{...} - (regexp-quote "Here is one in the middle src_sh[ :exports code]{echo 1} of a line. -Here is one at the end of a line. src_sh[ :exports code]{echo 2} -src_sh[ :exports code]{echo 3} Here is one at the beginning of a line. + (regexp-quote "Here is one in the middle src_sh[]{echo 1} of a line. +Here is one at the end of a line. src_sh[]{echo 2} +src_sh[]{echo 3} Here is one at the beginning of a line. Here is one that is also evaluated: src_sh[ :exports both]{echo 4} {{{results(=4=)}}}") nil t) (org-test-at-id "cd54fc88-1b6b-45b6-8511-4d8fa7fc8076" @@ -430,7 +430,7 @@ be evaluated." "Test exporting a source block with a flag." (should (string-match - "\\`#\\+BEGIN_SRC emacs-lisp -some-flag$" + "\\`#\\+BEGIN_SRC emacs-lisp :flags -some-flag$" (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp :flags -some-flag\n\(+ 1 1)\n#+END_SRC" (org-babel-exp-process-buffer) @@ -570,7 +570,7 @@ src_emacs-lisp{(+ 1 1)}" (ert-deftest ob-export/body-with-coderef () "Test exporting a code block with coderefs." (should - (equal "#+begin_src emacs-lisp\n0 (ref:foo)\n#+end_src" + (equal "#+begin_src emacs-lisp :exports code\n0 (ref:foo)\n#+end_src" (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp :exports code\n0 (ref:foo)\n#+END_SRC" (let ((org-export-use-babel t) @@ -579,7 +579,7 @@ src_emacs-lisp{(+ 1 1)}" (buffer-string)))) (should (equal - "#+begin_src emacs-lisp -l \"r:%s\"\n1 r:foo\n#+end_src" + "#+begin_src emacs-lisp -l \"r:%s\" -lisp :exports code\n1 r:foo\n#+end_src" (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -l \"r:%s\" -lisp :exports code\n1 r:foo\n#+END_SRC" (let ((org-export-use-babel t)) |