;;; build.el --- publish org files to html -*- lexical-binding: t; -*-
(package-initialize)
(require 'font-lock)
(require 'cl-seq)
(require 'subr-x)
;; htmlize fontifies #+begin_src blocks. Every color it asks about is
;; answered with nil, so only the dumb face defaults survive -- some stuff
;; bold, some italic, no colors -- and Emacs 31's `reset' face attribute
;; can't reach htmlize's string matching to crash it.
(require 'htmlize)
(advice-add 'htmlize-color-to-rgb :override (lambda (&rest _) nil))
(require 'org)
(require 'ox)
(require 'ox-html)
(require 'ox-publish)
(setq org-html-postamble nil)
(setq org-html-css-default nil)
(setq org-export-with-timestamps nil)
(setq org-export-time-stamp-file nil)
;; Export using the file name as the title, exactly as if "TITLE" had been
;; set on the org file (e.g. "AssFiles" for "AssFiles.org"). This is the
;; default value of the TITLE export option; an explicit "#+TITLE:" in the
;; org file still wins, since in-buffer options are read last.
;;
;; The 4th element of an option entry is the form Org `eval's to get the
;; default value (see `org-export--get-global-options'), and "parse" marks
;; it as Org syntax, so the title is transcoded just like "#+TITLE:".
(setq org-export-options-alist
(cons '(:title "TITLE" nil
(file-name-base (or buffer-file-name (buffer-name)))
parse)
(assq-delete-all :title org-export-options-alist)))
;; Publishing must not run page code, and must not rewrite the record of
;; the one time that code was run. Pages keep the output of commands that
;; were run by hand -- mostly over ssh -- as #+RESULTS: blocks, and that
;; transcript is the artifact: a re-export would re-execute the commands
;; and replace the recorded output with whatever the machine says today.
;;
;; Saying so to Babel takes two steps, because header arguments are read
;; as Lisp while a block is parsed, before any :eval could veto anything.
;; The page containing
;;
;; #+BEGIN_SRC csh :dir (concat "/ssh:" my/host ":/etc")
;;
;; aborts the whole publish with "void-variable my/host": my/host is set by
;; an earlier block of that page -- in the interactive session the page was
;; captured in -- and this batch Emacs never runs it.
;;
;; 1. Every block defaults to :eval never-export, so an already recorded
;; result is what gets exported and no block is evaluated here. The
;; rest of Babel's export semantics still applies, `:exports none'
;; included, which is why this is preferable to turning off
;; `org-export-use-babel': that would also publish blocks whose whole
;; point is to be dropped from the output.
;; 2. Header argument values are read as text instead of being evaluated,
;; so the ":dir" above stays the literal string it looks like and no
;; unbound variable can be touched. Nothing is lost by reading them
;; this way: a value's evaluated form only ever mattered to execute the
;; block, and step 1 saw to it that no block runs. (Unlike step 1 this
;; is global to the session, which is fine -- build.el is only ever
;; loaded by batch Emacs.)
(require 'ob-core)
(add-to-list 'org-babel-default-header-args '(:eval . "never-export"))
(defun my/read-header-arg-literally (read cell &rest _)
"Call READ on CELL with Lisp evaluation inhibited."
(funcall read cell t))
(advice-add 'org-babel-read :around #'my/read-header-arg-literally)
(setq org-publish-project-alist
'(("org-files"
:base-directory "."
:base-extension "org"
:publishing-directory "."
:recursive t
:publishing-function org-html-publish-to-html
;; preserve export behavior
:with-toc nil
:with-section-numbers nil
:html-style nil
:with-author nil
:with-creator nil
:with-date nil)))
;; Per-file preprocessing, run before Org processes a buffer for export,
;; lives in modules beside this file: prefill.el carries the #+LF like
;; form and the #+NF / #+XASS / diss boilerplate, tiles.el the
;; #+begin_tiles expansion, and `my/preprocess' below composes their
;; passes. Each module's own header says what its pass does.
(defvar my/project-root
(file-name-directory (or load-file-name
(expand-file-name "build.el" default-directory)))
"Directory the published tree is rooted at, used to resolve our targets.")
;; prepr.el holds the helpers the modules share; then the modules
;; themselves. All are loaded by name -- batch Emacs does not put the
;; current directory on load-path for `require' to find.
(load (expand-file-name "prepr.el" my/project-root))
(load (expand-file-name "prefill.el" my/project-root))
(load (expand-file-name "tiles.el" my/project-root))
(defun my/preprocess (_backend)
(let ((file (buffer-file-name)))
(when file
(my/ass-preprocess file)
(my/like-substitute file)
(my/tiles-substitute)
;; every page's head carries a to the .org it
;; was published from, under the plain file name because the .org and
;; its .html are published as siblings. Org reads #+HTML_HEAD
;; wherever it stands in the buffer, so the position does not matter.
(goto-char (point-min))
(insert (format
"#+HTML_HEAD: \n"
(my/html-escape (file-name-nondirectory file)))))))
(add-hook 'org-export-before-processing-hook 'my/preprocess)
;; ibuild.sh sets INCREMENTAL=1 and Org then only re-exports a page when its
;; .org is newer than (or its .html missing beside) the stored timestamp, so
;; an unchanged file is skipped entirely before any preprocessing runs. A
;; plain `build' leaves INCREMENTAL unset and forces the whole tree.
(org-publish "org-files" (unless (equal (getenv "INCREMENTAL") "1") t))