1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 08:19:00 +01:00
gojekyll/collection.go

97 lines
2.7 KiB
Go
Raw Normal View History

package gojekyll
2017-06-13 15:01:20 +02:00
import (
"fmt"
"os"
"path/filepath"
"strings"
2017-06-22 17:02:32 +02:00
"github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/templates"
2017-06-13 15:01:20 +02:00
)
// Collection is a Jekyll collection.
type Collection struct {
2017-06-22 16:37:31 +02:00
Site *Site
Name string
Data templates.VariableMap
2017-06-22 17:02:32 +02:00
pages []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
func NewCollection(s *Site, name string, d templates.VariableMap) *Collection {
2017-06-13 15:01:20 +02:00
return &Collection{
2017-06-22 16:37:31 +02:00
Site: s,
Name: name,
Data: d,
2017-06-13 15:01:20 +02:00
}
}
2017-06-15 13:19:49 +02:00
// IsPosts returns true if the collection is the special "posts" collection.
2017-06-22 16:37:31 +02:00
func (c *Collection) IsPosts() bool { return c.Name == "posts" }
// Output returns a bool indicating whether files in this collection should be written.
func (c *Collection) Output() bool { return c.Data.Bool("output", false) }
2017-06-13 15:01:20 +02:00
// PathPrefix returns the collection's directory prefix, e.g. "_posts/"
2017-06-22 16:37:31 +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 16:37:31 +02:00
func (c *Collection) Source() string { return filepath.Join(c.Site.Source, "_"+c.Name) }
2017-06-22 17:55:58 +02:00
// Pages is a list of pages.
func (c *Collection) Pages() []pages.Page {
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-06-22 17:55:58 +02:00
func (c *Collection) TemplateVariable() ([]templates.VariableMap) {
d := []templates.VariableMap{}
for _, p := range c.Pages() {
d = append(d, p.PageVariables())
2017-06-22 16:37:31 +02:00
}
2017-06-22 17:55:58 +02:00
return d
2017-06-13 15:01:20 +02:00
}
// ReadPages scans the file system for collection pages, and adds them to c.Pages.
func (c *Collection) ReadPages() error {
collectionDefaults := templates.MergeVariableMaps(c.Data, templates.VariableMap{
"collection": c.Name,
2017-06-14 23:41:15 +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) {
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-22 16:37:31 +02:00
relname, err := filepath.Rel(c.Site.Source, filename)
2017-06-13 15:01:20 +02:00
switch {
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
}
defaults := templates.MergeVariableMaps(c.Site.GetFrontMatterDefaults(relname, ""), collectionDefaults)
2017-06-22 17:02:32 +02:00
p, err := pages.NewPageFromFile(c.Site, c, filename, relname, defaults)
2017-06-13 15:01:20 +02:00
switch {
case err != nil:
return err
case p.Static():
2017-06-22 16:37:31 +02:00
fmt.Printf("skipping static file inside collection: %s\n", filename)
case p.Published():
c.pages = append(c.pages, p)
2017-06-13 15:01:20 +02:00
}
return nil
}
return filepath.Walk(c.Source(), walkFn)
2017-06-13 15:01:20 +02:00
}