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.
-
1Use `:exports results`; see [Exporting code blocks](http://orgmode.org/manual/Exporting-code-blocks.html). (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 – h4labs Dec 23 '14 at 03:07
2 Answers
#+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.

- 11,222
- 2
- 30
- 55
-
It will also redefine org-babel-execute:html for the rest of that emacs session. – Malabarba Dec 23 '14 at 11:50
-
-
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
-
-
This didn't actually work for me. I didn't get any html output. I even try something simpler like
– h4labs Dec 29 '14 at 04:55 -
@h4labs OK, probably some explanation is due here. If a code block states `:exports none`, this means it will not be exported, i.e. you would need to evaluate it manually (although, only once per Emacs session in this case). Second assumption I've made here is that you would call `(org-babel-do-load-languages 'org-babel-load-languages '((html . t)))` (but you would certainly want it to have more languages listed). I'll add this to the answer as well. – wvxvw Dec 29 '14 at 06:19
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.

- 123
- 1
- 1
- 9

- 9,072
- 1
- 34
- 49
-
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 09:23