-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlon-import.el
334 lines (283 loc) · 13.2 KB
/
tlon-import.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
322
323
324
325
326
327
328
329
330
331
332
333
334
;;; tlon-import.el --- Import functions for Tlön -*- lexical-binding: t -*-
;; Copyright (C) 2024
;; Author: Pablo Stafforini
;; This file is NOT part of GNU Emacs.
;; 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/>.
;;; Commentary:
;; Import functions for Tlön.
;;; Code:
(require 'request)
(require 'tlon-cleanup)
(require 'tlon-fix)
;;;; Variables
;;;;; EAF API
(defconst tlon-import-eaf-api-url
"https://forum.effectivealtruism.org/graphql"
"URL for the EAF GraphQL API endpoint.")
(defconst tlon-import-eaf-objects
'(post tag)
"List of entities supported by the EAF GraphQL API.")
;;;;; Pandoc
(defconst tlon-pandoc-to-markdown
"-t markdown-raw_html-native_divs-native_spans-fenced_divs-bracketed_spans-header_attributes-fenced_code_blocks --wrap=none --strip-comments '%s' -o '%s'"
"Pandoc command to convert HTML to Markdown.")
(defconst tlon-pandoc-convert-from-file
(format "pandoc %s" tlon-pandoc-to-markdown)
"Command to convert from HTML file to Markdown.")
(defconst tlon-pandoc-convert-from-url
(format "pandoc -r html %s" tlon-pandoc-to-markdown)
"Command to convert from URL to Markdown.")
;;;;; Executables
(defvar tlon-pdf2md
(file-name-concat paths-dir-external-repos "pdf2md/lib/pdf2md-cli.js")
"Path to `pdf2md-cli.js' executable.")
(defvar tlon-pdftotext
"pdftotext"
"Path to `pdftotext' executable.")
;;;; Functions
;;;;; General import
(declare-function simple-extras-string-is-url-p "simple-extras")
(defun tlon-import-document ()
"Import a new document from a URL or a PDF file."
(interactive)
(when (or (derived-mode-p 'ebib-entry-mode 'ebib-index-mode)
(y-or-n-p "Ideally, this command should be run from an Ebib buffer. Continue and enter details manually? "))
(let* ((ebib-data (tlon-get-import-details-from-ebib))
(identifier (or (nth 0 ebib-data) (read-string "Identifier (URL or PDF path): ")))
(title (or (nth 1 ebib-data) (read-string "Title: ")))
(key (nth 2 ebib-data)))
(if (simple-extras-string-is-url-p identifier)
(tlon-import-html identifier title)
(tlon-import-pdf (expand-file-name identifier)))
key)))
(declare-function ebib-extras-get-field "ebib-extras")
(declare-function ebib-extras-get-file "ebib-extras")
(declare-function ebib--get-key-at-point "ebib")
(defun tlon-get-import-details-from-ebib ()
"Get the relevant details for importing a document from the current BibTeX entry."
(when (derived-mode-p 'ebib-entry-mode 'ebib-index-mode)
(if-let ((identifier (or (ebib-extras-get-field "url")
(ebib-extras-get-file "md")))
(title (ebib-extras-get-field "title"))
(key (ebib--get-key-at-point)))
(list identifier title key)
(user-error "The current Ebib entry seems to be missing one of the following
fields, which are needed to create a new job: `url' or `file',
`title' and `key'"))))
(defun tlon-import-html (url &optional title)
"Import the HTML in URL and convert it to Markdown.
TITLE optionally specifies the title of the file to be imported."
(if-let ((id-or-slug (tlon-import-eaf-get-id-or-slug-from-identifier url)))
(tlon-import-eaf-html id-or-slug title)
(tlon-import-convert-html-to-markdown url title)))
;; TODO: make it also work with LessWrong
(declare-function tlon-set-file-from-title "tlon")
;; (declare-function delete-tlon-yaml-insert-field "tlon-yaml")
(defun tlon-import-eaf-html (id-or-slug &optional title)
"Import the HTML of EAF entity with ID-OR-SLUG to TARGET and convert it to MD.
TITLE optionally specifies the title of the entity to be imported."
(let* ((response (tlon-import-eaf-request id-or-slug))
(type (tlon-import-eaf-get-type id-or-slug))
(title (or title (pcase type
('article (tlon-import-eaf-get-article-title response))
('tag (tlon-import-eaf-get-tag-title response)))))
(target (tlon-import-set-target title (pcase type
('article "articles")
('tag "tags"))))
(html (pcase type
('article (tlon-import-eaf-get-article-html response))
('tag (tlon-import-eaf-get-tag-html response))))
(html-file (tlon-import-save-html-to-file html)))
(shell-command
(format tlon-pandoc-convert-from-file html-file target))
(with-current-buffer (find-file-noselect target)
(tlon-cleanup-common)
(tlon-cleanup-eaf)
(tlon-autofix-all)
;; (delete-tlon-yaml-insert-field "type" (symbol-name type))
(save-buffer))
(find-file target)))
(defun tlon-import-save-html-to-file (html)
"Save the HTML string HTML to a temporary file."
(let ((filename (make-temp-file "tlon-request-" nil ".html")))
(with-temp-file filename
(insert html))
filename))
(declare-function tlon-select-bare-dir "tlon-counterpart")
(defun tlon-import-convert-html-to-markdown (source &optional title)
"Convert HTML text in SOURCE to Markdown.
SOURCE can be a URL or a file path. If TITLE is not provided, prompt the user
for one."
(let* ((bare-dir (tlon-select-bare-dir "en"))
(target (tlon-import-set-target title bare-dir))
(pandoc (if (simple-extras-string-is-url-p source)
tlon-pandoc-convert-from-url
tlon-pandoc-convert-from-file)))
(shell-command
(format pandoc source target))
(with-current-buffer (find-file-noselect target)
(tlon-cleanup-common)
(tlon-autofix-all)
;; (delete-tlon-yaml-insert-field "type" (substring bare-dir 0 -1))
)
(find-file target)))
(defun tlon-import-set-target (&optional title bare-dir)
"Set the target file path for the imported document.
If TITLE is nil, prompt the user for one. BARE-DIR specifies the bare-dir of
entity being imported (e.g., article or tag)."
(let* ((dir (tlon-repo-lookup :dir :name "uqbar-en")))
(read-string "Save file in: "
(tlon-set-file-from-title title (file-name-concat dir bare-dir)))))
;; TODO: cleanup two functions below
(defun tlon-import-pdf (path &optional title)
"Import the PDF in PATH to TARGET and convert it to Markdown.
This command requires the user to supply values for the header and footer
elements to be excluded from the conversion, which are different for each PDF.
To determine these values, measure the distance between the top/bottom of the
PDF (which will open in the other window) and note the number of pixels until
the end of the header/footer. (You can measure the number of pixels between two
points by taking a screenshot: note the numbers next to the pointer.) Then enter
these values when prompted.
If TITLE is nil, prompt the user for one."
(find-file-other-window path)
(let ((target (read-string "Save file in: " (tlon-set-file-from-title title)))
(header (read-string "Header: "))
(footer (read-string "Footer: ")))
(unless (executable-find "pdftotext")
(user-error "`pdftotext' not found. Please install it (`brew install poppler') and set `tlon-pdftotext' to its path"))
(shell-command (format "'%s' -margint %s -marginb %s '%s' '%s'"
tlon-pdftotext header footer path target))
(find-file target)))
(defun tlon-convert-pdf (source &optional destination)
"Convert PDF in SOURCE to Markdown in DESTINATION.
If DESTINATION is nil, return the Markdown string."
(shell-command-to-string (format "%s '%s' '%s'"
tlon-pdftotext
(expand-file-name source)
(if destination
(expand-file-name destination)
"-"))))
;;;;; EAF API
(defun tlon-import-eaf-article-query (id)
"Return an EA Forum GraphQL query for post whose ID is ID."
(concat "{\"query\":\"{\\n post(\\n input: {\\n selector: {\\n _id: \\\""
id
"\\\"\\n }\\n }\\n ) {\\n result {\\n _id\\n postedAt\\n url\\n canonicalSource\\n title\\n contents {\\n markdown\\n ckEditorMarkup\\n }\\n slug\\n commentCount\\n htmlBody\\n baseScore\\n voteCount\\n pageUrl\\n legacyId\\n question\\n tableOfContents\\n author\\n user {\\n username\\n displayName\\n slug\\n bio\\n }\\n coauthors {\\n _id\\n username\\n displayName\\n slug\\n }\\n }\\n }\\n}\\n\"}"))
(defun tlon-import-eaf-tag-query (slug)
"Return an EA Forum GraphQL query for tag whose slug is SLUG."
(concat "{\"query\":\"{\\n tag(input: { selector: { slug: \\\""
slug
"\\\" } }) {\\n result {\\n name\\n slug\\n description {\\n html\\n }\\n parentTag {\\n name\\n }\\n }\\n }\\n}\\n\"}"))
(defun tlon-import-eaf-request (id-or-slug &optional async)
"Run an EAF request for ID-OR-SLUG.
If ASYNC is t, run the request asynchronously."
(let* ((type (tlon-import-eaf-get-type id-or-slug))
(fun (pcase type
('article 'tlon-import-eaf-article-query)
('tag 'tlon-import-eaf-tag-query)
(_ (error "Invalid type: %S" type))))
(query (funcall fun id-or-slug))
response)
(request
tlon-import-eaf-api-url
:type "POST"
:headers '(("Content-Type" . "application/json"))
:data query
:parser 'json-read
:sync (not async)
:success (cl-function
(lambda (&key data &allow-other-keys)
(setq response (cdr (assoc 'data data)))))
:error (cl-function
(lambda (&rest args &key error-thrown &allow-other-keys)
(message "Error: %S" error-thrown))))
response))
(defun tlon-import-eaf-get-article-result (response)
"Get article details from EA Forum API RESPONSE."
(let* ((article (cdr (assoc 'post response)))
(result (cdr (assoc 'result article))))
result))
(defun tlon-import-eaf-get-article-html (response)
"Get article HTML from EA Forum API RESPONSE."
(let* ((result (tlon-import-eaf-get-article-result response))
(html (cdr (assoc 'htmlBody result))))
html))
(defun tlon-import-eaf-get-article-title (response)
"Get article title from EA Forum API RESPONSE."
(let* ((result (tlon-import-eaf-get-article-result response))
(title (cdr (assoc 'title result))))
title))
(defun tlon-import-eaf-get-tag-result (response)
"Get tag details from EA Forum API RESPONSE."
(let* ((tag (cdr (assoc 'tag response)))
(result (cdr (assoc 'result tag))))
result))
(defun tlon-import-eaf-get-tag-html (response)
"Get tag HTML from EA Forum API RESPONSE."
(let* ((result (tlon-import-eaf-get-tag-result response))
(description (cdr (assoc 'description result)))
(html (cdr (assoc 'html description))))
html))
(defun tlon-import-eaf-get-tag-title (response)
"Get tag title from EA Forum API RESPONSE."
(let* ((result (tlon-import-eaf-get-tag-result response))
(title (cdr (assoc 'name result))))
(tlon-import-eaf-shorten-title title)))
(defun tlon-import-eaf-shorten-title (title)
"Return a shortened version of TITLE."
(string-match "\\([[:alnum:] ,'‘’“”@#$%*\\^`~&\"]*\\)" title)
(match-string 1 title))
;;;;; EAF validation
(defun tlon-eaf-base-regexp (url)
"Return t if URL is an EAF URL, nil otherwise."
(not (not (string-match tlon-eaf-base-regexp url))))
(defun tlon-import-eaf-article-id-p (identifier)
"Return t if IDENTIFIER is a post ID, nil otherwise."
(not (not (string-match (format "^%s$" tlon-eaf-id-regexp) identifier))))
(defun tlon-import-eaf-tag-slug-p (identifier)
"Return t if IDENTIFIER is a tag slug, nil otherwise."
(not (not (string-match (format "^%s$" tlon-eaf-tag-slug-regexp) identifier))))
(defun tlon-import-eaf-get-id-or-slug-from-identifier (identifier)
"Return the EAF post ID or tag slug from IDENTIFIER, if found.
IDENTIFIER can be an URL, a post ID or a tag slug."
(interactive "sURL: ")
(if (simple-extras-string-is-url-p identifier)
(or (tlon-import-eaf-get-id-from-identifier identifier)
(tlon-import-eaf-get-slug-from-identifier identifier))
;; return id or slug if identifier is an id or slug
(pcase identifier
((pred tlon-import-eaf-article-id-p) identifier)
((pred tlon-import-eaf-tag-slug-p) identifier))))
;; TODO: make it work with LW, AF, if it doesn’t already
(defun tlon-import-eaf-get-id-from-identifier (identifier)
"Return the EAF post ID from IDENTIFIER, if found."
(when (or (string-match tlon-eaf-url-post-collection identifier)
(string-match tlon-eaf-url-post-canonical identifier))
(match-string-no-properties 2 identifier)))
(defun tlon-import-eaf-get-slug-from-identifier (identifier)
"Return the EAF tag slug from IDENTIFIER, if found."
(when (string-match (format "^.+?forum.effectivealtruism.org/topics/%s"
tlon-eaf-tag-slug-regexp)
identifier)
(match-string-no-properties 1 identifier)))
(defun tlon-import-eaf-get-type (id-or-slug)
"Return the EAF type in ID-OR-SLUG."
(let ((type (cond ((tlon-import-eaf-article-id-p id-or-slug)
'article)
((tlon-import-eaf-tag-slug-p id-or-slug)
'tag)
(t (user-error "Not an ID or slug: %S" id-or-slug)))))
type))
(provide 'tlon-import)
;;; tlon-import.el ends here