Reputation:
Here's what I'm trying to do:
I have a snippet of JavaScript code that I want to both display in the HTML generated from an org document and I want that code to be executed in the HTML page. For a simplistic example, let it be something like
#+NAME: block-name
#+BEGIN_SRC javascript
alert("Ding!");
#+END_SRC
#+NAME: insert-script
#+BEGIN_SRC emacs-lisp :export results
(format "<script type=\"text/javascript\">%s</script>" block-name)
#+END_SRC
#+CALL: insert-script()
But this complains about undefined symbol block-name
.
I've found this question, which is very similar, but the answer given there doesn't work for me. Make the source code from one code block the input to another code block in Emacs org-mode
What is the syntax for referring to the content / name of the block?
EDIT:
I'm getting closer, but still not there:
#+NAME: block-name
#+BEGIN_SRC javascript :exports code
alert("Ding!");
#+END_SRC
#+BEGIN_SRC emacs-lisp :exports results :var script=block-name
(print (format "<script type=\"text/javascript\">%s</script>" script))
#+END_SRC
#+RESULTS:
This has two problems. The value of script
variable is nil
and the script tags are being escaped (angle brackets being replaced by <
and >
. I could find this particular case of replacement by putting the script tags outside the evaluation, but I won't be able to prevent this as a general rule (if there is a less-then or greater-then signs in the script, they will get replaced).
EDIT1:
Almost there!
Advised the org-babel-get-src-block-info
to have it store locally the content of the block of code, if it is named into a variable <name>-text
, so I can get it later.
(defadvice org-babel-get-src-block-info (after org-babel-store-info)
(let* ((info-copy ad-return-value)
(block-name (nth 4 info-copy))
(block-text (nth 1 info-copy)))
(when block-name
(set (make-local-variable
(intern (format "%s-text" block-name))) block-text))
info-copy))
(ad-activate 'org-babel-get-src-block-info)
Example usage:
#+NAME: block-name
#+BEGIN_SRC javascript :exports code
alert("Ding!");
#+END_SRC
#+NAME: insert-script
#+BEGIN_SRC emacs-lisp :exports results :results html
(print (format "<script type=\"text/javascript\">%s</script>"
block-name-text))
#+END_SRC
#+RESULTS:
The escaping is handled with :results html
option - this causes Org to insert the HTML literally.
#+NAME: math
#+BEGIN_SRC js :exports none :noweb yes
// Logarithm of base two:
var y = Math.log(x) / Math.log(2);
#+END_SRC
#+BEGIN_SRC emacs-lisp :tagnle example :exports results :noweb yes :results html
(print (format "<script>%s</script>" "<<math>>"))
#+END_SRC
#+RESULTS:
Here's the minimal example that fails.
but this will work and produce the "expected" result:
#+NAME: math
#+BEGIN_SRC js :exports none :noweb yes
// Logarithm of base two:
var y = Math.log(x) / Math.log(2);
#+END_SRC
#+BEGIN_SRC emacs-lisp :tagnle example :exports both :noweb yes :results html
; <<math>>
(print (format "<script>%s</script>" "your script could be here"))
#+END_SRC
with the output of <script>your script could be here</script>
and the JavaScript code in the comments of eLisp code block.
Upvotes: 3
Views: 2951
Reputation: 1
#+BEGIN_HTML
<script>
(function(){alert("ding !");})();
</script>
#+END_HTML
Upvotes: -3
Reputation: 4506
Use a variable in your "insert-script", for example
:var data=block-name
if you want the results of block-name Inside your "insert-script".
If you want the contents, use the NoWeb notation, with ":noweb yes", that is use:
<<block-name>>
in your "insert-script".
Upvotes: 3