2017-06-17 16:51:32 +02:00
|
|
|
package gojekyll
|
2017-06-13 15:01:20 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-06-19 04:24:32 +02:00
|
|
|
"strings"
|
2017-06-13 15:01:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Collection is a Jekyll collection.
|
|
|
|
type Collection struct {
|
2017-06-13 17:00:24 +02:00
|
|
|
Site *Site
|
2017-06-13 15:01:20 +02:00
|
|
|
Name string
|
2017-06-14 23:41:15 +02:00
|
|
|
Data VariableMap
|
2017-06-13 15:01:20 +02:00
|
|
|
Output bool
|
2017-06-20 14:59:54 +02:00
|
|
|
pages []Page
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|
|
|
|
|
2017-06-17 02:06:55 +02:00
|
|
|
// NewCollection creates a new Collection with defaults d
|
2017-06-16 22:44:09 +02:00
|
|
|
func NewCollection(s *Site, name string, d VariableMap) *Collection {
|
2017-06-13 15:01:20 +02:00
|
|
|
return &Collection{
|
2017-06-13 17:00:24 +02:00
|
|
|
Site: s,
|
2017-06-13 15:01:20 +02:00
|
|
|
Name: name,
|
2017-06-13 17:00:24 +02:00
|
|
|
Data: d,
|
2017-06-14 23:41:15 +02:00
|
|
|
Output: d.Bool("output", false),
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 14:59:54 +02:00
|
|
|
// CollectionValue returns an array of page objects, for use as the template variable
|
2017-06-13 15:01:20 +02:00
|
|
|
// value of the collection.
|
2017-06-22 14:35:18 +02:00
|
|
|
func (c *Collection) CollectionValue() (d []VariableMap) {
|
|
|
|
for _, page := range c.Pages() {
|
2017-06-20 22:17:59 +02:00
|
|
|
d = append(d, page.Variables())
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-15 13:19:49 +02:00
|
|
|
// IsPosts returns true if the collection is the special "posts" collection.
|
2017-06-22 14:35:18 +02:00
|
|
|
func (c *Collection) IsPosts() bool {
|
|
|
|
return c.Name == "posts"
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|
|
|
|
|
2017-06-17 05:50:30 +02:00
|
|
|
// PathPrefix returns the collection's directory prefix, e.g. "_posts/"
|
2017-06-22 14:35:18 +02:00
|
|
|
func (c *Collection) PathPrefix() string {
|
|
|
|
return filepath.FromSlash("_" + c.Name + "/")
|
2017-06-17 02:19:46 +02:00
|
|
|
}
|
|
|
|
|
2017-06-14 23:41:15 +02:00
|
|
|
// Source returns the source directory for pages in the collection.
|
2017-06-22 14:35:18 +02:00
|
|
|
func (c *Collection) Source() string {
|
|
|
|
return filepath.Join(c.Site.Source, "_"+c.Name)
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 14:35:18 +02:00
|
|
|
// ReadPages scans the file system for collection pages, and adds them to c.Pages.
|
|
|
|
func (c *Collection) ReadPages() error {
|
|
|
|
basePath := c.Site.Source
|
|
|
|
collectionDefaults := MergeVariableMaps(c.Data, VariableMap{
|
|
|
|
"collection": c.Name,
|
2017-06-14 23:41:15 +02:00
|
|
|
})
|
2017-06-13 15:01:20 +02:00
|
|
|
|
2017-06-17 02:06:55 +02:00
|
|
|
walkFn := func(name 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-22 14:35:18 +02:00
|
|
|
if !c.IsPosts() {
|
|
|
|
fmt.Printf("Missing collection directory: _%s\n", c.Name)
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2017-06-17 05:30:10 +02:00
|
|
|
relname, err := filepath.Rel(basePath, name)
|
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-06-22 14:35:18 +02:00
|
|
|
defaults := MergeVariableMaps(c.Site.GetFrontMatterDefaults(relname, ""), collectionDefaults)
|
2017-06-22 15:36:39 +02:00
|
|
|
p, err := NewPageFromFile(c.Site, c, relname, defaults)
|
2017-06-13 15:01:20 +02:00
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return err
|
2017-06-15 02:44:22 +02:00
|
|
|
case p.Static():
|
2017-06-17 02:06:55 +02:00
|
|
|
fmt.Printf("skipping static file inside collection: %s\n", name)
|
2017-06-15 02:44:22 +02:00
|
|
|
case p.Published():
|
2017-06-22 14:35:18 +02:00
|
|
|
c.Site.Paths[p.Permalink()] = p
|
|
|
|
c.pages = append(c.pages, p)
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-22 14:35:18 +02:00
|
|
|
return filepath.Walk(c.Source(), walkFn)
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|