This repository has been archived by the owner on Apr 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathpost.go
68 lines (59 loc) · 1.62 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
package main
import (
"errors"
"fmt"
"path/filepath"
"strings"
"time"
)
var (
ErrBadPostName = errors.New("Invalid post name. Expecting format YYYY-MM-DD-name-of-post.markdown")
)
// ParseParse will parse a file with front-end YAML and markup content, and
// return a key-value Post structure.
func ParsePost(fn string) (Page, error) {
post, err := ParsePage(fn)
if err != nil {
return nil, err
}
// parse the Date and Title from the post's file name
_,f := filepath.Split(fn)
t, d, err := parsePostName(f)
if err != nil {
return nil, err
}
// set the post's date and title
// ignore the title if the user specified in the front-end yaml
post["date"] = d
if post.GetTitle() == "" {
post["title"] = t
}
// figoure out the Posts permalink
mon := fmt.Sprintf("%02d", d.Month())
day := fmt.Sprintf("%02d", d.Day())
year := fmt.Sprintf("%02d", d.Year())
name := replaceExt(f, ".html")
post["id"] = filepath.Join(year, mon, day, f) // TODO try to remember why I need this field
post["url"]= filepath.Join(year, mon, day, name[11:])
return post, nil
}
// Helper function to parse a blog posts filename, which is in the following
// format: YYYY-MM-DD-name-of-post.markdown
//
// the name of the post will be separated from the time of the post, both of
// which are returned by this function
func parsePostName(fn string) (name string, date time.Time, err error) {
if len(fn) < 12 {
err = ErrBadPostName
return
}
date, err = time.Parse("2006-01-02", fn[:10])
if err != nil {
return
}
name = fn[11:]
name = removeExt(name)
name = strings.Replace(name, "-", " ", -1)
name = strings.ToTitle(name)
return
}