2017-06-13 15:01:20 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-15 02:44:22 +02:00
|
|
|
Pages []Page
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|
|
|
|
|
2017-06-14 23:41:15 +02:00
|
|
|
func makeCollection(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-15 13:19:49 +02:00
|
|
|
// PageTemplateObjects 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-15 13:19:49 +02:00
|
|
|
func (c *Collection) PageTemplateObjects() (d []VariableMap) {
|
2017-06-13 15:01:20 +02:00
|
|
|
for _, p := range c.Pages {
|
2017-06-15 13:19:49 +02:00
|
|
|
d = append(d, p.TemplateObject())
|
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-14 23:41:15 +02:00
|
|
|
func (c *Collection) IsPosts() bool {
|
2017-06-13 15:01:20 +02:00
|
|
|
return c.Name == "posts"
|
|
|
|
}
|
|
|
|
|
2017-06-14 23:41:15 +02:00
|
|
|
// Source returns the source directory for pages in the collection.
|
|
|
|
func (c *Collection) Source() string {
|
2017-06-13 17:27:24 +02:00
|
|
|
return filepath.Join(c.Site.Source, "_"+c.Name)
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadPages scans the file system for collection pages, and adds them to c.Pages.
|
2017-06-13 17:00:24 +02:00
|
|
|
func (c *Collection) ReadPages() error {
|
2017-06-13 17:27:24 +02:00
|
|
|
basePath := c.Site.Source
|
2017-06-14 23:41:15 +02:00
|
|
|
defaults := mergeVariableMaps(c.Data, VariableMap{
|
2017-06-13 15:01:20 +02:00
|
|
|
"collection": c.Name,
|
2017-06-14 23:41:15 +02:00
|
|
|
})
|
2017-06-13 15:01:20 +02:00
|
|
|
|
|
|
|
walkFn := func(path string, info os.FileInfo, err error) error {
|
|
|
|
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-14 23:41:15 +02:00
|
|
|
if !c.IsPosts() {
|
2017-06-13 15:01:20 +02:00
|
|
|
fmt.Println("Missing directory for collection", c.Name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rel, err := filepath.Rel(basePath, path)
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return err
|
|
|
|
case info.IsDir():
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-16 21:14:56 +02:00
|
|
|
p, err := ReadPage(site, rel, 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-13 15:01:20 +02:00
|
|
|
fmt.Printf("skipping static file inside collection: %s\n", path)
|
2017-06-15 02:44:22 +02:00
|
|
|
case p.Published():
|
|
|
|
c.Site.Paths[p.Permalink()] = p
|
2017-06-13 15:01:20 +02:00
|
|
|
c.Pages = append(c.Pages, p)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-14 23:41:15 +02:00
|
|
|
return filepath.Walk(c.Source(), walkFn)
|
2017-06-13 15:01:20 +02:00
|
|
|
}
|