forked from FG42/FG42
164 lines
5.9 KiB
EmacsLisp
164 lines
5.9 KiB
EmacsLisp
;;; ox-template.el --- A HTML exporter via templates for org-mode
|
|
;; Copyright (C) 2021-2022 Sameer Rahmani
|
|
;; Author: Sameer Rahmani <lxsameer@gnu.org>
|
|
;; 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 <http://www.gnu.org/licenses/>.
|
|
;;; 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 'fg42/build/utils)
|
|
|
|
|
|
(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)))
|
|
|
|
|
|
(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))
|
|
tags
|
|
" | ")
|
|
""))
|
|
|
|
|
|
(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)))
|
|
(progn
|
|
|
|
(mustache-render (use-html post-template)
|
|
(ht
|
|
("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 " ")
|
|
text
|
|
(and tags "   ") 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))
|
|
("title" (get-file-global-props (plist-get info :input-file) "TITLE"))
|
|
("version" (fg42/version))
|
|
("description" (or (get-file-global-props (plist-get info :input-file) "DESC")
|
|
""))
|
|
("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)
|
|
"Return the drawer format for the given drawer NAME and CONTENT."
|
|
(format "<section class='%s-drawer'><p>%s</p>%s</section>"
|
|
(downcase name)
|
|
(capitalize name)
|
|
content))
|
|
|
|
|
|
(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 'fg42/build/ox-template)
|
|
;;; ox-template.el ends here
|