-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathnotebook.go
371 lines (324 loc) · 9.83 KB
/
notebook.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
package core
import (
"fmt"
"path/filepath"
"strings"
"time"
"github.com/zk-org/zk/internal/util"
"github.com/zk-org/zk/internal/util/errors"
"github.com/zk-org/zk/internal/util/opt"
"github.com/zk-org/zk/internal/util/paths"
)
// Notebook handles queries and commands performed on an opened notebook.
type Notebook struct {
Path string
Config Config
Parser NoteContentParser
index NoteIndex
templateLoaderFactory TemplateLoaderFactory
idGeneratorFactory IDGeneratorFactory
fs FileStorage
logger util.Logger
osEnv func() map[string]string
}
// NewNotebook creates a new Notebook instance.
func NewNotebook(
path string,
config Config,
ports NotebookPorts,
) *Notebook {
return &Notebook{
Path: path,
Config: config,
Parser: ports.NoteContentParser,
index: ports.NoteIndex,
templateLoaderFactory: ports.TemplateLoaderFactory,
idGeneratorFactory: ports.IDGeneratorFactory,
fs: ports.FS,
logger: ports.Logger,
osEnv: ports.OSEnv,
}
}
type NotebookPorts struct {
NoteIndex NoteIndex
NoteContentParser NoteContentParser
TemplateLoaderFactory TemplateLoaderFactory
IDGeneratorFactory IDGeneratorFactory
FS FileStorage
Logger util.Logger
OSEnv func() map[string]string
}
// NotebookFactory creates a new Notebook instance at the given root path.
type NotebookFactory func(path string, config Config) (*Notebook, error)
// Index indexes the content of the notebook to be searchable.
func (n *Notebook) Index(opts NoteIndexOpts) (stats NoteIndexingStats, err error) {
return n.IndexWithCallback(opts, func(change paths.DiffChange) {})
}
// Index indexes the content of the notebook to be searchable.
func (n *Notebook) IndexWithCallback(opts NoteIndexOpts, callback func(change paths.DiffChange)) (stats NoteIndexingStats, err error) {
err = n.index.Commit(func(index NoteIndex) error {
task := indexTask{
path: n.Path,
config: n.Config,
force: opts.Force,
verbose: opts.Verbose,
index: index,
parser: n,
logger: n.logger,
}
stats, err = task.execute(callback)
return err
})
err = errors.Wrap(err, "indexing")
return
}
// NewNoteOpts holds the options used to create a new note in a Notebook.
type NewNoteOpts struct {
// Title of the new note.
Title opt.String
// Initial content of the note.
Content string
// Directory in which to create the note, relative to the root of the notebook.
Directory opt.String
// Group this note belongs to.
Group opt.String
// Path to a custom template used to render the note.
Template opt.String
// Extra variables passed to the templates.
Extra map[string]string
// Creation date provided to the templates.
Date time.Time
// Don't save the generated note on the file system.
DryRun bool
// Use a provided id over generating one
ID string
}
// ErrNoteExists is an error returned when a note already exists with the
// generated filename.
type ErrNoteExists struct {
Name string
Path string
}
func (e ErrNoteExists) Error() string {
return fmt.Sprintf("%s: note already exists", e.Path)
}
// NewNote generates a new note in the notebook, index and returns it.
//
// Returns ErrNoteExists if no free filename can be generated for this note.
func (n *Notebook) NewNote(opts NewNoteOpts) (*Note, error) {
wrap := errors.Wrapper("new note")
dir, err := n.RequireDirAt(opts.Directory.OrString(n.Path).Unwrap())
if err != nil {
return nil, wrap(err)
}
config, err := n.Config.GroupConfigNamed(opts.Group.OrString(dir.Group).Unwrap())
if err != nil {
return nil, wrap(err)
}
extra := config.Extra
for k, v := range opts.Extra {
extra[k] = v
}
templates, err := n.templateLoaderFactory(config.Note.Lang)
if err != nil {
return nil, wrap(err)
}
var idGenerator IDGenerator
if opts.ID != "" {
idGenerator = func() string {
return opts.ID
}
} else {
idGenerator = n.idGeneratorFactory(config.Note.IDOptions)
}
task := newNoteTask{
dir: dir,
title: opts.Title.OrString(config.Note.DefaultTitle).Unwrap(),
content: opts.Content,
date: opts.Date,
extra: extra,
env: n.osEnv(),
fs: n.fs,
filenameTemplate: config.Note.FilenameTemplate + "." + config.Note.Extension,
bodyTemplatePath: opts.Template.Or(config.Note.BodyTemplatePath),
templates: templates,
genID: idGenerator,
dryRun: opts.DryRun,
}
path, content, err := task.execute()
if err != nil {
return nil, wrap(err)
}
note, err := n.ParseNoteWithContent(path, []byte(content))
if note == nil || err != nil {
return nil, wrap(err)
}
if !opts.DryRun {
id, err := n.index.Add(*note)
if err != nil {
return nil, wrap(err)
}
note.ID = id
}
return note, nil
}
// FindNotes retrieves the notes matching the given filtering options.
func (n *Notebook) FindNotes(opts NoteFindOpts) ([]ContextualNote, error) {
return n.index.Find(opts)
}
// FindNote retrieves the first note matching the given filtering options.
func (n *Notebook) FindNote(opts NoteFindOpts) (*Note, error) {
opts.Limit = 1
notes, err := n.FindNotes(opts)
switch {
case err != nil:
return nil, err
case len(notes) == 0:
return nil, nil
default:
return ¬es[0].Note, nil
}
}
// FindMinimalNotes retrieves lightweight metadata for the notes matching
// the given filtering options.
func (n *Notebook) FindMinimalNotes(opts NoteFindOpts) ([]MinimalNote, error) {
return n.index.FindMinimal(opts)
}
// FindMinimalNotes retrieves lightweight metadata for the first note matching
// the given filtering options.
func (n *Notebook) FindMinimalNote(opts NoteFindOpts) (*MinimalNote, error) {
opts.Limit = 1
notes, err := n.FindMinimalNotes(opts)
switch {
case err != nil:
return nil, err
case len(notes) == 0:
return nil, nil
default:
return ¬es[0], nil
}
}
// FindByHref retrieves the first note matching the given link href.
// If allowPartialHref is true, the href can match any unique sub portion of a note path.
func (n *Notebook) FindByHref(href string, allowPartialHref bool) (*MinimalNote, error) {
return n.FindMinimalNote(NoteFindOpts{
IncludeHrefs: []string{href},
AllowPartialHrefs: allowPartialHref,
})
}
// FindLinksBetweenNotes retrieves the links between the given notes.
func (n *Notebook) FindLinksBetweenNotes(ids []NoteID) ([]ResolvedLink, error) {
return n.index.FindLinksBetweenNotes(ids)
}
// FindCollections retrieves all the collections of the given kind.
func (n *Notebook) FindCollections(kind CollectionKind, sorters []CollectionSorter) ([]Collection, error) {
return n.index.FindCollections(kind, sorters)
}
// RelPath returns the path relative to the notebook root to the given path.
func (n *Notebook) RelPath(originalPath string) (string, error) {
wrap := errors.Wrapperf("%v: not a valid notebook path", originalPath)
path, err := n.fs.Abs(originalPath)
if err != nil {
return path, wrap(err)
}
path, err = filepath.Rel(n.Path, path)
if err != nil {
return path, wrap(err)
}
if strings.HasPrefix(path, "..") {
return path, fmt.Errorf("%s: path is outside the notebook at %s", originalPath, n.Path)
}
if path == "." {
path = ""
}
return path, nil
}
// Dir represents a directory inside a notebook.
type Dir struct {
// Name of the directory, which is the path relative to the notebook's root.
Name string
// Absolute path to the directory.
Path string
// Name of the config group this directory belongs to, if any.
Group string
}
// RootDir returns the root directory for this notebook.
func (n *Notebook) RootDir() Dir {
return Dir{
Name: "",
Path: n.Path,
Group: "",
}
}
// DirAt returns a Dir representation of the notebook directory at the given path.
func (n *Notebook) DirAt(path string) (Dir, error) {
path, err := n.fs.Abs(path)
if err != nil {
return Dir{}, err
}
name, err := n.RelPath(path)
if err != nil {
return Dir{}, err
}
group, err := n.Config.GroupNameForPath(name)
if err != nil {
return Dir{}, err
}
return Dir{
Name: name,
Path: path,
Group: group,
}, nil
}
// RequireDirAt is the same as DirAt, but checks that the directory exists
// before returning the Dir.
func (n *Notebook) RequireDirAt(path string) (Dir, error) {
dir, err := n.DirAt(path)
if err != nil {
return dir, err
}
exists, err := n.fs.DirExists(dir.Path)
if err != nil {
return dir, err
}
if !exists {
return dir, fmt.Errorf("%v: directory not found", path)
}
return dir, nil
}
// NewNoteFormatter returns a NoteFormatter used to format notes with the given template.
func (n *Notebook) NewNoteFormatter(templateString string) (NoteFormatter, error) {
templates, err := n.templateLoaderFactory(n.Config.Note.Lang)
if err != nil {
return nil, err
}
template, err := templates.LoadTemplate(templateString)
if err != nil {
return nil, err
}
linkFormatter, err := NewLinkFormatter(n.Config.Format.Markdown, templates)
if err != nil {
return nil, err
}
return newNoteFormatter(n.Path, template, linkFormatter, n.osEnv(), n.fs)
}
// NewCollectionFormatter returns a CollectionFormatter used to format notes with the given template.
func (n *Notebook) NewCollectionFormatter(templateString string) (CollectionFormatter, error) {
templates, err := n.templateLoaderFactory(n.Config.Note.Lang)
if err != nil {
return nil, err
}
template, err := templates.LoadTemplate(templateString)
if err != nil {
return nil, err
}
return newCollectionFormatter(template)
}
// NewLinkFormatter returns a LinkFormatter used to generate internal links between notes.
func (n *Notebook) NewLinkFormatter() (LinkFormatter, error) {
templates, err := n.templateLoaderFactory(n.Config.Note.Lang)
if err != nil {
return nil, err
}
return NewLinkFormatter(n.Config.Format.Markdown, templates)
}