1
0
Fork 0

[oni-org] Add a dynamic block for showing a Mastodon post as a quote

This commit is contained in:
Tom Willemse 2023-07-28 12:05:49 -07:00
parent ff837f30df
commit 346f4598a5

View file

@ -4,7 +4,7 @@
;; Author: Tom Willemse <tom@ryuslash.org> ;; Author: Tom Willemse <tom@ryuslash.org>
;; Keywords: local ;; Keywords: local
;; Version: 2023.0716.233213 ;; Version: 2023.0728.120517
;; Package-Requires: (oni-yasnippet oni-alert oni-hydra org org-bullets org-edna diminish all-the-icons olivetti form-feed org-pretty-table ob-async) ;; Package-Requires: (oni-yasnippet oni-alert oni-hydra org org-bullets org-edna diminish all-the-icons olivetti form-feed org-pretty-table ob-async)
;; This program is free software; you can redistribute it and/or modify ;; This program is free software; you can redistribute it and/or modify
@ -1285,5 +1285,44 @@ same code all the time."
%:link" %:link"
:immediate-finish t)) :immediate-finish t))
;;; Mastodon post dynamic block
(defun oni-org-dblock-write-mastodon-post (params)
(let* ((url (url-generic-parse-url (map-elt params :url)))
(status-id (car (last (split-string (url-filename url) "/")))))
(setf (url-filename url) (format "/api/v1/statuses/%s" status-id))
(let ((json (with-current-buffer (url-retrieve-synchronously url)
(goto-char (point-min))
(search-forward "\n\n")
(json-parse-buffer))))
(insert (with-temp-buffer
(insert (map-elt json "content"))
(let ((dom (libxml-parse-html-region (point-min) (point-max))))
(erase-buffer)
(insert "#+begin_quote")
(let ((start (point)))
(shr-insert-document dom)
(forward-char -1)
(insert " ---" (map-elt (map-elt json "account") "display_name"))
(org-escape-code-in-region start (point))
(save-excursion
(goto-char start)
(delete-all-space)
(insert "\n"))
(let ((fill-column (point-max)))
(fill-region start (point)))))
(forward-char)
(insert "#+end_quote")
(buffer-string))))))
(defun oni-org-insert-mastodon-post (url)
"Create a dynamic block showing a Mastodon post from URL."
(interactive "MURL: ")
(org-create-dblock `(:name "oni-mastodon-post" :url ,url))
(org-update-dblock))
(defalias 'org-dblock-write:oni-mastodon-post 'oni-org-dblock-write-mastodon-post)
(org-dynamic-block-define "oni-mastodon-post" #'oni-org-insert-mastodon-post)
(provide 'oni-org) (provide 'oni-org)
;;; oni-org.el ends here ;;; oni-org.el ends here