-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
129 lines (111 loc) · 2.81 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
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
package blog
import (
"html/template"
"strconv"
"time"
"github.com/blevesearch/bleve"
"github.com/pkg/errors"
)
const (
layoutUnix = "Mon Jan 2 15:04:05 -07 2006"
layoutISO = "2006-01-02"
)
// PostService is the interface that wraps method related to a blog post
type PostService interface {
GetAllPosts() []*Post
GetPostByURI(uri string) *Post
GetLatestPosts(days int) []*Post
GetRelatedPosts(currentPost *Post) []*Post
GetAllCategories() map[string][]*Post
GetPostsPerTag() map[string]int
GetAllTags() []string
GetImageAddresses() []string
GetPostURIByImage() map[string]string
GetPostsByCategory(category string) []*Post
GetPostsByTag(tag string) []*Post
GetPreviousAndNextPost(currentPost *Post) (previousPost, nextPost *Post)
GetYears() []string
GetMonthsInYear() map[string][]string
GetPostsByDate(year, month, date string) []*Post
GetPostsByMonth() map[string]map[string][]*Post
GetPostsByYear(year string) []*Post
}
// SearchService is the interface that wraps methods related to search
type SearchService interface {
GetIndex() bleve.Index
Index(*Post, *bleve.Batch) error
Search(value string) ([]*Post, error)
CloseIndex() error
}
// Post represents a blog post
type Post struct {
ID int
URI string
Title string
Date publishDate
Description string
Images []string
Content template.HTML
Summary template.HTML
Truncated bool
Categories []string
Tags []string
HasPrev bool
HasNext bool
}
type publishDate struct {
time.Time
}
func (d *publishDate) UnmarshalYAML(unmarshal func(interface{}) error) error {
var pd string
if err := unmarshal(&pd); err != nil {
return err
}
layouts := []string{layoutUnix, layoutISO}
for _, layout := range layouts {
date, err := time.Parse(layout, pd)
if err == nil {
d.Time = date
return nil
}
}
return errors.Errorf("Unrecognized date format: %s", pd)
}
// ToISODate converts date to ISO layout
func ToISODate(d publishDate) string {
return d.Time.Format(layoutISO)
}
// ToMonthName converts month from string number to name
func ToMonthName(month string) string {
m, _ := strconv.Atoi(month)
return time.Month(m).String()
}
// Contains checks if a slice of strings contains a string
func Contains(s []string, e string) bool {
for _, v := range s {
if v == e {
return true
}
}
return false
}
// GetYear gets year from publish date
func (d *publishDate) GetYear() string {
return strconv.Itoa(d.Time.Year())
}
// GetMonth gets month from publish date
func (d *publishDate) GetMonth() string {
month := int(d.Time.Month())
if month < 10 {
return "0" + strconv.Itoa(month)
}
return strconv.Itoa(month)
}
// GetDay gets day from publish date
func (d *publishDate) GetDay() string {
day := d.Time.Day()
if day < 10 {
return "0" + strconv.Itoa(day)
}
return strconv.Itoa(day)
}