-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathorg-books.el
321 lines (272 loc) · 12 KB
/
org-books.el
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
;;; org-books.el --- Reading list management with Org mode and helm -*- lexical-binding: t -*-
;; Copyright (C) 2017 Abhinav Tushar
;; Author: Abhinav Tushar <[email protected]>
;; Version: 0.3.0
;; Package-Requires: ((enlive "0.0.1") (s "1.11.0") (helm "2.9.2") (helm-org "1.0") (dash "2.14.1") (org "9.3") (emacs "25"))
;; URL: https://github.com/lepisma/org-books
;; Keywords: outlines
;;; Commentary:
;; org-books.el is a tool for managing reading list in an Org mode file.
;; This file is not a part of GNU Emacs.
;;; License:
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program 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 General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Code:
(require 'cl-lib)
(require 'dash)
(require 'enlive)
(require 'json)
(require 'helm)
(require 'helm-org)
(require 'org)
(require 's)
(require 'subr-x)
(require 'url)
(require 'url-parse)
(defgroup org-books nil
"Org reading list management."
:group 'org)
(defcustom org-books-file nil
"File for keeping reading list."
:type 'file
:group 'org-books)
(defcustom org-books-add-to-top t
"Should add new books as the first item under a heading?"
:type 'boolean
:group 'org-books)
(defcustom org-books-file-depth 2
"The max depth for adding book under headings."
:type 'integer
:group 'org-books)
(defcustom org-books-url-pattern-dispatches
'(("^\\(www\\.\\)?amazon\\." . org-books-get-details-amazon)
("^\\(www\\.\\)?goodreads\\.com" . org-books-get-details-goodreads)
("openlibrary\\.org" . org-books-get-details-isbn))
"Pairs of url patterns and functions taking url and returning
book details. Check documentation of `org-books-get-details' for
return structure from these functions."
:type '(alist :key-type string :value-type symbol)
:group 'org-books)
(defun org-books--get-json (url)
"Parse JSON data from given URL."
(with-current-buffer (url-retrieve-synchronously url)
(goto-char (point-min))
(re-search-forward "^$")
(json-read)))
(defun org-books--clean-str (text)
"Clean TEXT to remove extra whitespaces."
(s-trim (s-collapse-whitespace text)))
(defun org-books-get-details-amazon-authors (page-node)
"Return author names for amazon PAGE-NODE.
PAGE-NODE is the return value of `enlive-fetch' on the page url."
(or (mapcar #'enlive-text (enlive-query-all page-node [.a-section .author .contributorNameID]))
(mapcar #'enlive-text (enlive-query-all page-node [.a-section .author > a]))))
(defun org-books-get-details-amazon (url)
"Get book details from amazon URL."
(let* ((page-node (enlive-fetch url))
(title (org-books--clean-str (enlive-text (enlive-get-element-by-id page-node "productTitle"))))
(author (s-join ", " (org-books-get-details-amazon-authors page-node))))
(if (not (string-equal title ""))
(list title author `(("AMAZON" . ,url))))))
(defun org-books-get-details-goodreads (url)
"Get book details from goodreads URL."
(let* ((page-node (enlive-fetch url))
(title (org-books--clean-str (enlive-text (enlive-get-element-by-id page-node "bookTitle"))))
(author (org-books--clean-str (s-join ", " (mapcar #'enlive-text (enlive-query-all page-node [.authorName > span]))))))
(if (not (string-equal title ""))
(list title author `(("GOODREADS" . ,url))))))
(defun org-books-get-url-from-isbn (isbn)
"Make and return openlibrary url from ISBN."
(concat "https://openlibrary.org/api/books?bibkeys=ISBN:" isbn "&jscmd=data&format=json"))
(defun org-books-get-details-isbn (url)
"Get book details from openlibrary ISBN response from URL."
(let* ((json-object-type 'hash-table)
(json-array-type 'list)
(json-key-type 'string)
(json (org-books--get-json url))
(isbn (car (hash-table-keys json)))
(data (gethash isbn json))
(title (gethash "title" data))
(author (gethash "name" (car (gethash "authors" data)))))
(list title author `(("ISBN" . ,url)))))
(defun org-books-get-details (url)
"Fetch book details from given URL.
Return a list of three items: title (string), author (string) and
an alist of properties to be applied to the org entry. If the url
is not supported, throw an error."
(let ((output 'no-match)
(url-host-string (url-host (url-generic-parse-url url))))
(cl-dolist (pattern-fn-pair org-books-url-pattern-dispatches)
(when (s-matches? (car pattern-fn-pair) url-host-string)
(setq output (funcall (cdr pattern-fn-pair) url))
(cl-return)))
(if (eq output 'no-match)
(error (format "Url %s not understood" url))
output)))
(defun org-books-create-file (file-path)
"Write initialization stuff in a new file at FILE-PATH."
(interactive "FFile: ")
(if (file-exists-p file-path)
(message "There is already a file present, skipping.")
(with-temp-file file-path
(insert "#+TITLE: Reading List\n"
"#+AUTHOR: " (replace-regexp-in-string "" " " user-full-name) "\n\n"
"#+TODO: READING NEXT | READ\n\n"))))
(defun org-books-all-authors ()
"Return a list of authors in the `org-books-file'."
(with-current-buffer (find-file-noselect org-books-file)
(->> (org-property-values "AUTHOR")
(-reduce-from (lambda (acc line) (append acc (s-split "," line))) nil)
(mapcar #'s-trim)
(-distinct)
(-sort #'s-less-p))))
(defun org-books-entry-p ()
"Tell if current entry is an org-books entry."
(if (org-entry-get nil "AUTHOR") t))
(defun org-books-get-closed-time ()
"Return closed time of the current entry."
(let ((ent-body (buffer-substring-no-properties (org-entry-beginning-position) (org-entry-end-position))))
(if (string-match org-closed-time-regexp ent-body)
(parse-time-string (match-string-no-properties 1 ent-body)))))
(defun org-books-map-entries (func &optional match scope &rest skip)
"Similar to `org-map-entries' but only walks on org-books entries.
Arguments FUNC, MATCH, SCOPE and SKIP follow their definitions
from `org-map-entries'."
(with-current-buffer (find-file-noselect org-books-file)
(let ((ignore-sym (gensym)))
(-remove-item ignore-sym
(apply #'org-map-entries
(lambda ()
(if (org-books-entry-p)
(if (functionp func) (funcall func) (funcall (list 'lambda () func)))
ignore-sym))
match scope skip)))))
(defun org-books--get-active-books (&optional todo-keyword)
"Return books that are currently active. Each item returned is
a pair of book name and position of the headline. Activity is
assumed, by default, to be marked by READING TODO state."
(let ((active-todo-keyword "READING"))
(org-books-map-entries
(lambda ()
(cons
(substring-no-properties (org-get-heading) (+ 1 (length (or todo-keyword active-todo-keyword))))
(point)))
(format "TODO=\"%s\"" (or todo-keyword active-todo-keyword)))))
(defun org-books-visit-book-log ()
"Ask to pick a book from currently active one and position
cursor to add log entry."
(let ((active-books (org-books--get-active-books)))
(if (null active-books)
(message "No books active at the moment.")
(let ((picked-book
(helm :sources (helm-build-sync-source "Active books"
:candidates active-books)
:buffer "*helm active books*")))
(find-file org-books-file)
(goto-char picked-book)
(unless (re-search-forward "^*+ Log$" nil t)
(org-insert-heading-after-current)
(org-do-demote)
(insert "Log\n"))))))
;;;###autoload
(defun org-books-cliplink ()
"Clip link from clipboard."
(interactive)
(let ((url (substring-no-properties (current-kill 0))))
(org-books-add-url url)))
;;;###autoload
(defun org-books-add-url (url)
"Add book from web URL."
(interactive "sUrl: ")
(let ((details (org-books-get-details url)))
(if (null details)
(message "Error in fetching url. Please retry.")
(apply #'org-books-add-book details))))
;;;###autoload
(defun org-books-add-isbn (isbn)
"Add book from ISBN."
(interactive "sISBN: ")
(org-books-add-url (org-books-get-url-from-isbn isbn)))
(defun org-books-format (level title author &optional props)
"Return details as an org headline entry.
LEVEL specifies the headline level. TITLE goes as the main text.
AUTHOR and properties from PROPS go as org-property."
(with-temp-buffer
(org-mode)
(insert (make-string level ?*) " " title "\n")
(org-set-property "AUTHOR" author)
(org-set-property "ADDED" (format-time-string "[%Y-%02m-%02d]"))
(dolist (prop props)
(org-set-property (car prop) (cdr prop)))
(buffer-substring-no-properties (point-min) (point-max))))
(defun org-books--insert (level title author &optional props)
"Insert book template at current position in buffer.
Formatting is specified by LEVEL, TITLE, AUTHOR and PROPS as
described in docstring of `org-books-format' function."
(insert (org-books-format level title author props)))
(defun org-books--insert-at-pos (pos title author &optional props)
"Goto POS in current buffer, insert a new entry and save buffer.
TITLE, AUTHOR and PROPS are formatted using `org-books-format'."
(org-content)
(goto-char pos)
(let ((level (or (org-current-level) 0)))
(org-books-goto-place)
(insert "\n")
(org-books--insert (+ level 1) title author props)
(save-buffer)))
(defun org-books-goto-place ()
"Move to the position where insertion should happen."
(if org-books-add-to-top
(let ((level (or (org-current-level) 0))
(bound (save-excursion (org-get-next-sibling))))
(if (re-search-forward (format "^\\*\\{%s\\}" (+ level 1)) bound t)
(forward-line -1)))
(if (org-get-next-sibling)
(forward-line -1)))
(goto-char (line-end-position)))
(defun org-books-get-headers ()
"Return list of categories under which books can be filed.
Each item in list is a pair of title (propertized) and marker
specifying the position in the file."
(let ((helm-org-headings-max-depth org-books-file-depth))
(mapcar (lambda (it)
(cons it (get-text-property 0 'helm-realvalue it)))
(helm-org--get-candidates-in-file org-books-file helm-org-headings-fontify t nil t))))
;;;###autoload
(defun org-books-add-book (title author &optional props)
"Add a book (specified by TITLE and AUTHOR) to the `org-books-file'.
Optionally apply PROPS."
(interactive
(let ((completion-ignore-case t))
(list
(read-string "Book Title: ")
(s-join ", " (completing-read-multiple "Author(s): " (org-books-all-authors))))))
(if org-books-file
(save-excursion
(with-current-buffer (find-file-noselect org-books-file)
(let ((headers (org-books-get-headers)))
(if headers
(helm :sources (helm-build-sync-source "org-book categories"
:candidates (mapcar (lambda (h) (cons (car h) (marker-position (cdr h)))) headers)
:action (lambda (pos) (org-books--insert-at-pos pos title author props)))
:buffer "*helm org-books add*")
(goto-char (point-max))
(org-books--insert 1 title author props)
(save-buffer)))))
(message "org-books-file not set")))
;;;###autoload
(defun org-books-rate-book (rating)
"Apply RATING to book at current point."
(interactive "nRating (stars 1-5): ")
(if (> rating 0)
(org-set-property "RATING" (s-repeat rating ":star:"))))
(provide 'org-books)
;;; org-books.el ends here