2017-07-04 15:09:36 +02:00
|
|
|
package collection
|
2017-07-02 00:43:22 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2017-07-03 16:39:55 +02:00
|
|
|
|
2017-07-09 22:17:20 +02:00
|
|
|
"github.com/osteele/gojekyll/utils"
|
2017-07-02 00:43:22 +02:00
|
|
|
)
|
|
|
|
|
2017-08-20 18:12:06 +02:00
|
|
|
// A collectionStrategy encapsulates behavior differences between the `_post`
|
|
|
|
// collection and other collections.
|
2017-07-02 00:43:22 +02:00
|
|
|
type collectionStrategy interface {
|
2017-07-03 16:39:55 +02:00
|
|
|
defaultPermalinkPattern() string
|
2017-08-20 18:12:06 +02:00
|
|
|
isCollectible(filename string) bool
|
|
|
|
isFuture(filename string) bool
|
|
|
|
parseFilename(string, map[string]interface{})
|
2017-07-02 00:43:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Collection) strategy() collectionStrategy {
|
|
|
|
if c.IsPostsCollection() {
|
|
|
|
return postsStrategy{}
|
|
|
|
}
|
|
|
|
return defaultStrategy{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type defaultStrategy struct{}
|
|
|
|
|
2017-08-20 18:54:24 +02:00
|
|
|
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 }
|
2017-07-02 00:43:22 +02:00
|
|
|
|
|
|
|
type postsStrategy struct{}
|
|
|
|
|
2017-08-20 18:12:06 +02:00
|
|
|
func (s postsStrategy) parseFilename(filename string, fm map[string]interface{}) {
|
2017-08-20 18:22:58 +02:00
|
|
|
if t, title, found := utils.ParseFilenameDateTitle(filename); found {
|
2017-07-02 00:43:22 +02:00
|
|
|
fm["date"] = t
|
2017-08-16 22:08:49 +02:00
|
|
|
fm["title"] = title
|
2017-08-20 18:54:24 +02:00
|
|
|
fm["slug"] = utils.Slugify(title)
|
2017-07-02 00:43:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 18:12:06 +02:00
|
|
|
func (s postsStrategy) isCollectible(filename string) bool {
|
2017-08-20 18:22:58 +02:00
|
|
|
_, _, ok := utils.ParseFilenameDateTitle(filename)
|
2017-07-02 00:43:22 +02:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2017-08-20 18:12:06 +02:00
|
|
|
func (s postsStrategy) isFuture(filename string) bool {
|
2017-08-20 18:22:58 +02:00
|
|
|
t, _, ok := utils.ParseFilenameDateTitle(filename)
|
2017-07-02 00:43:22 +02:00
|
|
|
return ok && t.After(time.Now())
|
|
|
|
}
|
|
|
|
|
2017-07-03 16:39:55 +02:00
|
|
|
// 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
|
2017-07-02 00:43:22 +02:00
|
|
|
}
|