Add function to create a note from a URL

This commit is contained in:
Bram Schoenmakers 2023-09-29 21:47:56 +02:00
parent b6d0f96a40
commit 9a76d39524
2 changed files with 93 additions and 0 deletions

View file

@ -727,6 +727,55 @@ Based on an answer at the [[https://emacs.stackexchange.com/a/77480/34645][Emacs
(narrow-to-region (point-min) (point)))
#+end_src
* Create note with Denote from a URL :emacs:
#+begin_src elisp :tangle gists/denote-create-note-url.el
(defconst my/termux-p (getenv "ANDROID_ROOT"))
(defun my/clipboard-get ()
"Return the text on the system clipboard.
This function treats Termux systems differently, the clipboard is only
accessible through the termux-clipboard-get commandline interface,
part of the the termux-api package."
(if my/termux-p
(shell-command-to-string "termux-clipboard-get")
(current-kill 0)))
(defun my/get-url-title (url)
"Attempt to retrieve the title string from the given URL.
Assuming the URL points to an HTML source.
Returns nil if there is a non-200 return status or no title could
be found."
(let* ((command (format "curl --fail --silent %s" url))
(html (shell-command-to-string command))
(regexp (rx (seq "<title>"
(group (+ (not (any "<" ">"))))
"</title>")))
(matches (string-match regexp html))
(suggested-title (match-string 1 html)))
(read-string (format-prompt "Title" "") suggested-title t)))
(defun my/denote/url (url)
"Create a new Org-based note based on an URL.
Use the URL on the clipboard if there is one. Then, the title
is retrieved from the HTML source to suggest the title for the
note."
(interactive (list
(let* ((clipboard (my/clipboard-get))
(clipboard-url (when (org-url-p clipboard) clipboard)))
(read-string (format-prompt "URL" "")
clipboard-url
t))))
(denote (my/get-url-title url) (denote-keywords-prompt) 'org (denote-subdirectory-prompt))
(org-set-property "URL" url))
#+end_src
* Meta
** License

View file

@ -0,0 +1,44 @@
(defconst my/termux-p (getenv "ANDROID_ROOT"))
(defun my/clipboard-get ()
"Return the text on the system clipboard.
This function treats Termux systems differently, the clipboard is only
accessible through the termux-clipboard-get commandline interface,
part of the the termux-api package."
(if my/termux-p
(shell-command-to-string "termux-clipboard-get")
(current-kill 0)))
(defun my/get-url-title (url)
"Attempt to retrieve the title string from the given URL.
Assuming the URL points to an HTML source.
Returns nil if there is a non-200 return status or no title could
be found."
(let* ((command (format "curl --fail --silent %s" url))
(html (shell-command-to-string command))
(regexp (rx (seq "<title>"
(group (+ (not (any "<" ">"))))
"</title>")))
(matches (string-match regexp html))
(suggested-title (match-string 1 html)))
(read-string (format-prompt "Title" "") suggested-title t)))
(defun my/denote/url (url)
"Create a new Org-based note based on an URL.
Use the URL on the clipboard if there is one. Then, the title
is retrieved from the HTML source to suggest the title for the
note."
(interactive (list
(let* ((clipboard (my/clipboard-get))
(clipboard-url (when (org-url-p clipboard) clipboard)))
(read-string (format-prompt "URL" "")
clipboard-url
t))))
(denote (my/get-url-title url) (denote-keywords-prompt) 'org (denote-subdirectory-prompt))
(org-set-property "URL" url))