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