2010-08-27 18:59:08 -04:00
|
|
|
;;; ob-js.el --- org-babel functions for Javascript
|
|
|
|
|
2014-01-07 08:18:17 -05:00
|
|
|
;; Copyright (C) 2010-2014 Free Software Foundation, Inc.
|
2010-08-27 18:59:08 -04:00
|
|
|
|
|
|
|
;; Author: Eric Schulte
|
|
|
|
;; Keywords: literate programming, reproducible research, js
|
|
|
|
;; Homepage: http://orgmode.org
|
|
|
|
|
2011-08-24 14:55:11 -04:00
|
|
|
;; This file is part of GNU Emacs.
|
2010-08-27 18:59:08 -04:00
|
|
|
|
2011-08-24 14:55:11 -04:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2010-08-27 18:59:08 -04:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2011-08-24 14:55:11 -04:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2010-08-27 18:59:08 -04:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
2011-08-24 14:55:11 -04:00
|
|
|
|
2010-08-27 18:59:08 -04:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2011-08-24 14:55:11 -04:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
2010-08-27 18:59:08 -04:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Now working with SBCL for both session and external evaluation.
|
|
|
|
;;
|
|
|
|
;; This certainly isn't optimally robust, but it seems to be working
|
|
|
|
;; for the basic use cases.
|
|
|
|
|
|
|
|
;;; Requirements:
|
|
|
|
|
2010-09-01 10:48:21 -04:00
|
|
|
;; - a non-browser javascript engine such as node.js http://nodejs.org/
|
|
|
|
;; or mozrepl http://wiki.github.com/bard/mozrepl/
|
2011-08-24 14:55:11 -04:00
|
|
|
;;
|
2010-09-01 10:48:21 -04:00
|
|
|
;; - for session based evaluation mozrepl and moz.el are required see
|
|
|
|
;; http://wiki.github.com/bard/mozrepl/emacs-integration for
|
|
|
|
;; configuration instructions
|
2010-08-27 18:59:08 -04:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
(require 'ob)
|
2010-09-01 10:48:21 -04:00
|
|
|
(eval-when-compile (require 'cl))
|
|
|
|
|
|
|
|
(declare-function run-mozilla "ext:moz" (arg))
|
2010-08-27 18:59:08 -04:00
|
|
|
|
|
|
|
(defvar org-babel-default-header-args:js '()
|
|
|
|
"Default header arguments for js code blocks.")
|
|
|
|
|
2010-09-01 10:48:21 -04:00
|
|
|
(defvar org-babel-js-eoe "org-babel-js-eoe"
|
|
|
|
"String to indicate that evaluation has completed.")
|
|
|
|
|
2010-08-27 18:59:08 -04:00
|
|
|
(defcustom org-babel-js-cmd "node"
|
|
|
|
"Name of command used to evaluate js blocks."
|
|
|
|
:group 'org-babel
|
2012-03-19 16:38:12 -04:00
|
|
|
:version "24.1"
|
2010-08-27 18:59:08 -04:00
|
|
|
:type 'string)
|
|
|
|
|
|
|
|
(defvar org-babel-js-function-wrapper
|
|
|
|
"require('sys').print(require('sys').inspect(function(){%s}()));"
|
|
|
|
"Javascript code to print value of body.")
|
|
|
|
|
|
|
|
(defun org-babel-execute:js (body params)
|
|
|
|
"Execute a block of Javascript code with org-babel.
|
|
|
|
This function is called by `org-babel-execute-src-block'"
|
2010-10-21 07:52:24 -04:00
|
|
|
(let* ((org-babel-js-cmd (or (cdr (assoc :cmd params)) org-babel-js-cmd))
|
|
|
|
(result-type (cdr (assoc :result-type params)))
|
2010-10-21 07:05:59 -04:00
|
|
|
(full-body (org-babel-expand-body:generic
|
2013-04-06 12:58:10 -04:00
|
|
|
body params (org-babel-variable-assignments:js params)))
|
|
|
|
(result (if (not (string= (cdr (assoc :session params)) "none"))
|
|
|
|
;; session evaluation
|
|
|
|
(let ((session (org-babel-prep-session:js
|
|
|
|
(cdr (assoc :session params)) params)))
|
|
|
|
(nth 1
|
|
|
|
(org-babel-comint-with-output
|
|
|
|
(session (format "%S" org-babel-js-eoe) t body)
|
|
|
|
(mapc
|
|
|
|
(lambda (line)
|
|
|
|
(insert (org-babel-chomp line))
|
|
|
|
(comint-send-input nil t))
|
|
|
|
(list body (format "%S" org-babel-js-eoe))))))
|
|
|
|
;; external evaluation
|
|
|
|
(let ((script-file (org-babel-temp-file "js-script-")))
|
|
|
|
(with-temp-file script-file
|
|
|
|
(insert
|
|
|
|
;; return the value or the output
|
|
|
|
(if (string= result-type "value")
|
|
|
|
(format org-babel-js-function-wrapper full-body)
|
|
|
|
full-body)))
|
|
|
|
(org-babel-eval
|
|
|
|
(format "%s %s" org-babel-js-cmd
|
|
|
|
(org-babel-process-file-name script-file)) "")))))
|
|
|
|
(org-babel-result-cond (cdr (assoc :result-params params))
|
|
|
|
result (org-babel-js-read result))))
|
2010-08-27 18:59:08 -04:00
|
|
|
|
|
|
|
(defun org-babel-js-read (results)
|
|
|
|
"Convert RESULTS into an appropriate elisp value.
|
|
|
|
If RESULTS look like a table, then convert them into an
|
|
|
|
Emacs-lisp table, otherwise return the results as a string."
|
|
|
|
(org-babel-read
|
|
|
|
(if (and (stringp results) (string-match "^\\[.+\\]$" results))
|
|
|
|
(org-babel-read
|
|
|
|
(concat "'"
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"\\[" "(" (replace-regexp-in-string
|
|
|
|
"\\]" ")" (replace-regexp-in-string
|
|
|
|
", " " " (replace-regexp-in-string
|
|
|
|
"'" "\"" results))))))
|
|
|
|
results)))
|
|
|
|
|
|
|
|
(defun org-babel-js-var-to-js (var)
|
|
|
|
"Convert VAR into a js variable.
|
|
|
|
Convert an elisp value into a string of js source code
|
|
|
|
specifying a variable of the same value."
|
|
|
|
(if (listp var)
|
|
|
|
(concat "[" (mapconcat #'org-babel-js-var-to-js var ", ") "]")
|
ob-js: Fix passing multiline variables
* lisp/ob-js.el (org-babel-js-var-to-js): Replace newline characters
with "\n" in strings.
Let's say I have a multi-line string stored in an example block.
I want to store my CSV in an example block.
ColA,ColB,ColC
1,2,3
4,5,6
I have a JavaScript function that accepts a string named 'csv' and passing in 'my-csv-data'.
console.log(csv);
When I expand the source block I end up with:
var csv="ColA,ColB,ColC
1,2,3
4,5,6";
console.log(csv);
This will not execute correctly because JavaScript does not support newlines in strings.
What I want instead is:
var csv="ColA,ColB,ColC\n 1,2,3\n 4,5,6";
console.log(csv);
TINYCHANGE
2014-11-18 18:58:17 -05:00
|
|
|
(replace-regexp-in-string "\n" "\\\\n" (format "%S" var))))
|
2010-08-27 18:59:08 -04:00
|
|
|
|
|
|
|
(defun org-babel-prep-session:js (session params)
|
|
|
|
"Prepare SESSION according to the header arguments specified in PARAMS."
|
2010-09-01 10:48:21 -04:00
|
|
|
(let* ((session (org-babel-js-initiate-session session))
|
2010-10-21 07:05:59 -04:00
|
|
|
(var-lines (org-babel-variable-assignments:js params)))
|
2010-09-01 10:48:21 -04:00
|
|
|
(when session
|
|
|
|
(org-babel-comint-in-buffer session
|
|
|
|
(sit-for .5) (goto-char (point-max))
|
|
|
|
(mapc (lambda (var)
|
|
|
|
(insert var) (comint-send-input nil t)
|
|
|
|
(org-babel-comint-wait-for-output session)
|
|
|
|
(sit-for .1) (goto-char (point-max))) var-lines)))
|
|
|
|
session))
|
2010-08-27 18:59:08 -04:00
|
|
|
|
2010-10-21 07:05:59 -04:00
|
|
|
(defun org-babel-variable-assignments:js (params)
|
2012-07-30 04:08:15 -04:00
|
|
|
"Return list of Javascript statements assigning the block's variables."
|
2010-10-21 07:05:59 -04:00
|
|
|
(mapcar
|
|
|
|
(lambda (pair) (format "var %s=%s;"
|
|
|
|
(car pair) (org-babel-js-var-to-js (cdr pair))))
|
|
|
|
(mapcar #'cdr (org-babel-get-header params :var))))
|
|
|
|
|
2010-08-27 18:59:08 -04:00
|
|
|
(defun org-babel-js-initiate-session (&optional session)
|
|
|
|
"If there is not a current inferior-process-buffer in SESSION
|
|
|
|
then create. Return the initialized session."
|
2010-09-01 10:48:21 -04:00
|
|
|
(unless (string= session "none")
|
|
|
|
(cond
|
|
|
|
((string= "mozrepl" org-babel-js-cmd)
|
|
|
|
(require 'moz)
|
|
|
|
(let ((session-buffer (save-window-excursion
|
|
|
|
(run-mozilla nil)
|
|
|
|
(rename-buffer session)
|
|
|
|
(current-buffer))))
|
|
|
|
(if (org-babel-comint-buffer-livep session-buffer)
|
|
|
|
(progn (sit-for .25) session-buffer)
|
|
|
|
(sit-for .5)
|
|
|
|
(org-babel-js-initiate-session session))))
|
|
|
|
((string= "node" org-babel-js-cmd )
|
2012-08-12 23:59:44 -04:00
|
|
|
(error "Session evaluation with node.js is not supported"))
|
2010-09-01 10:48:21 -04:00
|
|
|
(t
|
2012-08-12 23:59:44 -04:00
|
|
|
(error "Sessions are only supported with mozrepl add \":cmd mozrepl\"")))))
|
2010-08-27 18:59:08 -04:00
|
|
|
|
|
|
|
(provide 'ob-js)
|
|
|
|
|
2011-08-15 14:04:38 -04:00
|
|
|
|
2010-08-27 18:59:08 -04:00
|
|
|
|
|
|
|
;;; ob-js.el ends here
|