1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 18:37:50 +01:00
gojekyll/collection/read.go

86 lines
2.1 KiB
Go
Raw Normal View History

2017-07-04 15:09:36 +02:00
package collection
import (
"os"
"path/filepath"
2017-07-03 15:48:41 +02:00
"sort"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/templates"
2017-07-09 22:17:20 +02:00
"github.com/osteele/gojekyll/utils"
)
2017-07-03 17:48:06 +02:00
const draftsPath = "_drafts"
// ReadPages scans the file system for collection pages, and adds them to c.Pages.
func (c *Collection) ReadPages() error {
if c.IsPostsCollection() && c.config.Drafts {
if err := c.scanDirectory(draftsPath); err != nil {
return err
}
}
if err := c.scanDirectory(c.PathPrefix()); err != nil {
return err
}
if c.IsPostsCollection() {
sort.Sort(pagesByDate{c.pages})
}
return nil
}
// scanDirectory scans the file system for collection pages, and adds them to c.Pages.
//
// This function is distinct from ReadPages so that the posts collection can call it twice.
func (c *Collection) scanDirectory(dirname string) error {
var (
sitePath = c.config.Source
dir = filepath.Join(sitePath, dirname)
)
walkFn := func(filename string, info os.FileInfo, err error) error {
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
siteRel := utils.MustRel(sitePath, filename)
switch {
case info.IsDir():
return nil
case c.site.Exclude(siteRel):
return nil
default:
2017-07-14 17:30:39 +02:00
return c.readPost(filename, utils.MustRel(dir, filename))
}
}
return filepath.Walk(dir, walkFn)
2017-07-04 14:29:11 +02:00
}
2017-07-14 17:30:39 +02:00
func (c *Collection) readPost(abs string, rel string) error {
siteRel := utils.MustRel(c.config.Source, abs)
strategy := c.strategy()
switch {
case !strategy.collectible(rel):
return nil
2017-07-03 19:03:45 +02:00
case strategy.future(rel) && !c.config.Future:
return nil
}
pageDefaults := map[string]interface{}{
"collection": c.Name,
"permalink": c.PermalinkPattern(),
}
fm := templates.MergeVariableMaps(pageDefaults, c.config.GetFrontMatterDefaults(c.Name, siteRel))
strategy.addDate(rel, fm)
2017-07-10 19:23:51 +02:00
f, err := pages.NewFile(c.site, abs, filepath.ToSlash(rel), fm)
switch {
case err != nil:
return err
2017-07-02 18:09:15 +02:00
case f.Static():
return nil
2017-07-03 19:03:45 +02:00
case f.Published() || c.config.Unpublished:
2017-07-14 17:30:39 +02:00
p := f.(pages.Page) // f.Static() guarantees this
c.pages = append(c.pages, p)
}
return nil
}