Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions org-web-tools.el
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,22 @@ Pandoc output."
;;;; Commands

;;;###autoload
(defun org-web-tools-insert-link-for-url (url)
(defun org-web-tools-insert-link-for-url (url &optional begin end)
"Insert Org link to URL using title of HTML page at URL.
If URL is not given, look for first URL in `kill-ring'."
(interactive (list (org-web-tools--get-first-url)))
(insert (org-web-tools--org-link-for-url url)))
If URL is not given, look for first URL in `kill-ring'.
If the region is active, use the text in the region as the link text.
With a prefix argument, prompt for the link text."
(interactive (list (string-trim (substring-no-properties (org-web-tools--get-first-url)))
(region-beginning)
(region-end)))
(let* ((link-text (if current-prefix-arg
(read-string "Link text: ")
(if (use-region-p)
(buffer-substring-no-properties begin end)
(org-web-tools--title-for-url url)))))
(if (use-region-p)
(delete-region begin end))
(insert (org-link-make-string url link-text))))

;;;###autoload
(cl-defun org-web-tools-insert-web-page-as-entry (url &key (capture-function #'org-web-tools--url-as-readable-org))
Expand Down Expand Up @@ -330,12 +341,21 @@ outside of it) will be converted."
"Return Org link to URL using title of HTML page at URL.
If URL is not given, look for first URL in `kill-ring'. If page
at URL has no title, return URL."
(if-let ((title (org-web-tools--title-for-url url)))
(org-link-make-string url (org-web-tools--cleanup-title title))
(message "HTML page at URL has no title")
url))

(cl-defun org-web-tools--title-for-url (&optional (url (org-web-tools--get-first-url)))
"Return title of HTML page at URL.
If URL is not given, look for first URL in `kill-ring'. If page
at URL has no title, return nil."
(if-let ((dom (plz 'get url :as (lambda ()
(libxml-parse-html-region (point-min) (point-max)))))
(title (cl-caddr (car (dom-by-tag dom 'title)))))
(org-link-make-string url (org-web-tools--cleanup-title title))
(org-web-tools--cleanup-title title)
(message "HTML page at URL has no title")
url))
nil))

(defun org-web-tools--eww-readable (dom)
"Return \"readable\" part of DOM with title.
Expand Down