forked from ahyatt/ekg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ekg-logseq.el
480 lines (437 loc) · 20.2 KB
/
ekg-logseq.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
;;; ekg-logseq.el --- ekg and logseq integration -*- lexical-binding: t -*-
;; Copyright (c) 2023 Andrew Hyatt <[email protected]>
;; 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/>.
;;; Commentary:
;; This exports data to logseq. Because ekg and logseq have somewhat different
;; properties, the mapping process needs to be described. First, because notes
;; in ekg have no titles, they should not be separate logseq notes. Instead, a
;; tag is a logseq note, and contains all notes within it. This means that notes
;; are in multiple tags. We put the note in the first logseq page (tag) we find
;; it in, and then we tag all other tags, which will add it to those pages as
;; well.
(require 'ekg)
(require 'triples)
(require 'seq)
(require 'org nil t)
(require 'org-element nil t)
(declare-function org-element-map "ext:org-element.el")
(declare-function org-element-property "ext:org-element.el")
(declare-function org-element-put-property "ext:org-element.el")
(declare-function org-element-interpret-data "ext:org-element.el")
(declare-function org-element-parse-buffer "ext:org-element.el")
(declare-function org-element-create "ext:org-element.el")
(declare-function org-map-entries "ext:org.el")
(declare-function org-mode "ext:org.el")
(declare-function org-do-demote "ext:org.el")
(defgroup ekg-logseq nil
"Customization for ekg's logseq integration."
:group 'ekg)
(defcustom ekg-logseq-dir nil
"Parent directory for logseq files, contains pages and diary directories."
:type 'directory
:group 'ekg-logseq)
(defun ekg-logseq-connect ()
"Connect to ekg and ensure logseq schema is set up."
(ekg-connect)
(ekg-logseq-add-schema))
(defun ekg-logseq-add-schema ()
"Add the logseq schema to the ekg database."
(triples-add-schema ekg-db 'logseq
'(last-export :base/unique t :base/type integer)
'(last-import :base/unique t :base/type integer)))
(defun ekg-logseq-get-last-export ()
"Get the last export time."
(or (plist-get
(triples-get-type ekg-db 'logseq 'logseq)
:last-export) 0))
(defun ekg-logseq-get-last-import ()
"Get the last export time."
(or (plist-get
(triples-get-type ekg-db 'logseq 'logseq)
:last-import) 0))
(defun ekg-logseq-set-last-export (time)
"Set the last export time to TIME."
(let ((plist (triples-get-type ekg-db 'logseq 'logseq)))
(apply #'triples-set-type ekg-db 'logseq 'logseq
(plist-put plist :last-export (floor (float-time time))))))
(defun ekg-logseq-set-last-import (time)
"Set the last import time to TIME."
(let ((plist (triples-get-type ekg-db 'logseq 'logseq)))
(apply #'triples-set-type ekg-db 'logseq 'logseq
(plist-put plist :last-import (floor (float-time time))))))
(defun ekg-logseq-convert-ekg-tag (tag)
"Convert an ekg TAG to a logseq tag."
(if (ekg-date-tag-p tag)
(replace-regexp-in-string "date/" "" tag) tag))
(defun ekg-logseq-property (name value org-mode-p)
"Create a logseq property with NAME and VALUE.
If ORG-MODE-P is true, use org-mode syntax."
(if org-mode-p (format "#+%s: %s\n" name value)
(format "%s:: %s\n" name value)))
(defun ekg-logseq-primary-tag (tags)
"From TAGS, return the primary tag.
The primary tag will be the tag in the note is exported in. The
others will have backreferences to the note in logseq.
We just use the first tag that is not a date tag, if it exists."
(seq-find (lambda (tag) (not (ekg-date-tag-p tag)))
tags
(car tags)))
(defun ekg-logseq-hash (text)
"Return the hash of TEXT."
(secure-hash 'sha1 text))
(defun ekg-logseq-text-to-logseq (note)
"Convert a note's text to logseq format.
This inserts the results of inlines, although for note
transclusion, it uses the logseq equivalent."
(ekg-insert-inlines-and-process
(ekg-note-text note)
(ekg-note-inlines note)
(lambda (i)
(if (eq (car (ekg-inline-command i)) 'transclude-note)
(format "{{embed ((%s))}}" (cadr (ekg-inline-command i)))
(ekg-inline-to-result i note)))))
(defun ekg-logseq-note-to-logseq-org (note tag)
"Return logseq text to store for NOTE in TAG.
This will store the note text as org-mode, regardless of the mode
of the note."
(with-temp-buffer
(org-mode)
(when (ekg-should-show-id-p (ekg-note-id note))
(insert (ekg-note-id note) "\n"))
(insert (ekg-logseq-text-to-logseq note))
;; Demote all other headings to level 2.
(org-map-entries (lambda () (org-do-demote)))
(org-element-map (org-element-parse-buffer) 'link
(lambda (link)
(when (string= "ekg" (org-element-property :type link))
(let ((id (org-element-property :path link)))
(org-element-put-property link :type "id")
(org-element-put-property link :path id)))))
(goto-char (point-min))
(insert (org-element-interpret-data
(org-element-create 'headline
`(:level 1 :title ,(or (car (plist-get (ekg-note-properties note) :titled/title))
"Untitled Note")))))
;; Can't figure out how to get org-element to do this for me based off of
;; properties in the headline, so let's put the properties on here manually.
(let ((tag-text (mapconcat (lambda (tag)
(format "#[[%s]]"
(ekg-logseq-convert-ekg-tag tag)))
(seq-difference
(ekg-note-active-tags note) (list tag))
" ")))
(insert (format ":PROPERTIES:\n:ID: %s\n:EKG_HASH: %s\n:END:\n%s%s"
(ekg-note-id note) (ekg-logseq-hash (ekg-note-text note))
tag-text (if (> (length tag-text) 0) "\n" ""))))
(buffer-substring-no-properties (point-min) (point-max))))
(defun ekg-logseq-note-to-logseq-md (note tag)
"Return logseq text to store for NOTE in TAG.
This will store the note text as markdown, regardless of the mode
of the note."
(with-temp-buffer
(insert "- "
(or (car (plist-get (ekg-note-properties note) :titled/title))
"Untitled Note")
"\n "
(format "id:: %s\n ekg_hash:: %s\n "
(ekg-note-id note) (ekg-logseq-hash (ekg-note-text note)))
(mapconcat (lambda (tag)
(format "#[[%s]]"
(ekg-logseq-convert-ekg-tag tag)))
(seq-difference (ekg-note-active-tags note) (list tag))
" ")
"\n "
(if (ekg-should-show-id-p (ekg-note-id note))
(format "%s\n " (ekg-note-id note))
"")
(string-replace "\n" "\n " (ekg-logseq-text-to-logseq note)))
(string-trim (buffer-substring-no-properties (point-min) (point-max)) nil (rx (1+ blank)))))
(defun ekg-logseq-notes-to-logseq (notes tag org-mode-p)
"Return logseq text to store for NOTES in TAG.
If ORG-MODE-P is non-nil, store the notes as org-mode, otherwise
store as markdown."
(concat
(ekg-logseq-property "title" (ekg-logseq-convert-ekg-tag tag)
org-mode-p)
(ekg-logseq-property "ekg_export" "true" org-mode-p)
"\n"
(mapconcat (lambda (note)
(if org-mode-p
(ekg-logseq-note-to-logseq-org note tag)
(ekg-logseq-note-to-logseq-md note tag)))
(seq-filter (lambda (note)
(and (equal tag (ekg-logseq-primary-tag (ekg-note-tags note)))
(not (equal "" (ekg-note-text note))))) notes)
"\n")))
(defun ekg-logseq-tag-to-file (tag)
"Return text to populate to a logseq file for TAG."
(with-temp-buffer
(when (eq ekg-capture-default-mode 'org-mode)
(org-mode))
(insert (ekg-logseq-notes-to-logseq
(sort
(ekg-get-notes-with-tag tag)
(lambda (a b)
(time-less-p (ekg-note-creation-time a) (ekg-note-creation-time b))))
tag
(eq major-mode 'org-mode)))
(buffer-substring-no-properties (point-min) (point-max))))
(defun ekg-logseq-tag-to-filename (tag)
"Return the filename for TAG."
(replace-regexp-in-string
"/" "$"
(format "%s.%s"
(ekg-logseq-convert-ekg-tag tag)
(if (eq ekg-capture-default-mode 'org-mode)
"org" "md"))))
(defun ekg-logseq-filename-to-tag (filename)
"Return the tag for FILENAME."
(replace-regexp-in-string
(rx "$") "/"
(file-name-sans-extension (file-name-nondirectory filename))))
(defun ekg-logseq-export-tag (tag)
"Export TAG to logseq.
This may make files with no content if there are notes with no
backlinks.
Do not overwrite a file if nothing has changed.
Return non-nil if the file was written."
(let ((contents (ekg-logseq-tag-to-file tag))
(filename (expand-file-name
(ekg-logseq-tag-to-filename tag)
(file-name-concat ekg-logseq-dir
(if (ekg-date-tag-p tag)
"journals" "pages")))))
(unless
(and
(file-exists-p filename)
(string= contents (with-temp-buffer
(insert-file-contents filename)
(buffer-string))))
(message "ekg-logseq-export: exporting to file %s" filename)
(with-temp-file filename
(insert contents)
t))))
(defun ekg-logseq--to-import-text ()
"Return a list of notes to the current buffer to import.
This is the text not marked as exported by ekg. If org-mode, the
buffer is divided into its top level subtrees. Any subtree that
isn't exported by EKG is included.
For markdown, it's the same thing except for list items, which
are always the top-level constructs in markdown mode.
For markdown, remove the leading dash. For org-mode, don't remove
the leading star, because a nested structure beneath seems to
make less sense without it."
(save-excursion
(if (eq major-mode 'org-mode)
(org-element-map (org-element-parse-buffer) 'headline
(lambda (headline)
(unless (or (org-element-property :ekg_hash headline)
(org-element-property :EKG_HASH headline)
(> (org-element-property :level headline) 1))
(buffer-substring-no-properties
(org-element-property :begin headline)
(org-element-property :end headline))))
nil nil 'headline)
;; Not org-mode, it must be markdown. Iterate over the top-level list
;; items, keeping any that don't have an "ekg_id".
(let ((pos (point-min))
(items nil))
(goto-char (point-min))
(cl-loop
do
;; According to the docs this should set the point to the end when
;; there is no match, but it doesn't seem to happen.
(unless (re-search-forward "^- " nil t)
(goto-char (point-max)))
(let ((text
(replace-regexp-in-string
(rx bol "- ") ""
(buffer-substring-no-properties
pos (point)))))
(unless (or
;; Don't take the text pre-first item.
(eq pos (point-min))
(equal text "")
(string-match-p "ekg_hash::" text))
(push text items))
(setq pos (match-end 0)))
until (eq (point) (point-max)))
(nreverse items)))))
(defun ekg-logseq--to-import-tags (text)
"Return a list of tags in logseq format to import from TEXT.
We look for strings of the format #tag and #[[tag]]."
(with-temp-buffer
(insert text)
(let ((tags ()))
(goto-char (point-min))
(while (re-search-forward
(rx (or (seq "#" (group-n 1 (1+ alnum)))
(seq "#[[" (group-n 1 (1+ (or alnum space))) "]]"))) nil t)
(when-let ((tag (match-string-no-properties 1)))
(push tag tags)))
(nreverse (seq-uniq tags)))))
(defun ekg-logseq--to-import-md-id (text)
"Return the logseq id from markdown TEXT."
(when (string-match (rx (seq "id:: " (group-n 1 (1+ (or "-" hex-digit))))) text)
(match-string-no-properties 1 text)))
(defun ekg-logseq--to-import-org-id (text)
"Return the logseq id from org TEXT."
(with-temp-buffer
(insert text)
(org-mode)
;; There should just be one thing here.
(car
(org-element-map (org-element-parse-buffer) 'headline
(lambda (headline)
(org-element-property :ID headline))
nil nil 'headline))))
(defun ekg-logseq--text-to-note (tag text)
"Return the note to import from logseq TEXT.
TAG is the current tag being imported in logseq."
(let* ((in-cons (ekg-extract-inlines
(replace-regexp-in-string
(rx (seq "{{embed" (* space) "(("
(group-n 1 (1+ (not ")"))) "))}}"))
"%(transclude-note \"\\1\")" text)))
(note (ekg-note-create :text (car in-cons)
:mode (when (eq major-mode 'org-mode)
'org-mode 'markdown-mode)
:tags (cons tag (ekg-logseq--to-import-tags text)))))
(setf (ekg-note-inlines note)
;; These must all be transclusion commands, check each id (the first
;; argument) against the database to understand whether it should be
;; quoted or not.
(mapcar (lambda (i)
(if (ekg-note-with-id-exists-p (read (nth 1 (ekg-inline-command i))))
(progn
(setf (ekg-inline-command i)
(list 'transclude-note (read (nth 1 (ekg-inline-command i)))))
i) i)) (cdr in-cons)))
(when-let (id (if (eq major-mode 'org-mode)
(ekg-logseq--to-import-org-id text)
(ekg-logseq--to-import-md-id text)))
(setf (ekg-note-id note) (if (ekg-note-with-id-exists-p (read id)) (read id) id)))
note))
(defun ekg-logseq-import ()
"Import from the current logseq directory.
This will create new nodes based on parts of the logseq pages
that were previously not exported. It will never re-import
something exported, so it's expected that changing things in
logseq will not result in those things showing up in ekg.
An import must be followed by an export, otherwise we can end up
importing the same thing multiple times. Because of this, if you
run this interactively, only run it once if you want an initial
population of data into ekg. Otherwise, run `ekg-logseq-sync',
which will import and re-export back to logseq."
(unless ekg-logseq-dir
(error "ekg-logseq-dir must be set"))
;; Force a backup pre-import.
(triples-backup ekg-db ekg-db-file most-positive-fixnum)
(let ((count 0)
(last-import (ekg-logseq-get-last-import))
(start-time (current-time)))
(message "ekg-logseq-import: importing logseq files changed since %s"
(if (= last-import 0) "the beginning of time itself"
(format-time-string "%F %X" last-import)))
(cl-loop for subdir in '("journals" "pages") do
(cl-loop for file in
(directory-files
(file-name-concat ekg-logseq-dir subdir) t
(rx (seq "." (or "org" "md") eol))) do
;; Only import files not modified since last-import
(when (time-less-p last-import
(nth 5 (file-attributes file)))
(let ((tag (concat (if (equal subdir "journals")
"date/" "")
(ekg-logseq-filename-to-tag file))))
(with-temp-buffer
(insert-file-contents file)
(when (equal "org" (file-name-extension file))
(org-mode))
;; No need to do the same for markdown, because we
;; don't need any special parsing capabilities.
(let ((items (ekg-logseq--to-import-text)))
(cl-loop for text in items
do
(message "ekg-logseq-import: saving note from file %s" file)
(cl-incf count)
(ekg-save-note (ekg-logseq--text-to-note tag text)))))))))
(message "ekg-logseq-import: imported %d notes" count)
(ekg-logseq-set-last-import start-time)))
(defun ekg-logseq-tags-with-notes-modified-since (time)
"Return a list of tags with notes modified since TIME."
(if (= time 0)
(ekg-tags)
(seq-filter #'ekg-content-tag-p
(seq-uniq
(cl-loop for triples in
(triples-db-select-pred-op ekg-db :time-tracked/modified-time '> time)
nconc (ekg-note-tags (ekg-get-note-with-id (car triples))))))))
(defun ekg-logseq-export ()
"Export the current ekg database to logseq.
Because this overwrites logseq data, running this by itself
should only be done if your logseq is meant as a read-only copy
of your ekg database. If you intend to add to your logseq, or you
have already have information in logseq, you should run
`ekg-logseq-sync' instead."
(interactive)
(unless ekg-logseq-dir
(error "ekg-logseq-dir must be set"))
(ekg-logseq-connect)
(let* ((deleted 0)
(modified 0)
(export-time (ekg-logseq-get-last-export))
(start-time (current-time))
(tags (ekg-logseq-tags-with-notes-modified-since export-time)))
(cl-loop for subdir in '("journals" "pages") do
(cl-loop for file in
(seq-filter #'file-regular-p
(directory-files
(file-name-concat ekg-logseq-dir subdir) t))
do
(unless (member (concat (if (equal subdir "journals") "date/" "")
(ekg-logseq-filename-to-tag file)) tags)
(with-temp-buffer
(insert-file-contents file)
(when (string-match
(rx (seq line-start "#+ekg-export: true" line-end))
(buffer-substring-no-properties
(point-min)
(point-max)))
(delete-file file)
(message "ekg-logseq-export: deleting obsolete previously exported file %s" file)
(cl-incf deleted))))))
(message "ekg-logseq-export: exporting all tags modified since %s: %S"
(if (= 0 export-time)
"the beginning of time itself"
(format-time-string "%F %X" export-time))
tags)
(cl-loop for tag in tags do
(when (ekg-logseq-export-tag tag)
(cl-incf modified)))
(message "ekg-logseq-export: deleted %d files, modified %d files" deleted modified)
(ekg-logseq-set-last-export start-time)))
(defun ekg-logseq-sync ()
"Sync ekg and logseq.
This will import from logseq to populate anything new into ekg,
then export ekg so that logseq is up to date, and information in
logseq is marked as being part of logseq.
All logic will be run in the background."
(interactive)
(ekg-logseq-connect)
(message "ekg-logseq-sync: Starting in the background")
(ekg-logseq-import)
(ekg-logseq-export))
(provide 'ekg-logseq)
;;; ekg-logseq.el ends here