emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Peter Moresi <peter.moresi@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Issue with multiline string variable for JavaScript source code blocks
Date: Mon, 17 Nov 2014 12:44:59 -0800	[thread overview]
Message-ID: <CAK_Lq01_EM0+8rG6L9HjC1AxwDD69PqY499tO2F0EoQC4H676Q@mail.gmail.com> (raw)


[-- 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

             reply	other threads:[~2014-11-17 20:45 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-17 20:44 Peter Moresi [this message]
2014-11-18 21:30 ` Issue with multiline string variable for JavaScript source code blocks Nicolas Goaziou
2014-11-19  0:04   ` Peter Moresi
2014-11-21 23:07     ` Nicolas Goaziou
2014-11-22  0:12       ` Peter Moresi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAK_Lq01_EM0+8rG6L9HjC1AxwDD69PqY499tO2F0EoQC4H676Q@mail.gmail.com \
    --to=peter.moresi@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).