feat: add spine-render-index and empty-state view functions

This commit is contained in:
2026-06-20 16:59:06 -04:00
parent cc74ca5cfa
commit 94c310a0b3
+101
View File
@@ -120,6 +120,107 @@ Returns nil if the file is missing or cannot be parsed."
spine-org-file (error-message-string err)) spine-org-file (error-message-string err))
nil)))) nil))))
(defun spine-render-index (books &optional selected-id)
"Render the index page HTML for BOOKS, expanding the book matching SELECTED-ID."
(let ((order '("WANT" "READING" "READ" "ABANDONED"))
(grouped (make-hash-table :test 'equal)))
;; Group books by status
(dolist (book books)
(let* ((status (or (plist-get book :status) "WANT")))
(push book (gethash status grouped))))
;; Render
(with-temp-buffer
(insert "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n")
(insert "<meta charset=\"utf-8\">\n")
(insert "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n")
(insert "<title>Spine</title>\n</head>\n<body>\n")
(insert "<div class=\"frame\">\n")
(insert "<div class=\"titlebar\">Spine</div>\n")
;; Each group in order
(dolist (status order)
(let ((group-books (nreverse (gethash status grouped))))
(when group-books
(insert (format "<div class=\"group\">%s (%d)</div>\n"
status (length group-books)))
(dolist (book group-books)
(let ((selected (equal (plist-get book :id) selected-id)))
(insert (format "<div class=\"row%s\">\n"
(if selected " selected" "")))
;; Format glyph
(let ((fmt (plist-get book :format)))
(insert (format "<span class=\"glyph\">%s</span>\n"
(cond ((equal fmt "hardcover") "[H]")
((equal fmt "ebook") "[E]")
((equal fmt "audiobook") "[A]")
(t "")))))
;; Title + author
(insert "<span class=\"title\">")
(insert (spine--h (plist-get book :title)))
(let ((author (plist-get book :author)))
(when author
(insert (format " <span class=\"muted\">%s</span>"
(spine--h author)))))
(insert "</span>\n")
;; Tags
(let ((tags (plist-get book :tags)))
(when tags
(insert (format "<span class=\"tags\">%s</span>\n"
(mapconcat (lambda (t) (concat ":" t ":"))
tags " ")))))
;; Recommendation trail
(let ((rec-by (plist-get book :rec_by))
(rec-note (plist-get book :rec_note)))
(when (and rec-by (> (length rec-by) 0))
(insert
(format "<span class=\"trail\">%s%s</span>\n"
(spine--h rec-by)
(if (and rec-note (> (length rec-note) 0))
(concat ": " (spine--h rec-note))
"")))))
;; Status pill
(let ((status (or (plist-get book :status) "WANT")))
(insert (format "<span class=\"pill %s\">%s</span>\n"
(downcase status) status)))
;; Expanded detail
(when selected
(insert "<div class=\"detail\">\n")
(dolist (note (plist-get book :notes))
(insert (format
"<div class=\"logline\"><span class=\"logdate\">[%s]</span> %s</div>\n"
(spine--h (car note))
(spine--h (cadr note)))))
(let ((rating (plist-get book :rating)))
(when (and rating (> (length rating) 0))
(insert (format "<div>Rating: %s/5</div>\n"
(spine--h rating)))))
(let ((isbn (plist-get book :isbn)))
(when (and isbn (> (length isbn) 0))
(insert (format "<div>ISBN: %s</div>\n"
(spine--h isbn)))))
(let ((fmt (plist-get book :format)))
(when (and fmt (> (length fmt) 0))
(insert (format "<div>Format: %s</div>\n"
(spine--h fmt)))))
(let ((added (plist-get book :added)))
(when (and added (> (length added) 0))
(insert (format "<div>Added: %s</div>\n"
(spine--h added)))))
(insert "</div>\n"))
(insert "</div>\n"))))))
(insert "</div>\n</body>\n</html>\n")
(buffer-string))))
(defun spine-render-empty-state ()
"Render a page indicating no books file was found."
(concat "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n"
"<meta charset=\"utf-8\">\n"
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
"<title>Spine</title>\n</head>\n<body>\n"
"<div class=\"frame\">\n"
"<div class=\"titlebar\">Spine</div>\n"
"<div class=\"row\">No books yet.</div>\n"
"</div>\n</body>\n</html>\n"))
(defvar spine-skip-server-start nil (defvar spine-skip-server-start nil
"When non-nil, do not start the HTTP server. "When non-nil, do not start the HTTP server.
Set by test harnesses that only need the model functions.") Set by test harnesses that only need the model functions.")