Created
April 18, 2015 17:11
-
-
Save codeout/f26bb562a48642227dc3 to your computer and use it in GitHub Desktop.
Expand a shortened URL into the original
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
(defvar url-pattern "https\?://[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+") | |
(defun expand-url-region (beginning end) | |
"Expand shortened URLs within region." | |
(interactive "r") | |
(save-excursion | |
(goto-char beginning) | |
(setq expanded-text "") | |
(setq cursor beginning) | |
(while (re-search-forward url-pattern end t) | |
(setq beginning-of-url (match-beginning 0)) | |
(setq end-of-url (match-end 0)) | |
(setq expanded-text (concat expanded-text | |
(buffer-substring cursor beginning-of-url) | |
(expand-url (match-string 0)))) | |
(setq cursor end-of-url)) | |
(setq expanded-text (concat expanded-text (buffer-substring (point) end))) | |
(delete-region beginning end) | |
(insert expanded-text))) | |
(defun expand-url (short-url) | |
"Expand a shortened URL into original one." | |
(let ((url-request-method "HEAD")) | |
(with-current-buffer (url-retrieve-synchronously short-url) (url-view-url t)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment