7acc4a5605deeb8c063bb138301d0b27136753a7
[~bandali/bndl.org] / publish.el
1 ;; Inspiration: https://gitlab.com/ambrevar/ambrevar.gitlab.io.
2
3 ;; TODO: use git time-stamps
4
5 (require 'ox-publish)
6
7 (add-to-list 'load-path ".")
8
9 (defun aminb--readfile (filepath)
10 "Return filepath's content."
11 (with-temp-buffer
12 (insert-file-contents filepath)
13 (buffer-string)))
14
15 (defun aminb--find-replace (match-with replace-with string)
16 "Return the string resulting from replacing `match-with' with
17 `replace-with' in `string'."
18 (when (string-match match-with string)
19 (replace-match replace-with nil nil string)))
20
21 (defun aminb--my-html-body-light-filter (output backend info)
22 "Make adjustments needed for dark/light mode <input> tag to work."
23 (when (eq backend 'html)
24 (aminb--find-replace
25 "</body>\n"
26 "</div>\n</body>\n"
27 (aminb--find-replace
28 "<body>\n"
29 "<body>\n<input class=\"light-off\" id=\"light-off\" type=\"checkbox\">\n<div class=\"page\">"
30 output))))
31
32 (add-to-list 'org-export-filter-final-output-functions
33 'aminb--my-html-body-light-filter)
34
35 ;; re-defining this (originally in `ox-html.el') to add `?g'
36 (defun org-html-format-spec (info)
37 "Return format specification for preamble and postamble.
38 INFO is a plist used as a communication channel."
39 (let ((timestamp-format (plist-get info :html-metadata-timestamp-format))
40 (git-repo "<a href=\"https://git.sr.ht/~aminb/aminb.org/commit/?id=%s\">@%s</a>")
41 (git-rev (shell-command-to-string "git rev-parse HEAD"))
42 (git-rev-short (shell-command-to-string "git rev-parse --short HEAD")))
43 `((?t . ,(org-export-data (plist-get info :title) info))
44 (?s . ,(org-export-data (plist-get info :subtitle) info))
45 (?d . ,(org-export-data (org-export-get-date info timestamp-format)
46 info))
47 (?T . ,(format-time-string timestamp-format))
48 (?a . ,(org-export-data (plist-get info :author) info))
49 (?e . ,(mapconcat
50 (lambda (e) (format "<a href=\"mailto:%s\">%s</a>" e e))
51 (split-string (plist-get info :email) ",+ *")
52 ", "))
53 (?g . ,(format git-repo git-rev git-rev-short))
54 (?c . ,(plist-get info :creator))
55 (?C . ,(let ((file (plist-get info :input-file)))
56 (format-time-string timestamp-format
57 (and file (nth 5 (file-attributes file))))))
58 (?v . ,(or (plist-get info :html-validation-link) "")))))
59
60 ;; timestamps can be used to avoid rebuilding everything
61 ;; should probably be put inside the public/ directory
62 (setq org-publish-use-timestamps-flag t
63 org-publish-timestamp-directory "./")
64
65 ;; get rid of index.html~ and other backup files that may be created during generation
66 (setq make-backup-files nil)
67
68 (setq org-export-with-section-numbers nil
69 org-export-with-smart-quotes t
70 org-export-with-email t
71 org-export-with-date t
72 org-export-with-tags 'not-in-toc
73 org-export-with-toc t)
74
75 (setq org-html-divs '((preamble "header" "preamble")
76 (content "main" "content")
77 (postamble "footer" "postamble"))
78 ;; TODO: link last update to commit history of file
79 org-html-preamble t
80 org-html-postamble t
81 org-html-preamble-format `(("en" ,(aminb--readfile "partials/preamble.html")))
82 org-html-postamble-format `(("en" ,(aminb--readfile "partials/postamble.html")))
83 org-html-container-element "section"
84 org-html-metadata-timestamp-format "%Y-%m-%d"
85 org-html-checkbox-type 'html
86 org-html-html5-fancy t
87 ;; use custom css
88 ;; this removes the dependency on `htmlize',
89 ;; but we also lose syntax highlighting.
90 org-html-htmlize-output-type nil
91 org-html-validation-link nil
92 org-html-doctype "html5")
93
94 (setq org-publish-project-alist
95 (list
96 (list "site-org"
97 :base-directory "./source/"
98 :exclude "macros.org"
99 :recursive t
100 :publishing-function '(org-html-publish-to-html)
101 :publishing-directory "./public/"
102 :html-head-include-default-style nil
103 :html-head-include-scripts nil)
104 (list "site-static"
105 :base-directory "source/"
106 :exclude "\\.org\\'"
107 :base-extension 'any
108 :publishing-directory "./public"
109 :publishing-function 'org-publish-attachment
110 :recursive t)
111 (list "site" :components '("site-org"))))
112
113 (defun aminb/publish ()
114 (org-publish-all))