summaryrefslogtreecommitdiff
path: root/publish.el
blob: 6786c7df01c8e3b9f98dafb0acc4fe0cfdb1ceb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env -S emacs -Q --batch --script

(require 'ox)
(require 'esxml)

(defun thomaslabs-navbar ()
  (sxml-to-xml
   `(nav
     (ul
      (li (a (@ (href "/"))
             "Home"))
      (li (a (@ (href "/z80/"))
             "Z80"))
      (li (a (@ (href "/programs/"))
             "programs"))
      (li (a (@ (href "/math/"))
             "math"))
      (li (a (@ (href "https://git.thomaslabs.org"))
             "git"))
      (li (a (@ (href "/privacy.html"))
             "contact & privacy policy"))))))

(defun thomaslabs-footer ()
  (sxml-to-xml
   `(footer
     (p
      "Copyright © 2021 - 2023 Thomas Albers Raviola"
      (br)
      "This website and its contents are published under the
following licences, unless otherwise specified"
      (ul
       (li "Website and images:"
           (a (@ (href "https://creativecommons.org/licenses/by-nc-sa/4.0/"))
              "CC BY-NC-SA 4.0"))
       (li "Schematics:"
           (a (@ (href "https://creativecommons.org/licenses/by-nc-nd/4.0/"))
              "CC BY-NC-ND 4.0"))
       (li "Code:"
           (a (@ (href "https://www.gnu.org/licenses/gpl-3.0.html"))
              "GPL-3.0")))))))

(defun thomaslabs-template (contents info)
  (concat
   "<!DOCTYPE html>\n"
   (sxml-to-xml
    `(html (@ (lang "en"))
           (head
            ,(org-html--build-meta-info
              `(:title ,(if (plist-get info :title)
                            (format "Thomas' Labs | %s" (car (plist-get info :title)))
                          "Thomas' Labs")
                       ,@info))
            ,(org-html--build-head info))
           (body
            ,(if-let ((title (plist-get info :title)))
                 (let ((subtitle (plist-get info :subtitle)))
                   `(header
                     (div "Thomas' Labs")
                     (h1 ,(org-export-data title info))
                     ,@(if subtitle
                           `((div (@ (class "subtitle"))
                                   ,(car subtitle)))
                         '())))
               `(header
                 (h1 "Thomas' Labs")))
            ,(thomaslabs-navbar)
            (div (@ (class "content"))
                 (main
                  (article ,contents)))
            (hr)
            ,(thomaslabs-footer))))))

(defun math-preamble (&rest args)
  (sxml-to-xml
   '(div
     (h2 "Disclaimer")
     (p "This site as of now just a technology demonstration and its claims
should not be taken as true (even though I myself am pretty confident
they are"))))

(defun math-sitemap (title entries)
  (concat "#+title: " title "\n"
          "#+setupfile: ../../math_options.org\n\n"
          (org-list-to-org entries)))


(defun org-local-link-export (link description format info)
  (format "<a href=\"%s\">%s</a>"
          link
          description))

(defun org-img-link-export (link description format info)
  (format "<a href=\"%s\">%s</a>"
          link
          description))

(defun org-local-link-follow (&rest args)
  t)

(org-link-set-parameters "local"
                         :follow #'org-local-link-follow
                         :export #'org-local-link-export)

(org-link-set-parameters "img"
                         :follow #'org-local-link-follow
                         :export #'org-img-link-export)

(defvar root-dir (expand-file-name ""))

(defun org-export-file-uri (filename)
  "Return file URI associated to FILENAME."
  (cond ((not (file-name-absolute-p filename))
         (string-remove-prefix "site" filename))
        ((string-suffix-p ".jpg" filename)
         (concat "/img-small" (string-remove-prefix "/img" filename)))
	    (t
         (string-remove-prefix (concat root-dir "/site")
                               (expand-file-name filename)))))

(org-export-define-derived-backend 'thomaslabs-html 'html
  :translate-alist
  '((template . thomaslabs-template)))

(defun thomaslabs-publish-to-html (plist filename pub-dir)
  "Publish an org file to HTML, using the FILENAME as the output directory."
  (org-publish-org-to 'thomaslabs-html
                      filename
                      ".html"
                      plist
                      pub-dir))

(defun image-dimensions (pathname)
  (with-temp-buffer
    (call-process "identify" nil (current-buffer) nil
                  "-ping" "-format" "%wx%h" pathname)
    (mapcar #'cl-parse-integer (split-string (buffer-string) "x"))))

(defun resize-image (factor input output)
  (let ((percentage (format "%.01f%%" (* 100.0 factor))))
    (call-process "convert" nil nil nil input "-resize" percentage output)))

(defun thomaslabs-publish-image (_plist filename pub-dir)
  (unless (file-directory-p pub-dir)
    (make-directory pub-dir t))
  (let ((output (expand-file-name (file-name-nondirectory filename) pub-dir)))
    (unless (file-equal-p (expand-file-name (file-name-directory filename))
			  (file-name-as-directory (expand-file-name pub-dir)))
      (let* ((dimensions (image-dimensions filename))
             (width (cl-first dimensions))
             (height (cl-second dimensions))
             (factor (min (/ 800.0 width) (/ 800.0 height) 1.0)))
        (resize-image factor filename output)))
    ;; Return file name.
    output))

(defun org-html--wrap-latex-environment (contents _ &optional caption label)
  "Wrap CONTENTS string within appropriate environment for equations.
When optional arguments CAPTION and LABEL are given, use them for
caption and \"id\" attribute."
  (format "\n<div%s class=\"equation-container\">\n%s%s\n</div>"
          ;; ID.
          (if (org-string-nw-p label) (format " id=\"%s\"" label) "")
          ;; Contents.
          (format "<span class=\"equation\">\n%s\n</span>" contents)
          ;; Caption.
          (if (not (org-string-nw-p caption)) ""
            (format "\n<span class=\"equation-label\">(%s)</span>"
                    caption))))

(defvar default-html-project-properties
  '(:publishing-function thomaslabs-publish-to-html
                         :with-toc nil
                         :recursive nil

                         :language "en_US"
                         :html-head-include-default-style nil
                         :html-head-include-scripts nil
                         :html-preamble nil
                         :html-postamble nil
                         :html-use-infojs nil
                         :html-html5-fancy t
                         :html-doctype "html5"
                         :with-toc nil
                         :section-numbers nil
                         :with-latex dvisvgm))

(setq org-publish-project-alist
      `(("root"
         :base-directory "src"
         :publishing-directory "site"
         ,@default-html-project-properties)
        ("arm"
         :base-directory "src/arm"
         :publishing-directory "site/arm"
         ,@default-html-project-properties
         :recursive t)
        ("programs"
         :base-directory "src/programs"
         :publishing-directory "site/programs"
         ,@default-html-project-properties
         :recursive t)
        ("z80"
         :base-directory "src/z80"
         :publishing-directory "site/z80"
         ,@default-html-project-properties
         :recursive t)

        ("math"
         :base-directory "src/math"
         :publishing-directory "site/math"
         :publishing-function thomaslabs-publish-to-html
         ,@(org-plist-delete default-html-project-properties :html-preamble)

         :html-preamble math-preamble

         :auto-sitemap t
         :sitemap-filename "index.org"
         :sitemap-title "Math and Physics articles"
         :sitemap-function math-sitemap)

        ("img"
         :base-directory "site/img"
         :publishing-directory "site/img-small"
         :base-extension "png\\|jpg\\|gif"
         :recursive t
         :publishing-function thomaslabs-publish-image)


        ("thomaslabs"
         :components ("root" "arm" "programs" "z80" "math" "img"))))

;; (plist-put org-format-latex-options :html-foreground "#cccccc")
(plist-put org-format-latex-options :html-foreground "#000000")

(setq org-publish-timestamp-directory (concat (getenv "XDG_CACHE_HOME") "/org-timestamps/"))
(setq org-preview-latex-image-directory (expand-file-name "site/svg"))
(setq org-html-htmlize-output-type 'css)

(org-publish "thomaslabs" t)
;; fork ox-html?