# Expanding some elisp into HTML inside Org

I am creating an HTML file containing some notes exported from org mode. I would like to add all images from a folder into the HTML file generated i.e the images should display in the webpage when opened.

Is there a way to insert a bit of local Emacs-Lisp code in the org mode file such that on exporting to HTML, the code block expands to something a sequence of <img> tags containing file names from the folder images. (I guess I am looking for something like a kind of local macro expansion?)

To be precise I want something like

blah blah
blah blah

<<< Elisp code that expands to HTML inside Org file>>>

blah blah blah
blah blah


to be converted into

blah blah
blah blah

<figure>
<img src="./images/image1.png" width="300px" />
</figure>

<figure>
<img src="./images/image2.png" width="300px" />
</figure>
.
.
.

<figure>
<img src="./images/image700.png" width="300px" />
</figure>

blah blah blah
blah blah


Note that I don't want this expanded HTML gunk inside the org mode file itself, but inside the exported HTML file.

Something like this perhaps - I've written the code block in python but you can do it in any language you want:

* foo

blah blah blah

#+begin_src python :results output :exports results :var n=700 :wrap export html
for i in range(1,n+1):
print('<figure>\n    <img src="./images/image{}.png" width="300px" />\n</figure>\n'.format(i))

#+end_src

blah blah blah
blah blah bla


The :wrap export html is the important thing: it will wrap the results in

#+begin_export html

#+end_export


allowing the exporter to just copy them verbatim to the HTML output file.

The :exports results header makes sure that the block will be evaluated when you export and will produce the boilerplate, but as long as you are careful NOT to evaluate it in the buffer with C-c C-c you won't see the results there. If you do evaluate it by mistake, you can get rid of the results with M-x org-babel-remove-result RET.

• Beautiful! Thanks! It worked. It's great I can use Python for this. Not a big Lisper myself. – smilingbuddha Sep 24 '20 at 4:02