ob-ruby.el 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. ;;; ob-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 'ob)
  34. (require 'inf-ruby)
  35. (defun org-babel-expand-body:ruby (body params &optional processed-params)
  36. "Expand BODY according to PARAMS, return the expanded body."
  37. (let ((vars (second (or processed-params (org-babel-process-params params)))))
  38. (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")))
  45. (defun org-babel-execute:ruby (body params)
  46. "Execute a block of Ruby code with org-babel. This function is
  47. called by `org-babel-execute-src-block'."
  48. (message "executing Ruby source code block")
  49. (let* ((processed-params (org-babel-process-params params))
  50. (session (org-babel-ruby-initiate-session (first processed-params)))
  51. (result-params (third processed-params))
  52. (result-type (fourth processed-params))
  53. (full-body (org-babel-expand-body:ruby
  54. body params processed-params))
  55. (result (org-babel-ruby-evaluate session full-body result-type)))
  56. (or (cdr (assoc :file params))
  57. (org-babel-reassemble-table
  58. result
  59. (org-babel-pick-name (nth 4 processed-params)
  60. (cdr (assoc :colnames params)))
  61. (org-babel-pick-name (nth 5 processed-params)
  62. (cdr (assoc :rownames params)))))))
  63. (defun org-babel-prep-session:ruby (session params)
  64. "Prepare SESSION according to the header arguments specified in PARAMS."
  65. ;; (message "params=%S" params) ;; debugging
  66. (let* ((session (org-babel-ruby-initiate-session session))
  67. (vars (org-babel-ref-variables params))
  68. (var-lines (mapcar ;; define any variables
  69. (lambda (pair)
  70. (format "%s=%s"
  71. (car pair)
  72. (org-babel-ruby-var-to-ruby (cdr pair))))
  73. vars)))
  74. (org-babel-comint-in-buffer session
  75. (sit-for .5) (goto-char (point-max))
  76. (mapc (lambda (var)
  77. (insert var) (comint-send-input nil t)
  78. (org-babel-comint-wait-for-output session)
  79. (sit-for .1) (goto-char (point-max))) var-lines))
  80. session))
  81. (defun org-babel-load-session:ruby (session body params)
  82. "Load BODY into SESSION."
  83. (save-window-excursion
  84. (let ((buffer (org-babel-prep-session:ruby session params)))
  85. (with-current-buffer buffer
  86. (goto-char (process-mark (get-buffer-process (current-buffer))))
  87. (insert (org-babel-chomp body)))
  88. buffer)))
  89. ;; helper functions
  90. (defun org-babel-ruby-var-to-ruby (var)
  91. "Convert an elisp var into a string of ruby source code
  92. specifying a var of the same value."
  93. (if (listp var)
  94. (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
  95. (format "%S" var)))
  96. (defun org-babel-ruby-table-or-string (results)
  97. "If RESULTS look like a table, then convert them into an
  98. Emacs-lisp table, otherwise return the results as a string."
  99. (message "converting %S" results)
  100. (org-babel-read
  101. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  102. (org-babel-read
  103. (concat "'"
  104. (replace-regexp-in-string
  105. "\\[" "(" (replace-regexp-in-string
  106. "\\]" ")" (replace-regexp-in-string
  107. ", " " " (replace-regexp-in-string
  108. "'" "\"" results))))))
  109. results)))
  110. (defun org-babel-ruby-initiate-session (&optional session params)
  111. "If there is not a current inferior-process-buffer in SESSION
  112. then create one. Return the initialized session."
  113. (unless (string= session "none")
  114. (let ((session-buffer (save-window-excursion
  115. (run-ruby nil session) (current-buffer))))
  116. (if (org-babel-comint-buffer-livep session-buffer)
  117. (progn (sit-for .25) session-buffer)
  118. (sit-for .5)
  119. (org-babel-ruby-initiate-session session)))))
  120. (defvar org-babel-ruby-last-value-eval "_"
  121. "When evaluated by Ruby this returns the return value of the last statement.")
  122. (defvar org-babel-ruby-pp-last-value-eval "require 'pp'; pp(_)"
  123. "When evaluated by Ruby this pretty prints value of the last statement.")
  124. (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
  125. "Used to indicate that evaluation is has completed.")
  126. (defvar org-babel-ruby-wrapper-method
  127. "
  128. def main()
  129. %s
  130. end
  131. results = main()
  132. File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
  133. ")
  134. (defvar org-babel-ruby-pp-wrapper-method
  135. "
  136. require 'pp'
  137. def main()
  138. %s
  139. end
  140. results = main()
  141. File.open('%s', 'w') do |f|
  142. $stdout = f
  143. pp results
  144. end
  145. ")
  146. (defun org-babel-ruby-evaluate (buffer body &optional result-type)
  147. "Pass BODY to the Ruby process in BUFFER. If RESULT-TYPE equals
  148. 'output then return a list of the outputs of the statements in
  149. BODY, if RESULT-TYPE equals 'value then return the value of the
  150. last statement in BODY, as elisp."
  151. (if (not session)
  152. ;; external process evaluation
  153. (save-excursion
  154. (case result-type
  155. (output
  156. (with-temp-buffer
  157. (insert body)
  158. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  159. (org-babel-shell-command-on-region
  160. (point-min) (point-max) "ruby" 'current-buffer 'replace)
  161. (buffer-string)))
  162. (value
  163. (let* ((tmp-file (make-temp-file "ruby-functional-results"))
  164. exit-code
  165. (stderr
  166. (with-temp-buffer
  167. (insert (format (if (member "pp" result-params)
  168. org-babel-ruby-pp-wrapper-method
  169. org-babel-ruby-wrapper-method)
  170. body tmp-file))
  171. (setq exit-code
  172. (org-babel-shell-command-on-region
  173. (point-min) (point-max) "ruby"
  174. nil 'replace (current-buffer)))
  175. (buffer-string))))
  176. (if (> exit-code 0) (org-babel-error-notify exit-code stderr))
  177. (let ((raw (with-temp-buffer
  178. (insert-file-contents
  179. (org-babel-maybe-remote-file tmp-file))
  180. (buffer-string))))
  181. (if (or (member "code" result-params)
  182. (member "pp" result-params))
  183. raw
  184. (org-babel-ruby-table-or-string raw)))))))
  185. ;; comint session evaluation
  186. (let* ((full-body
  187. (mapconcat
  188. #'org-babel-chomp
  189. (list body (if (member "pp" result-params)
  190. org-babel-ruby-pp-last-value-eval
  191. org-babel-ruby-last-value-eval)
  192. org-babel-ruby-eoe-indicator) "\n"))
  193. (raw (org-babel-comint-with-output
  194. (buffer org-babel-ruby-eoe-indicator t full-body)
  195. (insert full-body) (comint-send-input nil t)))
  196. (results (cdr (member
  197. org-babel-ruby-eoe-indicator
  198. (reverse (mapcar #'org-babel-ruby-read-string
  199. (mapcar #'org-babel-trim raw)))))))
  200. (case result-type
  201. (output (mapconcat #'identity (reverse (cdr results)) "\n"))
  202. (value
  203. (if (or (member "code" result-params) (member "pp" result-params))
  204. (car results)
  205. (org-babel-ruby-table-or-string (car results))))))))
  206. (defun org-babel-ruby-read-string (string)
  207. "Strip \\\"s from around a ruby string."
  208. (if (string-match "^\"\\([^\000]+\\)\"$" string)
  209. (match-string 1 string)
  210. string))
  211. (provide 'ob-ruby)
  212. ;;; ob-ruby.el ends here