这是我正在尝试做的事情:
我有一段 JavaScript 代码,我想将其显示在从组织文档生成的 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()
但这会提示 undefined symbol block-name
.
我发现了这个问题,它非常相似,但那里给出的答案对我不起作用。 Make the source code from one code block the input to another code block in Emacs org-mode
引用 block 的内容/名称的语法是什么?
编辑:
我越来越近了,但还没有到:
#+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
并且脚本标签正在被转义(尖括号被 <
和 >
取代。我可以通过将脚本标签放在评估之外来找到这种特殊的替换案例,但我无法将其作为一般规则(如果脚本中有 less-then 或 greater-then 符号,它们将被替换)。
编辑 1:
快到了!
建议 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代码块注释中的JavaScript代码。
最佳答案
例如,在您的“插入脚本”中使用一个变量
:var data=block-name
如果您想要 block 名称的结果 在您的“插入脚本”中。
如果您想要内容,请使用 NoWeb 表示法,加上“:noweb yes”,即使用:
<<block-name>>
在你的“插入脚本”中。
关于emacs - Org-mode 在导出期间获取 src block 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17179479/