* 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
* Re: Issue with multiline string variable for JavaScript source code blocks
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
0 siblings, 1 reply; 5+ messages in thread
From: Nicolas Goaziou @ 2014-11-18 21:30 UTC (permalink / raw)
To: Peter Moresi; +Cc: emacs-orgmode
Hello,
Peter Moresi <peter.moresi@gmail.com> writes:
> I'm found a bug in ob-js.el when passing multi-line strings into a
> JavaScript source code blocks.
Thanks for your report.
> The attached org file describes the problem and fix that I'm using to work
> around the issue.
Would you mind providing a patch for that? See
http://orgmode.org/worg/org-contribute.html
if you're interested.
Regards,
--
Nicolas Goaziou
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Issue with multiline string variable for JavaScript source code blocks
2014-11-18 21:30 ` Nicolas Goaziou
@ 2014-11-19 0:04 ` Peter Moresi
2014-11-21 23:07 ` Nicolas Goaziou
0 siblings, 1 reply; 5+ messages in thread
From: Peter Moresi @ 2014-11-19 0:04 UTC (permalink / raw)
To: Peter Moresi, emacs-orgmode
[-- Attachment #1.1: Type: text/plain, Size: 33 bytes --]
Sure, the patch is attached.
[-- Attachment #1.2: Type: text/html, Size: 54 bytes --]
[-- Attachment #2: 0001-Fix-problem-passing-multiline-variables-into-JS-sour.patch --]
[-- Type: text/x-patch, Size: 872 bytes --]
From 2c8214a8e45a6368c709ae26d6d20c7458ebe0dd Mon Sep 17 00:00:00 2001
From: Peter Moresi <peter.moresi@pnmac.com>
Date: Tue, 18 Nov 2014 15:58:17 -0800
Subject: [PATCH] Fix problem passing multiline variables into JS source code
block
---
lisp/ob-js.el | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lisp/ob-js.el b/lisp/ob-js.el
index 47355b3..1a95216 100644
--- a/lisp/ob-js.el
+++ b/lisp/ob-js.el
@@ -114,7 +114,7 @@ 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)))
+ (replace-regexp-in-string "\n" "\\\\n" (format "%S" var))))
(defun org-babel-prep-session:js (session params)
"Prepare SESSION according to the header arguments specified in PARAMS."
--
1.7.10.4
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: Issue with multiline string variable for JavaScript source code blocks
2014-11-19 0:04 ` Peter Moresi
@ 2014-11-21 23:07 ` Nicolas Goaziou
2014-11-22 0:12 ` Peter Moresi
0 siblings, 1 reply; 5+ messages in thread
From: Nicolas Goaziou @ 2014-11-21 23:07 UTC (permalink / raw)
To: Peter Moresi; +Cc: emacs-orgmode
Hello,
Peter Moresi <peter.moresi@gmail.com> writes:
> Sure, the patch is attached.
Applied. Thank you.
However I had to fill your commit message, which was incomplete. For
reference, here is what I used, from your initial report:
--8<---------------cut here---------------start------------->8---
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.
#+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
TINYCHANGE
--8<---------------cut here---------------end--------------->8---
Regards,
--
Nicolas Goaziou
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Issue with multiline string variable for JavaScript source code blocks
2014-11-21 23:07 ` Nicolas Goaziou
@ 2014-11-22 0:12 ` Peter Moresi
0 siblings, 0 replies; 5+ messages in thread
From: Peter Moresi @ 2014-11-22 0:12 UTC (permalink / raw)
To: Peter Moresi, emacs-orgmode@gnu.org
[-- Attachment #1: Type: text/plain, Size: 1931 bytes --]
Excellent and thanks for the feedback. I'm glad I was able to give
something back to the community that has given me so much.
I've also had issues with JavaScript source code blocks truncating the
result when the value is a string with a comma. I'm still getting up to
speed with Emacs-lisp but when I figure out how to fix the issue I will
send the patch with proper comments.
Thanks,
Peter
On Friday, November 21, 2014, Nicolas Goaziou <mail@nicolasgoaziou.fr>
wrote:
> Hello,
>
> Peter Moresi <peter.moresi@gmail.com <javascript:;>> writes:
>
> > Sure, the patch is attached.
>
> Applied. Thank you.
>
> However I had to fill your commit message, which was incomplete. For
> reference, here is what I used, from your initial report:
>
> --8<---------------cut here---------------start------------->8---
> 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.
>
> #+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
>
> TINYCHANGE
> --8<---------------cut here---------------end--------------->8---
>
>
> Regards,
>
> --
> Nicolas Goaziou
>
[-- Attachment #2: Type: text/html, Size: 2528 bytes --]
^ 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).