forked from rainycape/gondola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
article.go
317 lines (297 loc) · 7.4 KB
/
article.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
// Package article includes common types and functions for the articles app and command.
package article
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"gnd.la/form/input"
"gnd.la/util/stringutil"
"gopkgs.com/vfs.v1"
)
var (
propertyRe = regexp.MustCompile(`(?m:^\[(\w+)]\s?=\s?(.*)$)`)
propertySeparator = []byte("\n\n")
idKey = "id"
titleKey = "title"
slugKey = "slug"
synopsisKey = "synopsis"
updatedKey = "updated"
priorityKey = "priority"
timeFormats = []string{
"2006-01-02",
"2006-01-02 15:04",
"2006-01-02 15:04:05",
time.RFC822,
time.RFC822Z,
}
)
const (
maxSlugLength = 50
)
// Article represents a loaded article.
type Article struct {
// Id is the article identifier, used by the template
// function reverse_article.
Id string
// Titles includes the current title and then all the previous ones.
// The previous ones are kept for redirecting old URLs to the
// new one.
Titles []string
// Slugs contains the current slug and all the previous ones (for
// redirecting all URLs). If no slugs are present, the current title
Slugs []string
// is slugified.
// The article synopsys shown in the articles list. Might be empty.
Synopsis string
// A list of times when the article was created.
Updated []time.Time
// The priority to sort articles in the index. Articles with the same
// priority are sorted by title.
Priority int
// Text contains the article text, with any properties stripped.
Text []byte
// Properties contains unknown properties, to allow forward
// compatibility. It might be nil.
Properties map[string][]string
// Filename indicates the base filename where this article was loaded
// from, always using UNIX style directory separators. Note that it might be empty.
Filename string
}
// Title returns the current article title.
func (a *Article) Title() string {
if len(a.Titles) > 0 {
return a.Titles[0]
}
return ""
}
// Slug returns the current article slug.
func (a *Article) Slug() string {
if len(a.Slugs) > 0 {
return a.Slugs[0]
}
return stringutil.SlugN(a.Title(), maxSlugLength)
}
// AllSlugs returns the current and all the previous article
// slugs, used for redirects.
func (a *Article) AllSlugs() []string {
slugs := make([]string, 0, len(a.Slugs)+len(a.Titles))
slugs = append(slugs, a.Slugs...)
for _, v := range a.Titles {
slugs = append(slugs, stringutil.SlugN(v, maxSlugLength))
}
return slugs
}
// Created returns the article creation time, or the zero time.Time
// if there are no recorded updates to the article.
func (a *Article) Created() time.Time {
if len(a.Updated) == 0 {
return time.Time{}
}
t := time.Unix(int64((^uint32(0) >> 1)), 0)
for _, v := range a.Updated {
if v.Sub(t) < 0 {
t = v
}
}
return t
}
// LastUpdate returns the most recent update time, or the zero time.Time
// if there are no recorded updates to the article.
func (a *Article) LastUpdate() time.Time {
var t time.Time
for _, v := range a.Updated {
if v.Sub(t) > 0 {
t = v
}
}
return t
}
// Set sets a property in the article. For slice properties, it prepends the
// new value to the existing ones.
func (a *Article) Set(key string, value string) error {
return a.add(key, value, false)
}
func (a *Article) add(key string, value string, begin bool) error {
var err error
switch strings.ToLower(key) {
case idKey:
err = input.Parse(value, &a.Id)
case titleKey:
if begin {
a.Titles = append([]string{value}, a.Titles...)
} else {
a.Titles = append(a.Titles, value)
}
case slugKey:
if begin {
a.Slugs = append([]string{value}, a.Slugs...)
} else {
a.Slugs = append(a.Slugs, value)
}
case synopsisKey:
a.Synopsis = value
case updatedKey:
var t time.Time
switch strings.ToLower(value) {
case "now":
t = time.Now().UTC()
case "today":
t = time.Now().UTC().Truncate(24 * time.Hour)
default:
for _, v := range timeFormats {
t, err = time.Parse(v, value)
if err == nil {
break
}
}
}
if err == nil {
if begin {
a.Updated = append([]time.Time{t}, a.Updated...)
} else {
a.Updated = append(a.Updated, t)
}
}
case priorityKey:
err = input.Parse(value, &a.Priority)
default:
if a.Properties == nil {
a.Properties = make(map[string][]string)
}
a.Properties[key] = append(a.Properties[key], value)
}
return err
}
func (a *Article) WriteTo(w io.Writer) (int64, error) {
var buf bytes.Buffer
buf.Write(a.Text)
buf.WriteByte('\n')
buf.Write(propertySeparator)
if a.Id != "" {
a.writeProperty(&buf, idKey, a.Id)
}
for _, v := range a.Titles {
a.writeProperty(&buf, titleKey, v)
}
for _, v := range a.Slugs {
a.writeProperty(&buf, slugKey, v)
}
if a.Synopsis != "" {
a.writeProperty(&buf, synopsisKey, a.Synopsis)
}
for _, v := range a.Updated {
a.writeProperty(&buf, updatedKey, v)
}
if a.Priority != 0 {
a.writeProperty(&buf, priorityKey, a.Priority)
}
for k, v := range a.Properties {
for _, prop := range v {
a.writeProperty(&buf, k, prop)
}
}
n, err := w.Write(buf.Bytes())
return int64(n), err
}
func (a *Article) writeProperty(buf *bytes.Buffer, key string, value interface{}) {
var s string
switch x := value.(type) {
case []byte:
s = string(x)
case string:
s = x
case int:
s = strconv.Itoa(x)
case time.Time:
var format string
if x.Location() != nil && x.Location() != time.UTC {
format = time.RFC822Z
} else {
if x.Second() == 0 {
if x.Hour() == 0 && x.Minute() == 0 {
format = "2006-01-02"
} else {
format = "2006-01-02 15:04"
}
} else {
format = "2006-01-02 15:04:05"
}
}
s = x.Format(format)
default:
panic(fmt.Errorf("can't write property %q %v of type %T", key, value, value))
}
if strings.Contains(s, "\n") {
s = strconv.Quote(s)
}
fmt.Fprintf(buf, "[%s] = %s\n", key, s)
}
// New returns a new Article by decoding it from the given io.Reader.
func New(r io.Reader) (*Article, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
article := &Article{}
var text, props []byte
// Find last empty line
sep := bytes.LastIndex(data, propertySeparator)
if sep >= 0 {
text = data[:sep]
props = data[sep:]
} else {
text = data
}
for _, v := range propertyRe.FindAllSubmatch(props, -1) {
key := string(v[1])
value := string(v[2])
if value != "" && value[0] == '"' && value[len(value)-1] == '"' {
val, err := strconv.Unquote(value)
if err != nil {
return nil, fmt.Errorf("error unquoting %q: %s", value, err)
}
value = val
}
if err := article.add(key, value, false); err != nil {
return nil, fmt.Errorf("error setting key %q with value %q: %s", key, string(value), err)
}
}
text = append(text, propertyRe.ReplaceAll(props, nil)...)
article.Text = bytes.TrimSpace(text)
return article, nil
}
// Open returns a new Article loading it from the given filename in the given fs.
func Open(fs vfs.VFS, filename string) (*Article, error) {
f, err := fs.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
article, err := New(f)
if err != nil {
return nil, err
}
article.Filename = filename
return article, nil
}
// OpenFile returns a new Article loading it from the file at the given path.
func OpenFile(filename string) (*Article, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
article, err := New(f)
if err != nil {
return nil, err
}
article.Filename = filepath.ToSlash(filename)
return article, nil
}