From a301634140208e787d11da6e7147af65c97df1cc Mon Sep 17 00:00:00 2001 From: Levi Neely <141506390+lneely@users.noreply.github.com> Date: Tue, 17 Sep 2024 15:23:37 +0200 Subject: [PATCH] add webpage to collection wrapper/internal --- emacs/module/module-collect.el | 60 ++++++++++------------------------ 1 file changed, 17 insertions(+), 43 deletions(-) diff --git a/emacs/module/module-collect.el b/emacs/module/module-collect.el index 5467927..0595551 100644 --- a/emacs/module/module-collect.el +++ b/emacs/module/module-collect.el @@ -13,6 +13,7 @@ (require 'url) (require 'time-date) +(require 'eww) (defvar collect/store-path "~/collections") @@ -163,37 +164,6 @@ (file-name-nondirectory file-to-add) selected-collection))))) -(defun collect/add-webpage-to-collection () - "Add a webpage to a specified collection." - (interactive) - (let* ((url (read-string "Enter URL to add: ")) - (all-files (directory-files collect/store-path t "^[^.]")) - (collections (cl-remove-if - (lambda (dir) - (or (not (file-directory-p dir)) - (string= (file-name-nondirectory dir) "no-name"))) - all-files)) - (collection-names - (mapcar (lambda (dir) - (let ((metadata-file (expand-file-name ".collection-metadata" dir))) - (if (file-exists-p metadata-file) - (with-temp-buffer - (insert-file-contents metadata-file) - (if (re-search-forward "^name: \\(.*\\)$" nil t) - (match-string 1) - (file-name-nondirectory dir))) - (file-name-nondirectory dir)))) - collections)) - (selected-collection (completing-read "Select collection to add to: " collection-names nil t)) - (target-dir (nth (cl-position selected-collection collection-names :test 'string=) collections))) - (when (and url target-dir) - (plz 'get url - :as 'response - :then (lambda (response) - (collect/process-webpage-response url response target-dir selected-collection)) - :else (lambda (error) - (message "Error fetching webpage: %s" error)))))) - (defun collect/get-collections () "Get a list of collections with their names and directories." (let ((all-dirs (directory-files collect/store-path t "^[^.]"))) @@ -222,19 +192,23 @@ (let* ((url (read-string "Enter URL to add: ")) (collections (collect/get-collections)) (selected-collection (completing-read "Select collection to add to: " (mapcar #'car collections) nil t)) - (target-dir (cdr (assoc selected-collection collections))) - (filename (collect/generate-filename url)) - (target-file (expand-file-name filename target-dir))) + (target-dir (cdr (assoc selected-collection collections)))) (when (and url (file-directory-p target-dir)) - (url-retrieve url - (lambda (status target-file filename collection-name) - (if (plist-get status :error) - (message "Error fetching webpage: %s" (plist-get status :error)) - (goto-char (point-min)) - (re-search-forward "\n\n") - (write-region (point) (point-max) target-file) - (message "Webpage '%s' added to collection '%s'." filename collection-name))) - (list target-file filename selected-collection))))) + (collect/add-webpage-to-collection-internal url target-dir selected-collection)))) + +(defun collect/add-webpage-to-collection-internal (url target-dir collection-name) + "Internal function to add a webpage to a collection." + (let* ((filename (collect/generate-filename url)) + (target-file (expand-file-name filename target-dir))) + (url-retrieve url + (lambda (status target-file filename collection-name) + (if (plist-get status :error) + (message "Error fetching webpage: %s" (plist-get status :error)) + (goto-char (point-min)) + (re-search-forward "\n\n") + (write-region (point) (point-max) target-file) + (message "Webpage '%s' added to collection '%s'." filename collection-name))) + (list target-file filename collection-name)))) (defun collect/generate-filename (url) "Generate a filename for the given URL."