-
Notifications
You must be signed in to change notification settings - Fork 13
/
strategies.go
62 lines (48 loc) · 1.78 KB
/
strategies.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
package collection
import (
"time"
"github.com/osteele/gojekyll/utils"
)
// A collectionStrategy encapsulates behavior differences between the _post
// collection and other collection.
type collectionStrategy interface {
addDate(filename string, fm map[string]interface{})
collectible(filename string) bool
defaultPermalinkPattern() string
future(filename string) bool
}
func (c *Collection) strategy() collectionStrategy {
if c.IsPostsCollection() {
return postsStrategy{}
}
return defaultStrategy{}
}
type defaultStrategy struct{}
func (s defaultStrategy) addDate(_ string, _ map[string]interface{}) {}
func (s defaultStrategy) collectible(filename string) bool { return true }
func (s defaultStrategy) future(filename string) bool { return false }
type postsStrategy struct{}
func (s postsStrategy) addDate(filename string, fm map[string]interface{}) {
if t, title, found := utils.FilenameDate(filename); found {
fm["date"] = t
fm["title"] = title
}
}
func (s postsStrategy) collectible(filename string) bool {
_, _, ok := utils.FilenameDate(filename)
return ok
}
func (s postsStrategy) future(filename string) bool {
t, _, ok := utils.FilenameDate(filename)
return ok && t.After(time.Now())
}
// DefaultCollectionPermalinkPattern is the default permalink pattern for pages in the posts collection
const DefaultCollectionPermalinkPattern = "/:collection/:path:output_ext"
// DefaultPostsCollectionPermalinkPattern is the default collection permalink pattern
const DefaultPostsCollectionPermalinkPattern = "/:categories/:year/:month/:day/:title.html"
func (s defaultStrategy) defaultPermalinkPattern() string {
return DefaultCollectionPermalinkPattern
}
func (s postsStrategy) defaultPermalinkPattern() string {
return DefaultPostsCollectionPermalinkPattern
}