aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-hnreader.el
blob: 4176f8bf98ab684bd69b11264c6c99f5642abdce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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