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

config.lisp 3.3KB

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