2017-06-22 21:21:47 +02:00
|
|
|
package collections
|
2017-06-13 15:01:20 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-06-19 04:24:32 +02:00
|
|
|
"strings"
|
2017-07-01 04:05:55 +02:00
|
|
|
"time"
|
2017-06-22 16:42:57 +02:00
|
|
|
|
2017-07-01 20:55:50 +02:00
|
|
|
"github.com/osteele/gojekyll/config"
|
2017-06-23 16:12:23 +02:00
|
|
|
"github.com/osteele/gojekyll/constants"
|
2017-06-22 17:02:32 +02:00
|
|
|
"github.com/osteele/gojekyll/pages"
|
2017-06-22 16:42:57 +02:00
|
|
|
"github.com/osteele/gojekyll/templates"
|
2017-07-01 04:05:55 +02:00
|
|
|
"github.com/osteele/liquid/generics"
|
2017-06-13 15:01:20 +02:00
|
|
|
)
|
|
|
|
|
2017-06-22 21:31:55 +02:00
|
|
|
// Collection is a Jekyll collection https://jekyllrb.com/docs/collections/.
|
2017-06-13 15:01:20 +02:00
|
|
|
type Collection struct {
|
2017-06-24 18:03:33 +02:00
|
|
|
Name string
|
2017-07-01 05:56:29 +02:00
|
|
|
Metadata map[string]interface{}
|
2017-06-24 18:03:33 +02:00
|
|
|
container pages.Container
|
2017-06-29 16:13:25 +02:00
|
|
|
pages []pages.Document
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 23:53:46 +02:00
|
|
|
// NewCollection creates a new Collection
|
2017-07-01 05:56:29 +02:00
|
|
|
func NewCollection(name string, metadata map[string]interface{}, c pages.Container) *Collection {
|
2017-06-13 15:01:20 +02:00
|
|
|
return &Collection{
|
2017-06-24 18:03:33 +02:00
|
|
|
Name: name,
|
|
|
|
Metadata: metadata,
|
|
|
|
container: c,
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-01 20:55:50 +02:00
|
|
|
// Config is in the page.Container interface.
|
|
|
|
func (c *Collection) Config() config.Config {
|
|
|
|
return c.container.Config()
|
|
|
|
}
|
|
|
|
|
2017-07-01 01:37:31 +02:00
|
|
|
// OutputExt is in the page.Container interface.
|
2017-06-24 17:58:12 +02:00
|
|
|
func (c *Collection) OutputExt(pathname string) string {
|
2017-06-24 18:03:33 +02:00
|
|
|
return c.container.OutputExt(pathname)
|
2017-06-24 17:58:12 +02:00
|
|
|
}
|
|
|
|
|
2017-07-01 01:37:31 +02:00
|
|
|
// PathPrefix is in the page.Container interface.
|
|
|
|
// PathPrefix returns the collection's directory prefix, e.g. "_posts/"
|
|
|
|
func (c *Collection) PathPrefix() string { return filepath.FromSlash("_" + c.Name + "/") }
|
|
|
|
|
2017-06-23 16:41:17 +02:00
|
|
|
// IsPostsCollection returns true if the collection is the special "posts" collection.
|
2017-06-23 16:12:23 +02:00
|
|
|
func (c *Collection) IsPostsCollection() bool { return c.Name == "posts" }
|
2017-06-22 16:37:31 +02:00
|
|
|
|
|
|
|
// Output returns a bool indicating whether files in this collection should be written.
|
2017-07-01 05:56:29 +02:00
|
|
|
func (c *Collection) Output() bool { return templates.VariableMap(c.Metadata).Bool("output", false) }
|
2017-06-13 15:01:20 +02:00
|
|
|
|
2017-06-22 17:55:58 +02:00
|
|
|
// Pages is a list of pages.
|
2017-06-29 16:13:25 +02:00
|
|
|
func (c *Collection) Pages() []pages.Document {
|
2017-06-22 17:55:58 +02:00
|
|
|
return c.pages
|
|
|
|
}
|
|
|
|
|
2017-06-22 16:37:31 +02:00
|
|
|
// TemplateVariable returns an array of page objects, for use as the template variable
|
|
|
|
// value of the collection.
|
2017-07-01 04:05:55 +02:00
|
|
|
func (c *Collection) TemplateVariable(ctx pages.RenderingContext, includeContent bool) ([]interface{}, error) {
|
|
|
|
d := []interface{}{}
|
2017-06-22 17:55:58 +02:00
|
|
|
for _, p := range c.Pages() {
|
2017-06-24 17:05:57 +02:00
|
|
|
v := p.PageVariables()
|
2017-06-29 16:13:25 +02:00
|
|
|
dp, ok := p.(*pages.Page)
|
2017-06-24 17:05:57 +02:00
|
|
|
if includeContent && ok {
|
2017-07-01 15:35:54 +02:00
|
|
|
c, err := dp.Content(ctx)
|
2017-06-24 17:05:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-01 05:56:29 +02:00
|
|
|
v = templates.MergeVariableMaps(v, map[string]interface{}{
|
2017-06-24 17:05:57 +02:00
|
|
|
"content": string(c),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
d = append(d, v)
|
2017-06-22 16:37:31 +02:00
|
|
|
}
|
2017-07-01 04:05:55 +02:00
|
|
|
if c.IsPostsCollection() {
|
|
|
|
generics.SortByProperty(d, "date", true)
|
|
|
|
}
|
|
|
|
out := make([]interface{}, len(d))
|
|
|
|
for i, v := range d {
|
|
|
|
out[len(d)-1-i] = v
|
|
|
|
}
|
|
|
|
return out, nil
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|
|
|
|
|
2017-06-23 20:57:28 +02:00
|
|
|
// PermalinkPattern returns the permalink pattern for this collection.
|
|
|
|
func (c *Collection) PermalinkPattern() string {
|
|
|
|
defaultPattern := constants.DefaultCollectionPermalinkPattern
|
|
|
|
if c.IsPostsCollection() {
|
|
|
|
defaultPattern = constants.DefaultPostsCollectionPermalinkPattern
|
|
|
|
}
|
2017-07-01 05:56:29 +02:00
|
|
|
return templates.VariableMap(c.Metadata).String("permalink", defaultPattern)
|
2017-06-23 20:57:28 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 14:35:18 +02:00
|
|
|
// ReadPages scans the file system for collection pages, and adds them to c.Pages.
|
2017-07-01 05:56:29 +02:00
|
|
|
func (c *Collection) ReadPages(sitePath string, frontMatterDefaults func(string, string) map[string]interface{}) error {
|
|
|
|
pageDefaults := map[string]interface{}{
|
2017-06-22 14:35:18 +02:00
|
|
|
"collection": c.Name,
|
2017-06-23 20:57:28 +02:00
|
|
|
"permalink": c.PermalinkPattern(),
|
2017-06-22 21:31:55 +02:00
|
|
|
}
|
2017-06-13 15:01:20 +02:00
|
|
|
|
2017-06-22 16:37:31 +02:00
|
|
|
walkFn := func(filename string, info os.FileInfo, err error) error {
|
2017-06-13 15:01:20 +02:00
|
|
|
if err != nil {
|
2017-06-14 23:41:15 +02:00
|
|
|
// if the issue is simply that the directory doesn't exist, warn instead of error
|
2017-06-13 15:01:20 +02:00
|
|
|
if os.IsNotExist(err) {
|
2017-06-23 16:12:23 +02:00
|
|
|
if !c.IsPostsCollection() {
|
2017-06-22 14:35:18 +02:00
|
|
|
fmt.Printf("Missing collection directory: _%s\n", c.Name)
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2017-06-22 21:21:47 +02:00
|
|
|
relname, err := filepath.Rel(sitePath, filename)
|
2017-06-13 15:01:20 +02:00
|
|
|
switch {
|
2017-06-19 04:24:32 +02:00
|
|
|
case strings.HasPrefix(filepath.Base(relname), "."):
|
|
|
|
return nil
|
2017-06-13 15:01:20 +02:00
|
|
|
case err != nil:
|
|
|
|
return err
|
|
|
|
case info.IsDir():
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-01 04:05:55 +02:00
|
|
|
defaultFrontMatter := templates.MergeVariableMaps(pageDefaults, frontMatterDefaults(relname, c.Name))
|
|
|
|
if c.IsPostsCollection() {
|
|
|
|
filedate, ok := DateFromFilename(relname)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defaultFrontMatter["date"] = filedate
|
|
|
|
}
|
|
|
|
p, err := pages.NewFile(filename, c, filepath.ToSlash(relname), defaultFrontMatter)
|
2017-06-13 15:01:20 +02:00
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return err
|
2017-07-01 20:55:50 +02:00
|
|
|
case p.Published() || c.Config().Unpublished:
|
2017-06-22 14:35:18 +02:00
|
|
|
c.pages = append(c.pages, p)
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-22 21:21:47 +02:00
|
|
|
return filepath.Walk(filepath.Join(sitePath, c.PathPrefix()), walkFn)
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|
2017-07-01 04:05:55 +02:00
|
|
|
|
2017-07-01 05:10:58 +02:00
|
|
|
// DateFromFilename returns the date for a filename that uses Jekyll post convention.
|
|
|
|
// It also returns a bool indicating whether a date was found.
|
2017-07-01 04:05:55 +02:00
|
|
|
func DateFromFilename(s string) (time.Time, bool) {
|
|
|
|
layout := "2006-01-02-"
|
|
|
|
t, err := time.Parse(layout, filepath.Base(s + layout)[:len(layout)])
|
|
|
|
return t, err == nil
|
|
|
|
}
|