From 517837b3a167bf365be71186d13615f4f0c49dcf Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Sun, 27 Jun 2010 15:50:07 -0700 Subject: [PATCH 1/4] library of babel: added json function for accessing local or remote json objects A quick example of accessing remote json data from Babel code blocks. Evaluate the following to see a listing of parks in DC by ward. #+source: dc-parks #+begin_src emacs-lisp :var keys='(ward address) :var data=json(url="http://ogdi.cloudapp.net/v1/dc/RecreationParks?format=json") (append (list keys 'hline) (mapcar (lambda (lis) (mapcar (lambda (key) (cdr (assoc key lis))) keys)) (cdr (car data)))) #+end_src --- contrib/babel/library-of-babel.org | 37 ++++++++++++++++-------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/contrib/babel/library-of-babel.org b/contrib/babel/library-of-babel.org index 807ada4d9..330734440 100644 --- a/contrib/babel/library-of-babel.org +++ b/contrib/babel/library-of-babel.org @@ -1,22 +1,6 @@ #+title: The Library of Babel -#+SEQ_TODO: TODO PROPOSED | DONE DEFERRED REJECTED -#+OPTIONS: H:3 num:nil toc:2 \n:nil @:t ::t |:t ^:t -:t f:t *:t TeX:t LaTeX:t skip:nil d:(HIDE) tags:not-in-toc +#+author: Org-mode People #+STARTUP: odd hideblocks -#+STYLE: - -#+begin_html - -#+end_html * Introduction The Library of Babel is an extensible collection of ready-made and @@ -69,6 +53,25 @@ as a table in traditional Org-mode table syntax. nil #+end_src +** remote files + +Read local or remote file in [[http://www.json.org/][json]] format into emacs-lisp objects. +#+srcname: json +#+begin_src emacs-lisp :var file='() :var url='() + (require 'json) + (cond + (file + (with-temp-filebuffer file + (goto-char (point-min)) + (json-read))) + (url + (require 'w3m) + (with-temp-buffer + (w3m-retrieve url) + (goto-char (point-min)) + (json-read)))) +#+end_src + * Plotting code ** R From 2f0e2f1c5e2c0b29050bd25357fd40b58d50a702 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Sun, 27 Jun 2010 16:43:13 -0700 Subject: [PATCH 2/4] babel: python now treats 'hline lines in tables as "None"s Thanks to Christopher Webber (cwebb) for bringing this up. --- lisp/babel/langs/ob-python.el | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lisp/babel/langs/ob-python.el b/lisp/babel/langs/ob-python.el index 2ce9e1d3d..29bb16676 100644 --- a/lisp/babel/langs/ob-python.el +++ b/lisp/babel/langs/ob-python.el @@ -96,7 +96,7 @@ called by `org-babel-execute-src-block'." specifying a var of the same value." (if (listp var) (concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]") - (format "%S" var))) + (if (equal var 'hline) "None" (format "%S" var)))) (defun org-babel-python-table-or-string (results) "If the results look like a list or tuple, then convert them into an @@ -110,7 +110,9 @@ Emacs-lisp table, otherwise return the results as a string." "\\[" "(" (replace-regexp-in-string "\\]" ")" (replace-regexp-in-string ", " " " (replace-regexp-in-string - "'" "\"" results)))))) + "'" "\"" + (replace-regexp-in-string + "None" "hline" results t))))))) results))) (defvar org-babel-python-buffers '(:default . nil)) From 2653b62c477bd9092dbdd3583c53fefa92bdb7d2 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Sun, 27 Jun 2010 17:59:29 -0700 Subject: [PATCH 3/4] babel: now explicitly requiring outline.el from ob.el --- lisp/babel/ob.el | 1 + 1 file changed, 1 insertion(+) diff --git a/lisp/babel/ob.el b/lisp/babel/ob.el index c6aa7d24a..7da15f7ed 100644 --- a/lisp/babel/ob.el +++ b/lisp/babel/ob.el @@ -30,6 +30,7 @@ ;;; Code: (eval-when-compile (require 'cl)) +(require 'outline) (defvar org-babel-call-process-region-original) (declare-function show-all "outline" ()) From 05b7ea50354af98b4852d20a298b4aa874143037 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Sun, 27 Jun 2010 23:10:53 -0700 Subject: [PATCH 4/4] babel: ensure `declare-function' is available in all Emacsen Thanks to Daniel Mahler for finding this problem and proposing the fix * lisp/babel/ob.el (unless): ensure `declare-function' is available in all Emacsen --- lisp/babel/ob.el | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lisp/babel/ob.el b/lisp/babel/ob.el index 7da15f7ed..5a1089e5a 100644 --- a/lisp/babel/ob.el +++ b/lisp/babel/ob.el @@ -30,7 +30,9 @@ ;;; Code: (eval-when-compile (require 'cl)) -(require 'outline) +(eval-and-compile + (unless (fboundp 'declare-function) + (defmacro declare-function (fn file &optional arglist fileonly)))) (defvar org-babel-call-process-region-original) (declare-function show-all "outline" ())