ob-js.el 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ;;; ob-js.el --- org-babel functions for Javascript
  2. ;; Copyright (C) 2010 Free Software Foundation
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, js
  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. ;; Now working with SBCL for both session and external evaluation.
  24. ;;
  25. ;; This certainly isn't optimally robust, but it seems to be working
  26. ;; for the basic use cases.
  27. ;;; Requirements:
  28. ;; node.js | http://nodejs.org/
  29. ;;; Code:
  30. (require 'ob)
  31. (require 'ob-eval)
  32. (defvar org-babel-default-header-args:js '()
  33. "Default header arguments for js code blocks.")
  34. (defcustom org-babel-js-cmd "node"
  35. "Name of command used to evaluate js blocks."
  36. :group 'org-babel
  37. :type 'string)
  38. (defvar org-babel-js-function-wrapper
  39. "require('sys').print(require('sys').inspect(function(){%s}()));"
  40. "Javascript code to print value of body.")
  41. (defun org-babel-expand-body:js (body params &optional processed-params)
  42. "Expand BODY according to PARAMS, return the expanded body."
  43. (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
  44. (concat
  45. (mapconcat ;; define any variables
  46. (lambda (pair) (format "var %s=%s;"
  47. (car pair) (org-babel-js-var-to-js (cdr pair))))
  48. vars "\n") "\n" body "\n")))
  49. (defun org-babel-execute:js (body params)
  50. "Execute a block of Javascript code with org-babel.
  51. This function is called by `org-babel-execute-src-block'"
  52. (let* ((processed-params (org-babel-process-params params))
  53. (session (not (string= (nth 0 processed-params) "none")))
  54. (result-type (nth 3 processed-params))
  55. (full-body (org-babel-expand-body:js body params processed-params)))
  56. (org-babel-js-read
  57. (if session
  58. (error "javascript sessions are not yet supported.")
  59. (let ((script-file (org-babel-temp-file "js-script-")))
  60. (with-temp-file script-file
  61. (insert
  62. ;; return the value or the output
  63. (if (string= result-type "value")
  64. (format org-babel-js-function-wrapper full-body)
  65. full-body)))
  66. (org-babel-eval (format "%s %s" org-babel-js-cmd script-file) ""))))))
  67. (defun org-babel-js-read (results)
  68. "Convert RESULTS into an appropriate elisp value.
  69. If RESULTS look like a table, then convert them into an
  70. Emacs-lisp table, otherwise return the results as a string."
  71. (org-babel-read
  72. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  73. (org-babel-read
  74. (concat "'"
  75. (replace-regexp-in-string
  76. "\\[" "(" (replace-regexp-in-string
  77. "\\]" ")" (replace-regexp-in-string
  78. ", " " " (replace-regexp-in-string
  79. "'" "\"" results))))))
  80. results)))
  81. (defun org-babel-js-var-to-js (var)
  82. "Convert VAR into a js variable.
  83. Convert an elisp value into a string of js source code
  84. specifying a variable of the same value."
  85. (if (listp var)
  86. (concat "[" (mapconcat #'org-babel-js-var-to-js var ", ") "]")
  87. (format "%S" var)))
  88. (defun org-babel-prep-session:js (session params)
  89. "Prepare SESSION according to the header arguments specified in PARAMS."
  90. (error "not yet implemented"))
  91. (defun org-babel-js-initiate-session (&optional session)
  92. "If there is not a current inferior-process-buffer in SESSION
  93. then create. Return the initialized session."
  94. (error "Javascript sessions are not yet supported."))
  95. (provide 'ob-js)
  96. ;; arch-tag: 84401fb3-b8d9-4bb6-9a90-cbe2d103d494
  97. ;;; ob-js.el ends here