aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-hnreader.el
diff options
context:
space:
mode:
authorYuchen Pei <id@ypei.org>2023-06-17 17:20:29 +1000
committerYuchen Pei <id@ypei.org>2023-06-17 17:20:29 +1000
commit093ffa5fbf7143f4668bb0a3dc9659a5cc836e12 (patch)
tree1ed4e14b2a43b8e338f4ad6a04d969b99b9239be /emacs/.emacs.d/lisp/my/my-hnreader.el
parentabc686827ae38ee715d9eed1c5c29161c74127e6 (diff)
Moving things one level deeper
To ease gnu stow usage. Now we can do stow -t ~ emacs
Diffstat (limited to 'emacs/.emacs.d/lisp/my/my-hnreader.el')
-rw-r--r--emacs/.emacs.d/lisp/my/my-hnreader.el106
1 files changed, 106 insertions, 0 deletions
diff --git a/emacs/.emacs.d/lisp/my/my-hnreader.el b/emacs/.emacs.d/lisp/my/my-hnreader.el
new file mode 100644
index 0000000..4176f8b
--- /dev/null
+++ b/emacs/.emacs.d/lisp/my/my-hnreader.el
@@ -0,0 +1,106 @@
+;;; my-hnreader.el -- Extensions to hnreader -*- lexical-binding: t -*-
+
+;; Copyright (C) 2023 Free Software Foundation.
+
+;; Author: Yuchen Pei <id@ypei.org>
+;; Thanh Vuong <thanhvg@gmail.com>
+;; Maintainer: Yuchen Pei <id@ypei.org>
+;; Package-Requires: ((emacs "28.2"))
+
+;; This file is part of dotfiles.
+
+;; dotfiles is free software: you can redistribute it and/or modify it under
+;; the terms of the GNU Affero General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; dotfiles is distributed in the hope that it will be useful, but WITHOUT
+;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
+;; Public License for more details.
+
+;; You should have received a copy of the GNU Affero General Public
+;; License along with dotfiles. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Extensions to hnreader.
+
+;;; Code:
+
+;; To override `hnreader--print-frontpage-item'
+(defun my-hnreader--print-frontpage-item (thing subtext)
+ "Print THING dom and SUBTEXT dom."
+ (let* ((url (format "https://news.ycombinator.com/item?id=%s" (dom-attr thing 'id)))
+ (a-node (dom-child-by-tag (dom-by-class thing "^titleline$") 'a))
+ (title-link (dom-attr a-node 'href)))
+ (insert (format "\n* %s [[%s][%s]] (%s) [[elisp:(hnreader-comment \"%s\")][%s]]"
+ ;; rank
+ (dom-text (dom-by-class thing "^rank$"))
+ title-link
+ ;; title
+ (dom-text a-node)
+ ;; points
+ (dom-text (dom-by-class subtext "^score$"))
+ ;; comments
+ url
+ (dom-text (last (dom-by-tag subtext 'a)))))
+ ))
+
+
+;; To override `hnreader--print-frontpage'
+(defun my-hnreader--print-frontpage (dom buf url)
+ "Print raw DOM and URL on BUF."
+ (let ((things (dom-by-class dom "^athing$"))
+ (subtexts (dom-by-class dom "^subtext$")))
+ (with-current-buffer buf
+ (read-only-mode -1)
+ (erase-buffer)
+ (insert "#+STARTUP: overview indent\n")
+ (hnreader--print-header)
+ (insert (hnreader--get-route-top-info dom))
+ (cl-mapcar #'hnreader--print-frontpage-item things subtexts)
+ ;; (setq-local org-confirm-elisp-link-function nil)
+ (if hnreader--history
+ (insert "\n* "(format "[[elisp:(hnreader-back)][< Back]]" ) " | ")
+ (insert "\n* "))
+ (insert (hnreader--get-morelink dom) " | ")
+ (insert (format "[[elisp:(hnreader-read-page-back \"%s\")][Reload]]" url) )
+ (org-mode)
+ (goto-char (point-min))
+ (forward-line 2))))
+
+;; To override `hnreader--print-comments'
+(defun my-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))
+ (more-link (dom-by-class dom "morelink")))
+ (with-current-buffer (hnreader--get-hn-comment-buffer)
+ (read-only-mode -1)
+ (erase-buffer)
+ (insert "-*-Org-*-\n")
+ (insert "#+TITLE: " (car title))
+ (insert (format "\n%s\n" (cdr title)))
+ (insert (car info))
+ (when (cdr info)
+ (insert "\n")
+ (shr-insert-document (cdr info)))
+ (dolist (comment comments)
+ (insert (format "%s %s\n"
+ (hnreader--get-indent
+ (hnreader--get-img-tag-width comment))
+ (hnreader--get-comment-owner comment)))
+ (shr-insert-document (hnreader--get-comment comment)))
+ (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))
+ (forward-line 2))))
+
+(provide 'my-hnreader)
+;;; my-hnreader.el ends here