-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcache.go
144 lines (126 loc) · 2.9 KB
/
cache.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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)
type Link struct {
Filename string
Title string
LineNumber int
}
type Note struct {
Filename string
Title string
IsLabel bool
OutgoingLinks []*Link
IncomingLinks []*Link
Ctime time.Time
Mtime time.Time
Lines int
Words int
Chars int
}
var noteCache map[string]*Note
var fileRegex = regexp.MustCompile(`^[0-9a-f]{8}\.md$`)
var linkRegex = regexp.MustCompile(`\]\(([0-9a-f]{8}\.md)\)`)
func populateCache(dir string) {
if noteCache != nil {
return
}
noteCache = make(map[string]*Note)
files, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatalf("could not read directory: %s\n", err)
}
for _, file := range files {
filename := file.Name()
if !file.IsDir() && fileRegex.MatchString(filename) {
note, err := readNote(dir, filename)
if err != nil {
log.Fatalf("could not read note: %s\n", err)
}
noteCache[filename] = note
}
}
for _, note := range noteCache {
for _, link := range note.OutgoingLinks {
if targetNote, exists := noteCache[link.Filename]; exists {
link.Title = targetNote.Title
targetNote.IncomingLinks = append(targetNote.IncomingLinks, &Link{
Filename: note.Filename,
Title: note.Title,
LineNumber: link.LineNumber,
})
}
}
}
}
func readNote(dir string, filename string) (*Note, error) {
path := filepath.Join(dir, filename)
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("could not open file: %s", err)
}
defer file.Close()
info, err := file.Stat()
if err != nil {
return nil, fmt.Errorf("could not get file info: %s", err)
}
mtime := info.ModTime().Truncate(time.Second)
hexTime := strings.TrimSuffix(filename, ".md")
unixTime, err := strconv.ParseInt(hexTime, 16, 64)
if err != nil {
return nil, err
}
ctime := time.Unix(unixTime, 0)
var title string
var isLabel bool
var outgoingLinks []*Link
var lines, words, chars int
scanner := bufio.NewScanner(file)
lineNumber := 0
for scanner.Scan() {
lineNumber++
line := scanner.Text()
if line != "" {
lines++
words += len(strings.Fields(line))
chars += len(line)
}
if title == "" {
title = strings.TrimPrefix(line, "# ")
isLabel = len(strings.Fields(title)) == 1
continue
}
matches := linkRegex.FindAllStringSubmatch(line, -1)
for _, match := range matches {
outgoingLinks = append(outgoingLinks, &Link{LineNumber: lineNumber, Filename: match[1]})
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
if title == "" {
title = "untitled"
}
note := &Note{
Filename: filename,
Title: title,
IsLabel: isLabel,
OutgoingLinks: outgoingLinks,
Ctime: ctime,
Mtime: mtime,
Lines: lines,
Words: words,
Chars: chars,
}
return note, nil
}