【发布时间】:2013-06-18 21:40:25
【问题描述】:
这是我想要做的:
我有一个 JavaScript 代码的 sn-p,我想在从 org 文档生成的 HTML 中显示它,并且我希望在 HTML 页面中执行该代码。举个简单的例子,让它像
#+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()
但这抱怨未定义符号block-name。
我发现这个问题非常相似,但那里给出的答案对我不起作用。 Make the source code from one code block the input to another code block in Emacs org-mode
引用块的内容/名称的语法是什么?
编辑:
我越来越近了,但还没有:
#+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:
这有两个问题。 script 变量的值是 nil 并且脚本标签被转义(尖括号被 &lt; 和 &gt; 替换。我可以通过将脚本标签放在评估之外找到这种特殊的替换案例,但是作为一般规则,我无法阻止这种情况(如果脚本中有小于或大于的符号,它们将被替换)。
EDIT1:
快到了!
建议org-babel-get-src-block-info让它在本地存储代码块的内容,如果它被命名为变量<name>-text,那么我以后可以得到它。
(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)
示例用法:
#+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:
使用:results html 选项处理转义 - 这会导致 Org 按字面意思插入 HTML。
#+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:
这是失败的最小示例。
但这会起作用并产生“预期”的结果:
#+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
<script>your script could be here</script> 的输出和 eLisp 代码块的 cmets 中的 JavaScript 代码。
【问题讨论】:
标签: emacs org-mode org-babel advising-functions defadvice