aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThanh Vuong <thanhvg@gmail.com>2019-08-18 22:31:54 -0600
committerThanh Vuong <thanhvg@gmail.com>2019-08-18 22:31:54 -0600
commitb1f5f7fbaf04126a580d556607ea273940eacb00 (patch)
tree9314ba2f877b65a7b6109e73c74dccd9fac61eb6
parent8ea1ee23488db42092ae7f7b320a7301f4f4d5e1 (diff)
wip prototype
-rw-r--r--TODOs.org11
-rw-r--r--hnreader.el48
2 files changed, 59 insertions, 0 deletions
diff --git a/TODOs.org b/TODOs.org
index 491f55e..480852f 100644
--- a/TODOs.org
+++ b/TODOs.org
@@ -6,3 +6,14 @@ also the data layout is already available at hn webiste, one curl call and
then parse them to org would be faster
* parse front page
now what
+* parse comments
+ <table border="0" class='comment-tree'>
+ <tr class='athing comtr ' id='20724657'>
+comment are same level in regard to html node
+but child comments have different ident block width
+
+top level comment
+ <td class='ind'><img src="s.gif" height="1" width="0"></td>
+child
+
+ <td class='ind'><img src="s.gif" height="1" width="40"></td>
diff --git a/hnreader.el b/hnreader.el
index 4863402..57e8047 100644
--- a/hnreader.el
+++ b/hnreader.el
@@ -18,6 +18,12 @@
"Get hn commnet buffer."
(get-buffer-create hn--comment-buffer))
+(defvar hnreader--indent 40
+ "Tab value which is the width of an indent.
+top level commnet is 0 indent
+second one is 40
+third one is 80.")
+
(defun hnreader--promise-dom (url)
"Promise (url . dom) from URL with curl."
(promise-new
@@ -70,6 +76,40 @@
(cl-mapcar #'hnreader--print-frontpage-item things subtexts)
(org-mode))))
+(defun hnreader--print-comments (dom)
+ "Print DOM comment page to buffer."
+ (let ((comments (dom-by-class dom "^athing comtr $")))
+ (with-current-buffer (hnreader--get-hn-comment-buffer)
+ (read-only-mode -1)
+ (erase-buffer)
+ (dolist (comment comments)
+ ;; (setq thanh comment)
+ (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))))))
+
+(defun hnreader--get-img-tag-width (comment-dom)
+ "Get width attribute of img tag in COMMENT-DOM."
+ (string-to-number
+ (dom-attr (dom-by-tag (dom-by-class comment-dom "^ind$") 'img)
+ 'width)))
+
+(defun hnreader--get-indent (width)
+ "Return headline star string from WIDTH of img tag."
+ (let ((stars "\n*")) (dotimes (_ (round (/ width hnreader--indent)) stars)
+ (setq stars (concat stars "*")))))
+
+(defun hnreader--get-comment-owner (comment-dom)
+ "Return user who wrote this COMMENT-DOM."
+ (dom-text (dom-by-class comment-dom "^hnuser$")))
+
+(defun hnreader--get-comment (comment-dom)
+ "Get comment dom from COMMENT-DOM."
+ (dom-by-class comment-dom "^commtext"))
+
(defun hnreader-frontpage ()
"Testing."
(hnreader--prepare-buffer (hnreader--get-hn-buffer))
@@ -79,4 +119,12 @@
(promise-catch (lambda (reason)
(message "catch error in promise prontpage: %s" reason)))))
+(defun hnreader-comment (comment-id)
+ "Print hn COMMENT-ID 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))
+ (then #'hnreader--print-comments)
+ (promise-catch (lambda (reason)
+ (message "catch error in promise comments: %s" reason)))))
+
(provide 'hnreader)