Revamp CI config export to properly use htmlize

This commit is contained in:
TEC 2020-12-25 01:13:06 +08:00
parent ea959d619e
commit 099cab498d
Signed by: tec
GPG Key ID: 779591AFDB81F06C
1 changed files with 137 additions and 31 deletions

View File

@ -1,54 +1,160 @@
#!/usr/bin/env sh
":"; exec emacs --quick --script "$0" -- "$@" # -*- mode: emacs-lisp; lexical-binding: t; -*-
(setq start-time (float-time))
(message "\033[1;0mBuilding config file export\033[0;90m")
(setq start-time (float-time)
exit-code 0)
(defun timed-message (orig-fn format-str &rest args)
(if (string-match-p "[%\\.1fs]" format-str)
(apply orig-fn format-str args)
(apply orig-fn
(concat "[%.1fs] " format-str)
(append (list (- (float-time) start-time))
args))))
(advice-add 'message :around #'timed-message)
(defvar files-to-htmlize '("init.el" "config.el" "packages.el"))
(defvar htmlize-theme 'doom-one-light)
(defvar file-root (file-name-directory ; parent dir
(directory-file-name
(file-name-directory load-file-name))))
;;; Report errors
(setq debug-on-error t
doom-debug-p t)
(advice-add 'theme-magic-from-emacs :override #'ignore)
(advice-add 'format-all-buffer :override #'ignore)
(load (expand-file-name "~/.emacs.d/init.el"))
(setq log-file (expand-file-name "exporter-log.txt" (file-name-directory load-file-name)))
;;; If running just to to htmlization (i.e., in a terminal)
(unless noninteractive
(write-region "" nil log-file)
(defun logged-message (orig-fn &rest args)
(unless inhibit-message
(let ((inhibit-message t)
(msg (apply orig-fn args)))
(princ msg #'external-debugging-output)
(append-to-file msg nil log-file)
(append-to-file "\n" nil log-file))))
(advice-add 'message :around #'logged-message)
(advice-add 'theme-magic-from-emacs :override #'ignore)
(advice-add 'format-all-buffer :override #'ignore)
(load (expand-file-name "~/.emacs.d/init.el"))
(require 'htmlize)
(defun my-htmlize-file (file)
(message "Htmlizing %s" file)
(let ((output-file (expand-file-name
(htmlize-make-file-name (file-name-nondirectory file))
(file-name-directory file))))
(with-current-buffer (find-file-literally file)
(normal-mode)
(font-lock-ensure)
(set-buffer-modified-p nil)
(with-current-buffer (htmlize-buffer-1)
(goto-char (point-min))
(replace-string "</title>\n"
"</title>
<style>
body > pre {
font-size: 1rem;
max-width: min(100rem, 100%);
width: max-content;
white-space: pre-wrap;
margin: auto;
}
</style>\n")
(let ((inhibit-message t))
(write-file output-file)
(kill-buffer (current-buffer))))
(kill-buffer (current-buffer)))))
(load-theme htmlize-theme t)
(dolist (file files-to-htmlize)
(my-htmlize-file (expand-file-name file file-root)))
(kill-emacs exit-code))
;;; Running 'normally' (batch mode) from now on
;;; Colourful messaging and errors
(defun timed-coloured-message (orig-fn format-str &rest args)
(cond
((eq (car args) 'unmodified)
(apply orig-fn format-str (cdr args)))
((or (not format-str) (string-match-p "\\[%\\.1fs\\]" format-str))
(apply orig-fn format-str args))
(t
(apply orig-fn
(concat (if (string-match-p "^\\[[0-9;]+\\]" format-str)
(replace-regexp-in-string
"^\\(?:\\[\\([0-9;]+\\)\\] ?\\)?"
"\033[\\1m[%.1fs] "
format-str)
(concat "[%.1fs] " format-str))
"\033[0;90m")
(append (list (- (float-time) start-time))
args)))))
(advice-add 'message :around #'timed-coloured-message)
(setq exit-code 0)
(defun red-error (orig-fn &rest args)
(message "\033[0;31m")
(message "\033[0;31m" 'unmodified)
(apply orig-fn args)
(message "\033[0m")
(message "\033[0m" 'unmodified)
(setq exit-code 1))
(advice-add 'debug :around #'red-error)
(message "\033[0;34m[%.1fs] Opening config file: %s\033[90m"
(- (float-time) start-time)
(expand-file-name "../config.org"))
;;; Htmlization
(message "[0;1] Starting htmlization process for %s" files-to-htmlize)
(setq htmlization-process
(start-process "htmlize" nil
"script" "-eqfc"
(format "TERM=xterm-direct emacs --quick -nw --eval '(load (expand-file-name \"%s\"))'" load-file-name)))
(set-process-sentinel
htmlization-process
(lambda (process _signal)
(when (eq (process-status process) 'exit)
(if (= 0 (process-exit-status process))
(message "[1;35] Htmlization finished sucessfully")
(message "[31] Htmlization process failed!")
(message "\033[0;31m %s\033[0m"
'unmodified
(with-temp-buffer
(insert-file-contents-literally log-file)
(buffer-substring-no-properties (point-min) (point-max))))))))
(message "[0;1] Building config file export")
;;; Setup
(advice-add 'theme-magic-from-emacs :override #'ignore)
(advice-add 'format-all-buffer :override #'ignore)
(load (expand-file-name "~/.emacs.d/init.el"))
(when (and (featurep 'undo-tree) global-undo-tree-mode)
(global-undo-tree-mode -1)
(advice-add 'undo-tree-save-history :override #'ignore))
;;; Actually do the exporting now
(message "[34] Opening config file: %s"
(expand-file-name "config.org" file-root))
(require 'vc) ; need this for modification-time macro
(with-current-buffer (find-file (expand-file-name "../config.org"))
(message "\033[0;33m[%.1fs] Exporting %s\033[0m"
(- (float-time) start-time)
(buffer-file-name))
(with-current-buffer (find-file (expand-file-name "config.org" file-root))
(message "[33] Exporting %s" (buffer-file-name))
(org-html-export-to-html))
(message "\033[0;33m[%.1fs] Htmlizing %s\033[0m"
(- (float-time) start-time)
(buffer-file-name))
(htmlize-file (expand-file-name "../init.el"))
(htmlize-file (expand-file-name "../config.el"))
(htmlize-file (expand-file-name "../packages.el"))
(message "[1;32] Config export complete!")
(message "\033[1;32m[%.1fs] Config export complete!\033[0m"
(- (float-time) start-time))
(while (not (eq (process-status htmlization-process) 'exit))
(message "Waiting for Htmlization process to finish")
(sleep-for 1))
(setq inhibit-message t)
(kill-emacs exit-code)