src/themes.lisp (1864 bytes)
1 (in-package :coleslaw) 2 3 (defparameter *injections* '() 4 "A list that stores pairs of (string . predicate) to inject in the page.") 5 6 (defun add-injection (injection location) 7 "Adds an INJECTION to a given LOCATION for rendering. The INJECTION should be a 8 function that takes a DOCUMENT and returns NIL or a STRING for template insertion." 9 (push injection (getf *injections* location))) 10 11 (defun find-injections (content) 12 "Iterate over *INJECTIONS* collecting any that should be added to CONTENT." 13 (flet ((injections-for (location) 14 (loop for injection in (getf *injections* location) 15 collecting (funcall injection content)))) 16 (list :head (injections-for :head) 17 :body (injections-for :body)))) 18 19 (define-condition theme-does-not-exist (error) 20 ((theme :initarg :theme :reader theme)) 21 (:report (lambda (c stream) 22 (format stream "Cannot find the theme: '~A'" (theme c))))) 23 24 (defun theme-package (name) 25 "Find the package matching the theme NAME or signal THEME-DOES-NOT-EXIST." 26 (or (find-package (format nil "~:@(coleslaw.theme.~A~)" name)) 27 (error 'theme-does-not-exist :theme name))) 28 29 (defun theme-fn (name &optional (package (theme *config*))) 30 "Find the symbol NAME inside PACKAGE which defaults to the theme package." 31 (find-symbol (princ-to-string name) (theme-package package))) 32 33 (defun find-theme (theme) 34 "Find the theme prefering themes in the local repo." 35 (let ((local-theme (repo-path "themes/~a/" theme))) 36 (if (probe-file local-theme) 37 local-theme 38 (app-path "themes/~a/" theme)))) 39 40 (defun compile-theme (theme) 41 "Locate and compile the templates for the given THEME." 42 (do-files (file (find-theme theme) "tmpl") 43 (compile-template :common-lisp-backend file)) 44 (do-files (file (app-path "themes/") "tmpl") 45 (compile-template :common-lisp-backend file)))