org-babel-ruby.el 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. ;;; Requirements:
  25. ;; - ruby and irb executables :: http://www.ruby-lang.org/
  26. ;;
  27. ;; - ruby-mode :: Can be installed through ELPA, or from
  28. ;; http://github.com/eschulte/rinari/raw/master/util/ruby-mode.el
  29. ;;
  30. ;; - inf-ruby mode :: Can be installed through ELPA, or from
  31. ;; http://github.com/eschulte/rinari/raw/master/util/inf-ruby.el
  32. ;;; Code:
  33. (require 'org-babel)
  34. (require 'inf-ruby)
  35. (org-babel-add-interpreter "ruby")
  36. (add-to-list 'org-babel-tangle-langs '("ruby" "rb" "#!/usr/bin/env ruby"))
  37. (defun org-babel-execute:ruby (body params)
  38. "Execute a block of Ruby code with org-babel. This function is
  39. called by `org-babel-execute-src-block'."
  40. (message "executing Ruby source code block")
  41. (let* ((processed-params (org-babel-process-params params))
  42. (session (org-babel-ruby-initiate-session (first processed-params)))
  43. (vars (second processed-params))
  44. (result-params (third processed-params))
  45. (result-type (fourth processed-params))
  46. (full-body (concat
  47. (mapconcat ;; define any variables
  48. (lambda (pair)
  49. (format "%s=%s"
  50. (car pair)
  51. (org-babel-ruby-var-to-ruby (cdr pair))))
  52. vars "\n") "\n" body "\n")) ;; then the source block body
  53. (result (org-babel-ruby-evaluate session full-body result-type)))
  54. (or (cdr (assoc :file params)) result)))
  55. (defun org-babel-prep-session:ruby (session params)
  56. "Prepare SESSION according to the header arguments specified in PARAMS."
  57. ;; (message "params=%S" params) ;; debugging
  58. (let* ((session (org-babel-ruby-initiate-session session))
  59. (vars (org-babel-ref-variables params))
  60. (var-lines (mapcar ;; define any variables
  61. (lambda (pair)
  62. (format "%s=%s"
  63. (car pair)
  64. (org-babel-ruby-var-to-ruby (cdr pair))))
  65. vars)))
  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. session))
  73. (defun org-babel-load-session:ruby (session body params)
  74. "Load BODY into SESSION."
  75. (save-window-excursion
  76. (let ((buffer (org-babel-prep-session:ruby session params)))
  77. (with-current-buffer buffer
  78. (goto-char (process-mark (get-buffer-process (current-buffer))))
  79. (insert (org-babel-chomp body)))
  80. buffer)))
  81. ;; helper functions
  82. (defun org-babel-ruby-var-to-ruby (var)
  83. "Convert an elisp var into a string of ruby source code
  84. specifying a var of the same value."
  85. (if (listp var)
  86. (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
  87. (format "%S" var)))
  88. (defun org-babel-ruby-table-or-string (results)
  89. "If the results look like a table, then convert them into an
  90. Emacs-lisp table, otherwise return the results as a string."
  91. (org-babel-read
  92. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  93. (org-babel-read
  94. (replace-regexp-in-string
  95. "\\[" "(" (replace-regexp-in-string
  96. "\\]" ")" (replace-regexp-in-string
  97. ", " " " (replace-regexp-in-string
  98. "'" "\"" results)))))
  99. results)))
  100. (defun org-babel-ruby-initiate-session (&optional session)
  101. "If there is not a current inferior-process-buffer in SESSION
  102. then create. Return the initialized session."
  103. (unless (string= session "none")
  104. (let ((session-buffer (save-window-excursion (run-ruby nil session) (current-buffer))))
  105. (if (org-babel-comint-buffer-livep session-buffer)
  106. session-buffer
  107. (sit-for .5)
  108. (org-babel-ruby-initiate-session session)))))
  109. (defvar org-babel-ruby-last-value-eval "_"
  110. "When evaluated by Ruby this returns the return value of the last statement.")
  111. (defvar org-babel-ruby-pp-last-value-eval "require 'pp'; pp(_)"
  112. "When evaluated by Ruby this pretty prints value of the last statement.")
  113. (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
  114. "Used to indicate that evaluation is has completed.")
  115. (defvar org-babel-ruby-wrapper-method
  116. "
  117. def main()
  118. %s
  119. end
  120. results = main()
  121. File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
  122. ")
  123. (defvar org-babel-ruby-pp-wrapper-method
  124. "
  125. require 'pp'
  126. def main()
  127. %s
  128. end
  129. results = main()
  130. File.open('%s', 'w') do |f|
  131. $stdout = f
  132. pp results
  133. end
  134. ")
  135. (defun org-babel-ruby-evaluate (buffer body &optional result-type)
  136. "Pass BODY to the Ruby process in BUFFER. If RESULT-TYPE equals
  137. 'output then return a list of the outputs of the statements in
  138. BODY, if RESULT-TYPE equals 'value then return the value of the
  139. last statement in BODY, as elisp."
  140. (if (not session)
  141. ;; external process evaluation
  142. (save-window-excursion
  143. (case result-type
  144. (output
  145. (with-temp-buffer
  146. (insert body)
  147. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  148. (shell-command-on-region (point-min) (point-max) "ruby" 'replace)
  149. (buffer-string)))
  150. (value
  151. (let ((tmp-file (make-temp-file "ruby-functional-results")))
  152. (with-temp-buffer
  153. (insert (format (if (member "pp" result-params)
  154. org-babel-ruby-pp-wrapper-method
  155. org-babel-ruby-wrapper-method) body tmp-file))
  156. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  157. (shell-command-on-region (point-min) (point-max) "ruby"))
  158. (let ((raw (with-temp-buffer (insert-file-contents tmp-file)
  159. (buffer-string))))
  160. (if (or (member "code" result-params) (member "pp" result-params))
  161. raw
  162. (org-babel-ruby-table-or-string raw)))))))
  163. ;; comint session evaluation
  164. (let* ((full-body
  165. (mapconcat
  166. #'org-babel-chomp
  167. (list body (if (member "pp" result-params)
  168. org-babel-ruby-pp-last-value-eval
  169. org-babel-ruby-last-value-eval)
  170. org-babel-ruby-eoe-indicator) "\n"))
  171. (raw (org-babel-comint-with-output buffer org-babel-ruby-eoe-indicator t
  172. (insert full-body) (comint-send-input nil t)))
  173. (results (cdr (member org-babel-ruby-eoe-indicator
  174. (reverse (mapcar #'org-babel-ruby-read-string
  175. (mapcar #'org-babel-trim raw)))))))
  176. (case result-type
  177. (output (mapconcat #'identity (reverse (cdr results)) "\n"))
  178. (value
  179. (if (or (member "code" result-params) (member "pp" result-params))
  180. (car results)
  181. (org-babel-ruby-table-or-string (car results))))))))
  182. (defun org-babel-ruby-read-string (string)
  183. "Strip \\\"s from around ruby string"
  184. (if (string-match "\"\\([^\000]+\\)\"" string)
  185. (match-string 1 string)
  186. string))
  187. (provide 'org-babel-ruby)
  188. ;;; org-babel-ruby.el ends here