7

I'm using org-mode to generate web pages of my notes. Can I embed code to generate additional HTML, etc? org-babel will generate results but it seems to show the results in addition to the code. I would like the code to create html that would replace the code itself.

  • Use :exports results; see Exporting code blocks. (Let me know if this works for you and if I should convert this to an answer.) – Constantine Dec 22 '14 at 23:58
  • Will this work with HTML? I think the results are placed inside a <pre> tag. – h4labs Dec 23 '14 at 3:07
6

As I mentioned in a comment, section Exporting code blocks describes permissible values of the :exports option: code (contents of the block), results (results of its evaluation), both (code and results) and none (nothing is exported).

By default Org tries to interpret results of evaluating a source code block (and turn it into a table, for example). If such an interpretation fails, evaluation results are formatted as monospace text and look as if wrapped in <pre> ... </pre> when exported to HTML.

Luckily we can tell Org to put evaluation results in a special kind of a block using :results (see Results of evaluation and :results). If you want to be able to use a source code block to generate a part of an HTML page, use :results value html or :results output html and Org will wrap results in a #+BEGIN_HTML ... #+END_HTML block.

Here's a minimal example (#+OPTIONS are irrelevant, I added them to make it easier to inspect exported HTML code using C-c C-e h H):

#+OPTIONS: html-postamble:nil
#+OPTIONS: html-preamble:nil html-scripts:nil html-style:nil
#+OPTIONS: html5-fancy:nil tex:t

Some text.

#+BEGIN_SRC python :exports results :results value html
  return "<hr> Generated HTML code. <hr>"
#+END_SRC

More text.
  • how to just replace the code block with the result without any wrap like #+begin #+end... I am trying to insert a list of figures using a python script. – godblessfq Sep 16 '16 at 9:23
6
#+begin_src emacs-lisp :exports none
  (defun org-babel-execute:html (body params) body)
#+end_src

#+begin_src html :exports results :results html
  <input type="button" name="clickme" value="Click Me!">
#+end_src

This will generate HTML page (upon export to HTML) with a button.

To prevent confusion: the first block will not be evaluated during export. You would need to evaluate it by moving the point to it and pressing C-c C-c.

More so, you would need to call

(org-babel-do-load-languages 'org-babel-load-languages '((html . t)))

Or similar before this code gets executed, so that Babel will recognize html block as executable.

But really, the example was mostly intended to show how you can proceed about adding your own "language" which targets a particular backend. You could have easily done it without adding any new languages, and, perhaps, with less complication by doing something like this:

#+begin_src emacs-lisp :exports results :results html
  "<input type=\"button\" name=\"clickme\" value=\"Click Me!\">"
#+end_src

The advantage of my first approach is that once editing with C-c ', you'd get the html-mode turned on.

  • It will also redefine org-babel-execute:html for the rest of that emacs session. – Malabarba Dec 23 '14 at 11:50
  • @Malabarba I didn't know it had one (did it?). – wvxvw Dec 23 '14 at 20:20
  • I don't know, I just assumed it did. What is the purpose of the first code block? Doesn't it define a function that changes how the second code block is processes? – Malabarba Dec 23 '14 at 21:38
  • @Malabarba Yup, but to the best of my knowledge there isn't an ob-html.el. So to export a block of HTML one would have to define such a function (I do this for other markups I use, in particular for YAML). This is different from #+begin_html ... #+end_html in that I can also use this as a primitive template engine via passing variables to the blocks (my actual org-babel-execute:html is more involved than that). – wvxvw Dec 23 '14 at 22:13
  • I see. It makes sense now. – Malabarba Dec 23 '14 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.