如何在 html 导出时在 orgmode 中标记代码块评估?

How to label code block evaluations in orgmode on html export?

如果我在 orgmode 中有一个代码块,我可以使用 :exports both 选项导出它的评估。

#+begin_src cpp -n :includes <iostream> :exports both
std::cout << "Hello there";
#+end_src

#+RESULTS:
: Hello there

当我导出到html(C-c C-e h o)时,结果块跟在代码块后面。但是,我想更明确地表明第二个块是第一个块的结果,比如一个简单的标签。

如果我修改上面的,像这样:

#+begin_src cpp -n :includes <iostream> :exports both
std::cout << "Hello there";
#+end_src

Output:

#+RESULTS:
: Hello there

然后标签 "Output:" 出现,但结果块出现两次——一次在标签之前,一次在标签之后。更糟糕的是,如果我 运行 orgmode (C-c C-c) 中的代码,那么第二个结果块将放在文本标签 "Output:" 之前。我怀疑这也是导出时发生的情况。

我还注意到,当导出到 html 时,结果块被放置在 class examplepre 标签中。我想我可以用类似的东西修改 css:

pre.example::before { content: "Output:"; }

但不幸的是,这会将文本置于 pre 块内,我无法添加任何换行符。

是否有任何简单的方法可以在 orgmode 本身或通过 css 中向结果块添加文本标签?如果可能的话,我想避免 javascript。

您可以像这样使用派生后端:

(defun my-results (fixed-width contents info)
  "Transform a results block to make it more visible."
  (let ((results (org-element-property :results fixed-width))
    (format (elt (plist-get info :back-end) 2))
    (value (org-element-property :value fixed-width)))
    (cond 
     ((eq 'html format)
      (format "<pre>Output:<br> %s</pre>" value)))))


(org-export-define-derived-backend 'my-html 'html
  :translate-alist '((fixed-width . my-results)))

(browse-url (org-export-to-file 'my-html (concat (file-name-base (buffer-file-name)) ".html")))

这应该适用于最近的组织:

    #+name: foo
    #+begin_src cpp -n :includes <iostream> :exports both
    std::cout << "Hello there";
    #+end_src

    Output:

    #+RESULTS: foo
    : Hello there