Last active
September 8, 2015 18:45
-
-
Save rexim/8883151 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Copyright (c) 2014 Alexey Kutepov a.k.a. rexim | |
;; Permission is hereby granted, free of charge, to any person | |
;; obtaining a copy of this software and associated documentation files | |
;; (the "Software"), to deal in the Software without restriction, | |
;; including without limitation the rights to use, copy, modify, merge, | |
;; publish, distribute, sublicense, and/or sell copies of the Software, | |
;; and to permit persons to whom the Software is furnished to do so, | |
;; subject to the following conditions: | |
;; The above copyright notice and this permission notice shall be | |
;; included in all copies or substantial portions of the Software. | |
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
;; SOFTWARE. | |
(defun straight-string (s) | |
(mapconcat '(lambda (x) x) (split-string s) " ")) | |
(defun extract-title-from-html (html) | |
(let ((start (string-match "<title>" html)) | |
(end (string-match "</title>" html)) | |
(chars-to-skip (length "<title>"))) | |
(if (and start end (< start end)) | |
(substring html (+ start chars-to-skip) end) | |
nil))) | |
(defun prepare-cliplink-title (title) | |
(let ((replace-table '(("\\[" . "{") | |
("\\]" . "}") | |
("—" . "—"))) | |
(max-length 77) | |
(result (straight-string title))) | |
(dolist (x replace-table) | |
(setq result (replace-regexp-in-string (car x) (cdr x) result))) | |
(when (> (length result) max-length) | |
(setq result (concat (substring result 0 max-length) "..."))) | |
result)) | |
(defun perform-cliplink (buffer url content) | |
(let* ((decoded-content (decode-coding-string content 'utf-8)) | |
(title (prepare-cliplink-title | |
(extract-title-from-html decoded-content)))) | |
(with-current-buffer buffer | |
(insert (format "[[%s][%s]]" url title))))) | |
(defun cliplink () | |
(interactive) | |
(let ((dest-buffer (current-buffer)) | |
(url (substring-no-properties (current-kill 0)))) | |
(url-retrieve | |
url | |
`(lambda (s) | |
(perform-cliplink ,dest-buffer ,url | |
(buffer-string)))))) | |
(global-set-key (kbd "C-x p i") 'cliplink) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Almost a year ago I decided to move this code out of my personal emacs config and create org-cliplink. Currently, it has much more features than the code in the post. Any bug reports and PRs are welcome.