-
Notifications
You must be signed in to change notification settings - Fork 405
/
Copy pathblog.gno
398 lines (334 loc) · 7.95 KB
/
blog.gno
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package blog
import (
"chain"
"strconv"
"strings"
"time"
"gno.land/p/demo/avl"
"gno.land/p/demo/mux"
"gno.land/p/demo/ufmt"
)
type Blog struct {
Title string
Prefix string // i.e. r/gnoland/blog:
Posts avl.Tree // slug -> *Post
PostsPublished avl.Tree // published-date -> *Post
PostsAlphabetical avl.Tree // title -> *Post
NoBreadcrumb bool
}
func (b Blog) RenderLastPostsWidget(limit int) string {
if b.PostsPublished.Size() == 0 {
return "No posts."
}
output := ""
i := 0
b.PostsPublished.ReverseIterate("", "", func(key string, value any) bool {
p := value.(*Post)
output += ufmt.Sprintf("- [%s](%s)\n", p.Title, p.URL())
i++
return i >= limit
})
return output
}
func (b Blog) RenderHome(res *mux.ResponseWriter, req *mux.Request) {
if !b.NoBreadcrumb {
res.Write(breadcrumb([]string{b.Title}))
}
if b.Posts.Size() == 0 {
res.Write("No posts.")
return
}
res.Write("<div class='columns-3'>")
b.PostsPublished.ReverseIterate("", "", func(key string, value any) bool {
post := value.(*Post)
res.Write(post.RenderListItem())
return false
})
res.Write("</div>")
// FIXME: tag list/cloud.
}
func (b Blog) RenderPost(res *mux.ResponseWriter, req *mux.Request) {
slug := req.GetVar("slug")
post, found := b.Posts.Get(slug)
if !found {
res.Write("404")
return
}
p := post.(*Post)
res.Write("<main class='gno-tmpl-page'>" + "\n\n")
res.Write("# " + p.Title + "\n\n")
res.Write(p.Body + "\n\n")
res.Write("---\n\n")
res.Write(p.RenderTagList() + "\n\n")
res.Write(p.RenderAuthorList() + "\n\n")
res.Write(p.RenderPublishData() + "\n\n")
res.Write("---\n")
res.Write("<details><summary>Comment section</summary>\n\n")
// comments
p.Comments.ReverseIterate("", "", func(key string, value any) bool {
comment := value.(*Comment)
res.Write(comment.RenderListItem())
return false
})
res.Write("</details>\n")
res.Write("</main>")
}
func (b Blog) RenderTag(res *mux.ResponseWriter, req *mux.Request) {
slug := req.GetVar("slug")
if slug == "" {
res.Write("404")
return
}
if !b.NoBreadcrumb {
breadStr := breadcrumb([]string{
ufmt.Sprintf("[%s](%s)", b.Title, b.Prefix),
"t",
slug,
})
res.Write(breadStr)
}
nb := 0
b.Posts.Iterate("", "", func(key string, value any) bool {
post := value.(*Post)
if !post.HasTag(slug) {
return false
}
res.Write(post.RenderListItem())
nb++
return false
})
if nb == 0 {
res.Write("No posts.")
}
}
func (b Blog) Render(path string) string {
router := mux.NewRouter()
router.HandleFunc("", b.RenderHome)
router.HandleFunc("p/{slug}", b.RenderPost)
router.HandleFunc("t/{slug}", b.RenderTag)
return router.Render(path)
}
func (b *Blog) NewPost(publisher chain.Address, slug, title, body, pubDate string, authors, tags []string) error {
if _, found := b.Posts.Get(slug); found {
return ErrPostSlugExists
}
var parsedTime time.Time
var err error
if pubDate != "" {
parsedTime, err = time.Parse(time.RFC3339, pubDate)
if err != nil {
return err
}
} else {
// If no publication date was passed in by caller, take current block time
parsedTime = time.Now()
}
post := &Post{
Publisher: publisher,
Authors: authors,
Slug: slug,
Title: title,
Body: body,
Tags: tags,
CreatedAt: parsedTime,
}
return b.prepareAndSetPost(post, false)
}
func (b *Blog) prepareAndSetPost(post *Post, edit bool) error {
post.Title = strings.TrimSpace(post.Title)
post.Body = strings.TrimSpace(post.Body)
if post.Title == "" {
return ErrPostTitleMissing
}
if post.Body == "" {
return ErrPostBodyMissing
}
if post.Slug == "" {
return ErrPostSlugMissing
}
post.Blog = b
post.UpdatedAt = time.Now()
trimmedTitleKey := getTitleKey(post.Title)
pubDateKey := getPublishedKey(post.CreatedAt)
if !edit {
// Cannot have two posts with same title key
if _, found := b.PostsAlphabetical.Get(trimmedTitleKey); found {
return ErrPostTitleExists
}
// Cannot have two posts with *exact* same timestamp
if _, found := b.PostsPublished.Get(pubDateKey); found {
return ErrPostPubDateExists
}
}
// Store post under keys
b.PostsAlphabetical.Set(trimmedTitleKey, post)
b.PostsPublished.Set(pubDateKey, post)
b.Posts.Set(post.Slug, post)
return nil
}
func (b *Blog) RemovePost(slug string) {
p, exists := b.Posts.Get(slug)
if !exists {
panic("post with specified slug doesn't exist")
}
post := p.(*Post)
titleKey := getTitleKey(post.Title)
publishedKey := getPublishedKey(post.CreatedAt)
_, _ = b.Posts.Remove(slug)
_, _ = b.PostsAlphabetical.Remove(titleKey)
_, _ = b.PostsPublished.Remove(publishedKey)
}
func (b *Blog) GetPost(slug string) *Post {
post, found := b.Posts.Get(slug)
if !found {
return nil
}
return post.(*Post)
}
type Post struct {
Blog *Blog
Slug string // FIXME: save space?
Title string
Body string
CreatedAt time.Time
UpdatedAt time.Time
Comments avl.Tree
Authors []string
Publisher chain.Address
Tags []string
CommentIndex int
}
func (p *Post) Update(title, body, publicationDate string, authors, tags []string) error {
p.Title = title
p.Body = body
p.Tags = tags
p.Authors = authors
parsedTime, err := time.Parse(time.RFC3339, publicationDate)
if err != nil {
return err
}
p.CreatedAt = parsedTime
return p.Blog.prepareAndSetPost(p, true)
}
func (p *Post) AddComment(author chain.Address, comment string) error {
if p == nil {
return ErrNoSuchPost
}
p.CommentIndex++
commentKey := strconv.Itoa(p.CommentIndex)
comment = strings.TrimSpace(comment)
p.Comments.Set(commentKey, &Comment{
Post: p,
CreatedAt: time.Now(),
Author: author,
Comment: comment,
})
return nil
}
func (p *Post) DeleteComment(index int) error {
if p == nil {
return ErrNoSuchPost
}
commentKey := strconv.Itoa(index)
p.Comments.Remove(commentKey)
return nil
}
func (p *Post) HasTag(tag string) bool {
if p == nil {
return false
}
for _, t := range p.Tags {
if t == tag {
return true
}
}
return false
}
func (p *Post) RenderListItem() string {
if p == nil {
return "error: no such post\n"
}
output := "<div>\n\n"
output += ufmt.Sprintf("### [%s](%s)\n", p.Title, p.URL())
// output += ufmt.Sprintf("**[Learn More](%s)**\n\n", p.URL())
output += " " + p.CreatedAt.Format("02 Jan 2006")
// output += p.Summary() + "\n\n"
// output += p.RenderTagList() + "\n\n"
output += "\n"
output += "</div>"
return output
}
// Render post tags
func (p *Post) RenderTagList() string {
if p == nil {
return "error: no such post\n"
}
if len(p.Tags) == 0 {
return ""
}
output := "Tags: "
for idx, tag := range p.Tags {
if idx > 0 {
output += " "
}
tagURL := p.Blog.Prefix + "t/" + tag
output += ufmt.Sprintf("[#%s](%s)", tag, tagURL)
}
return output
}
// Render authors if there are any
func (p *Post) RenderAuthorList() string {
out := "Written"
if len(p.Authors) != 0 {
out += " by "
for idx, author := range p.Authors {
out += author
if idx < len(p.Authors)-1 {
out += ", "
}
}
}
out += " on " + p.CreatedAt.Format("02 Jan 2006")
return out
}
func (p *Post) RenderPublishData() string {
out := "Published "
if p.Publisher != "" {
out += "by " + p.Publisher.String() + " "
}
out += "to " + p.Blog.Title
return out
}
func (p *Post) URL() string {
if p == nil {
return p.Blog.Prefix + "404"
}
return p.Blog.Prefix + "p/" + p.Slug
}
func (p *Post) Summary() string {
if p == nil {
return "error: no such post\n"
}
// FIXME: better summary.
lines := strings.Split(p.Body, "\n")
if len(lines) <= 3 {
return p.Body
}
return strings.Join(lines[0:3], "\n") + "..."
}
type Comment struct {
Post *Post
CreatedAt time.Time
Author chain.Address
Comment string
}
func (c Comment) RenderListItem() string {
output := "<h5>"
output += c.Comment + "\n\n"
output += "</h5>"
output += "<h6>"
output += ufmt.Sprintf("by %s on %s", c.Author, c.CreatedAt.Format(time.RFC822))
output += "</h6>\n\n"
output += "---\n\n"
return output
}