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

102 lines
2.7 KiB
Go
Raw Permalink 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"
"strings"
"github.com/danog/gojekyll/config"
"github.com/danog/gojekyll/pages"
"github.com/danog/gojekyll/renderers"
"github.com/danog/gojekyll/templates"
"github.com/danog/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
2017-09-03 18:18:17 +02:00
pages []Page
site Site
}
// Site is the interface a site provides to its collections.
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-08-18 17:07:01 +02:00
RendererManager() renderers.Renderers
2017-06-13 15:01:20 +02:00
}
2017-09-03 18:18:17 +02:00
// Page is in package pages.
type Page = pages.Page
const draftsPath = "_drafts"
const postsName = "posts"
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 == postsName }
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-09-03 18:18:17 +02:00
// Pages in the Post collection are ordered by date.
func (c *Collection) 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": strings.TrimSuffix(c.PathPrefix(), "/"),
2017-07-02 18:09:15 +02:00
"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-08-27 20:18:57 +02:00
pattern := c.strategy().defaultPermalinkPattern(c.cfg)
return templates.VariableMap(c.Metadata).String("permalink", pattern)
2017-06-23 20:57:28 +02:00
}