diff options
-rw-r--r-- | README.org | 7 | ||||
-rw-r--r-- | TODOs.org | 7 | ||||
-rw-r--r-- | hnreader.el | 43 |
3 files changed, 45 insertions, 12 deletions
@@ -44,6 +44,10 @@ 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 + 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 * Remarks Listing buffer is called ~*HN*~ Command buffer is called ~*HNComments*~ @@ -129,6 +133,3 @@ Hack to use `insert-sliced-image' to avoid jerky image scrolling." (insert (or alt ""))))) #+end_src -* Demo -TBD - @@ -114,3 +114,10 @@ instead with select-window (select-window (display-buffer buf '(display-buffer-use-some-window))) * DONE add header in list pages +* DONE comment to open comment page +CLOSED: [2020-03-21 Sat 12:34] +(hnreader-read-item "https://news.ycombinator.com/item?id=22603129") +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]] 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 |