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.
This commit is contained in:
Eric Schulte 2010-11-22 18:20:56 -07:00
parent 29e2b27b5a
commit b174975eb7
4 changed files with 35 additions and 34 deletions

View File

@ -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.

View File

@ -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)))

View File

@ -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.

View File

@ -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