-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
171 lines (159 loc) · 3.81 KB
/
index.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
// index is used to keep in memory copy of post metadata
// mainly used for sorting so that blog posts are displayed in order
package main
import (
"fmt"
"html"
"log"
"math"
"net/url"
"os"
"path"
"regexp"
"sort"
"strings"
"sync"
"time"
)
var (
timeFormat = "2006-01-02 15:04"
publishedRe = regexp.MustCompile(`<!--.*published="(.+)".*-->`)
authorRe = regexp.MustCompile(`<!--.*author="(.+)".*-->`)
titleRe = regexp.MustCompile(`(?m)^#\s+(.+)`)
)
type postIndex struct {
pubSorted []string
metaData map[string]postMetadata
pageLast int
latestPosts string
sync.RWMutex
}
type postMetadata struct {
author string
published time.Time
modified time.Time
title string
url string
}
func (idx *postIndex) rescan() {
start := time.Now()
idx.Lock()
idx.metaData = make(map[string]postMetadata)
idx.Unlock()
d, err := os.ReadDir(path.Join(*rootDir, *postsDir))
if err != nil {
log.Fatal(err)
}
for _, f := range d {
idx.addOnly(f.Name())
}
idx.sequence()
idx.RLock()
defer idx.RUnlock()
log.Printf("idx: indexed %v articles, sequenced: %+v, last page is %v, duration %v", len(idx.pubSorted), idx.pubSorted, idx.pageLast, time.Since(start))
}
func (idx *postIndex) sequence() {
seq := []string{}
idx.Lock()
defer idx.Unlock()
for n := range idx.metaData {
seq = append(seq, n)
}
sort.Slice(seq, func(i, j int) bool {
return idx.metaData[seq[j]].published.Before(idx.metaData[seq[i]].published)
})
idx.pubSorted = seq
idx.pageLast = int(math.Ceil(float64(len(seq))/float64(*artPerPg)) - 1)
idx.latestPosts = ""
for i, s := range seq {
if i >= *ltsPosts {
break
}
if idx.metaData[s].published.IsZero() {
continue
}
idx.latestPosts += fmt.Sprintf("» <a href=\"/%v\">%v</a><br>\n", url.QueryEscape(idx.metaData[s].url), html.EscapeString(idx.metaData[s].title))
}
}
func (idx *postIndex) addOnly(name string) bool {
if name[0:1] == "." || !strings.HasSuffix(name, ".md") {
return false
}
fullName := path.Join(*rootDir, *postsDir, name)
fi, err := os.Stat(fullName)
if err != nil {
log.Printf("unable to stat %q: %v", fullName, err)
return false
}
if fi.IsDir() {
return false
}
a, err := os.ReadFile(fullName)
if err != nil {
log.Printf("error reading %v: %v", name, err)
return false
}
author := authorRe.FindSubmatch(a)
if len(author) < 2 {
author = [][]byte{[]byte(""), []byte("unknown")}
}
m := publishedRe.FindSubmatch(a)
if len(m) < 1 {
m = [][]byte{[]byte(""), []byte("")}
}
title := titleRe.FindSubmatch(a)
if len(title) < 2 {
title = [][]byte{[]byte(""), []byte(strings.TrimSuffix(name, ".md"))}
}
t, err := time.Parse(timeFormat, string(m[1]))
if err != nil {
t = time.Time{}
}
idx.Lock()
defer idx.Unlock()
// TODO: add title from regexp
idx.metaData[name] = postMetadata{
modified: fi.ModTime(),
published: t,
author: string(author[1]),
title: strings.TrimSuffix(string(title[1]), "\r"),
url: url.QueryEscape(strings.TrimSuffix(name, ".md")),
}
log.Printf("idx: added %q (%v)", name, idx.metaData[name].title)
return true
}
func (idx *postIndex) add(name string) {
idx.addOnly(name)
idx.sequence()
}
func (idx *postIndex) update(name string) {
idx.delete(name)
idx.add(name)
}
func (idx *postIndex) rename(old, new string) {
idx.Lock()
defer idx.Unlock()
for n, p := range idx.pubSorted {
if p != old {
continue
}
idx.pubSorted[n] = new
}
idx.metaData[new] = idx.metaData[old]
delete(idx.metaData, old)
log.Printf("idx: rename %q to %q, new index: %+v", old, new, idx.pubSorted)
}
func (pi *postIndex) delete(name string) {
pi.Lock()
defer pi.Unlock()
seq := []string{}
for _, s := range pi.pubSorted {
if s == name {
continue
}
seq = append(seq, s)
}
pi.pubSorted = seq
delete(pi.metaData, name)
log.Printf("idx: deleted post %v, new index: %+v", name, pi.pubSorted)
}