summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Schulte <schulte.eric@gmail.com>2010-11-22 18:20:56 -0700
committerEric Schulte <schulte.eric@gmail.com>2010-11-22 18:51:00 -0700
commitb174975eb7a828d77816a9bf8107fc1a3ae597d7 (patch)
tree3beb6f811dd83a7c0e90c65177224fed98dc6511
parent29e2b27b5af19530f2cbdca34a65590b12813d6e (diff)
downloadorg-mode-b174975eb7a828d77816a9bf8107fc1a3ae597d7.tar.gz
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.
-rw-r--r--lisp/ob-haskell.el11
-rw-r--r--lisp/ob-python.el15
-rw-r--r--lisp/ob-ruby.el11
-rw-r--r--lisp/ob.el32
4 files changed, 35 insertions, 34 deletions
diff --git a/lisp/ob-haskell.el b/lisp/ob-haskell.el
index 1ae8fba..3539bfc 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 5aaa105..21b8ff1 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 9a9f21c..e7cfd48 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 584d627..2cb074d 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