From b174975eb7a828d77816a9bf8107fc1a3ae597d7 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 22 Nov 2010 18:20:56 -0700 Subject: [PATCH] ob: safer conversion of results of scripting languages into elisp objects * lisp/ob-python.el (org-babel-python-table-or-string): Using `org-babel-script-escape' for reading string input from scripting languages. * lisp/ob-ruby.el (org-babel-ruby-table-or-string): Using `org-babel-script-escape' for reading string input from scripting languages. * lisp/ob.el (org-babel-script-escape): Using `org-babel-script-escape' for reading string input from scripting languages. * lisp/ob-haskell.el (org-babel-haskell-table-or-string): Using `org-babel-script-escape' for reading string input from scripting languages. --- lisp/ob-haskell.el | 11 +---------- lisp/ob-python.el | 15 +-------------- lisp/ob-ruby.el | 11 +---------- lisp/ob.el | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/lisp/ob-haskell.el b/lisp/ob-haskell.el index 1ae8fba66..3539bfc8e 100644 --- a/lisp/ob-haskell.el +++ b/lisp/ob-haskell.el @@ -136,16 +136,7 @@ then create one. Return the initialized session." "Convert RESULTS to an Emacs-lisp table or string. If RESULTS look like a table, then convert them into an Emacs-lisp table, otherwise return the results as a string." - (org-babel-read - (if (and (stringp results) (string-match "^\\[.+\\]$" results)) - (org-babel-read - (concat "'" - (replace-regexp-in-string - "\\[" "(" (replace-regexp-in-string - "\\]" ")" (replace-regexp-in-string - "," " " (replace-regexp-in-string - "'" "\"" results)))))) - results))) + (org-babel-script-escape results)) (defun org-babel-haskell-var-to-haskell (var) "Convert an elisp value VAR into a haskell variable. diff --git a/lisp/ob-python.el b/lisp/ob-python.el index 5aaa1059a..21b8ff18c 100644 --- a/lisp/ob-python.el +++ b/lisp/ob-python.el @@ -117,20 +117,7 @@ specifying a variable of the same value." "Convert RESULTS into an appropriate elisp value. If the results look like a list or tuple, then convert them into an Emacs-lisp table, otherwise return the results as a string." - ((lambda (res) - (if (listp res) - (mapcar (lambda (el) (if (equal el 'None) 'hline el)) res) - res)) - (org-babel-read - (if (and (stringp results) (string-match "^[([].+[])]$" results)) - (org-babel-read - (concat "'" - (replace-regexp-in-string - "\\[" "(" (replace-regexp-in-string - "\\]" ")" (replace-regexp-in-string - ", " " " (replace-regexp-in-string - "'" "\"" results t)))))) - results)))) + (org-babel-script-escape results)) (defvar org-babel-python-buffers '((:default . nil))) diff --git a/lisp/ob-ruby.el b/lisp/ob-ruby.el index 9a9f21cde..e7cfd48e9 100644 --- a/lisp/ob-ruby.el +++ b/lisp/ob-ruby.el @@ -116,16 +116,7 @@ specifying a variable of the same value." "Convert RESULTS into an appropriate elisp value. If RESULTS look like a table, then convert them into an Emacs-lisp table, otherwise return the results as a string." - (org-babel-read - (if (and (stringp results) (string-match "^\\[.+\\]$" results)) - (org-babel-read - (concat "'" - (replace-regexp-in-string - "\\[" "(" (replace-regexp-in-string - "\\]" ")" (replace-regexp-in-string - ", " " " (replace-regexp-in-string - "'" "\"" results)))))) - results))) + (org-babel-script-escape results)) (defun org-babel-ruby-initiate-session (&optional session params) "Initiate a ruby session. diff --git a/lisp/ob.el b/lisp/ob.el index 584d6275f..2cb074dbf 100644 --- a/lisp/ob.el +++ b/lisp/ob.el @@ -1757,6 +1757,38 @@ block but are passed literally to the \"example-block\"." "Strip protective commas from bodies of source blocks." (replace-regexp-in-string "^,#" "#" body)) +(defun org-babel-script-escape (str) + "Safely convert tables into elisp lists." + (let (in-single in-double out) + (org-babel-read + (if (and (stringp str) (string-match "^\\[.+\\]$" str)) + (org-babel-read + (concat + "'" + (progn + (mapc + (lambda (ch) + (setq + out + (case ch + (91 (if (or in-double in-single) ; [ + (cons 91 out) + (cons 40 out))) + (93 (if (or in-double in-single) ; ] + (cons 93 out) + (cons 41 out))) + (44 (if (or in-double in-single) (cons 44 out) out)) ; , + (39 (if in-double ; ' + (cons 39 out) + (setq in-single (not in-single)) (cons 34 out))) + (34 (if in-single ; " + (append (list 34 32) out) + (setq in-double (not in-double)) (cons 34 out))) + (t (cons ch out))))) + (string-to-list str)) + (apply #'string (reverse out))))) + str)))) + (defun org-babel-read (cell) "Convert the string value of CELL to a number if appropriate. Otherwise if cell looks like lisp (meaning it starts with a