Browse Source

table assignment now working for ruby and python

Eric Schulte 16 years ago
parent
commit
c814a9f3e8
3 changed files with 52 additions and 11 deletions
  1. 3 3
      litorgy/init.el
  2. 30 8
      litorgy/litorgy-script.el
  3. 19 0
      rorg.org

+ 3 - 3
litorgy/init.el

@@ -1,8 +1,8 @@
 ;;; init.el --- loads litorgy
 
-;; Copyright (C) 2009 Eric Schulte, Dan Davison, Austin F. Frank
+;; Copyright (C) 2009 Eric Schulte
 
-;; Author: Eric Schulte, Dan Davison, Austin F. Frank
+;; Author: Eric Schulte
 ;; Keywords: literate programming, reproducible research
 ;; Homepage: http://orgmode.org
 ;; Version: 0.01
@@ -34,8 +34,8 @@
 (require 'litorgy-reference)
 
 ;; language specific files
+(require 'litorgy-script)
 (require 'litorgy-R)
 (require 'litorgy-lisp)
-(require 'litorgy-script)
 
 ;;; init.el ends here

+ 30 - 8
litorgy/litorgy-script.el

@@ -37,8 +37,9 @@
           (setq litorgy-interpreters (cons cmd litorgy-interpreters))
           (eval
            `(defun ,(intern (concat "litorgy-execute:" cmd)) (body params)
-              (concat "Evaluate a block of " ,cmd " script with litorgy.
-This function is called by `litorgy-execute-src-block'.")
+              ,(concat "Evaluate a block of " cmd " script with litorgy. This function is
+called by `litorgy-execute-src-block'.  This function is an
+automatically generated wrapper for `litorgy-script-execute'.")
               (litorgy-script-execute ,cmd body params))))
         cmds))
 
@@ -51,12 +52,33 @@ executed through litorgy."
 (defun litorgy-script-execute (cmd body params)
   "Run CMD on BODY obeying any options set with PARAMS.
 TODO: currently the params part is not implemented"
-  (message (format "executing source block with %s..." cmd))
-  (with-temp-buffer
-    (insert body)
-    (shell-command-on-region (point-min) (point-max) cmd nil 'replace)
-    (message "finished executing source block")
-    (buffer-string)))
+  (message (format "executing %s code block..." cmd))
+  (let ((vars (litorgy-reference-variables params)))
+    (save-window-excursion
+      (with-temp-buffer
+        ;; define any variables
+        (mapcar
+         (lambda (pair)
+           (case (intern cmd)
+             ((sh bash zsh) ;; TODO support table assignment in shell scripts
+              (error (format "table assignment is not supported for %s" cmd)))
+             ((ruby python)
+              (insert (format "%s=%s\n"
+                              (car pair)
+                              (litorgy-table-to-ruby/python (cdr pair)))))
+             ))
+         vars)
+        (insert body)
+        (shell-command-on-region (point-min) (point-max) cmd nil 'replace)
+        (message "finished executing source block")
+        (buffer-string)))))
+
+(defun litorgy-table-to-ruby/python (table)
+  "Convert an elisp table (nested lists) into a string of ruby
+source code specifying a table (nested arrays)."
+  (if (listp table)
+      (concat "[" (mapconcat #'litorgy-table-to-ruby/python table ", ") "]")
+    (format "%S" table)))
 
 (provide 'litorgy-script)
 ;;; litorgy-script.el ends here

+ 19 - 0
rorg.org

@@ -751,6 +751,7 @@ out...
   (apply #'mapcar* #'list table))
 #+end_src
 
+#+TBLNAME: sandbox
 | 1 |       2 | 3 |
 | 4 | schulte | 6 |
 
@@ -758,6 +759,24 @@ out...
 (transpose table)
 #+end_src
 
+#+begin_src emacs-lisp :var table=sandbox :replace t
+(transpose table)
+#+end_src
+
+#+begin_src ruby :var table=sandbox :replace t
+puts table.first.join(" - ")
+#+end_src
+
+: 1 - 2 - 3
+
+#+begin_src python :var table=sandbox :replace t
+print table[0]
+#+end_src
+
+: [1, 2, 3]
+
+
+
 
 * COMMENT Commentary
 I'm seeing this as like commit notes, and a place for less formal