all changes
This commit is contained in:
730
build-site.el
730
build-site.el
@@ -6,32 +6,127 @@
|
||||
|
||||
;;; Commentary:
|
||||
;; Run this file with:
|
||||
;; emacs -Q --script build-site.el
|
||||
;; emacs -Q --script build-site.el
|
||||
;;
|
||||
;; Optional env vars:
|
||||
;; SITE_FORCE=1 Force republish all files (default: incremental)
|
||||
;; SITE_DRY=1 Parse and prepare only; skip org-publish-all
|
||||
|
||||
;;; Code:
|
||||
|
||||
(require 'package)
|
||||
(defvar site-root
|
||||
(file-name-directory
|
||||
(or load-file-name buffer-file-name)))
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 0. Bootstrap: paths and load-path
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
(defconst z/build-start-time (float-time)
|
||||
"Wall-clock time when the build started.")
|
||||
|
||||
(defconst z/site-root
|
||||
(file-name-as-directory
|
||||
(file-name-directory
|
||||
(or load-file-name buffer-file-name)))
|
||||
"Absolute path to the site root (always ends with /).
|
||||
This is the single authoritative definition — lisp/* files must NOT
|
||||
redefine site-root themselves.")
|
||||
|
||||
;; Keep the old name around so any stray code still compiles.
|
||||
(defvaralias 'site-root 'z/site-root)
|
||||
|
||||
(defun site-path (path)
|
||||
(expand-file-name path site-root))
|
||||
"Expand PATH relative to `z/site-root'."
|
||||
(expand-file-name path z/site-root))
|
||||
|
||||
(add-to-list 'load-path (site-path "lisp"))
|
||||
|
||||
(add-to-list 'load-path
|
||||
(expand-file-name "lisp"
|
||||
(file-name-directory
|
||||
(or load-file-name buffer-file-name))))
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 1. Logging helpers (ANSI colours for terminal output)
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
(require 'recently-updated)
|
||||
(require 'tags)
|
||||
(require 'sidenotes)
|
||||
(require 'sitemaps)
|
||||
(defconst z/colour-reset "\033[0m")
|
||||
(defconst z/colour-bold "\033[1m")
|
||||
(defconst z/colour-dim "\033[2m")
|
||||
(defconst z/colour-green "\033[32m")
|
||||
(defconst z/colour-yellow "\033[33m")
|
||||
(defconst z/colour-cyan "\033[36m")
|
||||
(defconst z/colour-red "\033[31m")
|
||||
(defconst z/colour-blue "\033[34m")
|
||||
(defconst z/colour-magenta "\033[35m")
|
||||
|
||||
(setq package-user-dir (expand-file-name "./.packages"))
|
||||
(setq package-archives '(("melpa" . "https://melpa.org/packages/")
|
||||
("elpa" . "https://elpa.gnu.org/packages/")))
|
||||
(defun z/log (level fmt &rest args)
|
||||
"Emit a coloured build-log line.
|
||||
LEVEL is one of: info warn error step metric done.
|
||||
FMT / ARGS are passed to `format'."
|
||||
(let* ((msg (apply #'format fmt args))
|
||||
(prefix
|
||||
(pcase level
|
||||
('info (concat z/colour-cyan " INFO " z/colour-reset))
|
||||
('warn (concat z/colour-yellow " WARN " z/colour-reset))
|
||||
('error (concat z/colour-red " ERROR " z/colour-reset))
|
||||
('step (concat z/colour-blue z/colour-bold
|
||||
" ──── " z/colour-reset))
|
||||
('metric (concat z/colour-magenta " ◈ " z/colour-reset))
|
||||
('done (concat z/colour-green z/colour-bold
|
||||
" ✓ " z/colour-reset))
|
||||
(_ " "))))
|
||||
(message "%s%s" prefix msg)))
|
||||
|
||||
(defun z/log-separator (&optional label)
|
||||
"Print a coloured separator line, optionally with LABEL."
|
||||
(if label
|
||||
(message "%s%s── %s %s%s"
|
||||
z/colour-dim z/colour-bold label
|
||||
(make-string (max 0 (- 60 (length label))) ?─)
|
||||
z/colour-reset)
|
||||
(message "%s%s%s"
|
||||
z/colour-dim
|
||||
(make-string 66 ?─)
|
||||
z/colour-reset)))
|
||||
|
||||
(defmacro z/with-timing (label &rest body)
|
||||
"Execute BODY, then log elapsed seconds under LABEL."
|
||||
(declare (indent 1))
|
||||
(let ((t0 (gensym "t0")))
|
||||
`(let ((,t0 (float-time)))
|
||||
(prog1 (progn ,@body)
|
||||
(z/log 'metric "%s took %.2fs" ,label (- (float-time) ,t0))))))
|
||||
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 2. Package bootstrap
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
(z/log-separator "Package setup")
|
||||
|
||||
(require 'package)
|
||||
|
||||
(setq package-user-dir (site-path ".packages"))
|
||||
(setq package-archives
|
||||
'(("melpa" . "https://melpa.org/packages/")
|
||||
("elpa" . "https://elpa.gnu.org/packages/")))
|
||||
|
||||
(package-initialize)
|
||||
|
||||
(z/with-timing "Package archive refresh"
|
||||
(unless package-archive-contents
|
||||
(z/log 'info "Refreshing package archive contents…")
|
||||
(condition-case err
|
||||
(package-refresh-contents)
|
||||
(error
|
||||
(z/log 'warn "Could not refresh archives: %s" (error-message-string err))))))
|
||||
|
||||
(z/with-timing "htmlize install"
|
||||
(unless (package-installed-p 'htmlize)
|
||||
(z/log 'info "Installing htmlize…")
|
||||
(condition-case err
|
||||
(package-install 'htmlize)
|
||||
(error
|
||||
(z/log 'error "htmlize install failed: %s" (error-message-string err))
|
||||
(error "Cannot continue without htmlize")))))
|
||||
|
||||
(require 'htmlize)
|
||||
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 3. Org / ox requires
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
(require 'ox-publish)
|
||||
(require 'cl-lib)
|
||||
@@ -40,154 +135,176 @@
|
||||
(require 'ox-html)
|
||||
(require 'ox-md)
|
||||
|
||||
(package-initialize)
|
||||
(unless package-archive-contents
|
||||
(package-refresh-contents))
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 4. Local library requires (after load-path is set)
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
(package-install 'htmlize)
|
||||
(add-to-list 'load-path "~/master-folder/org_files/org_web/")
|
||||
(require 'htmlize)
|
||||
(z/log-separator "Local libraries")
|
||||
|
||||
(defvar z-shared-head
|
||||
"
|
||||
<link rel=\"icon\" type=\"image/png\" sizes=\"48x48\" href=\"/assets/icons/icons8-film-tape-100.png\" />
|
||||
(dolist (lib '(recently-updated tags sidenotes sitemaps))
|
||||
(condition-case err
|
||||
(progn
|
||||
(require lib)
|
||||
(z/log 'done "Loaded %s" lib))
|
||||
(error
|
||||
(z/log 'error "Failed to load %s: %s" lib (error-message-string err))
|
||||
(error "Aborting: required library %s missing" lib))))
|
||||
|
||||
<link rel=\"stylesheet\" href=\"/assets/styles/style.css\" />
|
||||
<link rel=\"stylesheet\" href=\"/assets/styles/bigger-picture.min.css\" />
|
||||
<link rel=\"stylesheet\" href=\"/assets/styles/comments.css\" />
|
||||
<link rel=\"stylesheet\" href=\"/assets/styles/kanban.css\" />
|
||||
<link rel=\"stylesheet\" href=\"/assets/styles/org-syntax.css\" />
|
||||
<link rel=\"stylesheet\" href=\"/assets/styles/misc.css\" />
|
||||
<link rel=\"stylesheet\" href=\"/assets/styles/toc.css\" />
|
||||
<link rel=\"stylesheet\" href=\"/assets/styles/media.css\" />
|
||||
<link rel=\"stylesheet\" href=\"/assets/styles/wird-tracker.css\" />
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 5. HTML export settings
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
<script src=\"/assets/scripts/script.js\" defer></script>
|
||||
<script src=\"/assets/scripts/lunr.js\" defer></script>
|
||||
<script src=\"/assets/scripts/competency-status-board.js\" defer></script>
|
||||
<script src=\"/assets/scripts/notes.js\" defer></script>
|
||||
<script src=\"/assets/scripts/comments.js\" defer></script>
|
||||
<script src=\"/assets/scripts/bigger-picture.min.js\" defer></script>
|
||||
<script src=\"/assets/scripts/search.js\" defer></script>
|
||||
<script src=\"/assets/scripts/svg-pan-zoom.min.js\" defer></script>
|
||||
<script src=\"/assets/scripts/gallery-init.js\" defer></script>
|
||||
<script src=\"/assets/scripts/sitemap-interactive.js\" defer></script>
|
||||
<script src=\"/assets/scripts/wird-tracker.js\" defer></script>
|
||||
"
|
||||
)
|
||||
(setq org-html-htmlize-output-type 'css)
|
||||
|
||||
(defvar z/shared-head
|
||||
(concat
|
||||
"<link rel=\"icon\" type=\"image/png\" sizes=\"48x48\""
|
||||
" href=\"/assets/icons/icons8-film-tape-100.png\" />\n"
|
||||
(mapconcat
|
||||
(lambda (f)
|
||||
(format "<link rel=\"stylesheet\" href=\"/assets/styles/%s\" />" f))
|
||||
'("style.css"
|
||||
"bigger-picture.min.css"
|
||||
"comments.css"
|
||||
"kanban.css"
|
||||
"org-syntax.css"
|
||||
"misc.css"
|
||||
"toc.css"
|
||||
"media.css"
|
||||
"wird-tracker.css")
|
||||
"\n")
|
||||
"\n"
|
||||
(mapconcat
|
||||
(lambda (f)
|
||||
(format "<script src=\"/assets/scripts/%s\" defer></script>" f))
|
||||
'("script.js"
|
||||
"lunr.js"
|
||||
"competency-status-board.js"
|
||||
"notes.js"
|
||||
"comments.js"
|
||||
"bigger-picture.min.js"
|
||||
"search.js"
|
||||
"svg-pan-zoom.min.js"
|
||||
"gallery-init.js"
|
||||
"sitemap-interactive.js"
|
||||
"wird-tracker.js")
|
||||
"\n"))
|
||||
"Shared <head> HTML fragment injected into every page.")
|
||||
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 6. Global Org macros
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
(setq org-export-global-macros
|
||||
(append
|
||||
org-export-global-macros
|
||||
'(("sidenote"
|
||||
. "@@html:<label for=\"sn$1\" class=\"margin-toggle sidenote-number\"></label><input type=\"checkbox\" id=\"sn$1\" class=\"margin-toggle\"/><span class=\"sidenote\">$2</span>@@")
|
||||
("epigraph" . "@@html:<div class=\"epigraph\"><blockquote>$1<footer>$2</footer></blockquote></div>@@")
|
||||
("epigraph_single" . "@@html:<div class=\"epigraph\"><blockquote>$1</blockquote></div>@@")
|
||||
("epigraph3" . "@@html:<div class=\"epigraph\"><blockquote>$1<footer>$2, <cite>$3</cite></footer></blockquote></div>@@")
|
||||
("kbd" . "@@html:<kbd>$1</kbd>@@@@latex:\\texttt{$1}@@")
|
||||
("margimg"
|
||||
. "@@html:<aside class=\"marginnote\"><figure class=\"mn-fig\"><img src=\"$1\" alt=\"$2\" class=\"mn-img\" loading=\"lazy\" decoding=\"async\"/>$3</figure></aside>@@")
|
||||
("countdown" . "@@html:<time class=\"countdown\" datetime=\"$1\" data-label=\"$2\"></time>@@")
|
||||
. "@@html:<label for=\"sn$1\" class=\"margin-toggle sidenote-number\"></label>\
|
||||
<input type=\"checkbox\" id=\"sn$1\" class=\"margin-toggle\"/>\
|
||||
<span class=\"sidenote\">$2</span>@@")
|
||||
("epigraph"
|
||||
. "@@html:<div class=\"epigraph\"><blockquote>$1<footer>$2</footer></blockquote></div>@@")
|
||||
("epigraph_single"
|
||||
. "@@html:<div class=\"epigraph\"><blockquote>$1</blockquote></div>@@")
|
||||
("epigraph3"
|
||||
. "@@html:<div class=\"epigraph\"><blockquote>$1<footer>$2, <cite>$3</cite></footer></blockquote></div>@@")
|
||||
("kbd"
|
||||
. "@@html:<kbd>$1</kbd>@@@@latex:\\texttt{$1}@@")
|
||||
("margimg"
|
||||
. "@@html:<aside class=\"marginnote\"><figure class=\"mn-fig\">\
|
||||
<img src=\"$1\" alt=\"$2\" class=\"mn-img\" loading=\"lazy\" decoding=\"async\"/>$3\
|
||||
</figure></aside>@@")
|
||||
("countdown"
|
||||
. "@@html:<time class=\"countdown\" datetime=\"$1\" data-label=\"$2\"></time>@@"))))
|
||||
|
||||
|
||||
org-export-global-macros)))
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 7. Preamble / postamble
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
(defvar z-preamble
|
||||
"
|
||||
<div class=\"banner-header\">
|
||||
<a href=\"/\"> <img src=\"/assets/images/gr.png\" alt=\"Site Logo\" class=\"banner-logo\" /> </a>
|
||||
(defvar z/preamble
|
||||
"<div class=\"banner-header\">
|
||||
<a href=\"/\"><img src=\"/assets/images/gr.png\" alt=\"Site Logo\" class=\"banner-logo\" /></a>
|
||||
<nav>
|
||||
<a href=\"/\">Home | </a>
|
||||
<a href=\"/blogs/blogs-list.html \">Blogs | </a>
|
||||
<a href=\"/blogs/blogs-list.html\">Blogs | </a>
|
||||
<a href=\"/posts/career/career-list.html\">Career | </a>
|
||||
<a href=\"https://zone.zainezq.com\">Dashboard</a>
|
||||
|
||||
<input type=\"search\\\" id=\"search-input\" placeholder=\"Search…\" aria-label=\"Search notes\" />
|
||||
<input type=\"search\" id=\"search-input\" placeholder=\"Search…\" aria-label=\"Search notes\" />
|
||||
<button id=\"search-btn\" aria-label=\"Search\">🔍</button>
|
||||
</nav>
|
||||
<button class=\"theme-toggle\" id=\"theme-toggle\" type=\"button\" aria-label=\"Toggle dark mode\">🌗 Theme</button>
|
||||
</div>
|
||||
<div id=\"updated\">Updated: %C</div>
|
||||
")
|
||||
|
||||
</div>
|
||||
<div id=\"updated\">Updated: %C</div>
|
||||
|
||||
"
|
||||
)
|
||||
|
||||
(defvar z-postamble
|
||||
(defvar z/postamble
|
||||
"<footer>
|
||||
<div class=\"copyright-container\">
|
||||
<div class=\"copyright\">
|
||||
Copyright © 2022-2026 Zaine Qayyum. All rights reserved unless otherwise noted.</div></div>
|
||||
<div class=\"generated\">
|
||||
Created with %c on <a href=\"https://www.archlinux.org/\">Arch</a> <a href=\"https://www.gnu.org\">GNU</a>/<a href=\"https://www.kernel.org/\">Linux</a>
|
||||
Created with %c on <a href=\"https://www.archlinux.org/\">Arch</a> \
|
||||
<a href=\"https://www.gnu.org\">GNU</a>/<a href=\"https://www.kernel.org/\">Linux</a>
|
||||
</div>
|
||||
</footer>")
|
||||
|
||||
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 8. Comments support
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
(defun z/comments-file-p (file)
|
||||
"Return non-nil if FILE has comments enabled.
|
||||
A file has comments if:
|
||||
- It has a #+COMMENTS: keyword with value \"t\" (case-insensitive)."
|
||||
"Return non-nil if FILE has #+COMMENTS: t."
|
||||
(when (file-exists-p file)
|
||||
(with-temp-buffer
|
||||
(insert-file-contents file)
|
||||
(org-mode)
|
||||
(let* ((keywords (org-collect-keywords '("COMMENTS")))
|
||||
(comments (cadr (assoc "COMMENTS" keywords))))
|
||||
(print keywords)
|
||||
|
||||
(and comments
|
||||
(string-match-p "^\\s-*t\\s-*$"
|
||||
(downcase comments)))))))
|
||||
(val (cadr (assoc "COMMENTS" keywords))))
|
||||
(and val
|
||||
(string-match-p "^\\s-*t\\s-*$" (downcase val)))))))
|
||||
|
||||
(defun z/comments-file-slug (file)
|
||||
"Return page slug from #+SLUG: or fallback to filename."
|
||||
"Return page slug from #+SLUG: or fallback to filename base."
|
||||
(with-temp-buffer
|
||||
(insert-file-contents file)
|
||||
(org-mode)
|
||||
(let* ((keywords (org-collect-keywords '("SLUG")))
|
||||
(slug (cadr (assoc "SLUG" keywords))))
|
||||
(slug (cadr (assoc "SLUG" keywords))))
|
||||
(if (and slug (string-match-p "\\S-" slug))
|
||||
slug
|
||||
(file-name-base file)))))
|
||||
|
||||
(defun z/org-html-insert-comments-into-body (body backend info)
|
||||
"Insert comments section at the end of the document body."
|
||||
"Append a comments section to BODY for files that opt in."
|
||||
(when (org-export-derived-backend-p backend 'html)
|
||||
(let ((input-file (plist-get info :input-file)))
|
||||
(if (and input-file
|
||||
(z/comments-file-p input-file))
|
||||
(if (and input-file (z/comments-file-p input-file))
|
||||
(let* ((slug (z/comments-file-slug input-file))
|
||||
(comments-html
|
||||
(format
|
||||
"
|
||||
<section id=\"comments\" class=\"comments\" data-slug=\"%s\">
|
||||
(html (format
|
||||
"\n<section id=\"comments\" class=\"comments\" data-slug=\"%s\">
|
||||
<h2>Comments</h2>
|
||||
|
||||
<div id=\"comments-list\" class=\"comments-list\">
|
||||
<noscript>Please enable JavaScript to view comments.</noscript>
|
||||
</div>
|
||||
|
||||
<form id=\"comment-form\" class=\"comment-form\">
|
||||
<label class=\"comment-author\">
|
||||
<span>Name (optional)</span>
|
||||
<input type=\"text\" name=\"author\"/>
|
||||
</label>
|
||||
|
||||
<label class=\"comment-content\">
|
||||
<span>Your comment</span>
|
||||
<textarea name=\"content\" required></textarea>
|
||||
</label>
|
||||
|
||||
<button type=\"submit\">Post comment</button>
|
||||
</form>
|
||||
</section>
|
||||
"
|
||||
slug)))
|
||||
(concat body comments-html))
|
||||
</section>\n"
|
||||
slug)))
|
||||
(concat body html))
|
||||
body))))
|
||||
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 9. Body-class injection (WIP / no-sidenotes)
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
(defun z/org-html-add-body-classes (output backend info)
|
||||
"Add layout-related classes to <body> based on file metadata."
|
||||
@@ -196,11 +313,9 @@ A file has comments if:
|
||||
(classes
|
||||
(delq nil
|
||||
(list
|
||||
(when (and input-file
|
||||
(z/wip-file-p input-file))
|
||||
(when (and input-file (z/wip-file-p input-file))
|
||||
"wip")
|
||||
(when (and input-file
|
||||
(z/no-sidenotes-file-p input-file))
|
||||
(when (and input-file (z/no-sidenotes-file-p input-file))
|
||||
"no-sidenotes")))))
|
||||
(if classes
|
||||
(replace-regexp-in-string
|
||||
@@ -215,33 +330,31 @@ A file has comments if:
|
||||
(add-to-list 'org-export-filter-body-functions
|
||||
#'z/org-html-insert-comments-into-body)
|
||||
|
||||
(defun z/lima--ensure-sitemap-file (project)
|
||||
"Ensure the sitemap source file exists under the project's base directory."
|
||||
(let* ((base-dir (file-name-as-directory
|
||||
(org-publish-property :base-directory project)))
|
||||
(fname (or (org-publish-property :sitemap-filename project)
|
||||
"lima-list.org"))
|
||||
(title (or (org-publish-property :sitemap-title project)
|
||||
"Lima"))
|
||||
(sitemap-path (expand-file-name fname base-dir)))
|
||||
;; If file missing or you want to always regenerate, write header.
|
||||
(unless (file-exists-p sitemap-path)
|
||||
(with-temp-file sitemap-path
|
||||
(insert "#+TITLE: " title "\n"
|
||||
"#+OPTIONS: toc:nil num:nil\n\n")))))
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 10. Custom z-html backend
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
(org-export-define-derived-backend 'z-html 'html
|
||||
:filters-alist '((:filter-final-output . z/insert-filetags-after-title)))
|
||||
|
||||
(defun z/z-publish-to-html (plist filename pub-dir)
|
||||
"Publish FILENAME to HTML using the z-html backend."
|
||||
(org-publish-org-to 'z-html filename ".html" plist pub-dir))
|
||||
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 11. Lima project helpers
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
(defun z/lima-sitemap-format-entry (entry style project)
|
||||
"Format sitemap entry for Lima; skip dirs, keep nested paths, and prefix /lima/."
|
||||
(let* ((file (if (listp entry) (car entry) entry))
|
||||
"Format a sitemap entry for Lima; skip directories."
|
||||
(let* ((file (if (listp entry) (car entry) entry))
|
||||
(base-dir (file-name-as-directory
|
||||
(org-publish-property :base-directory project)))
|
||||
;; Ensure we work with an absolute file path under base-dir
|
||||
(abs (if (file-name-absolute-p file)
|
||||
file
|
||||
(expand-file-name file base-dir))))
|
||||
;; Skip directories
|
||||
(abs (if (file-name-absolute-p file)
|
||||
file
|
||||
(expand-file-name file base-dir))))
|
||||
(unless (file-directory-p abs)
|
||||
(let* ((rel (file-relative-name abs base-dir)) ;; guaranteed no “…/..” backtracking now
|
||||
(let* ((rel (file-relative-name abs base-dir))
|
||||
(rel-noext (file-name-sans-extension rel))
|
||||
(title (org-publish-find-title abs project)))
|
||||
(format "[[file:%s.html][%s]]"
|
||||
@@ -249,69 +362,47 @@ A file has comments if:
|
||||
(or title (file-name-nondirectory rel-noext)))))))
|
||||
|
||||
(defun z/copy-neighbor-attachments (source-dir pub-dir)
|
||||
"Copy sibling directories named .attachments.* from SOURCE-DIR to PUB-DIR."
|
||||
(let ((dirs (directory-files source-dir t "^\\.attachments\\..+")))
|
||||
(dolist (d dirs)
|
||||
(when (file-directory-p d)
|
||||
(let* ((target (expand-file-name (file-name-nondirectory d) pub-dir)))
|
||||
(make-directory target t)
|
||||
;; copy-directory: (DIRECTORY NEWNAME &optional KEEP-TIME PARENTS COPY-CONTENTS)
|
||||
(copy-directory d target t t t))))))
|
||||
"Copy sibling .attachments.* directories from SOURCE-DIR into PUB-DIR."
|
||||
(dolist (d (directory-files source-dir t "^\\.attachments\\..+"))
|
||||
(when (file-directory-p d)
|
||||
(let ((target (expand-file-name (file-name-nondirectory d) pub-dir)))
|
||||
(make-directory target t)
|
||||
(copy-directory d target t t t)))))
|
||||
|
||||
(defun z/publish-lima-file (plist filename pub-dir)
|
||||
"Publish Org or Markdown file from lima directory."
|
||||
(let* ((ext (downcase (or (file-name-extension filename) "")))
|
||||
(base (file-name-base filename))
|
||||
"Publish an Org or Markdown file from the lima directory."
|
||||
(let* ((ext (downcase (or (file-name-extension filename) "")))
|
||||
(base (file-name-base filename))
|
||||
(src-dir (file-name-directory filename)))
|
||||
|
||||
(cond
|
||||
;; ORG FILES (publish as-is)
|
||||
;; ── Org files ──────────────────────────────────────────────────────────
|
||||
((string= ext "org")
|
||||
;; Ensure sibling .attachments.* are published
|
||||
(z/copy-neighbor-attachments src-dir pub-dir)
|
||||
(org-publish-org-to 'z-html filename ".html" plist pub-dir))
|
||||
|
||||
;; MARKDOWN FILES
|
||||
;; ── Markdown files ─────────────────────────────────────────────────────
|
||||
((string= ext "md")
|
||||
(let* ((temp-org
|
||||
(expand-file-name
|
||||
(concat base ".org")
|
||||
(make-temp-file "lima-build-" t))))
|
||||
|
||||
;; Convert Markdown → Org with pandoc
|
||||
(call-process "pandoc" nil nil nil
|
||||
filename "-f" "markdown" "-t" "org" "-o" temp-org)
|
||||
|
||||
;; Ensure #+TITLE exists
|
||||
|
||||
;; Ensure front matter at top of temp-org
|
||||
(with-temp-buffer
|
||||
(insert-file-contents temp-org)
|
||||
|
||||
;; Build values
|
||||
(let* ((title base)
|
||||
(slug base)
|
||||
(date (format-time-string "<%Y-%m-%d %a %H:%M>")))
|
||||
(goto-char (point-min))
|
||||
|
||||
;; Insert front matter
|
||||
;; (Always ensure they are at the absolute top)
|
||||
(insert "#+TITLE: " title "\n"
|
||||
"#+OPTIONS: num:nil\n"
|
||||
"#+DATE: " date "\n"
|
||||
"#+COMMENTS: t\n"
|
||||
"#+SLUG: " slug "\n\n"))
|
||||
|
||||
;; Write back to file
|
||||
(write-region (point-min) (point-max) temp-org))
|
||||
|
||||
;; Ensure sibling .attachments.* are published
|
||||
(let* ((tmp-dir (make-temp-file "lima-build-" t))
|
||||
(temp-org (expand-file-name (concat base ".org") tmp-dir)))
|
||||
;; Markdown → Org
|
||||
(let ((rc (call-process "pandoc" nil nil nil
|
||||
filename "-f" "markdown" "-t" "org" "-o" temp-org)))
|
||||
(unless (zerop rc)
|
||||
(z/log 'error "pandoc failed (rc=%d) for %s" rc filename)
|
||||
(error "pandoc conversion failed for %s" filename)))
|
||||
;; Prepend front matter
|
||||
(with-temp-buffer
|
||||
(insert-file-contents temp-org)
|
||||
(goto-char (point-min))
|
||||
(insert (format "#+TITLE: %s\n#+OPTIONS: num:nil\n#+DATE: %s\n#+COMMENTS: t\n#+SLUG: %s\n\n"
|
||||
base
|
||||
(format-time-string "<%Y-%m-%d %a %H:%M>")
|
||||
base))
|
||||
(write-region (point-min) (point-max) temp-org))
|
||||
;; Publish
|
||||
(z/copy-neighbor-attachments src-dir pub-dir)
|
||||
|
||||
;; Publish using ORIGINAL base name
|
||||
(let ((output-file (expand-file-name (concat base ".html") pub-dir)))
|
||||
(org-publish-org-to 'z-html temp-org ".html" plist pub-dir)
|
||||
;; temp-org base may differ; normalize to requested base
|
||||
(let ((generated (expand-file-name
|
||||
(concat (file-name-base temp-org) ".html")
|
||||
pub-dir)))
|
||||
@@ -319,24 +410,24 @@ A file has comments if:
|
||||
(not (string-equal generated output-file)))
|
||||
(rename-file generated output-file t))))))
|
||||
|
||||
;; ── Fallback: treat as attachment ──────────────────────────────────────
|
||||
(t
|
||||
;; Default: treat as attachment
|
||||
(org-publish-attachment plist filename pub-dir)))))
|
||||
|
||||
|
||||
(setq org-html-htmlize-output-type 'css)
|
||||
|
||||
(org-export-define-derived-backend 'z-html 'html
|
||||
:filters-alist '((:filter-final-output . z/insert-filetags-after-title)))
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 12. Preparation hooks
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
(defun z/org-main-prep (_project)
|
||||
(z/generate-recently-updated-org 26))
|
||||
"Pre-publish hook: regenerate recently-updated.org."
|
||||
(z/log 'step "Generating recently-updated.org…")
|
||||
(z/with-timing "recently-updated generation"
|
||||
(z/generate-recently-updated-org 26)))
|
||||
|
||||
(defun z/z-publish-to-html (plist filename pub-dir)
|
||||
"Publish FILENAME to HTML using the z-html backend."
|
||||
(org-publish-org-to 'z-html filename ".html" plist pub-dir))
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 13. Publishing project alist
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
;; Define the publishing project
|
||||
(setq org-publish-project-alist
|
||||
`(("org-main"
|
||||
:recursive t
|
||||
@@ -348,11 +439,18 @@ A file has comments if:
|
||||
:sitemap-filename "sitemap.org"
|
||||
:sitemap-title "Sitemap"
|
||||
:sitemap-sort-files chronologically
|
||||
:html-preamble ,z-preamble
|
||||
:html-postamble ,z-postamble
|
||||
:html-head ,z-shared-head
|
||||
:html-preamble ,z/preamble
|
||||
:html-postamble ,z/postamble
|
||||
:html-head ,z/shared-head
|
||||
:preparation-function z/org-main-prep)
|
||||
|
||||
("org-assets"
|
||||
:base-directory ,(site-path "assets/")
|
||||
:base-extension "css\\|js\\|png\\|jpg\\|gif\\|svg\\|pdf\\|woff\\|woff2\\|ttf"
|
||||
:publishing-directory ,(site-path "output/assets/")
|
||||
:recursive t
|
||||
:publishing-function org-publish-attachment)
|
||||
|
||||
("org-categories-sitemap"
|
||||
:recursive t
|
||||
:base-directory ,(site-path "home/")
|
||||
@@ -362,9 +460,9 @@ A file has comments if:
|
||||
:sitemap-filename "categories.org"
|
||||
:sitemap-title "Categories"
|
||||
:sitemap-function z/categories-sitemap
|
||||
:html-preamble ,z-preamble
|
||||
:html-postamble ,z-postamble
|
||||
:html-head ,z-shared-head)
|
||||
:html-preamble ,z/preamble
|
||||
:html-postamble ,z/postamble
|
||||
:html-head ,z/shared-head)
|
||||
|
||||
("org-posts"
|
||||
:base-directory ,(site-path "posts/")
|
||||
@@ -377,15 +475,15 @@ A file has comments if:
|
||||
:html-validation-link nil
|
||||
:with-toc t
|
||||
:section-numbers t
|
||||
:html-preamble ,z-preamble
|
||||
:html-postamble ,z-postamble
|
||||
:html-preamble ,z/preamble
|
||||
:html-postamble ,z/postamble
|
||||
:auto-sitemap t
|
||||
:sitemap-filename "posts-list.org"
|
||||
:sitemap-title "Posts List"
|
||||
:sitemap-style list
|
||||
:sitemap-function z/posts-sitemap
|
||||
:sitemap-sort-files anti-chronologically
|
||||
:html-head ,z-shared-head)
|
||||
:html-head ,z/shared-head)
|
||||
|
||||
("org-blogs"
|
||||
:base-directory ,(site-path "blogs/")
|
||||
@@ -396,15 +494,15 @@ A file has comments if:
|
||||
:with-author nil
|
||||
:with-creator nil
|
||||
:html-validation-link nil
|
||||
:html-preamble ,z-preamble
|
||||
:html-postamble ,z-postamble
|
||||
:html-preamble ,z/preamble
|
||||
:html-postamble ,z/postamble
|
||||
:auto-sitemap t
|
||||
:sitemap-filename "blogs-list.org"
|
||||
:sitemap-title "Blogs List"
|
||||
:sitemap-style list
|
||||
:sitemap-function z/blogs-grouped-sitemap
|
||||
:sitemap-sort-files anti-chronologically
|
||||
:html-head ,z-shared-head)
|
||||
:html-head ,z/shared-head)
|
||||
|
||||
("org-books"
|
||||
:base-directory ,(site-path "books/")
|
||||
@@ -412,49 +510,15 @@ A file has comments if:
|
||||
:recursive t
|
||||
:base-extension "org"
|
||||
:publishing-function z/z-publish-to-html
|
||||
:html-preamble ,z-preamble
|
||||
:html-postamble ,z-postamble
|
||||
:html-preamble ,z/preamble
|
||||
:html-postamble ,z/postamble
|
||||
:auto-sitemap t
|
||||
:sitemap-filename "books-list.org"
|
||||
:sitemap-title "Books List"
|
||||
:sitemap-style list
|
||||
:sitemap-function z/books-sitemap
|
||||
:sitemap-sort-files anti-chronologically
|
||||
:html-head ,z-shared-head)
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; ("org-2025" ;;
|
||||
;; :base-directory ,(site-path "blogs/2025/") ;;
|
||||
;; :publishing-directory ,(site-path "output/blogs/2025/") ;;
|
||||
;; :recursive t ;;
|
||||
;; :base-extension "org" ;;
|
||||
;; :publishing-function z/z-publish-to-html ;;
|
||||
;; :html-preamble ,z-preamble ;;
|
||||
;; :html-postamble ,z-postamble ;;
|
||||
;; :auto-sitemap t ;;
|
||||
;; :sitemap-filename "2025-list.org" ;;
|
||||
;; :sitemap-title "2025 List" ;;
|
||||
;; :sitemap-style list ;;
|
||||
;; :sitemap-function z/2025-sitemap ;;
|
||||
;; :sitemap-sort-files anti-chronologically ;;
|
||||
;; :html-head ,z-shared-head) ;;
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
;; ("org-2026"
|
||||
;; :base-directory ,(site-path "blogs/2026/")
|
||||
;; :publishing-directory ,(site-path "output/blogs/2026/")
|
||||
;; :recursive t
|
||||
;; :base-extension "org"
|
||||
;; :publishing-function z/z-publish-to-html
|
||||
;; :html-preamble ,z-preamble
|
||||
;; :html-postamble ,z-postamble
|
||||
;; :auto-sitemap t
|
||||
;; :sitemap-filename "2026-list.org"
|
||||
;; :sitemap-title "2026 List"
|
||||
;; :sitemap-style list
|
||||
;; :sitemap-function z/2026-sitemap
|
||||
;; :sitemap-sort-files anti-chronologically
|
||||
;; :html-head ,z-shared-head)
|
||||
:html-head ,z/shared-head)
|
||||
|
||||
("org-career"
|
||||
:base-directory ,(site-path "posts/career/")
|
||||
@@ -462,15 +526,15 @@ A file has comments if:
|
||||
:recursive t
|
||||
:base-extension "org"
|
||||
:publishing-function z/z-publish-to-html
|
||||
:html-preamble ,z-preamble
|
||||
:html-postamble ,z-postamble
|
||||
:html-preamble ,z/preamble
|
||||
:html-postamble ,z/postamble
|
||||
:auto-sitemap t
|
||||
:sitemap-filename "career-list.org"
|
||||
:sitemap-title "Career List"
|
||||
:sitemap-style list
|
||||
:sitemap-function z/career-sitemap
|
||||
:sitemap-sort-files anti-chronologically
|
||||
:html-head ,z-shared-head)
|
||||
:html-head ,z/shared-head)
|
||||
|
||||
("wip-pages"
|
||||
:base-directory ,(site-path "")
|
||||
@@ -478,15 +542,15 @@ A file has comments if:
|
||||
:recursive t
|
||||
:base-extension "org"
|
||||
:publishing-function z/z-publish-to-html
|
||||
:html-preamble ,z-preamble
|
||||
:html-postamble ,z-postamble
|
||||
:html-preamble ,z/preamble
|
||||
:html-postamble ,z/postamble
|
||||
:auto-sitemap t
|
||||
:sitemap-filename "wip.org"
|
||||
:sitemap-title "Work in progress"
|
||||
:sitemap-style list
|
||||
:sitemap-function z/wip-sitemap
|
||||
:sitemap-sort-files anti-chronologically
|
||||
:html-head ,z-shared-head)
|
||||
:html-head ,z/shared-head)
|
||||
|
||||
("org-tags"
|
||||
:base-directory ,(site-path "tags/")
|
||||
@@ -494,42 +558,152 @@ A file has comments if:
|
||||
:recursive t
|
||||
:base-extension "org"
|
||||
:publishing-function org-html-publish-to-html
|
||||
:html-preamble ,z-preamble
|
||||
:html-postamble ,z-postamble
|
||||
:html-head ,z-shared-head)
|
||||
:html-preamble ,z/preamble
|
||||
:html-postamble ,z/postamble
|
||||
:html-head ,z/shared-head)
|
||||
|
||||
("org-lima"
|
||||
:base-directory ,(site-path "lima/")
|
||||
:publishing-directory ,(site-path "output/lima/")
|
||||
:recursive t
|
||||
:base-extension "org\\|md"
|
||||
:publishing-function z/publish-lima-file
|
||||
:with-author nil
|
||||
:with-creator nil
|
||||
:html-validation-link nil
|
||||
:html-preamble ,z-preamble
|
||||
:html-postamble ,z-postamble
|
||||
:sitemap-filename "lima-list.org"
|
||||
:auto-sitemap t
|
||||
:sitemap-title "Lima"
|
||||
:sitemap-style list
|
||||
:sitemap-sort-files anti-chronologically
|
||||
:sitemap-format-entry z/lima-sitemap-format-entry
|
||||
:html-head ,z-shared-head)
|
||||
|
||||
("org-assets"
|
||||
:base-directory ,(site-path "assets/")
|
||||
:base-extension "css\\|js\\|png\\|jpg\\|gif\\|svg\\|pdf\\|woff\\|woff2\\|ttf"
|
||||
:publishing-directory ,(site-path "output/assets/")
|
||||
("org-lima"
|
||||
:base-directory ,(site-path "lima/")
|
||||
:publishing-directory ,(site-path "output/lima/")
|
||||
:recursive t
|
||||
:publishing-function org-publish-attachment)))
|
||||
:base-extension "org\\|md"
|
||||
:publishing-function z/publish-lima-file
|
||||
:with-author nil
|
||||
:with-creator nil
|
||||
:html-validation-link nil
|
||||
:html-preamble ,z/preamble
|
||||
:html-postamble ,z/postamble
|
||||
:sitemap-filename "lima-list.org"
|
||||
:auto-sitemap t
|
||||
:sitemap-title "Lima"
|
||||
:sitemap-style list
|
||||
:sitemap-sort-files anti-chronologically
|
||||
:sitemap-format-entry z/lima-sitemap-format-entry
|
||||
:html-head ,z/shared-head)))
|
||||
|
||||
(delete-directory (site-path "output/") t)
|
||||
(delete-directory (site-path "tags/") t)
|
||||
(message "Directory deleted")
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 14. Pre-build clean-up (opt-in via SITE_CLEAN=1)
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;;
|
||||
;; IMPORTANT: output/ and .org-timestamps/ are a matched pair.
|
||||
;; .org-timestamps/ is org-publish's cache of every file's last-published
|
||||
;; mtime. If output/ is wiped without also wiping .org-timestamps/, the next
|
||||
;; incremental build sees "cache says up-to-date" but the output file is gone,
|
||||
;; so it silently skips the file. That is why assets/ and index.html vanished.
|
||||
;;
|
||||
;; Rule: whenever output/ is deleted, .org-timestamps/ must be deleted too.
|
||||
|
||||
(z/write-tag-pages)
|
||||
(org-publish-all t)
|
||||
(message "Build complete!")
|
||||
(z/log-separator "Pre-build")
|
||||
|
||||
;;; build-site.el ends here
|
||||
(defun z/wipe-build-dirs ()
|
||||
"Delete output/, tags/, and .org-timestamps/ together."
|
||||
(dolist (dir (list (site-path "output/")
|
||||
(site-path "tags/")
|
||||
(site-path ".org-timestamps/")))
|
||||
(if (file-directory-p dir)
|
||||
(progn
|
||||
(z/with-timing (format "delete %s"
|
||||
(file-name-nondirectory
|
||||
(directory-file-name dir)))
|
||||
(delete-directory dir t))
|
||||
(z/log 'done "Deleted %s" dir))
|
||||
(z/log 'info "Already absent: %s" dir))))
|
||||
|
||||
(defun z/output-integrity-ok-p ()
|
||||
"Return t if critical output paths all exist.
|
||||
Returns nil (and logs warnings) if assets/ or any top-level HTML is missing.
|
||||
A nil result means the timestamp cache is stale and we must force-republish."
|
||||
(let ((missing '()))
|
||||
(unless (file-directory-p (site-path "output/assets/"))
|
||||
(push "output/assets/" missing))
|
||||
(unless (or (file-exists-p (site-path "output/index.html"))
|
||||
(file-exists-p (site-path "output/sitemap.html")))
|
||||
(push "output/index.html (or sitemap.html)" missing))
|
||||
(dolist (m missing)
|
||||
(z/log 'warn "Integrity check: missing %s" m))
|
||||
(null missing)))
|
||||
|
||||
(cond
|
||||
;; ── Explicit clean requested ───────────────────────────────────────────────
|
||||
((getenv "SITE_CLEAN")
|
||||
(z/log 'warn "SITE_CLEAN=1 — wiping output/, tags/, and .org-timestamps/")
|
||||
(z/wipe-build-dirs)
|
||||
(setenv "SITE_FORCE" "1"))
|
||||
|
||||
;; ── Output looks incomplete — cache is stale ──────────────────────────────
|
||||
;; Catches the case where output/ was wiped externally (git clean, manual rm,
|
||||
;; Syncthing conflict) without clearing .org-timestamps/. Without this guard
|
||||
;; org-publish would silently skip every "up-to-date" file and produce a
|
||||
;; broken site with missing assets and pages.
|
||||
((not (z/output-integrity-ok-p))
|
||||
(z/log 'warn "Output integrity check failed — timestamp cache is stale")
|
||||
(z/log 'warn "Clearing .org-timestamps/ and forcing full republish")
|
||||
(let ((ts-dir (site-path ".org-timestamps/")))
|
||||
(when (file-directory-p ts-dir)
|
||||
(z/with-timing "delete .org-timestamps"
|
||||
(delete-directory ts-dir t))
|
||||
(z/log 'done "Deleted .org-timestamps/")))
|
||||
(setenv "SITE_FORCE" "1"))
|
||||
|
||||
;; ── Normal incremental build ──────────────────────────────────────────────
|
||||
(t
|
||||
(z/log 'info "Incremental build — output/ looks complete")))
|
||||
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 15. Tag pages
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
(z/log-separator "Tag generation")
|
||||
|
||||
(z/with-timing "tag page generation"
|
||||
(condition-case err
|
||||
(let ((n (z/write-tag-pages)))
|
||||
(z/log 'done "Tag pages written%s"
|
||||
(if (numberp n) (format " (%d tags)" n) "")))
|
||||
(error
|
||||
(z/log 'error "Tag generation failed: %s" (error-message-string err))
|
||||
(error "Aborting build: tag generation error"))))
|
||||
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 16. Publish
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
(z/log-separator "Publishing")
|
||||
|
||||
(if (getenv "SITE_DRY")
|
||||
(z/log 'warn "SITE_DRY=1 — skipping org-publish-all (dry run)")
|
||||
(let ((force (not (not (getenv "SITE_FORCE")))))
|
||||
(when force
|
||||
(z/log 'info "SITE_FORCE=1 — forcing republish of all files"))
|
||||
(condition-case err
|
||||
(z/with-timing "org-publish-all"
|
||||
(org-publish-all force))
|
||||
(error
|
||||
(z/log 'error "org-publish-all failed: %s" (error-message-string err))
|
||||
(error "Build failed")))))
|
||||
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
;; 17. Build summary
|
||||
;; ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
(z/log-separator "Build summary")
|
||||
|
||||
(let* ((elapsed (- (float-time) z/build-start-time))
|
||||
(out-dir (site-path "output/"))
|
||||
(html-files (when (file-directory-p out-dir)
|
||||
(length (directory-files-recursively out-dir "\\.html\\'"))))
|
||||
(css-files (when (file-directory-p out-dir)
|
||||
(length (directory-files-recursively out-dir "\\.css\\'"))))
|
||||
(js-files (when (file-directory-p out-dir)
|
||||
(length (directory-files-recursively out-dir "\\.js\\'"))))
|
||||
(tag-files (let ((td (site-path "output/tags/")))
|
||||
(when (file-directory-p td)
|
||||
(length (directory-files td nil "\\.html\\'"))))))
|
||||
(z/log 'metric "Total elapsed : %.2fs" elapsed)
|
||||
(z/log 'metric "HTML files output: %d" (or html-files 0))
|
||||
(z/log 'metric "CSS files output: %d" (or css-files 0))
|
||||
(z/log 'metric "JS files output: %d" (or js-files 0))
|
||||
(z/log 'metric "Tag pages output: %d" (or tag-files 0))
|
||||
(z/log 'done "Build complete ✓"))
|
||||
|
||||
;;; build-site.el ends here
|
||||
Reference in New Issue
Block a user