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.
As I mentioned in a comment, section Exporting code blocks describes permissible values of the
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.
#+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.