2015-10-26 00:56:00 +00:00
|
|
|
;;; ob-js.el --- Babel Functions for Javascript -*- lexical-binding: t; -*-
|
2010-08-27 22:59:08 +00:00
|
|
|
|
2021-01-01 19:55:31 +00:00
|
|
|
;; Copyright (C) 2010-2021 Free Software Foundation, Inc.
|
2010-08-27 22:59:08 +00:00
|
|
|
|
|
|
|
;; Author: Eric Schulte
|
|
|
|
;; Keywords: literate programming, reproducible research, js
|
2018-01-16 16:22:00 +00:00
|
|
|
;; Homepage: https://orgmode.org
|
2010-08-27 22:59:08 +00:00
|
|
|
|
2011-08-24 18:55:11 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
2010-08-27 22:59:08 +00:00
|
|
|
|
2011-08-24 18:55:11 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2010-08-27 22:59:08 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2011-08-24 18:55:11 +00: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 22:59:08 +00: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 18:55:11 +00:00
|
|
|
|
2010-08-27 22:59:08 +00:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 22:52:52 +00:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2010-08-27 22:59:08 +00: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:
|
|
|
|
|
2020-10-24 18:22:33 +00:00
|
|
|
;; - a non-browser javascript engine such as node.js https://nodejs.org/
|
|
|
|
;; or mozrepl https://wiki.github.com/bard/mozrepl/
|
2011-08-24 18:55:11 +00:00
|
|
|
;;
|
2010-09-01 14:48:21 +00:00
|
|
|
;; - for session based evaluation mozrepl and moz.el are required see
|
2020-10-24 18:22:33 +00:00
|
|
|
;; https://wiki.github.com/bard/mozrepl/emacs-integration for
|
2010-09-01 14:48:21 +00:00
|
|
|
;; configuration instructions
|
2010-08-27 22:59:08 +00:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
(require 'ob)
|
2010-09-01 14:48:21 +00:00
|
|
|
|
|
|
|
(declare-function run-mozilla "ext:moz" (arg))
|
2018-05-10 00:10:52 +00:00
|
|
|
(declare-function httpd-start "ext:simple-httpd" ())
|
|
|
|
(declare-function run-skewer "ext:skewer-mode" ())
|
|
|
|
(declare-function skewer-repl "ext:skewer-repl" ())
|
|
|
|
(declare-function indium-run-node "ext:indium-nodejs" (command))
|
|
|
|
(declare-function indium-eval "ext:indium-interaction" (string &optional callback))
|
2010-08-27 22:59:08 +00:00
|
|
|
|
|
|
|
(defvar org-babel-default-header-args:js '()
|
|
|
|
"Default header arguments for js code blocks.")
|
|
|
|
|
2010-09-01 14:48:21 +00:00
|
|
|
(defvar org-babel-js-eoe "org-babel-js-eoe"
|
|
|
|
"String to indicate that evaluation has completed.")
|
|
|
|
|
2010-08-27 22:59:08 +00:00
|
|
|
(defcustom org-babel-js-cmd "node"
|
|
|
|
"Name of command used to evaluate js blocks."
|
|
|
|
:group 'org-babel
|
2012-03-19 20:38:12 +00:00
|
|
|
:version "24.1"
|
2018-03-08 09:15:58 +00:00
|
|
|
:type '(choice (const "node")
|
|
|
|
(const "mozrepl")
|
2018-03-17 17:19:29 +00:00
|
|
|
(const "skewer-mode")
|
2018-03-17 17:33:12 +00:00
|
|
|
(const "indium")
|
|
|
|
(const "js-comint"))
|
2018-03-08 09:15:58 +00:00
|
|
|
:safe #'stringp)
|
2010-08-27 22:59:08 +00:00
|
|
|
|
|
|
|
(defvar org-babel-js-function-wrapper
|
2020-05-22 14:07:55 +00:00
|
|
|
"require('process').stdout.write(require('util').inspect(function(){%s}()));"
|
2010-08-27 22:59:08 +00:00
|
|
|
"Javascript code to print value of body.")
|
|
|
|
|
|
|
|
(defun org-babel-execute:js (body params)
|
|
|
|
"Execute a block of Javascript code with org-babel.
|
2019-09-20 22:27:53 +00:00
|
|
|
This function is called by `org-babel-execute-src-block'."
|
2016-09-22 17:45:15 +00:00
|
|
|
(let* ((org-babel-js-cmd (or (cdr (assq :cmd params)) org-babel-js-cmd))
|
2018-03-08 09:15:58 +00:00
|
|
|
(session (cdr (assq :session params)))
|
2016-09-22 17:45:15 +00:00
|
|
|
(result-type (cdr (assq :result-type params)))
|
2010-10-21 11:05:59 +00:00
|
|
|
(full-body (org-babel-expand-body:generic
|
2013-04-06 16:58:10 +00:00
|
|
|
body params (org-babel-variable-assignments:js params)))
|
2018-03-08 09:15:58 +00:00
|
|
|
(result (cond
|
|
|
|
;; no session specified, external evaluation
|
|
|
|
((string= session "none")
|
2013-04-06 16:58:10 +00:00
|
|
|
(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
|
2018-03-08 09:15:58 +00:00
|
|
|
(org-babel-process-file-name script-file)) "")))
|
2018-04-02 21:45:29 +00:00
|
|
|
;; Indium Node REPL. Separate case because Indium
|
|
|
|
;; REPL is not inherited from Comint mode.
|
2018-03-17 17:19:29 +00:00
|
|
|
((string= session "*JS REPL*")
|
|
|
|
(require 'indium-repl)
|
|
|
|
(unless (get-buffer session)
|
2018-04-02 21:45:29 +00:00
|
|
|
(indium-run-node org-babel-js-cmd))
|
2018-03-17 17:19:29 +00:00
|
|
|
(indium-eval full-body))
|
2018-03-08 09:15:58 +00:00
|
|
|
;; session evaluation
|
|
|
|
(t
|
|
|
|
(let ((session (org-babel-prep-session:js
|
|
|
|
(cdr (assq :session params)) params)))
|
|
|
|
(nth 1
|
|
|
|
(org-babel-comint-with-output
|
|
|
|
(session (format "%S" org-babel-js-eoe) t body)
|
|
|
|
(dolist (code (list body (format "%S" org-babel-js-eoe)))
|
|
|
|
(insert (org-babel-chomp code))
|
|
|
|
(comint-send-input nil t)))))))))
|
2016-09-22 17:45:15 +00:00
|
|
|
(org-babel-result-cond (cdr (assq :result-params params))
|
2013-04-06 16:58:10 +00:00
|
|
|
result (org-babel-js-read result))))
|
2010-08-27 22:59:08 +00: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
|
2016-08-24 15:05:54 +00:00
|
|
|
(if (and (stringp results)
|
|
|
|
(string-prefix-p "[" results)
|
|
|
|
(string-suffix-p "]" results))
|
2010-08-27 22:59:08 +00:00
|
|
|
(org-babel-read
|
|
|
|
(concat "'"
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"\\[" "(" (replace-regexp-in-string
|
|
|
|
"\\]" ")" (replace-regexp-in-string
|
2014-08-24 03:48:48 +00:00
|
|
|
",[[:space:]]" " "
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"'" "\"" results))))))
|
2010-08-27 22:59:08 +00:00
|
|
|
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 23:58:17 +00:00
|
|
|
(replace-regexp-in-string "\n" "\\\\n" (format "%S" var))))
|
2010-08-27 22:59:08 +00:00
|
|
|
|
|
|
|
(defun org-babel-prep-session:js (session params)
|
|
|
|
"Prepare SESSION according to the header arguments specified in PARAMS."
|
2010-09-01 14:48:21 +00:00
|
|
|
(let* ((session (org-babel-js-initiate-session session))
|
2010-10-21 11:05:59 +00:00
|
|
|
(var-lines (org-babel-variable-assignments:js params)))
|
2010-09-01 14:48:21 +00:00
|
|
|
(when session
|
|
|
|
(org-babel-comint-in-buffer session
|
2018-03-25 03:26:37 +00:00
|
|
|
(goto-char (point-max))
|
|
|
|
(dolist (var var-lines)
|
|
|
|
(insert var)
|
|
|
|
(comint-send-input nil t)
|
|
|
|
(org-babel-comint-wait-for-output session)
|
|
|
|
(sit-for .1)
|
|
|
|
(goto-char (point-max)))))
|
2010-09-01 14:48:21 +00:00
|
|
|
session))
|
2010-08-27 22:59:08 +00:00
|
|
|
|
2010-10-21 11:05:59 +00:00
|
|
|
(defun org-babel-variable-assignments:js (params)
|
2012-07-30 08:08:15 +00:00
|
|
|
"Return list of Javascript statements assigning the block's variables."
|
2010-10-21 11:05:59 +00:00
|
|
|
(mapcar
|
|
|
|
(lambda (pair) (format "var %s=%s;"
|
|
|
|
(car pair) (org-babel-js-var-to-js (cdr pair))))
|
2015-10-29 19:26:11 +00:00
|
|
|
(org-babel--get-vars params)))
|
2010-10-21 11:05:59 +00:00
|
|
|
|
2018-04-01 09:42:07 +00:00
|
|
|
(defun org-babel-js-initiate-session (&optional session _params)
|
|
|
|
"If there is not a current inferior-process-buffer in `SESSION'
|
2010-08-27 22:59:08 +00:00
|
|
|
then create. Return the initialized session."
|
2018-03-08 09:15:58 +00:00
|
|
|
(cond
|
|
|
|
((string= session "none")
|
|
|
|
(warn "Session evaluation of ob-js is not supported"))
|
|
|
|
((string= "*skewer-repl*" session)
|
|
|
|
(require 'skewer-repl)
|
|
|
|
(let ((session-buffer (get-buffer "*skewer-repl*")))
|
2018-04-01 09:42:07 +00:00
|
|
|
(if (and session-buffer
|
|
|
|
(org-babel-comint-buffer-livep (get-buffer session-buffer))
|
2018-03-08 09:15:58 +00:00
|
|
|
(comint-check-proc session-buffer))
|
|
|
|
session-buffer
|
|
|
|
;; start skewer REPL.
|
|
|
|
(httpd-start)
|
|
|
|
(run-skewer)
|
2018-04-01 09:42:07 +00:00
|
|
|
(skewer-repl)
|
2018-03-08 09:15:58 +00:00
|
|
|
session-buffer)))
|
2018-03-17 17:33:12 +00:00
|
|
|
((string= "*Javascript REPL*" session)
|
|
|
|
(require 'js-comint)
|
|
|
|
(let ((session-buffer "*Javascript REPL*"))
|
|
|
|
(if (and (org-babel-comint-buffer-livep (get-buffer session-buffer))
|
|
|
|
(comint-check-proc session-buffer))
|
|
|
|
session-buffer
|
|
|
|
(call-interactively 'run-js)
|
|
|
|
(sit-for .5)
|
|
|
|
session-buffer)))
|
2018-03-08 09:15:58 +00:00
|
|
|
((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 )
|
|
|
|
(error "Session evaluation with node.js is not supported"))
|
|
|
|
(t
|
|
|
|
(error "Sessions are only supported with mozrepl add \":cmd mozrepl\""))))
|
2010-08-27 22:59:08 +00:00
|
|
|
|
|
|
|
(provide 'ob-js)
|
|
|
|
|
|
|
|
;;; ob-js.el ends here
|