-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinline-anki-anki-editor-fork.el
399 lines (346 loc) · 16.8 KB
/
inline-anki-anki-editor-fork.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
;;; inline-anki-anki-editor-fork.el --- Functions from anki-editor -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2018 Lei Tan <[email protected]>
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'cl-lib)
(require 'dash)
(require 'json)
(require 'org-element)
(require 'ox)
(require 'ox-html)
(require 'request)
(defcustom inline-anki-break-consecutive-braces-in-latex
nil
"If non-nil, consecutive `}' will be automatically separated by spaces to prevent early-closing of cloze.
See https://apps.ankiweb.net/docs/manual.html#latex-conflicts."
:type 'boolean
:group 'inline-anki)
(defcustom inline-anki-protected-tags
'("marked" "leech")
"A list of tags that won't be deleted from Anki even though they're absent in Org entries, such as special tags `marked', `leech'."
:type '(repeat string)
:group 'inline-anki)
(defcustom inline-anki-ignored-org-tags
(append org-export-select-tags org-export-exclude-tags)
"A list of Org tags that are ignored when constructing notes from entries."
:type '(repeat string)
:group 'inline-anki)
(defcustom inline-anki-anki-connect-listening-address
"127.0.0.1"
"The network address AnkiConnect is listening."
:type 'string
:group 'inline-anki)
(defcustom inline-anki-anki-connect-listening-port
"8765"
"The port number AnkiConnect is listening."
:type 'string
:group 'inline-anki)
(defcustom inline-anki-use-math-jax nil
"Use Anki's built in MathJax support instead of LaTeX."
:type 'boolean
:group 'inline-anki)
;;; AnkiConnect
(defun inline-anki--anki-connect-action (action &optional params version)
(let (a)
(when version
(push `(version . ,version) a))
(when params
(push `(params . ,params) a))
(push `(action . ,action) a)))
(defun inline-anki--anki-connect-invoke-queue ()
(let (action-queue)
(lambda (&optional action params handler)
(if action
(push (cons (inline-anki--anki-connect-action action params) handler) action-queue)
(when action-queue
(apply #'inline-anki--anki-connect-invoke-multi (nreverse action-queue))
(setq action-queue nil))))))
(defun inline-anki--anki-connect-invoke (action &optional params)
"Invoke AnkiConnect with ACTION and PARAMS."
(let ((request-body (json-encode (inline-anki--anki-connect-action action params 5)))
(request-backend 'curl)
(json-array-type 'list)
reply err)
(let ((response (request (format "http://%s:%s"
inline-anki-anki-connect-listening-address
inline-anki-anki-connect-listening-port)
:type "POST"
:parser 'json-read
:data request-body
:success (cl-function (lambda (&key data &allow-other-keys)
(setq reply data)))
:error (cl-function (lambda (&key _ &key error-thrown &allow-other-keys)
(setq err (string-trim (cdr error-thrown)))))
:sync t)))
;; HACK: With sync set to t, `request' waits for curl process to
;; exit, then response data becomes available, but callbacks
;; might not be called right away but at a later time, that's
;; why here we manually invoke callbacks to receive the result.
(unless (request-response-done-p response)
(request--curl-callback (get-buffer-process (request-response--buffer response)) "finished\n")))
(when err (error "Error communicating with AnkiConnect using cURL: %s" err))
(or reply (error "Got empty reply from AnkiConnect"))))
(defmacro inline-anki--anki-connect-invoke-result (&rest args)
"Invoke AnkiConnect with ARGS, return the result from response or raise an error."
`(let-alist (inline-anki--anki-connect-invoke ,@args)
(when .error (error .error))
.result))
(defun inline-anki--anki-connect-invoke-multi (&rest actions)
(-zip-with (lambda (result handler)
(when-let ((_ (listp result))
(err (alist-get 'error result)))
(error err))
(and handler (funcall handler result)))
(inline-anki--anki-connect-invoke-result
"multi" `((actions . ,(mapcar #'car actions))))
(mapcar #'cdr actions)))
(defun inline-anki--anki-connect-map-note (note)
"Convert NOTE to the form that AnkiConnect accepts."
(let-alist note
(list (cons "id" .note-id)
(cons "deckName" .deck)
(cons "modelName" .note-type)
(cons "fields" .fields)
;; Convert tags to a vector since empty list is identical to nil
;; which will become None in Python, but AnkiConnect requires it
;; to be type of list.
(cons "tags" (vconcat .tags)))))
(defun inline-anki--anki-connect-store-media-file (path)
"Store media file for PATH, which is an absolute file name.
The result is the path to the newly stored media file."
(let* ((hash (secure-hash 'sha1 path))
(media-file-name (format "%s-%s%s"
(file-name-base path)
hash
(file-name-extension path t)))
content)
(when (equal :json-false (inline-anki--anki-connect-invoke-result
"retrieveMediaFile"
`((filename . ,media-file-name))))
(message "Storing media file to Anki for %s..." path)
(setq content (base64-encode-string
(with-temp-buffer
(insert-file-contents path)
(buffer-string))))
(inline-anki--anki-connect-invoke-result
"storeMediaFile"
`((filename . ,media-file-name)
(data . ,content))))
media-file-name))
;;; Org Export Backend
(defconst inline-anki--ox-anki-html-backend
(if inline-anki-use-math-jax
(org-export-create-backend
:parent 'html
:transcoders '((latex-fragment . inline-anki--ox-latex-for-mathjax)
(latex-environment . inline-anki--ox-latex-for-mathjax)))
(org-export-create-backend
:parent 'html
:transcoders '((latex-fragment . inline-anki--ox-latex)
(latex-environment . inline-anki--ox-latex)))))
(defun inline-anki--translate-latex-delimiters (latex-code)
(catch 'done
(let ((delimiter-map (list (list (cons (format "^%s" (regexp-quote "$$")) "[$$]")
(cons (format "%s$" (regexp-quote "$$")) "[/$$]"))
(list (cons (format "^%s" (regexp-quote "$")) "[$]")
(cons (format "%s$" (regexp-quote "$")) "[/$]"))
(list (cons (format "^%s" (regexp-quote "\\(")) "[$]")
(cons (format "%s$" (regexp-quote "\\)")) "[/$]"))
(list (cons (format "^%s" (regexp-quote "\\[")) "[$$]")
(cons (format "%s$" (regexp-quote "\\]")) "[/$$]"))))
(matched nil))
(save-match-data
(dolist (pair delimiter-map)
(dolist (delimiter pair)
(when (setq matched (string-match (car delimiter) latex-code))
(setq latex-code (replace-match (cdr delimiter) t t latex-code))))
(when matched (throw 'done latex-code)))))
latex-code))
(defun inline-anki--translate-latex-delimiters-to-anki-mathjax-delimiters (latex-code)
(catch 'done
(let ((delimiter-map (list (list (cons (format "^%s" (regexp-quote "$$")) "\\[")
(cons (format "%s$" (regexp-quote "$$")) "\\]"))
(list (cons (format "^%s" (regexp-quote "$")) "\\(")
(cons (format "%s$" (regexp-quote "$")) "\\)"))))
(matched nil))
(save-match-data
(dolist (pair delimiter-map)
(dolist (delimiter pair)
(when (setq matched (string-match (car delimiter) latex-code))
(setq latex-code (replace-match (cdr delimiter) t t latex-code))))
(when matched (throw 'done latex-code)))))
latex-code))
(defun inline-anki--wrap-latex (content)
"Wrap CONTENT with Anki-style latex markers."
(format "<p><div>[latex]</div>%s<div>[/latex]</div></p>" content))
(defun inline-anki--wrap-latex-for-mathjax (content)
"Wrap CONTENT for Anki's native MathJax support."
(format "<p>%s</p>" content))
(defun inline-anki--wrap-div (content)
(format "<div>%s</div>" content))
(defun inline-anki--ox-latex (latex _contents _info)
"Transcode LATEX from Org to HTML.
CONTENTS is nil. INFO is a plist holding contextual information."
(let ((code (org-remove-indentation (org-element-property :value latex))))
(setq code
(pcase (org-element-type latex)
('latex-fragment (inline-anki--translate-latex-delimiters code))
('latex-environment (inline-anki--wrap-latex
(mapconcat #'inline-anki--wrap-div
(split-string (org-html-encode-plain-text code) "\n")
"")))))
(if inline-anki-break-consecutive-braces-in-latex
(replace-regexp-in-string "}}" "} } " code)
code)))
(defun inline-anki--ox-latex-for-mathjax (latex _contents _info)
"Transcode LATEX from Org to HTML.
CONTENTS is nil. INFO is a plist holding contextual information."
(let ((code (org-remove-indentation (org-element-property :value latex))))
(setq code
(pcase (org-element-type latex)
('latex-fragment (inline-anki--translate-latex-delimiters-to-anki-mathjax-delimiters code))
('latex-environment (inline-anki--wrap-latex-for-mathjax
(mapconcat #'inline-anki--wrap-div
(split-string (org-html-encode-plain-text code) "\n")
"")))))
(if inline-anki-break-consecutive-braces-in-latex
(replace-regexp-in-string "}}" "} } " code)
code)))
;; For use during html export
(defun inline-anki--ox-html-link (oldfun link desc info)
"When LINK is a link to local file, transcodes it to html and stores the target file to Anki, otherwise calls OLDFUN for help.
The implementation is borrowed and simplified from ox-html."
(or (catch 'giveup
(unless (plist-get info :anki-editor-mode)
(throw 'giveup nil))
(let* ((type (org-element-property :type link))
(raw-path (org-element-property :path link))
(desc (org-string-nw-p desc))
(path
(cond
((string= type "file")
;; Possibly append `:html-link-home' to relative file
;; name.
(let ((inhibit-message nil)
(home (and (plist-get info :html-link-home)
(org-trim (plist-get info :html-link-home)))))
(when (and home
(plist-get info :html-link-use-abs-url)
(file-name-absolute-p raw-path))
(setq raw-path (concat (file-name-as-directory home) raw-path)))
;; storing file to Anki and return the modified path
(inline-anki--anki-connect-store-media-file (expand-file-name (url-unhex-string raw-path)))))
(t (throw 'giveup nil))))
(attributes-plist
(let* ((parent (org-export-get-parent-element link))
(link (let ((container (org-export-get-parent link)))
(if (and (eq (org-element-type container) 'link)
(org-html-inline-image-p link info))
container
link))))
(and (eq (org-element-map parent 'link 'identity info t) link)
(org-export-read-attribute :attr_html parent))))
(attributes
(let ((attr (org-html--make-attribute-string attributes-plist)))
(if (org-string-nw-p attr) (concat " " attr) ""))))
(cond
;; Image file.
((and (plist-get info :html-inline-images)
(org-export-inline-image-p
link (plist-get info :html-inline-image-rules)))
(org-html--format-image path attributes-plist info))
;; Audio file.
((string-suffix-p ".mp3" path t)
(format "[sound:%s]" path))
;; External link with a description part.
((and path desc) (format "<a href=\"%s\"%s>%s</a>"
(org-html-encode-plain-text path)
attributes
desc))
;; External link without a description part.
(path (let ((path (org-html-encode-plain-text path)))
(format "<a href=\"%s\"%s>%s</a>"
path
attributes
(org-link-unescape path))))
(t (throw 'giveup nil)))))
(funcall oldfun link desc info)))
;;; Core Functions
(defun inline-anki--create-note (note)
"Request AnkiConnect for creating NOTE."
(let ((queue (inline-anki--anki-connect-invoke-queue)))
(funcall queue
'addNote
`((note . ,(inline-anki--anki-connect-map-note note)))
#'inline-anki--dangerously-write-id)
(funcall queue)))
(defun inline-anki--update-note (note)
"Request AnkiConnect for updating fields and tags of NOTE."
(let ((queue (inline-anki--anki-connect-invoke-queue)))
(funcall queue
'updateNoteFields
`((note . ,(inline-anki--anki-connect-map-note note))))
(funcall queue
'notesInfo
`((notes . (,(alist-get 'note-id note))))
(lambda (result)
;; update tags
(let* ((existing-note (car result))
(tags-to-add (-difference (-difference (alist-get 'tags note)
(alist-get 'tags existing-note))
inline-anki-ignored-org-tags))
(tags-to-remove (-difference (-difference (alist-get 'tags existing-note)
(alist-get 'tags note))
inline-anki-protected-tags))
(tag-queue (inline-anki--anki-connect-invoke-queue)))
(when tags-to-add
(funcall tag-queue
'addTags `((notes . (,(alist-get 'note-id note)))
(tags . ,(string-join tags-to-add " ")))))
(when tags-to-remove
(funcall tag-queue
'removeTags `((notes . (,(alist-get 'note-id note)))
(tags . ,(string-join tags-to-remove " ")))))
(funcall tag-queue))))
(funcall queue
'findCards
`((query . ,(concat "nid:" (number-to-string
(alist-get 'note-id note)))))
(lambda (cards)
;; suspend or unsuspend
(if cards
(let ((card-queue (inline-anki--anki-connect-invoke-queue)))
(if (alist-get 'suspend? note)
(funcall card-queue
'suspend
`((cards . ,cards)))
(funcall card-queue
'unsuspend
`((cards . ,cards))))
(funcall card-queue))
(error "No cards for note %d" (alist-get 'note-id note)))))
(funcall queue)))
(provide 'inline-anki-anki-editor-fork)
;;; inline-anki-anki-editor-fork.el ends here