ob-exp: Preserve header arguments in source block after processing

* lisp/ob-exp.el (org-babel-exp-process-buffer): Fix infinite loop
when the inline src block replacement is the same as the existing src
block.
(org-babel-exp-code): Support new placeholder %header-args for
non-default header arguments.
(org-babel-exp-inline-code-template):
(org-babel-exp-code-template): Change the templates to include header
arguments.
* testing/lisp/test-ob-exp.el (ob-exp/exports-inline-code):
(ob-exp/exports-inline-code-double-eval-exports-both):
(ob-export/export-with-results-before-block): Adjust tests.
* etc/ORG-NEWS (=ox-org= preserves header non-default arguments in src
blocks): Document the breaking change.

This change fixes ox-org export for src blocks.  Previously src blocks
did not preserve their header arguments.  Now, non-default header
arguments are preserved.

Link: https://github.com/emacsorphanage/ox-pandoc/issues/37
This commit is contained in:
Ihor Radchenko 2024-04-24 14:48:19 +03:00
parent ff9d00c9c3
commit c6c5474b7e
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
3 changed files with 58 additions and 13 deletions

View File

@ -83,6 +83,11 @@ transcoders.
Users can disable citations processors by customizing new Users can disable citations processors by customizing new
~org-export-process-citations~ option. ~org-export-process-citations~ option.
*** =ox-org= preserves header non-default 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.
*** =ox-org= disables citation processors by default *** =ox-org= disables citation processors by default

View File

@ -238,7 +238,10 @@ this template."
((not (string= replacement ((not (string= replacement
(buffer-substring begin end))) (buffer-substring begin end)))
(delete-region begin end) (delete-region begin end)
(insert replacement)))))) (insert replacement))
;; Replacement is the same as the source
;; block. Continue onwards.
(t (goto-char end))))))
((or `babel-call `inline-babel-call) ((or `babel-call `inline-babel-call)
(org-babel-exp-do-export (org-babel-exp-do-export
(or (org-babel-lob-get-info element) (or (org-babel-lob-get-info element)
@ -367,7 +370,7 @@ The function respects the value of the :exports header argument."
nil)))) nil))))
(defcustom org-babel-exp-code-template (defcustom org-babel-exp-code-template
"#+begin_src %lang%switches%flags\n%body\n#+end_src" "#+begin_src %lang%switches%flags%header-args\n%body\n#+end_src"
"Template used to export the body of code blocks. "Template used to export the body of code blocks.
This template may be customized to include additional information This template may be customized to include additional information
such as the code block name, or the values of particular header such as the code block name, or the values of particular header
@ -379,6 +382,7 @@ and the following %keys may be used.
body ------ the body of the code block body ------ the body of the code block
switches -- the switches associated to the code block switches -- the switches associated to the code block
flags ----- the flags passed to the code block flags ----- the flags passed to the code block
header-args the non-default header arguments of the code block
In addition to the keys mentioned above, every header argument In addition to the keys mentioned above, every header argument
defined for the code block may be used as a key and will be defined for the code block may be used as a key and will be
@ -388,7 +392,7 @@ replaced with its value."
:package-version '(Org . "9.6")) :package-version '(Org . "9.6"))
(defcustom org-babel-exp-inline-code-template (defcustom org-babel-exp-inline-code-template
"src_%lang[%switches%flags]{%body}" "src_%lang[%switches%flags%header-args]{%body}"
"Template used to export the body of inline code blocks. "Template used to export the body of inline code blocks.
This template may be customized to include additional information This template may be customized to include additional information
such as the code block name, or the values of particular header such as the code block name, or the values of particular header
@ -400,6 +404,7 @@ and the following %keys may be used.
body ------ the body of the code block body ------ the body of the code block
switches -- the switches associated to the code block switches -- the switches associated to the code block
flags ----- the flags passed to the code block flags ----- the flags passed to the code block
header-args the non-default header arguments of the code block
In addition to the keys mentioned above, every header argument In addition to the keys mentioned above, every header argument
defined for the code block may be used as a key and will be defined for the code block may be used as a key and will be
@ -432,6 +437,41 @@ replaced with its value."
(and (org-string-nw-p f) (concat " " f)))) (and (org-string-nw-p f) (concat " " f))))
("flags" . ,(let ((f (assq :flags (nth 2 info)))) ("flags" . ,(let ((f (assq :flags (nth 2 info))))
(and f (concat " " (cdr f))))) (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))))
,@(mapcar (lambda (pair) ,@(mapcar (lambda (pair)
(cons (substring (symbol-name (car pair)) 1) (cons (substring (symbol-name (car pair)) 1)
(format "%S" (cdr pair)))) (format "%S" (cdr pair))))

View File

@ -205,14 +205,14 @@ Here is one at the end of a line. {{{results(=2=)}}}
(ert-deftest ob-exp/exports-inline-code () (ert-deftest ob-exp/exports-inline-code ()
(should (should
(equal "src_emacs-lisp[]{(+ 1 1)}" (equal "src_emacs-lisp[ :exports code]{(+ 1 1)}"
(org-test-with-temp-text "src_emacs-lisp[:exports code]{(+ 1 1)}" (org-test-with-temp-text "src_emacs-lisp[:exports code]{(+ 1 1)}"
(let ((org-babel-inline-result-wrap "=%s=") (let ((org-babel-inline-result-wrap "=%s=")
(org-export-use-babel t)) (org-export-use-babel t))
(org-babel-exp-process-buffer)) (org-babel-exp-process-buffer))
(buffer-string)))) (buffer-string))))
(should (should
(equal "src_emacs-lisp[]{(+ 1 1)}" (equal "src_emacs-lisp[ :exports code]{(+ 1 1)}"
(org-test-with-temp-text "src_emacs-lisp[ :exports code ]{(+ 1 1)}" (org-test-with-temp-text "src_emacs-lisp[ :exports code ]{(+ 1 1)}"
(let ((org-babel-inline-result-wrap "=%s=") (let ((org-babel-inline-result-wrap "=%s=")
(org-export-use-babel t)) (org-export-use-babel t))
@ -220,14 +220,14 @@ Here is one at the end of a line. {{{results(=2=)}}}
(buffer-string)))) (buffer-string))))
;; Do not escape characters in inline source blocks. ;; Do not escape characters in inline source blocks.
(should (should
(equal "src_c[]{*a}" (equal "src_c[ :exports code]{*a}"
(org-test-with-temp-text "src_c[ :exports code ]{*a}" (org-test-with-temp-text "src_c[ :exports code ]{*a}"
(let ((org-babel-inline-result-wrap "=%s=") (let ((org-babel-inline-result-wrap "=%s=")
(org-export-use-babel t)) (org-export-use-babel t))
(org-babel-exp-process-buffer)) (org-babel-exp-process-buffer))
(buffer-string)))) (buffer-string))))
(should (should
(equal "src_emacs-lisp[]{(+ 1 1)} {{{results(=2=)}}}" (equal "src_emacs-lisp[ :exports both]{(+ 1 1)} {{{results(=2=)}}}"
(org-test-with-temp-text "src_emacs-lisp[:exports both]{(+ 1 1)}" (org-test-with-temp-text "src_emacs-lisp[:exports both]{(+ 1 1)}"
(let ((org-babel-inline-result-wrap "=%s=") (let ((org-babel-inline-result-wrap "=%s=")
(org-export-use-babel t)) (org-export-use-babel t))
@ -262,10 +262,10 @@ Here is one at the end of a line. {{{results(=2=)}}}
(string-match (string-match
(replace-regexp-in-string (replace-regexp-in-string
"\\\\\\[]{" "\\(?:\\[]\\)?{" ;accept both src_sh[]{...} or src_sh{...} "\\\\\\[]{" "\\(?:\\[]\\)?{" ;accept both src_sh[]{...} or src_sh{...}
(regexp-quote "Here is one in the middle src_sh[]{echo 1} of a line. (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[]{echo 2} Here is one at the end of a line. src_sh[ :exports code]{echo 2}
src_sh[]{echo 3} Here is one at the beginning of a line. src_sh[ :exports code]{echo 3} Here is one at the beginning of a line.
Here is one that is also evaluated: src_sh[]{echo 4} {{{results(=4=)}}}") Here is one that is also evaluated: src_sh[ :exports both]{echo 4} {{{results(=4=)}}}")
nil t) nil t)
(org-test-at-id "cd54fc88-1b6b-45b6-8511-4d8fa7fc8076" (org-test-at-id "cd54fc88-1b6b-45b6-8511-4d8fa7fc8076"
(org-narrow-to-subtree) (org-narrow-to-subtree)
@ -301,7 +301,7 @@ be evaluated."
(ert-deftest ob-exp/exports-inline-code-double-eval-exports-both () (ert-deftest ob-exp/exports-inline-code-double-eval-exports-both ()
(let ((org-export-use-babel t)) (let ((org-export-use-babel t))
(should (should
(string-match (concat "\\`src_emacs-lisp\\(?:\\[]\\)?{(\\+ 1 1)} " (string-match (concat "\\`src_emacs-lisp\\(?:\\[.+?]\\)?{(\\+ 1 1)} "
"{{{results(src_emacs-lisp\\[ :exports code\\]{2})}}}$") "{{{results(src_emacs-lisp\\[ :exports code\\]{2})}}}$")
(org-test-with-temp-text (org-test-with-temp-text
(concat "src_emacs-lisp[:exports both :results code " (concat "src_emacs-lisp[:exports both :results code "
@ -403,7 +403,7 @@ be evaluated."
: 2 : 2
#+NAME: src1 #+NAME: src1
#+begin_src emacs-lisp #+begin_src emacs-lisp :exports both
\(+ 1 1) \(+ 1 1)
#+end_src" #+end_src"
(org-test-with-temp-text (org-test-with-temp-text