;;; ox-template.el --- A HTML exporter via templates for org-mode ;; Copyright (C) 2021 Sameer Rahmani ;; Author: Sameer Rahmani ;; URL: https://devheroes.codes/lxsameer/lxhome ;; ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;;; Commentary: ;; Inspired by Juan Jose Garcia Ripoll work. ;;; Code: ;; We ensure the org infrastructure (require 'org) (require 'ox-publish) (require 'mustache) (require 'htmlize) (require 'ht) (advice-add 'org-html--tags :override 'org-html-tag-template) (defun org-html-tag-template (tags info) (message "<<<<<<<<<<<< %s" tags) (let ((template (plist-get info :html-headline-template)) (ctx (ht ("tags" (format "%s" tags)) ("base-url" (plist-get info :base-url))))) (mustache-render (use-html template) ctx))) (defun use-html (path-to-template) "Use the given template at PATH-TO-TEMPLATE as a template." (with-temp-buffer (insert-file-contents path-to-template) (buffer-string))) (org-export-define-derived-backend 'templated-html 'html :translate-alist '(;;(headline . templated-html-headline-fn) (template . templated-html-template-fn))) (defun render-template (template-name contents info) "Render the given template TEMPLATE-NAME using CONTENTS and INFO." (let ((ctx (ht ("content" contents) ("head" (plist-get info :html-head-extra)) ("base-url" (plist-get info :base-url))))) (mustache-render (use-html template-name) ctx))) (defun templated-html-template-fn (contents info) "Return the finalized html CONTENTS using the INFO and templates." (let ((template (plist-get info :html-template))) (if template (render-template template contents info) (org-html-template contents info)))) (defun org-html-publish-to-templated-html (plist filename pub-dir) "Publish an org file to HTML. FILENAME is the filename of the Org file to be published. PLIST is the property list for the given project. PUB-DIR is the publishing directory. Return output file name." (org-publish-org-to 'templated-html filename (concat "." (or (plist-get plist :html-extension) org-html-extension "html")) plist pub-dir)) (provide 'lisp/ox-template) ;;; ox-template.el ends here