This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
quiver.go
588 lines (508 loc) · 14.4 KB
/
quiver.go
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
Package quiver implements a simple library for parsing quiver libraries, notebooks and notes.
The most straightforward way to use it is to load a library from disk, and then iterate the object tree:
lib, _ := quiver.ReadLibrary("/path/to/library.quiverlibrary", false)
// Print the title of all the notes in all the notebooks
for _, notebooks := range lib.Notebooks {
for _, note := notebook.Notes {
fmt.Println(n.Title)
//...
}
}
*/
package quiver
import (
"bufio"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"os"
"path/filepath"
"strings"
"time"
)
// The version of the quiver package
const Version = "0.3.4"
// The data tree: holds all the data of the library
// Library represents the contents of a Quiver library (.qvlibrary) file.
type Library struct {
*LibraryMetadata
// The list of Notebooks found inside the Library.
Notebooks []*Notebook `json:"notebooks"`
}
// LibraryMetadata represents the contents of a Quiver library metadata (meta.json) file.
type LibraryMetadata struct {
// The root of the notebook hierarchy
Children []NotebookHierarchyInfo `json:"children"`
}
// A note in the Quote notebooks hierarchy
type NotebookHierarchyInfo struct {
// The UUID of the Notebook.
UUID string `json:"uuid"`
// The list of its children
Children []NotebookHierarchyInfo `json:"children"`
}
// Notebook represents the contents of a Quiver notebook (.qvnotebook) directory.
type Notebook struct {
*NotebookMetadata
// The list of Notes found inside the Notebook.
Notes []*Note `json:"notes"`
}
// NotebookMetadata represents the contents of a Quiver notebook (.qvnotebook) directory.
type NotebookMetadata struct {
// The Name of the Notebook.
Name string `json:"name"`
// The UUID of the Notebook.
UUID string `json:"uuid"`
}
// NoteContent represents the contents of a Quiver note (.qvnote) directory.
type Note struct {
*NoteMetadata
*NoteContent
// The list of all Resources attached to this Note.
Resources []*NoteResource `json:"resources,omitempty"`
}
// NoteMetadata represents the contents of a Quiver note metadata (meta.json) file.
type NoteMetadata struct {
// The time the note was created.
CreatedAt TimeStamp `json:"created_at"`
// A list of tags attached to the Note.
Tags []string `json:"tags"`
// The Title of the Note.
Title string `json:"title"`
// The time the note was last updated.
UpdatedAt TimeStamp `json:"updated_at"`
// The UUID of the Note.
UUID string `json:"uuid"`
}
// A timestamp in a Quiver note metadata file (meta.json).
// It holds time info (from time.Time) and marshals as an integer.
type TimeStamp time.Time
// MarshalJSON marshals TimeStamp as an integer (seconds since Epoch).
func (u *TimeStamp) MarshalJSON() ([]byte, error) {
secs := (*time.Time)(u).Unix()
return json.Marshal(secs)
}
// MarshalJSON unmarshals TimeStamp from an integer (seconds since Epoch).
func (u *TimeStamp) UnmarshalJSON(data []byte) error {
var secs int64
err := json.Unmarshal(data, &secs)
if err != nil {
return err
}
// copy values
*u = TimeStamp(time.Unix(secs, 0))
return nil
}
// NoteContent represents the contents of a Quiver note resource: any file found under the resources/ folder in the note.
type NoteResource struct {
// The file name.
Name string `json:"name"`
// The file data as raw bytes.
// It serializes in JSON as a data URI.
Data []byte `json:"data"`
}
// MarshalJSON marshals
func (n *NoteResource) MarshalJSON() ([]byte, error) {
// Build a data uri for the resource
ext := filepath.Ext(n.Name)
mimeType := mime.TypeByExtension(ext)
b64 := base64.RawURLEncoding.EncodeToString(n.Data)
url := fmt.Sprintf("data:%v,%v", mimeType, b64)
// And then encode the uri as a JSON string
aux := struct {
Name string
Data string
}{
n.Name,
url,
}
return json.Marshal(aux)
}
// UnmarshalJSON unmarshals NoteResource from data:// url
func (u *NoteResource) UnmarshalJSON(data []byte) error {
var aux struct {
Name string
URL string
}
err := json.Unmarshal(data, aux)
if err != nil {
return err
}
// Name OK
u.Name = aux.Name
// Split data url
if !strings.HasPrefix(aux.URL, "data:") {
return fmt.Errorf("Invalid data URL %v", aux.URL)
}
s := strings.SplitN(aux.URL, ",", 2)
if len(s) != 2 {
return fmt.Errorf("data URL %v", aux.URL)
}
// Decode the base64-encoded data
resData, err := base64.RawURLEncoding.DecodeString(s[1])
if err != nil {
return err
}
// Data found !
u.Data = resData
return nil
}
// NoteContent represents the contents of a Quiver not content (content.json) file.
//
// Beware: this structure does note contain the Title of the cell, since it is already held in the
// NoteMetadata file.
type NoteContent struct {
// The list of all cells in the note.
Cells []*Cell `json:"cells"`
}
// The type of a cell inside of a Quiver Note
type CellType string
// The recognized types of Quiver cells
const (
CodeCell CellType = "code"
TextCell CellType = "text"
MarkdownCell CellType = "markdown"
LatexCell CellType = "latex"
DiagramCell CellType = "diagram"
)
// A cell inside a Quiver note.
type Cell struct {
// The type of the cell.
Type CellType `json:"type"`
// The language of the cell: only relevant for CodeCell cells.
Language string `json:"language,omitempty"`
// The type of diagram: only relevant for DiagramCell cells.
DiagramType string `json:"diagramType,omitempty"`
// The data for the cell, aka. all the actual content.
Data string `json:"data"`
}
// IsCode returns true if the Cell is of Type CodeCell.
func (c *Cell) IsCode() bool {
return c.Type == CodeCell
}
// IsMarkdown returns true if the Cell is of Type MarkdownCell.
func (c *Cell) IsMarkdown() bool {
return c.Type == MarkdownCell
}
// IsText returns true if the Cell is of Type TextCell.
func (c *Cell) IsText() bool {
return c.Type == TextCell
}
// IsLatex returns true if the Cell is of Type LatexCell.
func (c *Cell) IsLatex() bool {
return c.Type == LatexCell
}
// IsDiagram returns true if the Cell is of Type DiagramCell.
func (c *Cell) IsDiagram() bool {
return c.Type == DiagramCell
}
// IsLibrary checks that the element at the given path is indeed a Quiver library, and
// returns true if found or false with an error otherwise.
func IsLibrary(path string) (bool, error) {
// it should exist and be a library
stat, err := os.Stat(path)
if err != nil {
return false, err
}
if !stat.IsDir() {
return false, errors.New("The Quiver Library should be a dictionary")
}
// and end with .qvlibrary
if !strings.HasSuffix(stat.Name(), ".qvlibrary") {
return false, errors.New("The Quiver Library should have .qvlibrary extension")
}
return true, nil
}
// ReadLibrary loads the Quiver library at the given path.
// The loadResources parameter tells the function if note resources should be loaded too.
func ReadLibrary(path string, loadResources bool) (*Library, error) {
_, err := IsLibrary(path)
if err != nil {
return nil, err
}
// list the files in the library (aka. the notes)
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
var metadata *LibraryMetadata
notebooks := make([]*Notebook, 0, len(files))
for _, f := range files {
p := filepath.Join(path, f.Name())
// ignore root meta.json
if f.Name() == "meta.json" {
metadata, err = ReadLibraryMetadata(p)
if err != nil {
return nil, err
}
} else {
// all other elements should be notebooks
n, err := ReadNotebook(p, loadResources)
if err != nil {
return nil, err
}
notebooks = append(notebooks, n)
}
}
return &Library{metadata, notebooks}, nil
}
// WalkNotebooksHierarchy returns all the notebooks in order, allowing to "explore" the internal hierarchy of the
// Quiver library.
func (m *Library) WalkNotebooksHierarchy(f func(n *Notebook, parents []*Notebook) error) error {
notebooks := make(map[string]*Notebook, len(m.Notebooks))
for _, n := range m.Notebooks {
notebooks[n.UUID] = n
}
parents := make([]string, 0)
for _, n := range m.LibraryMetadata.Children {
err := walkNotebooksHierarchy(n, parents, func(c string, parents []string) error {
pp := make([]*Notebook, len(parents))
for i, p := range parents {
pp[i] = notebooks[p]
}
return f(notebooks[c], pp)
})
if err != nil {
return err
}
}
return nil
}
func walkNotebooksHierarchy(n NotebookHierarchyInfo, parents []string, f func(n string, parents []string) error) error {
err := f(n.UUID, parents)
if err != nil {
return err
}
if len(n.Children) > 0 {
p := append(parents[:], n.UUID)
for _, c := range n.Children {
err = walkNotebooksHierarchy(c, p, f)
if err != nil {
return err
}
}
}
return nil
}
// ReadLibraryMetadata loads the library "meta.json" at the given path.
func ReadLibraryMetadata(path string) (*LibraryMetadata, error) {
// find and read metadata file
mf, err := os.Open(path)
if err != nil {
return nil, err
}
defer mf.Close()
// Read metadata
buf := bufio.NewReader(mf)
return ParseLibraryMetadata(buf)
}
// IsNoteBook checks that the element at the given path is indeed a Quiver notebook, and
// returns true if found or false with an error otherwise.
func IsNotebook(path string) (bool, error) {
// it should exist and be a directory
stat, err := os.Stat(path)
if err != nil {
return false, err
}
if !stat.IsDir() {
return false, errors.New("A Quiver Notebook should be a directory")
}
// and end with .qvnotebook
if !strings.HasSuffix(stat.Name(), ".qvnotebook") {
return false, errors.New("A Quiver Notebook should have .qvnotebook extension")
}
return true, nil
}
// ReadNotebook loads the Quiver notebook in the given path.
// The loadResources parameter tells the function if note resources should be loaded too.
func ReadNotebook(path string, loadResources bool) (*Notebook, error) {
_, err := IsNotebook(path)
if err != nil {
return nil, err
}
// list the files in the notebook (aka. the notes)
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
var metadata *NotebookMetadata
var numNotes = 0
if len(files) > 1 {
numNotes = len(files) - 1
}
notes := make([]*Note, numNotes)
for i, f := range files {
p := filepath.Join(path, f.Name())
if f.Name() == "meta.json" {
metadata, err = ReadNotebookMetadata(p)
if err != nil {
return nil, err
}
} else {
n, err := ReadNote(p, loadResources)
if err != nil {
return nil, err
}
notes[i] = n
}
}
return &Notebook{metadata, notes}, nil
}
// IsNote checks that the element at the given path is indeed a Quiver note, and
// returns true if found or false with an error otherwise.
func IsNote(path string) (bool, error) {
// it should exist and be a directory
stat, err := os.Stat(path)
if err != nil {
return false, err
}
if !stat.IsDir() {
return false, errors.New("A Quiver Note should be a directory")
}
// and end with .qvnote
if !strings.HasSuffix(stat.Name(), ".qvnote") {
return false, errors.New("A Quiver Note should have .qvnote extension")
}
return true, nil
}
// ReadNote loads the Quiver note in the given path.
// The loadResources parameter tells the function if note resources should be loaded too.
func ReadNote(path string, loadResources bool) (*Note, error) {
_, err := IsNote(path)
if err != nil {
return nil, err
}
// Read the metadata file
mp := filepath.Join(path, "meta.json")
m, err := ReadNoteMetadata(mp)
if err != nil {
return nil, err
}
// Read the content file
cp := filepath.Join(path, "content.json")
c, err := ReadNoteContent(cp)
if err != nil {
return nil, err
}
var res []*NoteResource
if loadResources {
rp := filepath.Join(path, "resources")
res, err = ReadNoteResources(rp)
// we check for error but ignore not existing dir
if err != nil && !os.IsNotExist(err) {
return nil, err
}
}
return &Note{m, c, res}, nil
}
// ReadNoteResource loads the resource (any file actually) into a NoteResource instance.
func ReadNoteResources(path string) ([]*NoteResource, error) {
stat, err := os.Stat(path)
if err != nil {
return nil, err
}
if !stat.IsDir() {
return nil, errors.New("Quiver Note Resources should be held in a directory")
}
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
res := make([]*NoteResource, len(files))
for i, file := range files {
name := file.Name()
fp := filepath.Join(path, name)
// Read the file completely in memory
f, err := os.Open(fp)
if err != nil {
return nil, err
}
buf, err := ioutil.ReadAll(f)
f.Close()
if err != nil {
return nil, err
}
// And add the note to the list
res[i] = &NoteResource{name, buf}
}
return res, nil
}
// ReadNoteResource loads the note "meta.json" at the given path.
func ReadNoteMetadata(path string) (*NoteMetadata, error) {
// find and read metadata file
mf, err := os.Open(path)
if err != nil {
return nil, err
}
defer mf.Close()
// Read metadata
buf := bufio.NewReader(mf)
return ParseNoteMetadata(buf)
}
// ReadNoteContent loads the note "content.json" at the given path.
func ReadNoteContent(path string) (*NoteContent, error) {
// find and read content file
cf, err := os.Open(path)
if err != nil {
return nil, err
}
defer cf.Close()
// Read Content
buf := bufio.NewReader(cf)
return ParseContent(buf)
}
// ReadNotebookMetadata loads the notebook "meta.json" at the given path.
func ReadNotebookMetadata(path string) (*NotebookMetadata, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
buf := bufio.NewReader(f)
return ParseNotebookMetadata(buf)
}
// ParseLibraryMetadata loads the JSON from the given stream into a LibraryMetadata.
func ParseLibraryMetadata(r io.Reader) (*LibraryMetadata, error) {
d := json.NewDecoder(r)
m := new(LibraryMetadata)
err := d.Decode(m)
if err != nil {
return nil, err
}
return m, nil
}
// ParseNotebookMetadata loads the JSON from the given stream into a NotebookMetadata.
func ParseNotebookMetadata(r io.Reader) (*NotebookMetadata, error) {
d := json.NewDecoder(r)
m := new(NotebookMetadata)
err := d.Decode(m)
if err != nil {
return nil, err
}
return m, nil
}
// ParseNoteMetadata loads the JSON from the given stream into a NoteMetadata.
func ParseNoteMetadata(r io.Reader) (*NoteMetadata, error) {
d := json.NewDecoder(r)
m := new(NoteMetadata)
err := d.Decode(m)
if err != nil {
return nil, err
}
return m, nil
}
// ParseNoteMetadata loads the JSON from the given stream into a NoteContent.
func ParseContent(r io.Reader) (*NoteContent, error) {
d := json.NewDecoder(r)
n := new(NoteContent)
err := d.Decode(n)
if err != nil {
return nil, err
}
return n, err
}