emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Issue with multiline string variable for JavaScript source code blocks
@ 2014-11-17 20:44 Peter Moresi
  2014-11-18 21:30 ` Nicolas Goaziou
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Moresi @ 2014-11-17 20:44 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 212 bytes --]

Hi,

I'm found a bug in ob-js.el when passing multi-line strings into a
JavaScript source code blocks.

The attached org file describes the problem and fix that I'm using to work
around the issue.

Thanks,
Peter

[-- Attachment #1.2: Type: text/html, Size: 324 bytes --]

[-- Attachment #2: example-multiline-js-input.org --]
[-- Type: application/octet-stream, Size: 1689 bytes --]

#+TITLE: Bug in multiline strings in ob-js.el

This org-mode file describes a bug in ob-js.el and proposes a solution.

* Problem Description
Let's say I have a multi-line string stored in an example block.

I want to store my CSV in an example block.

#+NAME: my-csv-data
#+BEGIN_EXAMPLE
  ColA,ColB,ColC
  1,2,3
  4,5,6
#+END_EXAMPLE

I have a JavaScript function that accepts a string named 'csv' and passing in 'my-csv-data'.

#+BEGIN_SRC js :var csv=my-csv-data :results output
  console.log(csv);
#+END_SRC

When I expand the source block I end up with:

#+BEGIN_SRC js
var csv="ColA,ColB,ColC
  1,2,3
  4,5,6";
console.log(csv);
#+END_SRC

This will not execute correctly because JavaScript does not support newlines in strings.

What I want instead is:

#+BEGIN_SRC js
  var csv="ColA,ColB,ColC\n  1,2,3\n  4,5,6";
  console.log(csv);
#+END_SRC

* Proposed Fix

  To fix this problem I changed function "org-babel-js-var-to-js" in ob-js.el:116.

  #+BEGIN_SRC emacs-lisp
    (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 ", ") "]")
        (format "%S" var)))
  #+END_SRC

  to

  #+BEGIN_SRC emacs-lisp
    (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 ", ") "]")
        (replace-regexp-in-string "\n" "\\\\n" (format "%S" var))))
  #+END_SRC

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-11-22  0:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-17 20:44 Issue with multiline string variable for JavaScript source code blocks Peter Moresi
2014-11-18 21:30 ` Nicolas Goaziou
2014-11-19  0:04   ` Peter Moresi
2014-11-21 23:07     ` Nicolas Goaziou
2014-11-22  0:12       ` Peter Moresi

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).