-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathnote.go
More file actions
319 lines (276 loc) · 8.96 KB
/
note.go
File metadata and controls
319 lines (276 loc) · 8.96 KB
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
package notes
import (
"bufio"
"bytes"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/mattn/go-runewidth"
"github.com/pkg/errors"
)
var (
reTitleBar = regexp.MustCompile("^=+$")
// Match to horizontal ruler of markdown: https://spec.commonmark.org/0.28/#thematic-break
// such as:
// ---
// ***
// ___
// - - - - -
reHorizontalRule = regexp.MustCompile(`^\s{0,3}(?:(?:-+\s*){3,}|(?:\*+\s*){3,}|(?:_+\s*){3,})$`)
closingComment = []byte("-->\n")
)
// MismatchCategoryError represents an error caused when a user specifies mismatched category
type MismatchCategoryError struct {
cat, pathcat, path string
}
func (e *MismatchCategoryError) Error() string {
return fmt.Sprintf("Category does not match to file path. Category is '%s' but it should be '%s' from its file path. File path is '%s'", e.cat, e.pathcat, e.path)
}
// Is returns if given error is a MismatchCategoryError or not
func (e *MismatchCategoryError) Is(target error) bool {
_, ok := target.(*MismatchCategoryError)
return ok
}
// Note represents a note stored on filesystem or will be created
type Note struct {
// Config is a configuration of notes command which was created by NewConfig()
Config *Config
// Category is a category string. It must not be empty
Category string
// Tags is tags of note. It can be empty and cannot contain comma
Tags []string
// Created is a datetime when note was created
Created time.Time
// File is a file name of the note
File string
// Title is a title string of the note. When the note is not created yet, it may be empty
Title string
}
// DirPath returns the absolute category directory path of the note
func (note *Note) DirPath() string {
return filepath.Join(note.Config.HomePath, filepath.FromSlash(note.Category))
}
// FilePath returns the absolute file path of the note
func (note *Note) FilePath() string {
return filepath.Join(note.Config.HomePath, filepath.FromSlash(note.Category), note.File)
}
// RelFilePath returns the relative file path of the note from home directory
func (note *Note) RelFilePath() string {
return filepath.Join(filepath.FromSlash(note.Category), note.File)
}
// TemplatePath resolves a path to template file of the note. If no template is found, it returns
// false as second return value
func (note *Note) TemplatePath() (string, bool) {
p := note.DirPath()
for {
f := filepath.Join(p, ".template.md")
if s, err := os.Stat(f); err == nil && !s.IsDir() {
return f, true
}
if p == note.Config.HomePath {
return "", false
}
p = filepath.Dir(p)
}
}
// Create creates a file of the note. When title is empty, file name omitting file extension is used
// for it. This function will fail when the file is already existing.
func (note *Note) Create() error {
var template []byte
if p, ok := note.TemplatePath(); ok {
b, err := os.ReadFile(p)
if err != nil {
return errors.Wrapf(err, "Cannot read template file %q", p)
}
template = b
}
var b bytes.Buffer
// Write title
title := note.Title
if title == "" {
title = strings.TrimSuffix(note.File, filepath.Ext(note.File))
}
b.WriteString(title + "\n")
b.WriteString(strings.Repeat("=", runewidth.StringWidth(title)) + "\n")
if template != nil && bytes.HasPrefix(template, []byte("-->")) {
// User expects metadata to be commented out. Start to surround metadata with comment
b.WriteString("<!--\n")
}
// Write metadata
fmt.Fprintf(&b, "- Category: %s\n", note.Category)
fmt.Fprintf(&b, "- Tags: %s\n", strings.Join(note.Tags, ", "))
fmt.Fprintf(&b, "- Created: %s\n", note.Created.Format(time.RFC3339))
if len(template) > 0 {
b.Write(template)
} else {
// When template is not inserted, it's better to separate metadata and body with empty line
b.WriteRune('\n')
}
d := note.DirPath()
if err := os.MkdirAll(d, 0755); err != nil {
return errors.Wrapf(err, "Could not create category directory '%s'", d)
}
p := filepath.Join(d, note.File)
if _, err := os.Stat(p); err == nil {
return errors.Errorf("Cannot create new note since file '%s' already exists. Please edit it", note.RelFilePath())
}
f, err := os.Create(p)
if err != nil {
return errors.Wrap(err, "Cannot create note file")
}
defer f.Close()
f.Write(b.Bytes())
return nil
}
// Open opens the note using an editor command user set. When user did not set any editor command
// with $NOTES_CLI_EDITOR, this method fails. Otherwise, an editor process is spawned with argument
// of path to the note file
func (note *Note) Open() error {
return openEditor(note.Config, note.FilePath())
}
// ReadBodyLines reads body of note until maxLines lines and returns it as string and number of lines as int
func (note *Note) ReadBodyLines(maxLines int) (string, int, error) {
path := note.FilePath()
f, err := os.Open(path)
if err != nil {
return "", 0, errors.Wrap(err, "Cannot open note file")
}
defer f.Close()
// Skip metadata
r := bufio.NewReader(f)
sawCat, sawTags, sawCreated := false, false, false
for {
t, err := r.ReadString('\n')
if strings.HasPrefix(t, "- Category: ") {
sawCat = true
} else if strings.HasPrefix(t, "- Tags:") {
sawTags = true
} else if strings.HasPrefix(t, "- Created: ") {
sawCreated = true
}
if sawCat && sawTags && sawCreated {
break
}
if err != nil {
return "", 0, errors.Wrapf(err, "Cannot read metadata of note file. Some metadata may be missing in '%s'", note.RelFilePath())
}
}
var buf bytes.Buffer
var readLines int
// Skip empty lines
for {
b, err := r.ReadBytes('\n')
if err != nil {
break
}
if len(b) > 1 && !reHorizontalRule.Match(b) && !bytes.Equal(b, closingComment) {
buf.Write(b)
readLines++
break
}
}
for numLines := 1; numLines < maxLines; numLines++ {
b, err := r.ReadBytes('\n')
if err != nil {
break
}
readLines++
buf.Write(b)
}
return buf.String(), readLines, nil
}
// NewNote creates a new note instance with given parameters and configuration. Category and file name
// cannot be empty. If given file name lacks file extension, it automatically adds ".md" to file name.
func NewNote(cat, tags, file, title string, cfg *Config) (*Note, error) {
cat = strings.TrimSpace(cat)
file = strings.TrimSpace(file)
title = strings.TrimSpace(title)
for _, part := range strings.Split(cat, "/") {
if err := validateDirname(part); err != nil {
return nil, errors.Wrapf(err, "Invalid category part '%s' as directory name", part)
}
}
if file == "" || strings.HasPrefix(file, ".") {
return nil, errors.New("File name cannot be empty and cannot start with '.'")
}
ts := []string{}
for _, t := range strings.Split(tags, ",") {
t = strings.TrimSpace(t)
if t != "" {
ts = append(ts, t)
}
}
if !strings.HasSuffix(file, ".md") {
file += ".md"
}
return &Note{cfg, cat, ts, time.Now(), file, title}, nil
}
// LoadNote reads note file from given path, parses it and creates Note instance. When given file path
// does not exist or when the file does note contain mandatory metadata ('Category', 'Tags' and 'Created'),
// this function returns an error
func LoadNote(path string, cfg *Config) (*Note, error) {
// This is necessary for macOS, where path contains NFD format
path = normPathNFD(path)
f, err := os.Open(path)
if err != nil {
return nil, errors.Wrap(err, "Cannot open note file")
}
defer f.Close()
note := &Note{Config: cfg}
note.File = filepath.Base(path)
s := bufio.NewScanner(f)
titleFound := false
for s.Scan() {
line := s.Text()
// First line is title
if !titleFound {
if reTitleBar.MatchString(line) {
if note.Title == "" {
note.Title = "(no title)"
}
titleFound = true
} else {
note.Title = strings.TrimSpace(line)
}
} else if strings.HasPrefix(line, "- Category: ") {
note.Category = strings.TrimSpace(line[12:])
} else if strings.HasPrefix(line, "- Tags:") {
tags := strings.Split(line[7:], ",")
note.Tags = make([]string, 0, len(tags))
for _, t := range tags {
t = strings.TrimSpace(t)
if t != "" {
note.Tags = append(note.Tags, t)
}
}
} else if strings.HasPrefix(line, "- Created: ") {
t, err := time.Parse(time.RFC3339, strings.TrimSpace(line[11:]))
if err != nil {
return nil, errors.Wrapf(err, "Cannot parse created date time as RFC3339 format: %s", line)
}
note.Created = t
}
if note.Category != "" && note.Tags != nil && !note.Created.IsZero() && note.Title != "" {
break
}
}
if err := s.Err(); err != nil {
return nil, errors.Wrapf(err, "Cannot read note file '%s'", canonPath(path))
}
if !titleFound {
return nil, errors.Errorf("No title found in note '%s'. Didn't you use '====' bar for h1 title?", canonPath(path))
}
if note.Category == "" || note.Tags == nil || note.Created.IsZero() {
return nil, errors.Errorf("Missing metadata in file '%s'. 'Category', 'Tags', 'Created' are mandatory", canonPath(path))
}
parent := filepath.Dir(path)
rel, err := filepath.Rel(cfg.HomePath, parent)
name := filepath.ToSlash(rel)
if err != nil || filepath.ToSlash(rel) != note.Category {
return note, &MismatchCategoryError{note.Category, name, path}
}
return note, nil
}