org-babel-read: Allow reading multi-line strings

* lisp/ob-core.el (org-babel-read): Fix regexp for detecting
string-like CELLs.  Avoid calling `read' twice.  Recover gracefully if
`read' errs.
* testing/lisp/test-ob.el (test-ob/org-babel-read): Add more tests.

Reported-by: Max Nikulin <manikulin@gmail.com>
Link: https://orgmode.org/list/v155g2$ncm$1@ciao.gmane.io
This commit is contained in:
Ihor Radchenko 2024-05-04 14:46:46 +03:00
parent 1523e21d82
commit edb5eaaac3
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 20 additions and 10 deletions

View File

@ -3360,16 +3360,23 @@ situations in which is it not appropriate."
(string= cell "*this*"))) (string= cell "*this*")))
;; FIXME: Arbitrary code evaluation. ;; FIXME: Arbitrary code evaluation.
(eval (read cell) t)) (eval (read cell) t))
((save-match-data ((let (read-val)
(and (string-match "^[[:space:]]*\"\\(.*\\)\"[[:space:]]*$" cell) (save-match-data
(and (string-match
(rx bos (0+ space)
?\" (0+ anychar) ?\"
(0+ space) eos)
cell)
;; CELL is a single string ;; CELL is a single string
(with-temp-buffer (with-temp-buffer
(insert cell) (insert cell)
(goto-char 1) (goto-char 1)
(read (current-buffer)) (when (setq read-val
(ignore-errors
(read (current-buffer))))
(skip-chars-forward "[:space:]") (skip-chars-forward "[:space:]")
(eobp)))) (eobp)))
(read cell)) read-val))))
(t (org-no-properties cell)))) (t (org-no-properties cell))))
(defun org-babel--string-to-number (string) (defun org-babel--string-to-number (string)

View File

@ -2599,6 +2599,9 @@ abc
;; Special case: data inside quotes ;; Special case: data inside quotes
(should (equal "foo" (org-babel-read " \"foo\" " inhibit))) (should (equal "foo" (org-babel-read " \"foo\" " inhibit)))
(should (equal "foo with\" inside" (org-babel-read " \"foo with\\\" inside\" " inhibit))) (should (equal "foo with\" inside" (org-babel-read " \"foo with\\\" inside\" " inhibit)))
(should (equal "abc\nsdf" (org-babel-read "\"abc\nsdf\"" inhibit)))
(should (equal "foo" (org-babel-read "\"foo\"" inhibit)))
(should (equal "\"foo\"(\"bar\"" (org-babel-read "\"foo\"(\"bar\"" inhibit)))
;; Unpaired quotes ;; Unpaired quotes
(should (equal "\"foo\"\"bar\"" (org-babel-read "\"foo\"\"bar\"" inhibit))))) (should (equal "\"foo\"\"bar\"" (org-babel-read "\"foo\"\"bar\"" inhibit)))))