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

89 lines
2.5 KiB
Go
Raw Normal View History

2017-07-04 15:09:36 +02:00
package collection
2017-06-13 15:01:20 +02:00
import (
"path/filepath"
2017-07-01 20:55:50 +02:00
"github.com/osteele/gojekyll/config"
2017-06-22 17:02:32 +02:00
"github.com/osteele/gojekyll/pages"
2017-07-10 19:23:51 +02:00
"github.com/osteele/gojekyll/pipelines"
"github.com/osteele/gojekyll/templates"
2017-06-13 15:01:20 +02:00
)
// Collection is a Jekyll collection https://jekyllrb.com/docs/collections/.
2017-06-13 15:01:20 +02:00
type Collection struct {
Name string
Metadata map[string]interface{}
2017-07-03 19:03:45 +02:00
config *config.Config
pages []pages.Page
site Site
}
// Site is the interface a site provides to collections it contains.
type Site interface {
2017-07-03 17:48:06 +02:00
Config() *config.Config
Exclude(string) bool
2017-07-10 19:23:51 +02:00
RenderingPipeline() pipelines.PipelineInterface
OutputExt(pathname string) string
2017-06-13 15:01:20 +02:00
}
2017-07-04 15:09:36 +02:00
// New creates a new Collection
func New(s Site, name string, metadata map[string]interface{}) *Collection {
2017-06-13 15:01:20 +02:00
return &Collection{
Name: name,
Metadata: metadata,
2017-07-03 19:03:45 +02:00
config: s.Config(),
site: s,
2017-06-13 15:01:20 +02:00
}
}
// AbsDir returns the absolute path to the collection directory.
2017-07-02 18:09:15 +02:00
func (c *Collection) AbsDir() string {
2017-07-03 19:03:45 +02:00
return filepath.Join(c.config.SourceDir(), c.PathPrefix())
2017-07-02 18:09:15 +02:00
}
2017-07-01 01:37:31 +02:00
// 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.
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-07-02 19:46:05 +02:00
func (c *Collection) Pages() []pages.Page {
2017-06-22 17:55:58 +02:00
return c.pages
}
2017-07-03 15:48:41 +02:00
// SetPageContent sets up the collection's pages' "content".
2017-07-10 19:23:51 +02:00
func (c *Collection) SetPageContent() error {
2017-07-03 15:48:41 +02:00
for _, p := range c.Pages() {
2017-07-10 19:23:51 +02:00
_, err := p.Content()
2017-07-03 15:48:41 +02:00
if err != nil {
return err
}
2017-06-22 16:37:31 +02:00
}
2017-07-03 15:48:41 +02:00
return nil
2017-07-02 18:09:15 +02:00
}
2017-07-03 15:48:41 +02:00
// ToLiquid returns the value of the collection in the template
2017-07-02 18:09:15 +02:00
// "collections" array.
2017-07-03 15:48:41 +02:00
func (c *Collection) ToLiquid() interface{} {
2017-07-02 18:09:15 +02:00
return templates.MergeVariableMaps(
c.Metadata,
map[string]interface{}{
"label": c.Name,
2017-07-03 15:48:41 +02:00
"docs": c.pages,
2017-07-02 18:09:15 +02:00
"files": []string{},
"relative_directory": c.PathPrefix(),
"directory": c.AbsDir(),
})
2017-06-13 15:01:20 +02:00
}
2017-07-03 16:39:55 +02:00
// PermalinkPattern returns the default permalink pattern for this collection.
2017-06-23 20:57:28 +02:00
func (c *Collection) PermalinkPattern() string {
2017-07-03 16:39:55 +02:00
defaultPattern := c.strategy().defaultPermalinkPattern()
2017-07-01 05:56:29 +02:00
return templates.VariableMap(c.Metadata).String("permalink", defaultPattern)
2017-06-23 20:57:28 +02:00
}