Flexible Lisp Blogware. Fork for personal use. Mirrored from https://github.com/kingcons/coleslaw originally.

config.lisp 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. (in-package :coleslaw)
  2. (defclass blog ()
  3. ((author :initarg :author :initform "" :accessor author)
  4. (deploy :initarg :deploy :initform nil :accessor deploy)
  5. (domain :initarg :domain :initform "" :accessor domain)
  6. (feeds :initarg :feeds :initform nil :accessor feeds)
  7. (license :initarg :license :initform nil :accessor license)
  8. (plugins :initarg :plugins :initform nil :accessor plugins)
  9. (repo :initarg :repo :initform #p"/" :accessor repo)
  10. (sitenav :initarg :sitenav :initform nil :accessor sitenav)
  11. (staging :initarg :staging :initform #p"/tmp/coleslaw/" :accessor staging)
  12. (title :initarg :title :initform "" :accessor title)
  13. (theme :initarg :theme :initform "hyde" :accessor theme)))
  14. (define-condition unknown-config-section-error (error)
  15. ((text :initarg :text :reader text)))
  16. (defparameter *config* nil
  17. "A variable to store the blog configuration and plugin settings.")
  18. (defun enable-plugin (file &rest args)
  19. "Given a path to a plugin, FILE, compile+load it, then call its ENABLE function."
  20. (compile-file file)
  21. (load file)
  22. (let* ((pkgname (format nil "coleslaw-~a" (pathname-name file)))
  23. (plugin-pkg (find-package (string-upcase pkgname))))
  24. (apply (find-symbol "ENABLE" plugin-pkg) args)))
  25. (defun load-plugins (plugins)
  26. "Compile and load the listed PLUGINS. It is expected that matching *.lisp files
  27. are in the plugins folder in coleslaw's source directory."
  28. (flet ((plugin-path (sym)
  29. (app-path "plugins/~a" (string-downcase (symbol-name sym)))))
  30. (dolist (plugin plugins)
  31. (etypecase plugin
  32. (list (destructuring-bind (name &rest args) plugin
  33. (apply 'enable-plugin (plugin-path name) args)))
  34. (symbol (enable-plugin (plugin-path plugin)))))))
  35. (defun load-config (config-key &optional (dir (user-homedir-pathname)))
  36. "Load the coleslaw configuration for CONFIG-KEY from DIR/.coleslawrc. DIR is ~ by default."
  37. (with-open-file (in (merge-pathnames ".coleslawrc" dir))
  38. (let ((config-form (read in)))
  39. (if (symbolp (car config-form))
  40. ;; Single site config: ignore CONFIG-KEY.
  41. (setf *config* (apply #'make-instance 'blog config-form))
  42. ;; Multi-site config: load config section for CONFIG-KEY.
  43. (let* ((config-key-pathname (cl-fad:pathname-as-directory config-key))
  44. (section (assoc config-key-pathname config-form
  45. :key #'(lambda (str) (cl-fad:pathname-as-directory str))
  46. :test #'equal)))
  47. (if section
  48. (progn
  49. (setf *config* (apply #'make-instance 'blog (cdr section)))
  50. (setf (slot-value *config* 'repo) config-key))
  51. (error 'unknown-config-section-error
  52. :text (format nil "In ~A: No such key: '~A'." in config-key)))))
  53. (load-plugins (plugins *config*)))))