org-babel-ruby.el 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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' via multiple-value-bind."
  32. (message "executing Ruby source code block")
  33. (let ((full-body (concat
  34. (mapconcat ;; define any variables
  35. (lambda (pair)
  36. (format "%s=%s"
  37. (car pair)
  38. (org-babel-ruby-var-to-ruby (cdr pair))))
  39. vars "\n") "\n" body "\n")) ;; then the source block body
  40. (session (org-babel-ruby-initiate-session session)))
  41. (org-babel-ruby-evaluate session full-body result-type)))
  42. (defun org-babel-prep-session:ruby (session params)
  43. "Prepare SESSION according to the header arguments specified in PARAMS."
  44. ;; (message "params=%S" params) ;; debugging
  45. (let* ((session (org-babel-ruby-initiate-session session))
  46. (vars (org-babel-ref-variables params))
  47. (var-lines (mapcar ;; define any variables
  48. (lambda (pair)
  49. (format "%s=%s"
  50. (car pair)
  51. (org-babel-ruby-var-to-ruby (cdr pair))))
  52. vars)))
  53. ;; (message "vars=%S" vars) ;; debugging
  54. (org-babel-comint-in-buffer session
  55. (sit-for .5) (goto-char (point-max))
  56. (mapc (lambda (var)
  57. (insert var) (comint-send-input nil t)
  58. (org-babel-comint-wait-for-output session)
  59. (sit-for .1) (goto-char (point-max))) var-lines))))
  60. ;; helper functions
  61. (defun org-babel-ruby-var-to-ruby (var)
  62. "Convert an elisp var into a string of ruby source code
  63. specifying a var of the same value."
  64. (if (listp var)
  65. (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
  66. (format "%S" var)))
  67. (defun org-babel-ruby-table-or-string (results)
  68. "If the results look like a table, then convert them into an
  69. Emacs-lisp table, otherwise return the results as a string."
  70. (org-babel-read
  71. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  72. (org-babel-read
  73. (replace-regexp-in-string
  74. "\\[" "(" (replace-regexp-in-string
  75. "\\]" ")" (replace-regexp-in-string
  76. ", " " " (replace-regexp-in-string
  77. "'" "\"" results)))))
  78. results)))
  79. (defun org-babel-ruby-initiate-session (&optional session)
  80. "If there is not a current inferior-process-buffer in SESSION
  81. then create. Return the initialized session."
  82. (unless (string= session "none")
  83. (let ((session-buffer (save-window-excursion (run-ruby nil session) (current-buffer))))
  84. (if (org-babel-comint-buffer-livep session-buffer)
  85. session-buffer
  86. (sit-for .5)
  87. (org-babel-ruby-initiate-session session)))))
  88. (defvar org-babel-ruby-last-value-eval "_"
  89. "When evaluated by Ruby this returns the return value of the last statement.")
  90. (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
  91. "Used to indicate that evaluation is has completed.")
  92. (defvar org-babel-ruby-wrapper-method
  93. "
  94. def main()
  95. %s
  96. end
  97. results = main()
  98. File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
  99. ")
  100. (defun org-babel-ruby-evaluate (buffer body &optional result-type)
  101. "Pass BODY to the Ruby process in BUFFER. If RESULT-TYPE equals
  102. 'output then return a list of the outputs of the statements in
  103. BODY, if RESULT-TYPE equals 'value then return the value of the
  104. last statement in BODY, as elisp."
  105. (if (not session)
  106. ;; external process evaluation
  107. (save-window-excursion
  108. (case result-type
  109. (output
  110. (with-temp-buffer
  111. (insert body)
  112. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  113. (shell-command-on-region (point-min) (point-max) "ruby" 'replace)
  114. (buffer-string)))
  115. (value
  116. (let ((tmp-file (make-temp-file "ruby-functional-results")))
  117. (with-temp-buffer
  118. (insert (format org-babel-ruby-wrapper-method body tmp-file))
  119. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  120. (shell-command-on-region (point-min) (point-max) "ruby"))
  121. (org-babel-ruby-table-or-string
  122. (with-temp-buffer (insert-file-contents tmp-file) (buffer-string)))))))
  123. ;; comint session evaluation
  124. (let* ((full-body
  125. (mapconcat
  126. #'org-babel-chomp
  127. (list body org-babel-ruby-last-value-eval org-babel-ruby-eoe-indicator) "\n"))
  128. (raw (org-babel-comint-with-output buffer org-babel-ruby-eoe-indicator t
  129. (insert full-body) (comint-send-input nil t)))
  130. (results (cdr (member org-babel-ruby-eoe-indicator
  131. (reverse (mapcar #'org-babel-ruby-read-string
  132. (mapcar #'org-babel-trim raw)))))))
  133. (case result-type
  134. (output (mapconcat #'identity (reverse (cdr results)) "\n"))
  135. (value (org-babel-ruby-table-or-string (car results)))))))
  136. (defun org-babel-ruby-read-string (string)
  137. "Strip \\\"s from around ruby string"
  138. (if (string-match "\"\\([^\000]+\\)\"" string)
  139. (match-string 1 string)
  140. string))
  141. (provide 'org-babel-ruby)
  142. ;;; org-babel-ruby.el ends here