-
Notifications
You must be signed in to change notification settings - Fork 13
/
strategies.go
71 lines (56 loc) · 2 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
63
64
65
66
67
68
69
70
71
package collection
import (
"time"
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/utils"
)
// A collectionStrategy encapsulates behavior differences between the `_post`
// collection and other collections.
type collectionStrategy interface {
defaultPermalinkPattern(*config.Config) string
isCollectible(filename string) bool
isFuture(filename string) bool
parseFilename(string, map[string]interface{})
}
func (c *Collection) strategy() collectionStrategy {
if c.IsPostsCollection() {
return postsStrategy{}
}
return defaultStrategy{}
}
type defaultStrategy struct{}
func (s defaultStrategy) parseFilename(_ string, fm map[string]interface{}) {
// de facto
fm["draft"] = false
}
func (s defaultStrategy) isCollectible(string) bool { return true }
func (s defaultStrategy) isFuture(string) bool { return false }
type postsStrategy struct{}
func (s postsStrategy) parseFilename(filename string, fm map[string]interface{}) {
if t, title, found := utils.ParseFilenameDateTitle(filename); found {
fm["date"] = t
fm["title"] = title
fm["slug"] = utils.Slugify(title)
}
}
func (s postsStrategy) isCollectible(filename string) bool {
_, _, ok := utils.ParseFilenameDateTitle(filename)
return ok
}
func (s postsStrategy) isFuture(filename string) bool {
t, _, ok := utils.ParseFilenameDateTitle(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(*config.Config) string {
return DefaultCollectionPermalinkPattern
}
func (s postsStrategy) defaultPermalinkPattern(cfg *config.Config) string {
if s, ok := cfg.String("permalink"); ok {
return s
}
return DefaultPostsCollectionPermalinkPattern
}