;;; my-hnreader.el -- Extensions to hnreader -*- lexical-binding: t -*- ;; Copyright (C) 2023 Free Software Foundation. ;; Author: Yuchen Pei ;; 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 . ;;; 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--get-title' (defun my-hnreader--get-title (dom) "Get title and link from DOM comment page." (let ((a-link (dom-by-tag (dom-by-class dom "^titleline$") 'a))) (cons (dom-text a-link) (dom-attr a-link 'href)))) ;; 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")) (buffer (my-hnreader-buffer url))) (with-current-buffer 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)) (goto-char (point-min)) (forward-line 2) (save-buffer)) (switch-to-buffer buffer))) (defvar my-hnreader-save-dir (locate-user-emacs-file "hn-archive") "The directory to save hnreader org files.") (defun my-hnreader-buffer (url) "Return file name to save to for URL." (if (string-match "https://news\\.ycombinator\\.com/item\\?id=\\([0-9]+\\)" url) (find-file-noselect (expand-file-name (format "%s.org" (match-string 1 url)) my-hnreader-save-dir)) (error "URL %s not supported." url))) (provide 'my-hnreader) ;;; my-hnreader.el ends here