org-babel-ruby.el 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. ;;; org-babel-ruby.el --- org-babel functions for ruby evaluation
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Org-Babel support for evaluating ruby source code.
  24. ;;; Code:
  25. (require 'org-babel)
  26. (require 'inf-ruby)
  27. (org-babel-add-interpreter "ruby")
  28. (add-to-list 'org-babel-tangle-langs '("ruby" "rb" "#!/usr/bin/env ruby"))
  29. (defun org-babel-execute:ruby (body params)
  30. "Execute a block of Ruby code with org-babel. This function is
  31. called by `org-babel-execute-src-block'."
  32. (message "executing Ruby source code block")
  33. (let* ((vars (org-babel-ref-variables params))
  34. (result-params (split-string (or (cdr (assoc :results params)) "")))
  35. (result-type (cond ((member "output" result-params) 'output)
  36. ((member "value" result-params) 'value)
  37. (t 'value)))
  38. (full-body (concat
  39. (mapconcat ;; define any variables
  40. (lambda (pair)
  41. (format "%s=%s"
  42. (car pair)
  43. (org-babel-ruby-var-to-ruby (cdr pair))))
  44. vars "\n") "\n" body "\n")) ;; then the source block body
  45. (session (org-babel-ruby-initiate-session (cdr (assoc :session params))))
  46. (results (org-babel-ruby-evaluate session full-body result-type)))
  47. (if (member "scalar" result-params)
  48. results
  49. (case result-type ;; process results based on the result-type
  50. ('output (let ((tmp-file (make-temp-file "org-babel-ruby")))
  51. (with-temp-file tmp-file (insert results))
  52. (org-babel-import-elisp-from-file tmp-file)))
  53. ('value (org-babel-ruby-table-or-results results))))))
  54. (defun org-babel-prep-session:ruby (session params)
  55. "Prepare SESSION according to the header arguments specified in PARAMS."
  56. ;; (message "params=%S" params) ;; debugging
  57. (let* ((session (org-babel-ruby-initiate-session session))
  58. (vars (org-babel-ref-variables params))
  59. (var-lines (mapcar ;; define any variables
  60. (lambda (pair)
  61. (format "%s=%s"
  62. (car pair)
  63. (org-babel-ruby-var-to-ruby (cdr pair))))
  64. vars)))
  65. ;; (message "vars=%S" vars) ;; debugging
  66. (org-babel-comint-in-buffer session
  67. (sit-for .5) (goto-char (point-max))
  68. (mapc (lambda (var)
  69. (insert var) (comint-send-input nil t)
  70. (org-babel-comint-wait-for-output session)
  71. (sit-for .1) (goto-char (point-max))) var-lines))))
  72. ;; helper functions
  73. (defun org-babel-ruby-var-to-ruby (var)
  74. "Convert an elisp var into a string of ruby source code
  75. specifying a var of the same value."
  76. (if (listp var)
  77. (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
  78. (format "%S" var)))
  79. (defun org-babel-ruby-table-or-results (results)
  80. "If the results look like a table, then convert them into an
  81. Emacs-lisp table, otherwise return the results as a string."
  82. (org-babel-read
  83. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  84. (org-babel-read
  85. (replace-regexp-in-string
  86. "\\[" "(" (replace-regexp-in-string
  87. "\\]" ")" (replace-regexp-in-string
  88. ", " " " (replace-regexp-in-string
  89. "'" "\"" results)))))
  90. results)))
  91. (defun org-babel-ruby-initiate-session (&optional session)
  92. "If there is not a current inferior-process-buffer in SESSION
  93. then create. Return the initialized session."
  94. (unless (string= session "none")
  95. (let ((session-buffer (save-window-excursion (run-ruby nil session) (current-buffer))))
  96. (if (org-babel-comint-buffer-livep session-buffer)
  97. session-buffer
  98. (sit-for .5)
  99. (org-babel-ruby-initiate-session session)))))
  100. (defvar org-babel-ruby-last-value-eval "_"
  101. "When evaluated by Ruby this returns the return value of the last statement.")
  102. (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
  103. "Used to indicate that evaluation is has completed.")
  104. (defvar org-babel-ruby-wrapper-method
  105. "
  106. def main()
  107. %s
  108. end
  109. results = main()
  110. File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
  111. ")
  112. (defun org-babel-ruby-evaluate (buffer body &optional result-type)
  113. "Pass BODY to the Ruby process in BUFFER. If RESULT-TYPE equals
  114. 'output then return a list of the outputs of the statements in
  115. BODY, if RESULT-TYPE equals 'value then return the value of the
  116. last statement in BODY."
  117. (if (not session)
  118. ;; external process evaluation
  119. (save-window-excursion
  120. (case result-type
  121. (output
  122. (with-temp-buffer
  123. (insert body)
  124. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  125. (shell-command-on-region (point-min) (point-max) "ruby" 'replace)
  126. (buffer-string)))
  127. (value
  128. (let ((tmp-file (make-temp-file "ruby-functional-results")))
  129. (with-temp-buffer
  130. (insert (format org-babel-ruby-wrapper-method body tmp-file))
  131. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  132. (shell-command-on-region (point-min) (point-max) "ruby"))
  133. (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))))
  134. ;; comint session evaluation
  135. (message "session evaluation")
  136. (let* ((full-body (mapconcat #'org-babel-chomp
  137. (list body org-babel-ruby-last-value-eval org-babel-ruby-eoe-indicator) "\n"))
  138. (raw (org-babel-comint-with-output buffer org-babel-ruby-eoe-indicator t
  139. (insert full-body) (comint-send-input nil t)))
  140. (results (cdr (member org-babel-ruby-eoe-indicator
  141. (reverse (mapcar #'org-babel-ruby-read-string
  142. (mapcar #'org-babel-trim raw)))))))
  143. (case result-type
  144. (output (mapconcat #'identity (reverse (cdr results)) "\n"))
  145. (value (car results))))))
  146. (defun org-babel-ruby-read-string (string)
  147. "Strip \\\"s from around ruby string"
  148. (if (string-match "\"\\([^\000]+\\)\"" string)
  149. (match-string 1 string)
  150. string))
  151. (provide 'org-babel-ruby)
  152. ;;; org-babel-ruby.el ends here