No books yet.
\n" ";;; spine.el — personal reading tracker, served from Emacs ;; --- package bootstrap ------------------------------------------------ (require 'package) (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t) (package-initialize) (dolist (pkg '(simple-httpd mustache ht)) (unless (package-installed-p pkg) (package-refresh-contents) (package-install pkg))) (require 'simple-httpd) (require 'mustache) (require 'ht) (require 'cl-lib) ;; --- configuration ---------------------------------------------------- (defvar spine-port 8080 "Port for the Spine HTTP server.") (defvar spine-host nil "Host for the Spine HTTP server. nil means localhost only. Set to a string like \"0.0.0.0\" to listen on all interfaces.") (defvar spine-org-file "~/spine/books.org" "Path to the spine.org data file.") (defvar spine-template-dir (expand-file-name "templates/" (file-name-directory load-file-name)) "Directory containing .mustache templates.") ;; --- template rendering ----------------------------------------------- (defun spine-render (name context) "Render templates/NAME.mustache with CONTEXT (an ht hash table). Returns the rendered string." (mustache-render (with-temp-buffer (insert-file-contents (expand-file-name name spine-template-dir)) (buffer-string)) context)) (defun spine--h (text) "HTML-escape TEXT." (when text (let ((s text)) (setq s (replace-regexp-in-string "&" "&" s)) (setq s (replace-regexp-in-string "<" "<" s)) (setq s (replace-regexp-in-string ">" ">" s)) (setq s (replace-regexp-in-string "\"" """ s)) s))) (defun spine-index-model (books &optional selected-id) "Build the view model ht for templates/index.mustache from BOOKS. SELECTED-ID expands the matching book's detail section." (let* ((status-labels '(("WANT" . "on deck") ("READING" . "reading") ("READ" . "read") ("ABANDONED" . "abandoned"))) (format-icons '(("hardcover" . "ti-book") ("ebook" . "ti-device-tablet") ("audiobook" . "ti-headphones"))) (order '("WANT" "READING" "READ" "ABANDONED")) (grouped (make-hash-table :test 'equal)) (total (length books)) (reading-count 0)) (dolist (book books) (when (equal "READING" (plist-get book :status)) (cl-incf reading-count))) (dolist (book books) (let ((status (or (plist-get book :status) "WANT"))) (push book (gethash status grouped)))) (let ((groups nil)) (dolist (status order) (let ((group-books (nreverse (gethash status grouped)))) (when group-books (let ((book-models nil)) (dolist (book group-books) (let* ((id (plist-get book :id)) (selected (equal id selected-id)) (fmt (plist-get book :format)) (rating (plist-get book :rating)) (notes (plist-get book :notes)) (status (or (plist-get book :status) "WANT")) (model (ht ("format_icon" (or (cdr (assoc fmt format-icons)) "")) ("status_class" (downcase status)) ("status_label" (downcase status)) ("title" (plist-get book :title)) ("author" (or (plist-get book :author) "")) ("tags_inline" (let ((tags (plist-get book :tags))) (when tags (mapconcat (lambda (tag) (concat ":" tag ":")) tags " ")))) ("rec_via" (plist-get book :rec_by)) ("rating_display" (when (and rating (> (length rating) 0)) (make-string (string-to-number rating) ?\u2605))) ("selected" selected) ("detail" (when selected (ht ("meta" (let ((added (plist-get book :added)) (fmt (plist-get book :format))) (cond ((and added fmt) (format "started %s · %s" added fmt)) (added (format "started %s" added)) (fmt (format "format: %s" fmt)) (t "")))) ("notes" (when notes (cl-loop for (date text) in notes collect (ht ("date" (format "[%s]" date)) ("text" text))))))))))) (push model book-models))) (push (ht ("label" (cdr (assoc status status-labels))) ("count" (length group-books)) ("books" (nreverse book-models))) groups))))) (ht ("app_title" "spine") ("total_books" total) ("reading_count" reading-count) ("groups" (nreverse groups)))))) (defun spine--extract-notes (hl-begin hl-end) "Extract reading notes from headline body between HL-BEGIN and HL-END. Returns list of (date-string text-string) pairs." (let ((notes nil)) (save-excursion (goto-char hl-begin) ;; Skip past PROPERTIES and LOGBOOK drawers (while (re-search-forward "^[ \t]*:\\(PROPERTIES\\|LOGBOOK\\):" hl-end t) (re-search-forward "^[ \t]*:END:" hl-end t)) ;; Collect note lines: "- [YYYY-MM-DD] text" (while (re-search-forward "^- \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\)\\] \\(.*\\)" hl-end t) (push (list (match-string 1) (match-string 2)) notes))) (nreverse notes))) (defun spine--prop (pos prop) "Get Org property PROP at headline position POS. Returns nil for empty or missing properties." (let ((val (save-excursion (goto-char pos) (org-entry-get nil prop)))) (and val (> (length val) 0) val))) (defun spine-books () "Return a list of plists, one per top-level headline in `spine-org-file'. Returns nil if the file is missing or cannot be parsed." (if (not (file-exists-p spine-org-file)) (progn (message "spine-books: file not found: %s" spine-org-file) nil) (condition-case err (with-temp-buffer (insert-file-contents spine-org-file) (org-mode) (let ((books nil)) (org-element-map (org-element-parse-buffer 'headline) 'headline (lambda (hl) (when (= (org-element-property :level hl) 1) (let ((pos (org-element-property :begin hl))) (push (list :id (spine--prop pos "ID") :title (org-element-property :title hl) :status (org-element-property :todo-keyword hl) :author (spine--prop pos "AUTHOR") :isbn (spine--prop pos "ISBN") :cover (spine--prop pos "COVER") :format (spine--prop pos "FORMAT") :rec_by (spine--prop pos "REC_BY") :rec_note (spine--prop pos "REC_NOTE") :rating (spine--prop pos "RATING") :added (spine--prop pos "ADDED") :tags (org-element-property :tags hl) :notes (spine--extract-notes (org-element-property :begin hl) (org-element-property :end hl))) books))))) (nreverse books))) (error (message "spine-books: failed to parse %s: %s" spine-org-file (error-message-string err)) nil)))) (defun spine-render-empty-state () "Render a page indicating no books file was found." (concat "\n\n
\n" "\n" "\n" "No books yet.
\n" "