forked from scott-linder/blog
/
blog.go
192 lines (168 loc) · 5.45 KB
/
blog.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
// Package blog is a simple blog based on Gorilla mux and blackfriday markdown.
package blog
import (
"os"
"fmt"
"log"
"time"
"strings"
"net/http"
"html/template"
"path/filepath"
"io/ioutil"
"github.com/gorilla/mux"
"github.com/russross/blackfriday"
)
const (
// dateFormat is the expected format of post filename dates.
dateFormat = "2006-01-02"
// fieldSeparator is what is used to split post filenames.
fieldSeparator = "."
)
// Blog is a blog site Handler..
type Blog struct {
// Router is the mux router which Blog resides under.
Router *mux.Router
// TplPath is the relative path to the HTML template for the Blog.
TplPath string
// PostDir is the relative path to the directory containing blog posts.
PostDir string
// PageSize is the number of posts to a page.
PageSize int
}
// NewBlog returns a new Blog instance.
func NewBlog(router *mux.Router, tplPath, postDir string,
pageSize int) (blog *Blog) {
blog = &Blog{Router: router, TplPath: tplPath,
PostDir: postDir, PageSize: pageSize}
// Hook up paths for the main blog and post permalinks.
router.Handle("/", blog)
router.Handle("/post/{year:[0-9]+}/{month:[0-9]+}/{day:[0-9]+}/{name}/",
blog).Name("post")
return
}
func (self Blog) ServeHTTP(w http.ResponseWriter, r *http.Request) {
tpl, err := template.ParseFiles(self.TplPath)
if err != nil {
log.Fatal(err)
}
// data is the template data for the Blog.
data := struct {
// Posts is the slice of posts for this blog page.
Posts []post
} {}
// Pull out {…} variables from muxer.
vars := mux.Vars(r)
switch mux.CurrentRoute(r).GetName() {
case "post":
data.Posts = make([]post, 1)
newPost, err := self.getPost(vars["year"], vars["month"],
vars["day"], vars["name"])
if err != nil {
log.Fatal(err)
}
data.Posts[0] = *newPost
default:
data.Posts, err = self.getPage(0)
}
tpl.Execute(w, data)
}
// post is a single blog entry.
type post struct {
// Name is the name/title of the post.
Name string
// Body is the content of the post.
Body template.HTML
// Date is the date the post was published.
Date time.Time
// Permalink is a permanent URL pointing to this post.
Permalink string
}
// newPost returns a new post instance.
func (self Blog) newPost(path string, info os.FileInfo) (*post, error) {
// Extract the fields from the filename, assuming a format of:
// YYYY-MM-DD.NAME.md
// which produces a slice of:
// [0] => YYYY-MM-DD
// [1] => NAME
// [2] => .md
// where len(fields) >= 2
nameFields := strings.Split(info.Name(), fieldSeparator)
var dateField, nameField string
if len(nameFields) >= 2 {
dateField = nameFields[0]
nameField = nameFields[1]
}
postDate, err := time.ParseInLocation(dateFormat, dateField, time.UTC)
if err != nil {
return nil, err
}
postName := nameField
// Create a permalink to this post using the named route "post".
postPermalinkURL, err := self.Router.Get("post").
URL("year", fmt.Sprintf("%d", postDate.Year()),
"month", fmt.Sprintf("%d", postDate.Month()),
"day", fmt.Sprintf("%d", postDate.Day()),
"name", postName)
if err != nil {
return nil, err
}
postPermalink := postPermalinkURL.String()
// Get file, read contents, render as HTML.
postFile, err := os.Open(path)
if err != nil {
return nil, err
}
postMarkdown, err := ioutil.ReadAll(postFile)
if err != nil {
return nil, err
}
postHTML := template.HTML(blackfriday.MarkdownCommon(postMarkdown))
return &post{Name: postName, Body: postHTML, Date: postDate,
Permalink: postPermalink}, nil
}
// getPost retrieves a post from the given identifying information.
func (self Blog) getPost(year, month, day, name string) (*post, error) {
// Use permalink info to construct file path.
path := self.PostDir + fmt.Sprintf("%04s-%02s-%02s.%s.md", year, month, day, name);
// Make sure the file exists and get info.
info, err := os.Stat(path)
if err != nil {
return nil, err
}
newPost, err := self.newPost(path, info)
if err != nil {
return nil, err
}
return newPost, nil
}
// getPage retrieves one page of posts.
// XXX: pagination not actually implemented yet; page parameter ignored.
func (self Blog) getPage(page int) ([]post, error) {
var posts []post
// A function to walk the post directory and put together our slice.
buildPosts := func(path string, info os.FileInfo, err error) error {
if err != nil {
// Don't die, just note problem and move on.
log.Printf("Error walking posts: %v\n", err)
return nil
}
if !info.IsDir() {
newPost, err := self.newPost(path, info)
if err != nil {
return err
}
posts = append(posts, *newPost)
}
return nil
}
err := filepath.Walk(self.PostDir, buildPosts)
if err != nil {
return nil, err
}
// Reverse post order so most recent is shown first.
for i, j := 0, len(posts) - 1; i < j; i, j = i + 1, j - 1 {
posts[i], posts[j] = posts[j], posts[i]
}
return posts, nil
}