/
post.go
84 lines (68 loc) · 1.84 KB
/
post.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
package route
import (
"net/http"
"strings"
"github.com/crhuber/polarbearblog/app/lib/htmltemplate"
"github.com/crhuber/polarbearblog/app/model"
"github.com/matryer/way"
)
// Post -
type Post struct {
*Core
}
func registerPost(c *Post) {
c.Router.Get("/blog", c.index)
c.Router.Get("/:slug", c.show)
}
func (c *Post) index(w http.ResponseWriter, r *http.Request) (status int, err error) {
vars := make(map[string]interface{})
vars["tags"] = c.Storage.Site.Tags(true)
// Determine if there is query.
if q := r.URL.Query().Get("q"); len(q) > 0 {
vars["query"] = q
// Don't show tags when there is a filter.
delete(vars, "tags")
posts := make([]model.PostWithID, 0)
for _, v := range c.Storage.Site.PostsAndPages(true) {
match := false
for _, tag := range v.Tags {
if tag.Name == q {
match = true
break
}
}
if match {
posts = append(posts, v)
}
}
vars["posts"] = posts
} else {
vars["posts"] = c.Storage.Site.PublishedPosts()
}
return c.Render.Template(w, r, "base", "bloglist_index", vars)
}
func (c *Post) show(w http.ResponseWriter, r *http.Request) (status int, err error) {
slug := way.Param(r.Context(), "slug")
p := c.Storage.Site.PostBySlug(slug)
// Determine if in preview mode.
preview := false
if q := r.URL.Query().Get("preview"); len(q) > 0 && strings.ToLower(q) == "true" {
preview = true
}
// Show 404 if not published and not in preview mode.
if !p.Published && !preview {
return http.StatusNotFound, nil
}
vars := make(map[string]interface{})
// Don't show certain items on pages.
if !p.Page {
vars["title"] = p.Title
vars["pubdate"] = p.Timestamp
}
vars["tags"] = p.Tags
vars["canonical"] = p.Canonical
vars["id"] = p.ID
vars["posturl"] = p.URL
vars["metadescription"] = htmltemplate.PlaintextBlurb(p.Content)
return c.Render.Post(w, r, "base", p.Post, vars)
}