ob-ruby.el 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. ;;; ob-ruby.el --- org-babel functions for ruby evaluation
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs 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 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating ruby source code.
  20. ;;; Requirements:
  21. ;; - ruby and irb executables :: http://www.ruby-lang.org/
  22. ;;
  23. ;; - ruby-mode :: Can be installed through ELPA, or from
  24. ;; http://github.com/eschulte/rinari/raw/master/util/ruby-mode.el
  25. ;;
  26. ;; - inf-ruby mode :: Can be installed through ELPA, or from
  27. ;; http://github.com/eschulte/rinari/raw/master/util/inf-ruby.el
  28. ;;; Code:
  29. (require 'ob)
  30. (require 'inf-ruby)
  31. (add-to-list 'org-babel-tangle-lang-exts '("ruby" . "rb"))
  32. (defvar org-babel-default-header-args:ruby '())
  33. (defun org-babel-expand-body:ruby (body params &optional processed-params)
  34. "Expand BODY according to PARAMS, return the expanded body."
  35. (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
  36. (concat
  37. (mapconcat ;; define any variables
  38. (lambda (pair)
  39. (format "%s=%s"
  40. (car pair)
  41. (org-babel-ruby-var-to-ruby (cdr pair))))
  42. vars "\n") "\n" body "\n")))
  43. (defun org-babel-execute:ruby (body params)
  44. "Execute a block of Ruby code with org-babel. This function is
  45. called by `org-babel-execute-src-block'."
  46. (message "executing Ruby source code block")
  47. (let* ((processed-params (org-babel-process-params params))
  48. (session (org-babel-ruby-initiate-session (first processed-params)))
  49. (result-params (nth 2 processed-params))
  50. (result-type (nth 3 processed-params))
  51. (full-body (org-babel-expand-body:ruby
  52. body params processed-params))
  53. (result (org-babel-ruby-evaluate session full-body result-type)))
  54. (or (cdr (assoc :file params))
  55. (org-babel-reassemble-table
  56. result
  57. (org-babel-pick-name (nth 4 processed-params)
  58. (cdr (assoc :colnames params)))
  59. (org-babel-pick-name (nth 5 processed-params)
  60. (cdr (assoc :rownames params)))))))
  61. (defun org-babel-prep-session:ruby (session params)
  62. "Prepare SESSION according to the header arguments specified in PARAMS."
  63. ;; (message "params=%S" params) ;; debugging
  64. (let* ((session (org-babel-ruby-initiate-session session))
  65. (vars (org-babel-ref-variables params))
  66. (var-lines (mapcar ;; define any variables
  67. (lambda (pair)
  68. (format "%s=%s"
  69. (car pair)
  70. (org-babel-ruby-var-to-ruby (cdr pair))))
  71. vars)))
  72. (org-babel-comint-in-buffer session
  73. (sit-for .5) (goto-char (point-max))
  74. (mapc (lambda (var)
  75. (insert var) (comint-send-input nil t)
  76. (org-babel-comint-wait-for-output session)
  77. (sit-for .1) (goto-char (point-max))) var-lines))
  78. session))
  79. (defun org-babel-load-session:ruby (session body params)
  80. "Load BODY into SESSION."
  81. (save-window-excursion
  82. (let ((buffer (org-babel-prep-session:ruby session params)))
  83. (with-current-buffer buffer
  84. (goto-char (process-mark (get-buffer-process (current-buffer))))
  85. (insert (org-babel-chomp body)))
  86. buffer)))
  87. ;; helper functions
  88. (defun org-babel-ruby-var-to-ruby (var)
  89. "Convert an elisp var into a string of ruby source code
  90. specifying a var of the same value."
  91. (if (listp var)
  92. (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
  93. (format "%S" var)))
  94. (defun org-babel-ruby-table-or-string (results)
  95. "If RESULTS look like a table, then convert them into an
  96. Emacs-lisp table, otherwise return the results as a string."
  97. (message "converting %S" results)
  98. (org-babel-read
  99. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  100. (org-babel-read
  101. (concat "'"
  102. (replace-regexp-in-string
  103. "\\[" "(" (replace-regexp-in-string
  104. "\\]" ")" (replace-regexp-in-string
  105. ", " " " (replace-regexp-in-string
  106. "'" "\"" results))))))
  107. results)))
  108. (defun org-babel-ruby-initiate-session (&optional session params)
  109. "If there is not a current inferior-process-buffer in SESSION
  110. then create one. Return the initialized session."
  111. (unless (string= session "none")
  112. (let ((session-buffer (save-window-excursion
  113. (run-ruby nil session) (current-buffer))))
  114. (if (org-babel-comint-buffer-livep session-buffer)
  115. (progn (sit-for .25) session-buffer)
  116. (sit-for .5)
  117. (org-babel-ruby-initiate-session session)))))
  118. (defvar org-babel-ruby-last-value-eval "_"
  119. "When evaluated by Ruby this returns the return value of the last statement.")
  120. (defvar org-babel-ruby-pp-last-value-eval "require 'pp'; pp(_)"
  121. "When evaluated by Ruby this pretty prints value of the last statement.")
  122. (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
  123. "Used to indicate that evaluation is has completed.")
  124. (defvar org-babel-ruby-wrapper-method
  125. "
  126. def main()
  127. %s
  128. end
  129. results = main()
  130. File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
  131. ")
  132. (defvar org-babel-ruby-pp-wrapper-method
  133. "
  134. require 'pp'
  135. def main()
  136. %s
  137. end
  138. results = main()
  139. File.open('%s', 'w') do |f|
  140. $stdout = f
  141. pp results
  142. end
  143. ")
  144. (defun org-babel-ruby-evaluate (buffer body &optional result-type)
  145. "Pass BODY to the Ruby process in BUFFER. If RESULT-TYPE equals
  146. 'output then return a list of the outputs of the statements in
  147. BODY, if RESULT-TYPE equals 'value then return the value of the
  148. last statement in BODY, as elisp."
  149. (if (not session)
  150. ;; external process evaluation
  151. (save-excursion
  152. (case result-type
  153. (output
  154. (with-temp-buffer
  155. (insert body)
  156. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  157. (org-babel-shell-command-on-region
  158. (point-min) (point-max) "ruby" 'current-buffer 'replace)
  159. (buffer-string)))
  160. (value
  161. (let* ((tmp-file (make-temp-file "ruby-functional-results"))
  162. exit-code
  163. (stderr
  164. (with-temp-buffer
  165. (insert (format (if (member "pp" result-params)
  166. org-babel-ruby-pp-wrapper-method
  167. org-babel-ruby-wrapper-method)
  168. body tmp-file))
  169. (setq exit-code
  170. (org-babel-shell-command-on-region
  171. (point-min) (point-max) "ruby"
  172. nil 'replace (current-buffer)))
  173. (buffer-string))))
  174. (if (> exit-code 0) (org-babel-error-notify exit-code stderr))
  175. (let ((raw (with-temp-buffer
  176. (insert-file-contents
  177. (org-babel-maybe-remote-file tmp-file))
  178. (buffer-string))))
  179. (if (or (member "code" result-params)
  180. (member "pp" result-params))
  181. raw
  182. (org-babel-ruby-table-or-string raw)))))))
  183. ;; comint session evaluation
  184. (let* ((full-body
  185. (mapconcat
  186. #'org-babel-chomp
  187. (list body (if (member "pp" result-params)
  188. org-babel-ruby-pp-last-value-eval
  189. org-babel-ruby-last-value-eval)
  190. org-babel-ruby-eoe-indicator) "\n"))
  191. (raw (org-babel-comint-with-output
  192. (buffer org-babel-ruby-eoe-indicator t full-body)
  193. (insert full-body) (comint-send-input nil t)))
  194. (results (cdr (member
  195. org-babel-ruby-eoe-indicator
  196. (reverse (mapcar #'org-babel-ruby-read-string
  197. (mapcar #'org-babel-trim raw)))))))
  198. (case result-type
  199. (output (mapconcat #'identity (reverse (cdr results)) "\n"))
  200. (value
  201. (if (or (member "code" result-params) (member "pp" result-params))
  202. (car results)
  203. (org-babel-ruby-table-or-string (car results))))))))
  204. (defun org-babel-ruby-read-string (string)
  205. "Strip \\\"s from around a ruby string."
  206. (if (string-match "^\"\\([^\000]+\\)\"$" string)
  207. (match-string 1 string)
  208. string))
  209. (provide 'ob-ruby)
  210. ;; arch-tag: 3e9726db-4520-49e2-b263-e8f571ac88f5
  211. ;;; ob-ruby.el ends here