1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-26 23:14:40 +01:00
gojekyll/collection/collection.go

96 lines
2.6 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 (
2017-08-16 21:50:31 +02:00
"fmt"
2017-06-13 15:01:20 +02:00
"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-08-16 00:55:18 +02:00
"github.com/osteele/liquid"
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
cfg *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-24 15:32:57 +02:00
RelativePath(string) string
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,
cfg: s.Config(),
site: s,
2017-06-13 15:01:20 +02:00
}
}
2017-08-16 21:50:31 +02:00
func (c *Collection) String() string {
return fmt.Sprintf("%T{Name=%q}", c, c.Name)
}
// AbsDir returns the absolute path to the collection directory.
2017-07-02 18:09:15 +02:00
func (c *Collection) AbsDir() string {
return filepath.Join(c.cfg.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
// Pages is a list of pages. Pages in the Post collection are ordered by date.
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
}
// Render renders the collection's pages.
func (c *Collection) Render() error {
2017-07-03 15:48:41 +02:00
for _, p := range c.Pages() {
err := p.Render()
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-08-16 00:55:18 +02:00
return liquid.IterationKeyedMap(templates.MergeVariableMaps(
2017-07-02 18:09:15 +02:00
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-08-16 00:55:18 +02:00
}))
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
}