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

documents.lisp 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. (in-package :coleslaw)
  2. ;;;; The Document Protocol
  3. ;; Data Storage
  4. (defvar *site* (make-hash-table :test #'equal)
  5. "An in-memory database to hold all site documents, keyed on page-url.")
  6. (defun add-document (doc)
  7. "Add DOC to the in-memory database. Error if a matching entry is present."
  8. (let ((url (page-url doc)))
  9. (if (gethash url *site*)
  10. (error "There is already an existing document with the url ~a" url)
  11. (setf (gethash url *site*) doc))))
  12. ;; Class Methods
  13. (defun find-all (doc-type)
  14. "Return a list of all instances of a given DOC-TYPE."
  15. (loop for val being the hash-values in *site*
  16. when (typep val doc-type) collect val))
  17. (defun purge-all (doc-type)
  18. "Remove all instances of DOC-TYPE from memory."
  19. (dolist (obj (find-all doc-type))
  20. (remhash (page-url obj) *site*)))
  21. (defgeneric publish (doc-type)
  22. (:documentation "Write pages to disk for all documents of the given DOC-TYPE."))
  23. (defgeneric discover (doc-type)
  24. (:documentation "Load all documents of the given DOC-TYPE into memory.")
  25. (:method (doc-type)
  26. (let* ((class-name (class-name doc-type))
  27. (file-type (string-downcase (symbol-name class-name))))
  28. (do-files (file (repo *config*) file-type)
  29. (let ((obj (construct class-name (read-content file))))
  30. (add-document obj))))))
  31. (defmethod discover :before (doc-type)
  32. (purge-all (class-name doc-type)))
  33. ;; Instance Methods
  34. (defgeneric page-url (document)
  35. (:documentation "The url to the DOCUMENT without the domain."))
  36. (defmethod page-url :around ((document t))
  37. (let ((result (call-next-method)))
  38. (if (pathname-type result)
  39. result
  40. (make-pathname :type "html" :defaults result))))
  41. (defgeneric render (document &key &allow-other-keys)
  42. (:documentation "Render the given DOCUMENT to HTML."))