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-06-22 16:42:57 +02:00
|
|
|
|
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"
|
2017-06-22 16:42:57 +02:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2017-06-22 21:31:55 +02:00
|
|
|
// Collection is a Jekyll collection https://jekyllrb.com/docs/collections/.
|
2017-06-13 15:01:20 +02:00
|
|
|
type Collection struct {
|
2017-07-02 19:54:43 +02:00
|
|
|
Name string
|
|
|
|
Metadata map[string]interface{}
|
2017-07-03 19:03:45 +02:00
|
|
|
|
2017-08-10 17:03:24 +02:00
|
|
|
cfg *config.Config
|
|
|
|
pages []pages.Page
|
|
|
|
site Site
|
2017-07-02 19:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2017-07-10 19:54:52 +02:00
|
|
|
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
|
2017-07-02 19:54:43 +02:00
|
|
|
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{
|
2017-07-02 19:54:43 +02:00
|
|
|
Name: name,
|
|
|
|
Metadata: metadata,
|
2017-08-10 17:03:24 +02:00
|
|
|
cfg: s.Config(),
|
2017-07-02 19:54:43 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-07-10 19:54:52 +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-08-10 17:03:24 +02:00
|
|
|
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.
|
2017-06-23 16:12:23 +02:00
|
|
|
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-08-10 17:03:24 +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
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:03:24 +02:00
|
|
|
// Render renders the collection's pages.
|
|
|
|
func (c *Collection) Render() error {
|
2017-07-03 15:48:41 +02:00
|
|
|
for _, p := range c.Pages() {
|
2017-08-10 17:03:24 +02:00
|
|
|
err := p.Render()
|
2017-07-03 15:48:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-06-24 17:05:57 +02:00
|
|
|
}
|
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
|
|
|
}
|