arcology-elisp/arcology-util.el

118 lines
4.5 KiB
EmacsLisp

;;; arcology-util.el --- Interactive utilities for Arcology
;; Copyright (C) 2016 Ryan Rix
;; Author: Ryan Rix <ryan@whatthefuck.computer>
;; Version: 0.1
;; Package-Requires: ((json "0.1") (org "8.0"))
;; Keywords: web
;; URL: http://notes.whatthefuck.computer/
;;; Commentary:
;;; Code:
(defun arcology-show-mentions ()
"Show webmention data for my domain."
(interactive)
(let ((req (request (format
"http://webmention.io/api/mentions?perPage=20&domain=notes.whatthefuck.computer&token=%s"
arcology-webmention.io-api-key)
:type "GET"
:parser 'json-read
:sync t)))
(let ((output (mapconcat
(lambda (mention)
(let ((sentence (arcology-alist-get
'sentence_html
(arcology-alist-get 'activity mention)))
(url (arcology-alist-get
'url
(arcology-alist-get 'data mention)))
(content (or (arcology-alist-get
'content
(arcology-alist-get 'data mention))
"No Content")))
(format "<li><a href=\"%s\">URL</a> &rarr; %s &rarr; %s </li>" url content sentence)))
(arcology-alist-get 'links (request-response-data req))
"<br/>")))
(with-temp-buffer
(let ((filename (expand-file-name "~/tmp/arcology-mentions.html")))
(insert (format "<html><head><title>*Arcology Webmentions*</title</head><body><ul>%s</ul></body></html>" output))
(write-file filename)
(eww (format "file://%s" filename)))))))
(defun arcology-like-link (url)
(interactive "MURL? ")
(let ((org-capture-templates '(("A" "Arcology like of"
entry
(file org-default-notes-file)
"* Ryan likes %c :LIKE:EXPORT:
:PROPERTIES:
:U-LIKE-OF: %c
:END:
%U
"))))
(kill-new url)
(org-capture nil "A")))
(defun arcology-reply-to-link (url)
(interactive "MURL? ")
(let ((org-capture-templates '(("A" "Arcology reply to"
entry
(file org-default-notes-file)
"* %? :REPLY:EXPORT:
:PROPERTIES:
:U-IN-REPLY-TO: %c
:END:
%U
"))))
(kill-new url)
(org-capture nil "A")))
(add-to-list 'org-capture-templates `("_" "Published shortnote" entry (file ,(expand-file-name "~/Code/notes/index.org"))
"* %? :EXPORT:\n%U\n%a\n" :prepend t))
(defun arcology-web-mention (source target)
(interactive "MSource: \nMTarget")
(let* ((url-request-data (format "source=%s&target=%s" source target))
(url-request-method "POST")
(webmention-url (arcology-extract-webmention-endpoint target)))
(when webmention-url
(with-current-buffer (url-retrieve-synchronously webmention-url)
(buffer-string)))))
(defun arcology-extract-webmention-endpoint (url)
(let ((url-request-method "GET"))
(with-current-buffer (url-retrieve-synchronously url)
(goto-char url-http-end-of-headers)
(let ((start (search-forward-regexp "<link rel=\"webmention\" href=\""))
(end (search-forward "\"")))
(buffer-substring start (- end 1))))))
(defun arcology-send-web-mentions-for-post (point)
(interactive "d")
(save-excursion
(let* (urls
(properties (org-entry-properties point))
(base-url (plist-get arcology-publish-config :html-link-home))
(next-heading (save-excursion (outline-next-heading) (point)))
(permalink (alist-get "RSS_PERMALINK" properties nil nil 'equal)))
(org-back-to-heading)
(condition-case error
(while (re-search-forward "\\[\\[\\(http.[^\]]+\\)\\]\\[[^\]]+\\]\\]" next-heading)
(add-to-list 'urls (match-string-no-properties 1)))
(error nil))
(let ((in-reply-to (alist-get "U-IN-REPLY-TO" properties nil nil 'equal)))
(when in-reply-to
(add-to-list 'urls in-reply-to)))
(dolist (url urls)
(arcology-web-mention (concat base-url "/" permalink) url)))))
; Embed nextcloud direct export links as images
(add-to-list 'org-html-inline-image-rules '("https" . "/s/[A-Za-z0-9]+/preview"))
(provide 'arcology-util)
;;; arcology-util.el ends here