-
Notifications
You must be signed in to change notification settings - Fork 0
/
discover.go
97 lines (83 loc) · 2.21 KB
/
discover.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
package bearnotes
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strings"
"golang.org/x/text/unicode/norm"
"gopkg.in/yaml.v3"
)
// DiscoverNotes walk through recursively the Bear notes directory to find notes.
// It generates a tag configuration file, suitable for migration.
func DiscoverNotes(notesDir string, tagFile string) error {
var tags map[string]TagOptions = make(map[string]TagOptions)
var imageCount int
var fileCount int
var noteCount int
fmt.Printf("Looking for Bear notes into %s...\n", notesDir)
err := filepath.Walk(notesDir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Printf("stat: %s: %s\n", path, err)
return nil
}
if strings.HasSuffix(info.Name(), ".md") && !info.IsDir() { // it's a Markdown file!
content, err := ioutil.ReadFile(path)
if err != nil {
log.Printf("open: %s: %s\n", path, err)
return nil
}
note := LoadNote(string(content))
imageCount += len(note.Images)
fileCount += len(note.Files)
noteCount++
for _, tag := range note.Tags {
// just to be safe, normalize the tag name since it is used
// afterwards to generate paths and filenames
tag.Name = norm.NFC.String(tag.Name)
// all tags are lowercase in Bear
tagName := strings.ToLower(tag.Name)
tagEntry, ok := tags[tagName]
if !ok {
tags[tagName] = NewTagOptions(tag)
} else {
tagEntry.count++
tags[tagName] = tagEntry
}
}
}
return nil
})
if err != nil {
return err
}
fmt.Printf("Found %d notes, %d embedded images, %d attachments and %d unique tags.\n", noteCount, imageCount, fileCount, len(tags))
fmt.Println("")
// Displays all tags, sorted by their name
fmt.Println("Tag list:")
tagNames := make([]string, len(tags))
i := 0
for k := range tags {
tagNames[i] = k
i++
}
sort.Strings(tagNames)
for _, tagName := range tagNames {
fmt.Printf("#%s\n", tagName)
}
// Write the tag configuration file
fmt.Println("")
fmt.Printf("Writing all tags into %s...\n", tagFile)
fileContent, err := yaml.Marshal(tags)
if err != nil {
return err
}
err = ioutil.WriteFile(tagFile, fileContent, 0644)
if err != nil {
return err
}
return nil
}