Port the site to GNU Emacs + Org mode (using org-publish)
[~bandali/bndl.org] / publish.el
diff --git a/publish.el b/publish.el
new file mode 100644 (file)
index 0000000..81e5b60
--- /dev/null
@@ -0,0 +1,118 @@
+;; Inspiration: https://gitlab.com/ambrevar/ambrevar.gitlab.io.
+
+;; TODO: use git time-stamps
+
+(require 'ox-publish)
+(require 'seq)
+
+(add-to-list 'load-path ".")
+
+(add-to-list 'load-path "../xah-replace-pairs")
+(require 'xah-replace-pairs)
+
+(defun aminb--readfile (filepath)
+  "Return filepath's content."
+  (with-temp-buffer
+    (insert-file-contents filepath)
+    (buffer-string)))
+
+(defun aminb--find-replace (match-with replace-with string)
+  "Return the string resulting from replacing `match-with' with
+  `replace-with' in `string'."
+  (when (string-match match-with string)
+    (replace-match replace-with nil nil string)))
+
+(defun aminb--my-html-body-light-filter (output backend info)
+  "Make adjustments needed for dark/light mode <input> tag to work."
+  (when (eq backend 'html)
+    (aminb--find-replace
+     "</body>\n"
+     "</div>\n</body>\n"
+     (aminb--find-replace
+      "<body>\n"
+      "<body>\n<input class=\"light-off\" id=\"light-off\" type=\"checkbox\">\n<div class=\"page\">"
+      output))))
+
+(add-to-list 'org-export-filter-final-output-functions
+           'aminb--my-html-body-light-filter)
+
+;; re-defining this (originally in `ox-html.el') to add `?g'
+(defun org-html-format-spec (info)
+  "Return format specification for preamble and postamble.
+INFO is a plist used as a communication channel."
+  (let ((timestamp-format (plist-get info :html-metadata-timestamp-format))
+        (git-repo "<a href=\"https://git.sr.ht/~aminb/aminb.org/commit/?id=%s\">@%s</a>")
+        (git-rev (shell-command-to-string "git rev-parse HEAD"))
+        (git-rev-short (shell-command-to-string "git rev-parse --short HEAD")))
+    `((?t . ,(org-export-data (plist-get info :title) info))
+      (?s . ,(org-export-data (plist-get info :subtitle) info))
+      (?d . ,(org-export-data (org-export-get-date info timestamp-format)
+                             info))
+      (?T . ,(format-time-string timestamp-format))
+      (?a . ,(org-export-data (plist-get info :author) info))
+      (?e . ,(mapconcat
+             (lambda (e) (format "<a href=\"mailto:%s\">%s</a>" e e))
+             (split-string (plist-get info :email)  ",+ *")
+             ", "))
+      (?g . ,(format git-repo git-rev git-rev-short))
+      (?c . ,(plist-get info :creator))
+      (?C . ,(let ((file (plist-get info :input-file)))
+              (format-time-string timestamp-format
+                                  (and file (nth 5 (file-attributes file))))))
+      (?v . ,(or (plist-get info :html-validation-link) "")))))
+
+;; timestamps can be used to avoid rebuilding everything
+;; should probably be put inside the public/ directory
+(setq org-publish-use-timestamps-flag t
+      org-publish-timestamp-directory "./")
+
+;; get rid of index.html~ and other backup files that may be created during generation
+(setq make-backup-files nil)
+
+(setq org-export-with-section-numbers nil
+      org-export-with-smart-quotes t
+      org-export-with-email t
+      org-export-with-date t
+      org-export-with-tags 'not-in-toc
+      org-export-with-toc t)
+
+(setq org-html-divs '((preamble  "header" "preamble")
+                      (content   "main"   "content")
+                      (postamble "footer" "postamble"))
+      ;; TODO: link last update to commit history of file
+      org-html-preamble t
+      org-html-postamble t
+      org-html-preamble-format  `(("en" ,(aminb--readfile "partials/preamble.html")))
+      org-html-postamble-format `(("en" ,(aminb--readfile "partials/postamble.html")))
+      org-html-container-element "section"
+      org-html-metadata-timestamp-format "%Y-%m-%d"
+      org-html-checkbox-type 'html
+      org-html-html5-fancy t
+      ;; use custom css
+      ;; this removes the dependency on `htmlize',
+      ;; but we also lose syntax highlighting.
+      org-html-htmlize-output-type nil
+      org-html-validation-link nil
+      org-html-doctype "html5")
+
+(setq org-publish-project-alist
+      (list
+       (list "site-org"
+             :base-directory "./source/"
+             :exclude "macros.org"
+             :recursive t
+             :publishing-function '(org-html-publish-to-html)
+             :publishing-directory "./public/"
+             :html-head-include-default-style nil
+             :html-head-include-scripts nil)
+       (list "site-static"
+             :base-directory "source/"
+             :exclude "\\.org\\'"
+             :base-extension 'any
+             :publishing-directory "./public"
+             :publishing-function 'org-publish-attachment
+             :recursive t)
+       (list "site" :components '("site-org"))))
+
+(defun aminb/publish ()
+  (org-publish-all))