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