Compare commits
No commits in common. "55b2e50a816b90d833e5eb35ef2b2ee76d59b6ad" and "8267c1492c029aaa304d6c59d66403b938adc2a8" have entirely different histories.
55b2e50a81
...
8267c1492c
89
bookstack.el
89
bookstack.el
@ -61,9 +61,9 @@
|
|||||||
"Retrieves the ENDPOINT from the current `bookstack-server' asynchronously,
|
"Retrieves the ENDPOINT from the current `bookstack-server' asynchronously,
|
||||||
executing CALLBACK with the decoded result."
|
executing CALLBACK with the decoded result."
|
||||||
(unless bookstack-token
|
(unless bookstack-token
|
||||||
(user-error "`bookstack-token' must be defined"))
|
(user-error "%s" "`bookstack-token' must be defined"))
|
||||||
(unless bookstack-server
|
(unless bookstack-server
|
||||||
(user-error "`bookstack-server' must be defined"))
|
(user-error "%s" "`bookstack-server' must be defined"))
|
||||||
(let ((url-request-extra-headers
|
(let ((url-request-extra-headers
|
||||||
`(("Authorization" . ,(format "Token %s" bookstack-token))
|
`(("Authorization" . ,(format "Token %s" bookstack-token))
|
||||||
("Content-Type" . "application/json")))
|
("Content-Type" . "application/json")))
|
||||||
@ -84,31 +84,25 @@ executing CALLBACK with the decoded result."
|
|||||||
'utf-8)))
|
'utf-8)))
|
||||||
(bookstack--retrieve (format "pages/%d" id) (lambda (_)))))
|
(bookstack--retrieve (format "pages/%d" id) (lambda (_)))))
|
||||||
|
|
||||||
(defun bookstack--read (prompt items)
|
(defun bookstack--read-page (pages)
|
||||||
"Reads an item from the user with `completing-read', returning its ID. ITEMS
|
"Reads a page from the user with `completing-read', returning its ID.
|
||||||
should be a data sequence of items returned from a listing endpoint such as
|
PAGES should be a sequence of pages (as returned by `bookstack-pages')."
|
||||||
`bookstack-pages'."
|
|
||||||
(let* ((opts (seq-reduce
|
(let* ((opts (seq-reduce
|
||||||
(lambda (acc item)
|
(lambda (acc page)
|
||||||
(push `(,(alist-get 'name item) . ,(alist-get 'id item)) acc))
|
(push `(,(alist-get 'name page) . ,(alist-get 'id page)) acc))
|
||||||
items
|
pages
|
||||||
nil))
|
nil))
|
||||||
(page-name (progn
|
(page-name (progn
|
||||||
(switch-to-minibuffer)
|
(switch-to-minibuffer)
|
||||||
(completing-read prompt opts))))
|
(completing-read "Page: " opts))))
|
||||||
(cdr (assoc page-name opts))))
|
(cdr (assoc page-name opts))))
|
||||||
|
|
||||||
(defun bookstack--read-book (books) (bookstack--read "Book: " books))
|
|
||||||
(defun bookstack--read-chapter (chapters) (bookstack--read "Chapter: " chapters))
|
|
||||||
(defun bookstack--read-page (pages) (bookstack--read "Page: " pages))
|
|
||||||
|
|
||||||
(defun bookstack--edit-page (id)
|
(defun bookstack--edit-page (id)
|
||||||
"Asynchronously loads a page by ID for editing into a new buffer."
|
"Asynchronously loads a page by ID for editing into a new buffer."
|
||||||
(bookstack-page
|
(bookstack-page
|
||||||
id
|
id
|
||||||
(lambda (page)
|
(lambda (page)
|
||||||
(unless page
|
(or page (error "Missing full page '%d'" id))
|
||||||
(error "Missing page '%d'" id))
|
|
||||||
(let-alist page
|
(let-alist page
|
||||||
(let ((buffer (get-buffer-create (format "%s.md" .name)))
|
(let ((buffer (get-buffer-create (format "%s.md" .name)))
|
||||||
;; fix line endings
|
;; fix line endings
|
||||||
@ -121,18 +115,6 @@ should be a data sequence of items returned from a listing endpoint such as
|
|||||||
(switch-to-buffer buffer)
|
(switch-to-buffer buffer)
|
||||||
(bookstack-mode +1))))))
|
(bookstack-mode +1))))))
|
||||||
|
|
||||||
(defun bookstack-books (callback)
|
|
||||||
"Retrieve all books from `bookstack-server'."
|
|
||||||
(bookstack--retrieve
|
|
||||||
"books"
|
|
||||||
(lambda (res) (apply callback (list (alist-get 'data res))))))
|
|
||||||
|
|
||||||
(defun bookstack-chapters (callback)
|
|
||||||
"Retrieve all chapters from `bookstack-server'."
|
|
||||||
(bookstack--retrieve
|
|
||||||
"chapters"
|
|
||||||
(lambda (res) (apply callback (list (alist-get 'data res))))))
|
|
||||||
|
|
||||||
(defun bookstack-pages (callback)
|
(defun bookstack-pages (callback)
|
||||||
"Retrieve all pages from `bookstack-server'."
|
"Retrieve all pages from `bookstack-server'."
|
||||||
(bookstack--retrieve
|
(bookstack--retrieve
|
||||||
@ -152,63 +134,12 @@ should be a data sequence of items returned from a listing endpoint such as
|
|||||||
(bookstack--write-page bookstack--buffer-page-id (buffer-string))
|
(bookstack--write-page bookstack--buffer-page-id (buffer-string))
|
||||||
(message "Wrote %s/.../page/%s" bookstack-server slug)))
|
(message "Wrote %s/.../page/%s" bookstack-server slug)))
|
||||||
|
|
||||||
(defun bookstack--delete-page (id callback &optional force-p)
|
|
||||||
(bookstack-page
|
|
||||||
id
|
|
||||||
(lambda (page)
|
|
||||||
(let ((page-name (alist-get 'name page)))
|
|
||||||
(unless (or force-p (y-or-n-p (format "Really delete %S?" page-name)))
|
|
||||||
(user-error "Aborted"))
|
|
||||||
(let ((url-request-method "DELETE"))
|
|
||||||
(bookstack--retrieve
|
|
||||||
(format "pages/%d" page)
|
|
||||||
(lambda (_) (apply callback page))))))))
|
|
||||||
|
|
||||||
(defun bookstack-delete-page (&optional id force-p)
|
|
||||||
"Delete PAGE and kill its buffers.
|
|
||||||
|
|
||||||
If ID is not specified, defaults to the current buffer's page, if available.
|
|
||||||
|
|
||||||
If FORCE-P, delete without confirmation."
|
|
||||||
(interactive
|
|
||||||
(list bookstack--buffer-page-id
|
|
||||||
current-prefix-arg))
|
|
||||||
(unless (or id bookstack--buffer-page-id)
|
|
||||||
(user-error "Buffer is not visiting a BookStack page"))
|
|
||||||
(bookstack-pages
|
|
||||||
(lambda (pages)
|
|
||||||
(bookstack--delete-page (bookstack--read-page pages) force-p)
|
|
||||||
(lambda (page)
|
|
||||||
(let ((buf (current-buffer)))
|
|
||||||
(kill-buffer buf)
|
|
||||||
(message "Deleted %S" page))))))
|
|
||||||
|
|
||||||
(defun bookstack-delete-this-page (&optional id force-p)
|
|
||||||
"Delete PAGE and kill its buffers.
|
|
||||||
|
|
||||||
If ID is not specified, defaults to the current buffer's page, if available.
|
|
||||||
|
|
||||||
If FORCE-P, delete without confirmation."
|
|
||||||
(interactive
|
|
||||||
(list bookstack--buffer-page-id
|
|
||||||
current-prefix-arg))
|
|
||||||
(unless (or id bookstack--buffer-page-id)
|
|
||||||
(user-error "Buffer is not visiting a BookStack page"))
|
|
||||||
(bookstack-pages
|
|
||||||
(lambda (pages)
|
|
||||||
(bookstack--delete-page (bookstack--read-page pages) force-p)
|
|
||||||
(lambda (page)
|
|
||||||
(let ((buf (current-buffer)))
|
|
||||||
(kill-buffer buf)
|
|
||||||
(message "Deleted %S" page))))))
|
|
||||||
|
|
||||||
(define-minor-mode bookstack-mode
|
(define-minor-mode bookstack-mode
|
||||||
"Minor mode for editing remote BookStack buffers."
|
"Minor mode for editing remote BookStack buffers."
|
||||||
:init-value nil
|
:init-value nil
|
||||||
:lighter " BookStack"
|
:lighter " BookStack"
|
||||||
:keymap (let ((map (make-sparse-keymap)))
|
:keymap (let ((map (make-sparse-keymap)))
|
||||||
(define-key map [remap save-buffer] #'bookstack-save-buffer)
|
(define-key map [remap save-buffer] #'bookstack-save-buffer)
|
||||||
(define-key map [remap delete-file] #'bookstack-delete-page)
|
|
||||||
map))
|
map))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
|
Loading…
Reference in New Issue
Block a user