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

config.lisp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. (in-package :coleslaw)
  2. (defclass blog ()
  3. ((author :initarg :author :reader author)
  4. (charset :initarg :charset :reader charset)
  5. (deploy-dir :initarg :deploy-dir :reader deploy-dir)
  6. (domain :initarg :domain :reader domain)
  7. (feeds :initarg :feeds :reader feeds)
  8. (lang :initarg :lang :reader lang)
  9. (license :initarg :license :reader license)
  10. (page-ext :initarg :page-ext :reader page-ext)
  11. (plugins :initarg :plugins :reader plugins)
  12. (repo :initarg :repo :accessor repo-dir)
  13. (routing :initarg :routing :reader routing)
  14. (separator :initarg :separator :reader separator)
  15. (sitenav :initarg :sitenav :reader sitenav)
  16. (staging-dir :initarg :staging-dir :reader staging-dir)
  17. (theme :initarg :theme :reader theme)
  18. (title :initarg :title :reader title))
  19. (:default-initargs
  20. :feeds nil
  21. :license nil
  22. :plugins nil
  23. :sitenav nil
  24. :charset "UTF-8"
  25. :lang "en"
  26. :page-ext "html"
  27. :separator ";;;;;"
  28. :staging-dir "/tmp/coleslaw"))
  29. (defun dir-slot-reader (config name)
  30. "Take CONFIG and NAME, and return a directory pathname for the matching SLOT."
  31. (ensure-directory-pathname (slot-value config name)))
  32. (defmethod deploy-dir ((config blog)) (dir-slot-reader config 'deploy-dir))
  33. (defmethod repo-dir ((config blog)) (dir-slot-reader config 'repo))
  34. (defmethod staging-dir ((config blog)) (dir-slot-reader config 'staging-dir))
  35. (defparameter *config* nil
  36. "A variable to store the blog configuration and plugin settings.")
  37. (define-condition plugin-conf-error ()
  38. ((plugin :initarg :plugin :reader plugin)
  39. (message :initarg :message :reader message))
  40. (:report (lambda (condition stream)
  41. (format stream "~A: ~A" (plugin condition) (message condition))))
  42. (:documentation "Condition to signal when the plugin is misconfigured."))
  43. (defun enable-plugin (name args)
  44. "Given a plugin, NAME, compile+load it and call its ENABLE function with ARGS."
  45. (flet ((plugin-path (sym)
  46. (app-path "plugins/~(~A~)" sym))
  47. (plugin-package (sym)
  48. (format nil "~:@(coleslaw-~A~)" sym)))
  49. (let ((file (plugin-path name)))
  50. (load (compile-file file :verbose nil :print nil) :verbose t))
  51. (let ((package (find-package (plugin-package name))))
  52. (apply (find-symbol "ENABLE" package) args))))
  53. (defun load-plugins (plugins)
  54. "Compile and load the listed PLUGINS. It is expected that matching *.lisp files
  55. are in the plugins folder in coleslaw's source directory."
  56. (setf *injections* nil)
  57. (dolist (plugin plugins)
  58. (destructuring-bind (name &rest args) plugin
  59. (enable-plugin name args))))
  60. (defun discover-config-path (repo-path)
  61. "Check the supplied REPO-PATH for a .coleslawrc and if one
  62. doesn't exist, use the .coleslawrc in the home directory."
  63. (let ((repo-config (rel-path repo-path ".coleslawrc")))
  64. (if (file-exists-p repo-config)
  65. repo-config
  66. (rel-path (user-homedir-pathname) ".coleslawrc"))))
  67. (defun load-config (&optional (repo-dir ""))
  68. "Find and load the coleslaw configuration from .coleslawrc. REPO-DIR will be
  69. preferred over the home directory if provided."
  70. (with-open-file (in (discover-config-path repo-dir) :external-format :utf-8)
  71. (let ((config-form (read in)))
  72. (setf *config* (construct 'blog config-form)
  73. (repo-dir *config*) repo-dir)))
  74. (load-plugins (plugins *config*)))