public/archive-to-org.el

36 lines
1.4 KiB
EmacsLisp
Raw Normal View History

(defun replace-in-string (what with in)
"Return a string with WHAT replaced with WITH in IN."
(replace-regexp-in-string (regexp-quote what) with in nil 'literal))
(defun www-get-page-title (url)
"Get the title of the page at URL."
(with-current-buffer (url-retrieve-synchronously url)
(goto-char 0)
(re-search-forward "<title>\\(.*\\)<[/]title>" nil t 1)
(decode-coding-string (match-string 1) 'utf-8)))
(defun wasp/url-to-org (link-url destination-file)
"Save the content of LINK-URL as an org file in DESTINATION-FILE."
(interactive)
(let ((link-title (www-get-page-title link-url))
(buffer (generate-new-buffer "temp.org")) link-title)
(with-current-buffer buffer
(unless (fboundp 'org-web-tools-insert-web-page-as-entry)
(use-package org-web-tools))
(org-mode)
(org-web-tools-insert-web-page-as-entry link-url)
(write-region (point-min) (point-max) destination-file)
(kill-buffer))))
(defun wasp/archive-url-and-make-entry ()
"Archive an url from clipboard to a file.
Defauls to storing in the Org/web directory asking for file name,
then opening a new entry in the daily journal."
(interactive)
(let ((url (gui-get-selection 'CLIPBOARD))
(destination (concat "~/Org/Web/" (read-string "Enter target file name (without extension):") ".org")))
(wasp/url-to-org url destination)
(find-file destination)))
(global-set-key (kbd "C-c p") 'wasp/archive-url-and-make-entry)