mirror of
https://github.com/danog/gojekyll.git
synced 2024-12-02 17:17:49 +01:00
84 lines
2.0 KiB
Go
84 lines
2.0 KiB
Go
package collection
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
|
|
"github.com/osteele/gojekyll/pages"
|
|
"github.com/osteele/gojekyll/templates"
|
|
"github.com/osteele/gojekyll/utils"
|
|
)
|
|
|
|
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 {
|
|
sitePath := c.config.Source
|
|
pageDefaults := map[string]interface{}{
|
|
"collection": c.Name,
|
|
"permalink": c.PermalinkPattern(),
|
|
}
|
|
walkFn := func(filename string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
relname := utils.MustRel(sitePath, filename)
|
|
switch {
|
|
case info.IsDir():
|
|
return nil
|
|
case c.site.Exclude(relname):
|
|
return nil
|
|
default:
|
|
fm := templates.MergeVariableMaps(pageDefaults, c.config.GetFrontMatterDefaults(c.Name, relname))
|
|
return c.readFile(filename, relname, fm)
|
|
}
|
|
}
|
|
return filepath.Walk(filepath.Join(sitePath, dirname), walkFn)
|
|
}
|
|
|
|
// readFile mutates fm.
|
|
func (c *Collection) readFile(abs string, rel string, fm map[string]interface{}) error {
|
|
strategy := c.strategy()
|
|
switch {
|
|
case !strategy.collectible(rel):
|
|
return nil
|
|
case strategy.future(rel) && !c.config.Future:
|
|
return nil
|
|
default:
|
|
strategy.addDate(rel, fm)
|
|
}
|
|
f, err := pages.NewFile(c.site, abs, filepath.ToSlash(rel), fm)
|
|
switch {
|
|
case err != nil:
|
|
return err
|
|
case f.Static():
|
|
return nil
|
|
case f.Published() || c.config.Unpublished:
|
|
p := f.(pages.Page)
|
|
c.pages = append(c.pages, p)
|
|
}
|
|
return nil
|
|
}
|