My nest. My website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.7 KiB

;;; ox-template.el --- A HTML exporter via templates for org-mode
;; Copyright (C) 2021 Sameer Rahmani
;; Author: Sameer Rahmani <>
;; URL:
;; 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
;; 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 'seq)
(require 'ox-publish)
(require 'mustache)
(require 'pp)
(require 'ht)
(require 'lisp/utils)
(defun use-html (path-to-template)
"Use the given template at PATH-TO-TEMPLATE as a template."
(insert-file-contents path-to-template)
(defun render-tags (tags)
"Return a string representing TAGS html."
(if tags
(mapconcat (lambda (x) (format "<a href=\"/tags/%s.html\" class=\"tag-link\">#%s</a>" x x))
" | ")
(defun preamble-fn (info)
"Return a string for the header section of pages using INFO plist."
(let* ((file (plist-get info :input-file))
(page-template (or (plist-get info :html-page-preamble-template) "page-preamble.html"))
(post-template (or (plist-get info :html-post-preabmle-template) "post-preamble.html"))
(is-page? (string= (get-file-global-props file "PAGE") "true"))
(tags (get-file-tags file)))
(let ((title (get-file-global-props file "TITLE"))
(date (get-file-global-props file "DATE")))
(when (not title)
(message "'#+TITLE' is missing from '%s'" file)
(error "'#+TITLE' is missing from '%s'" file))
(if is-page?
(mustache-render (use-html page-template)
(ht ("title" title)))
(mustache-render (use-html post-template)
("title" title)
("date" (or date ""))
("tags" (render-tags tags)))))))))
(defun org-html-render-tag-template (tags info)
"Render the given TAGS and INFO using the :html-headline-template."
(let ((template (plist-get info :html-tags-template))
(ctx (ht ("tags" (format "%s" tags))
("base-url" (plist-get info :base-url)))))
(if (null tags)
(mustache-render (use-html template) ctx))))
;; Replace org-html--tags with our impelementation if the
;; `:html-headline-template' configuration exists
(advice-add 'org-html--tags :around 'org-html-tag-template)
(defun org-html-tag-template (orig-fn &rest args)
"Decide whether to run ORIG-FN with ARGS or the org-html-render-tag-template.
It looks for `:html-tags-template' in the info and if i exists it will
call `org-html-render-tag-template' otherwise will call ORIG-FN."
(let ((template (plist-get (cadr args) :html-tags-template)))
(if template
(apply #'org-html-render-tag-template args)
(apply orig-fn args))))
(org-export-define-derived-backend 'templated-html 'html
:translate-alist '((template . templated-html-template-fn)))
(defun headline-format (todo todo-type priority text tags info)
"Format the headline using TODO TODO-TYPE PRIORITY TEXT TAGS and INFO."
(let ((todo (org-html--todo todo info))
(priority (org-html--priority priority info))
;; We don't care about the tags here since we put the in the preamble
(tags nil))
(concat todo (and todo " ")
priority (and priority " ")
(and tags "&#xa0;&#xa0;&#xa0;") tags)))
(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))
("preamble" (org-html--build-pre/postamble 'preamble info))
("postamble" (org-html--build-pre/postamble 'postamble info)))))
(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 custom-drawer-format (name content)
(format "<section class='%s-drawer'><p>%s</p>%s</section>"
(downcase name)
(capitalize name)
(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)
plist pub-dir))
(provide 'lisp/ox-template)
;;; ox-template.el ends here