ndpian · 2022-08-11 00:01
Hi, I'm trying to use Org-mode HTML export with Liquid tags which has been set up by my team. The final exported html should be something like this:
{% extends 'base.html' %}
{% load static %}
{% block content %}
<div class="blah">
...blah ...
</div>
{% endblock %}
Now, I can easily use `#+begin_export html` in the beginning of my file to include the header tags. However, I'm unable to include the bottom liquid tag in the correct hierarchy, as Org export automatically adds a closing `</div>` *after* the included liquid tag. A minimal example:
#+title: My website
#+OPTIONS: toc:nil html-style:nil num:nil html-scripts:nil html-preamble:nil html-postamble:nil
#+begin_export html
{% extends 'base.html' %}
{% load static %}
{% block content %}
#+end_export
* Welcome to my website
Welcome to my small website.
* Contact us
Some info on how to contact us.
#+begin_export html
{% endblock %}
#+end_export
I know Org treats hierarchies strictly, so that is why perhaps the last html_export is added to the Contact Us section. Any ideas how to avoid that and keep this tag as the last output in the html? Thanks in advance!
**Edit** : Solved, check comments!
ndpian · 2022-08-11 00:03
This is how the output looks like with the above example:
{% extends 'base.html' %}
{% load static %}
{% block content %}
<div id="outline-container-orga5a2c25" class="outline-2">
<h2 id="orga5a2c25">Welcome to my website</h2>
<div class="outline-text-2" id="text-orga5a2c25">
<p>
Welcome to my small website.
</p>
</div>
</div>
<div id="outline-container-orgf204fa1" class="outline-2">
<h2 id="orgf204fa1">Contact us</h2>
<div class="outline-text-2" id="text-orgf204fa1">
<p>
Some info on how to contact us.
</p>
{% endblock %}
</div>
</div>
ndpian · 2022-08-11 14:52
I was able to solve the issue by using advice! It is indeed a very powerful mechanism in the Emacs toolset! I got inspiration from this SO answer: https://emacs.stackexchange.com/a/64719.
For posterity, this is how I solved it. I created a custom minor-mode for my website, and then added an advice which modifies the html *after it has been exported* :
(setq my-web-startblock "{% extends 'base.html' %}\n{% load static %}\n{% block content %}\n")
(setq my-web-endblock "\n{% endblock %}\n")
(define-minor-mode my-web-minor-mode
"Minor mode for modifying the output HTML for my website.")
(defun my-org-export-as-hack (orig-func &rest args)
(let* ((result (apply orig-func args))
(backend (nth 0 args)))
(when (and (eq backend 'html) (bound-and-true-p my-web-minor-mode))
(setq result (concat my-web-startblock result))
(setq result (concat result my-web-endblock)))
result))
(advice-add 'org-export-as :around #'my-org-export-as-hack)
Now, in my buffer all I need to do is enable `my-web-minor-mode`!