diff options
author | Thanh Vuong <thanhvg@gmail.com> | 2020-03-21 12:57:12 -0600 |
---|---|---|
committer | Thanh Vuong <thanhvg@gmail.com> | 2020-03-21 12:58:58 -0600 |
commit | a693a310c9b51ec213bfe9bc57ea821f70df2869 (patch) | |
tree | 860605f0af4d9e01d645cb122ad70bcafa4d1067 /hnreader.el | |
parent | 7e68beff596a7c67ff436be5cc29660acd46f5df (diff) |
new command hnreader-org-insert-hn-link and hnreader-read-item
Diffstat (limited to 'hnreader.el')
-rw-r--r-- | hnreader.el | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/hnreader.el b/hnreader.el index 17ba87d..d227e17 100644 --- a/hnreader.el +++ b/hnreader.el @@ -37,6 +37,7 @@ ;; hnreader-newest: Load new link page. ;; hnreader-more: Load more. ;; hnreader-back: Go back to previous page. +;; hnreader-read-item: read an HN item url such as https://news.ycombinator.com/item?id=1 ;;; Customization ;; hnreader-history-max: max number history items to remember. @@ -88,6 +89,9 @@ third one is 80.") (defvar hnreader--more-link nil "Load more link.") +(defvar hnreader--regex-id "id=\\([0-9]*\\)" + "Regex to capture id in url.") + ;;;###autoload (defun hnreader-back () "Go back to previous location in history." @@ -114,15 +118,15 @@ third one is 80.") (promise-new (lambda (resolve reject) (request url - :parser (lambda () - (goto-char (point-min)) - (while (re-search-forward ">\\*" nil t) - (replace-match ">-")) - (libxml-parse-html-region (point-min) (point-max))) - :error (cl-function (lambda (&rest args &key error-thrown &allow-other-keys) - (funcall reject error-thrown))) - :success (cl-function (lambda (&key data &allow-other-keys) - (funcall resolve data))))))) + :parser (lambda () + (goto-char (point-min)) + (while (re-search-forward ">\\*" nil t) + (replace-match ">-")) + (libxml-parse-html-region (point-min) (point-max))) + :error (cl-function (lambda (&rest args &key error-thrown &allow-other-keys) + (funcall reject error-thrown))) + :success (cl-function (lambda (&key data &allow-other-keys) + (funcall resolve data))))))) (defun hnreader--prepare-buffer (buf &optional msg) "Print MSG message and prepare window for BUF buffer." @@ -402,11 +406,32 @@ Also upate `hnreader--history'." (promise-catch (lambda (reason) (message "catch error in promise comments: %s" reason))))) +;;;###autoload (defun hnreader-comment (comment-id) "Print hn COMMENT-ID page to buffer." (interactive "sComment ID: ") (hnreader-promise-comment comment-id) nil) +;;;###autoload +(defun hnreader-read-item (url) + "Print hn item URL page to buffer." + (interactive "sUrl: ") + (let ((id (if (string-match hnreader--regex-id url) + (match-string 1 url) + "1"))) + (hnreader-comment id))) + +(defun hnreader-org-insert-hn-link (url) + "Insert link in org buffer to open a hn item link" + (interactive "sUrl: ") + (when-let (id (if (string-match hnreader--regex-id url) + (match-string 1 url) + nil)) + (with-current-buffer (current-buffer) + (insert (format "[[elisp:(hnreader-comment %s)][https://news.ycombinator.com/item?id=%s]]" + id + id))))) + (provide 'hnreader) ;;; hnreader.el ends here |