From e13b51cb345e3c8e61bfcb4b612219cf85565659 Mon Sep 17 00:00:00 2001 From: Thanh Vuong Date: Sun, 19 Jul 2020 22:07:33 -0600 Subject: refactor hn-comment to take url --- README.org | 2 +- TODOs.org | 4 ++++ hnreader.el | 69 +++++++++++++++++++++++++++++-------------------------------- 3 files changed, 38 insertions(+), 37 deletions(-) diff --git a/README.org b/README.org index c106bef..6e41a4b 100644 --- a/README.org +++ b/README.org @@ -44,7 +44,7 @@ User must have ~org-mode~ 9.2 or later installed also. - ~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 +- ~hnreader-comment~: read an HN item url such as https://news.ycombinator.com/item?id=1 this is handy when you have the link and want to read it in emacs, takes url as param - ~hnreader-org-insert-hn-link~: insert hn link to org buffer, take url as param diff --git a/TODOs.org b/TODOs.org index 5f4bcb7..120cc69 100644 --- a/TODOs.org +++ b/TODOs.org @@ -121,3 +121,7 @@ extract id then pass to hnreader-comment * DONE add function to insert hn item to org buffer CLOSED: [2020-03-21 Sat 12:48] [[elisp:(hnreader-comment 1)][https://news.ycombinator.com/item?id=1]] +* DONE refactor hnreader-comment to take url instead of id +CLOSED: [2020-07-19 Sun 21:49] +it reads comment id +actually we don't need it just read the url instead diff --git a/hnreader.el b/hnreader.el index 6ef6f12..b9ea432 100644 --- a/hnreader.el +++ b/hnreader.el @@ -5,7 +5,7 @@ ;; Author: Thanh Vuong ;; URL: https://github.com/thanhvg/emacs-hnreader/ ;; Package-Requires: ((emacs "25.1") (promise "1.1") (request "0.3.0") (org "9.2")) -;; Version: 0.1 +;; Version: 0.2 ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by @@ -37,7 +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 +;; hnreader-comment: read an HN item url such as https://news.ycombinator.com/item?id=1 ;;; Customization ;; hnreader-history-max: max number history items to remember. @@ -89,9 +89,6 @@ 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." @@ -155,7 +152,7 @@ third one is 80.") (defun hnreader--print-frontpage-item (thing subtext) "Print THING dom and SUBTEXT dom." - (let ((id (dom-attr thing 'id)) + (let ((url (format "https://news.ycombinator.com/item?id=%s" (dom-attr thing 'id))) (story-link (dom-attr (dom-by-class thing "^storylink$") 'href))) (insert (format "\n* %s %s (%s) [%s]\n" ;; rank @@ -170,9 +167,9 @@ third one is 80.") ;; link (insert (format "%s\n[[eww:%s][view story in eww]]\n" story-link story-link)) ;; comment link - (insert (format "[[elisp:(hnreader-comment %s)][https://news.ycombinator.com/item?id=%s]]" - id - id)))) + (insert (format "[[elisp:(hnreader-comment \"%s\")][%s]]" + url + url)))) (defun hnreader--get-morelink (dom) "Get more link from DOM." @@ -269,11 +266,12 @@ third one is 80.") (shr-use-fonts nil)) (shr-insert-document node))) -(defun hnreader--print-comments (dom id) - "Print DOM comment and ID to buffer." - (let ((comments (dom-by-class dom "^athing comtr $")) +(defun hnreader--print-comments (dom url) + "Print DOM comment and URL to buffer." + (let ((comments (dom-by-class dom "^athing comtr $")) (title (hnreader--get-title dom)) - (info (hnreader--get-post-info dom))) + (info (hnreader--get-post-info dom)) + (more-link (dom-by-class dom "morelink"))) (with-current-buffer (hnreader--get-hn-comment-buffer) (read-only-mode -1) (erase-buffer) @@ -291,7 +289,10 @@ third one is 80.") (hnreader--get-img-tag-width comment)) (hnreader--get-comment-owner comment))) (hnreader--print-node (hnreader--get-comment comment))) - (insert "\n* " (format "[[elisp:(hnreader-comment %s)][Reload]]" id)) + (when more-link + (insert "\n* " (format "[[elisp:(hnreader-comment \"%s\")][More]]" (concat "https://news.ycombinator.com/" + (dom-attr more-link 'href))))) + (insert "\n* " (format "[[elisp:(hnreader-comment \"%s\")][Reload]]" url)) (org-mode) ;; (org-shifttab 3) (goto-char (point-min)) @@ -397,42 +398,38 @@ Also upate `hnreader--history'." (hnreader-read-page hnreader--more-link) (message "no more link."))) -(defun hnreader-promise-comment (comment-id) - "Promise to print hn COMMENT-ID page to buffer." +(defun hnreader-promise-comment (url) + "Promise to print hn URL page to buffer." (hnreader--prepare-buffer (hnreader--get-hn-comment-buffer)) - (promise-chain (hnreader--promise-dom (format "https://news.ycombinator.com/item?id=%s" comment-id)) + (promise-chain (hnreader--promise-dom url) + (then (lambda (dom) + (hnreader--print-comments dom url))) + (promise-catch (lambda (reason) + (message "catch error in promise comments: %s" reason))))) + +(defun hnreader-promise-item (url) + "Promise to print hn URL page to buffer." + (hnreader--prepare-buffer (hnreader--get-hn-comment-buffer)) + (promise-chain (hnreader--promise-dom url) (then (lambda (dom) (hnreader--print-comments dom comment-id))) (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) +(defun hnreader-comment (url) + "Print hn URL page to buffer." + (hnreader-promise-comment url) 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))) - ;;;###autoload (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))))) + (insert (format "[[elisp:(hnreader-comment \"%s\")][%s]]" + url + url)))) (provide 'hnreader) ;;; hnreader.el ends here -- cgit v1.2.3