From 3773aa5dd3707dd8b9fb3ae556f038dd83678590 Mon Sep 17 00:00:00 2001 From: Willem van den Ende Date: Tue, 3 Nov 2026 11:57:59 +0000 Subject: [PATCH] add firehose blog image drop helper --- config.el | 1 + qwan/firehose-blog.el | 369 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 370 insertions(+) create mode 100644 qwan/firehose-blog.el diff --git a/config.el b/config.el index 88f2872..45a73e4 100644 --- a/config.el +++ b/config.el @@ -55,6 +55,7 @@ (require 'qwan-blog) (require 'qwan-refactor) +(require 'firehose-blog) ;; Whenever you reconfigure a package, make sure to wrap your config in an diff --git a/qwan/firehose-blog.el b/qwan/firehose-blog.el new file mode 100644 index 0000000..b91abb2 --- /dev/null +++ b/qwan/firehose-blog.el @@ -0,0 +1,369 @@ +;;; firehose-blog.el --- Blog helpers for the firehose repo -*- lexical-binding: t; -*- + +;;; Commentary: + +;; Drag-and-drop for firehose blog posts (app/priv/blog///*.md): +;; +;; - Dropping an image file copies it into +;; /app/priv/static/images/blog// and inserts +;; `![alt-text goes here](/images/blog//)' at point. +;; The year comes from the post's own directory; clashing names get a +;; _YYYYMMDD_HHMMSS suffix (and a counter if that still clashes). +;; - Dropping any other file opens it, like the default Emacs behaviour. +;; +;; These are the same conventions implemented by scripts/roam-export-blog.el +;; in the firehose repo for the org-roam export flow. +;; +;; All firehose-specific decisions (destination directory and web path) live +;; in `firehose-blog--drop-target'; the DND machinery around it is +;; site-agnostic, so a Jekyll equivalent only needs its own drop-target +;; function. + +;;; Code: + +(require 'dnd) + +(defgroup firehose-blog nil + "Helpers for writing posts in the firehose repo." + :group 'markdown) + +(defcustom firehose-blog-root "~/dev/elixir/firehose" + "Path to the firehose repository root." + :type 'directory) + +(defcustom firehose-blog-image-extensions + '("svg" "png" "jpg" "jpeg" "gif" "webp" "avif") + "Dropped files with these extensions count as images." + :type '(repeat string)) + +(defcustom firehose-blog-alt-text "alt-text goes here" + "Alt text inserted for dropped images; edited in place afterwards." + :type 'string) + +(defun firehose-blog--stamp () + "Timestamp used to uniquify dropped images with clashing names." + (format-time-string "%Y%m%d_%H%M%S")) + +(defun firehose-blog--post-year (post-file) + "Return the year directory of POST-FILE, or the current year. + +Posts live in app/priv/blog///.md; the immediate +parent directory is used as the year when it is four digits." + (let ((parent (car (nreverse (split-string (file-name-directory post-file) + "/" t))))) + (if (and parent (string-match-p "\\`[0-9]\\{4\\}\\'" parent)) + parent + (format-time-string "%Y")))) + +(defun firehose-blog--image-p (file) + "Return non-nil when FILE looks like an image." + (member (downcase (or (file-name-extension file) "")) + firehose-blog-image-extensions)) + +(defun firehose-blog--web-path (year name) + "Return the web path for NAME stored in the images dir of YEAR." + (format "/images/blog/%s/%s" year name)) + +(defun firehose-blog--unique-dest (dir name) + "Return a destination path for NAME in DIR that does not yet exist. + +When NAME already exists in DIR, a _YYYYMMDD_HHMMSS stamp (and, if +that still clashes, a counter) is inserted before the extension." + (let* ((base (file-name-sans-extension name)) + (ext (file-name-extension name)) + (stamp (firehose-blog--stamp)) + (candidate + (lambda (n) + (expand-file-name + (cond + ((zerop n) name) + ((and (= n 1) ext) (format "%s_%s.%s" base stamp ext)) + ((= n 1) (format "%s_%s" base stamp)) + (ext (format "%s_%s_%d.%s" base stamp (1- n) ext)) + (t (format "%s_%s_%d" base stamp (1- n)))) + dir))) + (dest (funcall candidate 0)) + (count 0)) + (while (file-exists-p dest) + (setq count (1+ count) + dest (funcall candidate count))) + dest)) + +(defun firehose-blog--drop-target (source) + "Return (DEST-FILE . WEB-PATH) for dropping image SOURCE. + +Called from a buffer visiting a firehose blog post; the year comes +from the post's own directory. This is the only firehose-specific +function; the DND machinery is site-agnostic so other blogs (e.g. +Jekyll) can supply their own drop-target." + (let* ((year (firehose-blog--post-year (buffer-file-name))) + (dest (firehose-blog--unique-dest + (expand-file-name + (format "app/priv/static/images/blog/%s" year) + firehose-blog-root) + (file-name-nondirectory source)))) + (cons dest (firehose-blog--web-path year (file-name-nondirectory dest))))) + +(defun firehose-blog--drop-image (source) + "Copy image SOURCE into the firehose static images dir and insert a link." + (let* ((target (firehose-blog--drop-target source)) + (dest (car target)) + (web-path (cdr target))) + (make-directory (file-name-directory dest) t) + (copy-file source dest) + (insert (format "![%s](%s)" firehose-blog-alt-text web-path)))) + +(defun firehose-blog-in-post-p () + "Return non-nil when the current buffer visits a firehose blog post." + (and buffer-file-name + (string-prefix-p (expand-file-name "app/priv/blog/" firehose-blog-root) + (expand-file-name buffer-file-name)))) + +(defun firehose-blog--dnd-file-handler (url &optional _action) + "DND handler for firehose blog posts. +Copy images into the static images dir and insert a web-path link; +open any other dropped file. A URI that cannot be resolved to a +local file is reported in *Messages* instead of opening nothing." + (let ((file (dnd-get-local-file-name url t))) + (cond + ((and file (firehose-blog--image-p file)) + (firehose-blog--drop-image file)) + (file (dnd-open-local-file url nil)) + (t (message "firehose-blog: cannot resolve dropped file URI %S" url) + nil)))) + +(defun firehose-blog--enable-dnd () + "Install the firehose DND handler in the current buffer. + +Only active in buffers visiting files under the firehose blog +tree; other markdown buffers keep the default handlers. Idempotent, +so it can be called from both the mode hook and the retrofit sweep." + (when (and (firehose-blog-in-post-p) + (not (rassq 'firehose-blog--dnd-file-handler dnd-protocol-alist))) + (setq-local dnd-protocol-alist + (cons '("^file:" . firehose-blog--dnd-file-handler) + dnd-protocol-alist)))) + +(defun firehose-blog--enable-dnd-in-existing-buffers () + "Install the firehose DND handler in already-open blog buffers. + +Mode hooks never re-run in existing buffers, so a post opened +before this file was loaded keeps the default handlers without +this sweep." + (dolist (buffer (buffer-list)) + (with-current-buffer buffer + (when (derived-mode-p 'markdown-mode) + (firehose-blog--enable-dnd))))) + +;; markdown-mode installs its own buffer-local `dnd-protocol-alist' in the +;; mode body, which runs before mode hooks, so hooking here makes our +;; handler take precedence in firehose posts. The sweep retrofits posts +;; that were already open when this file was loaded. +(with-eval-after-load 'markdown-mode + (add-hook 'markdown-mode-hook #'firehose-blog--enable-dnd) + (firehose-blog--enable-dnd-in-existing-buffers)) + +(defun firehose-blog--dnd-handle-one-url (fn window action url) + "Dispatch FN for a drop in the buffer of the drop WINDOW. + +`dnd-handle-one-url' consults `dnd-protocol-alist' in the current +buffer, which is not necessarily the buffer the file was dropped +on: dropping on a window while another buffer is current runs the +wrong (or no) buffer-local handler. x-dnd.el never switches to +the drop window's buffer (not even `dnd-insert-text'), so force it +here. This repairs buffer-local drop handlers in general -- ours, +markdown-mode's and org-download's alike." + (if (window-live-p window) + (with-current-buffer (window-buffer window) + (funcall fn window action url)) + (funcall fn window action url))) + +(advice-add 'dnd-handle-one-url :around #'firehose-blog--dnd-handle-one-url) + +(provide 'firehose-blog) +;;; firehose-blog.el ends here + +;;; Tests: + +(require 'ert) +(require 'cl-lib) + +(defvar firehose-blog--test-stamp nil + "Test override for `firehose-blog--stamp'.") + +(defun firehose-blog-test--make-repo () + "Create a fake firehose repo tree; return its root." + (let ((root (make-temp-file "firehose-blog-" t))) + (make-directory (expand-file-name "app/priv/blog/engineering/2026" root) t) + root)) + +(defun firehose-blog-test--post (root) + "Return a post path for the fake repo at ROOT." + (expand-file-name "app/priv/blog/engineering/2026/10-20-test-post.md" root)) + +(defun firehose-blog-test--image (name) + "Create an empty image file called NAME in a temp dir; return its path." + (let ((file (expand-file-name name (make-temp-file "firehose-blog-src-" t)))) + (write-region "" nil file) + file)) + +(defmacro firehose-blog-test--with-fixed-stamp (&rest body) + "Run BODY with `firehose-blog--stamp' pinned to a fixed value." + `(cl-letf (((symbol-function 'firehose-blog--stamp) + (lambda () (or firehose-blog--test-stamp "20260101_000000")))) + ,@body)) + +(ert-deftest firehose-blog-post-year-comes-from-post-directory () + (should (equal (firehose-blog--post-year + "/repo/app/priv/blog/engineering/2026/post.md") + "2026"))) + +(ert-deftest firehose-blog-post-year-falls-back-to-current-year () + (should (equal (firehose-blog--post-year "/repo/app/priv/blog/post.md") + (format-time-string "%Y")))) + +(ert-deftest firehose-blog-image-p-recognises-image-extensions () + (should (firehose-blog--image-p "shot.png")) + (should (firehose-blog--image-p "shot.PNG")) + (should (firehose-blog--image-p "diagram.svg")) + (should-not (firehose-blog--image-p "post.md")) + (should-not (firehose-blog--image-p "no-extension"))) + +(ert-deftest firehose-blog-web-path-uses-images-blog-year () + (should (equal (firehose-blog--web-path "2026" "shot.png") + "/images/blog/2026/shot.png"))) + +(ert-deftest firehose-blog-unique-dest-keeps-name-when-free () + (firehose-blog-test--with-fixed-stamp + (let ((dir (make-temp-file "firehose-blog-dst-" t))) + (should (equal (firehose-blog--unique-dest dir "shot.png") + (expand-file-name "shot.png" dir)))))) + +(ert-deftest firehose-blog-unique-dest-stamps-clashing-name () + (firehose-blog-test--with-fixed-stamp + (let ((dir (make-temp-file "firehose-blog-dst-" t))) + (write-region "" nil (expand-file-name "shot.png" dir)) + (should (equal (firehose-blog--unique-dest dir "shot.png") + (expand-file-name "shot_20260101_000000.png" dir)))))) + +(ert-deftest firehose-blog-unique-dest-counts-up-when-still-clashing () + (firehose-blog-test--with-fixed-stamp + (let ((dir (make-temp-file "firehose-blog-dst-" t))) + (write-region "" nil (expand-file-name "shot.png" dir)) + (write-region "" nil (expand-file-name "shot_20260101_000000.png" dir)) + (should (equal (firehose-blog--unique-dest dir "shot.png") + (expand-file-name "shot_20260101_000000_1.png" dir)))))) + +(ert-deftest firehose-blog-drop-image-copies-into-static-and-inserts-web-link () + (let* ((root (firehose-blog-test--make-repo)) + (source (firehose-blog-test--image "shot.png"))) + (let ((firehose-blog-root root)) + (with-current-buffer (find-file-noselect (firehose-blog-test--post root)) + (firehose-blog--drop-image source) + (should (file-exists-p + (expand-file-name + "app/priv/static/images/blog/2026/shot.png" root))) + (should (equal (buffer-string) + "![alt-text goes here](/images/blog/2026/shot.png)")))))) + +(ert-deftest firehose-blog-drop-image-uniquifies-web-path-on-clash () + (let* ((root (firehose-blog-test--make-repo)) + (source (firehose-blog-test--image "shot.png"))) + (let ((firehose-blog-root root)) + (with-current-buffer (find-file-noselect (firehose-blog-test--post root)) + (firehose-blog-test--with-fixed-stamp + (firehose-blog--drop-image source) + (firehose-blog--drop-image source) + (should (file-exists-p + (expand-file-name + "app/priv/static/images/blog/2026/shot.png" root))) + (should (file-exists-p + (expand-file-name + "app/priv/static/images/blog/2026/shot_20260101_000000.png" + root))) + (should (equal (buffer-string) + (concat "![alt-text goes here](/images/blog/2026/shot.png)" + "![alt-text goes here](/images/blog/2026/shot_20260101_000000.png)")))))))) + +(ert-deftest firehose-blog-in-post-p-accepts-blog-files-only () + (let* ((root (firehose-blog-test--make-repo)) + (readme (expand-file-name "README.md" root))) + (write-region "" nil readme) + (let ((firehose-blog-root root)) + (with-current-buffer (find-file-noselect (firehose-blog-test--post root)) + (should (firehose-blog-in-post-p))) + (with-current-buffer (find-file-noselect readme) + (should-not (firehose-blog-in-post-p)))))) + +(ert-deftest firehose-blog-enable-dnd-installs-handler-in-posts-only () + (let* ((root (firehose-blog-test--make-repo)) + (readme (expand-file-name "README.md" root))) + (write-region "" nil readme) + (let ((firehose-blog-root root)) + (with-current-buffer (find-file-noselect (firehose-blog-test--post root)) + (firehose-blog--enable-dnd) + (should (eq (cdr (car dnd-protocol-alist)) + #'firehose-blog--dnd-file-handler))) + (with-current-buffer (find-file-noselect readme) + (let ((before dnd-protocol-alist)) + (firehose-blog--enable-dnd) + (should (equal dnd-protocol-alist before))))))) + +(defun firehose-blog-test--handler-count () + "Count our handler entries in the current buffer's alist." + (let ((n 0)) + (dolist (entry dnd-protocol-alist n) + (when (eq (cdr entry) 'firehose-blog--dnd-file-handler) + (setq n (1+ n)))))) + +(ert-deftest firehose-blog-enable-dnd-is-idempotent () + (let ((root (firehose-blog-test--make-repo))) + (let ((firehose-blog-root root)) + (with-current-buffer (find-file-noselect (firehose-blog-test--post root)) + (firehose-blog--enable-dnd) + (firehose-blog--enable-dnd) + (should (= 1 (firehose-blog-test--handler-count))))))) + +(ert-deftest firehose-blog-enable-dnd-in-existing-buffers-retrofits-posts () + (let* ((root (firehose-blog-test--make-repo)) + (readme (expand-file-name "README.md" root))) + (write-region "" nil readme) + (let ((firehose-blog-root root) + (post-buffer (find-file-noselect (firehose-blog-test--post root))) + (readme-buffer (find-file-noselect readme))) + (cl-letf (((symbol-function 'derived-mode-p) (lambda (&rest _) t))) + (firehose-blog--enable-dnd-in-existing-buffers) + (with-current-buffer post-buffer + (should (eq (cdr (car dnd-protocol-alist)) + 'firehose-blog--dnd-file-handler))) + (with-current-buffer readme-buffer + (should-not (rassq 'firehose-blog--dnd-file-handler + dnd-protocol-alist))))))) + +(ert-deftest firehose-blog-dnd-file-handler-copies-dropped-image () + (let* ((root (firehose-blog-test--make-repo)) + (source (firehose-blog-test--image "shot.png"))) + (let ((firehose-blog-root root)) + (with-current-buffer (find-file-noselect (firehose-blog-test--post root)) + (firehose-blog-test--with-fixed-stamp + (firehose-blog--dnd-file-handler (concat "file://" source)) + (should (equal (buffer-string) + "![alt-text goes here](/images/blog/2026/shot.png)")) + (should (file-exists-p + (expand-file-name + "app/priv/static/images/blog/2026/shot.png" root)))))))) + +(ert-deftest firehose-blog-dnd-file-handler-reports-unresolvable-uri () + (let ((root (firehose-blog-test--make-repo))) + (let ((firehose-blog-root root)) + (with-current-buffer (find-file-noselect (firehose-blog-test--post root)) + (should-not (firehose-blog--dnd-file-handler "file:///does/not/exist.png")) + (should (equal (buffer-string) "")))))) + +(ert-deftest firehose-blog-dnd-advice-passes-through-without-window () + (let ((called nil)) + (cl-letf (((symbol-function 'dnd-handle-one-url) + (lambda (_window _action _url) (setq called t)))) + (firehose-blog--dnd-handle-one-url (lambda (&rest _) (setq called t)) + 'not-a-window 'copy "file:///x.png") + (should called))))