1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-11 19:49:42 +01:00
gojekyll/site/read.go

106 lines
2.7 KiB
Go
Raw Normal View History

2017-07-04 15:09:36 +02:00
package site
import (
"os"
"path/filepath"
2017-07-28 00:07:42 +02:00
"strings"
2017-07-04 15:09:36 +02:00
"github.com/osteele/gojekyll/collection"
2017-07-01 01:37:31 +02:00
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/pages"
2017-07-09 01:57:41 +02:00
"github.com/osteele/gojekyll/plugins"
2017-07-09 22:17:20 +02:00
"github.com/osteele/gojekyll/utils"
)
2017-07-04 15:09:36 +02:00
// FromDirectory reads the configuration file, if it exists.
2017-07-25 15:48:49 +02:00
func FromDirectory(dir string, flags config.Flags) (*Site, error) {
2017-07-04 15:09:36 +02:00
s := New(flags)
2017-07-25 15:48:49 +02:00
if err := s.config.FromDirectory(dir); err != nil {
2017-07-01 01:37:31 +02:00
return nil, err
}
2017-07-01 21:03:36 +02:00
s.config.ApplyFlags(s.flags)
2017-07-01 01:37:31 +02:00
return s, nil
}
2017-07-15 15:44:22 +02:00
// Read loads the site data and files.
2017-07-09 01:57:41 +02:00
func (s *Site) Read() error {
2017-07-24 13:44:49 +02:00
s.Routes = make(map[string]pages.Document)
2017-07-09 04:47:50 +02:00
plugins.Install(s.config.Plugins, s)
2017-07-24 13:44:49 +02:00
if err := s.findTheme(); err != nil {
return err
}
2017-07-09 01:57:41 +02:00
if err := s.readDataFiles(); err != nil {
return err
}
2017-07-24 13:44:49 +02:00
if err := s.readThemeAssets(); err != nil {
return err
}
if err := s.readFiles(s.SourceDir(), s.SourceDir()); err != nil {
return err
}
if err := s.ReadCollections(); err != nil {
2017-06-29 17:00:59 +02:00
return err
}
2017-07-15 14:59:19 +02:00
if err := s.initializeRenderingPipeline(); err != nil {
return err
}
2017-07-09 04:47:50 +02:00
return s.runHooks(func(p plugins.Plugin) error { return p.PostRead(s) })
}
2017-07-04 15:09:36 +02:00
// readFiles scans the source directory and creates pages and collection.
2017-07-24 13:44:49 +02:00
func (s *Site) readFiles(dir, base string) error {
return filepath.Walk(dir, func(filename string, info os.FileInfo, err error) error {
if err != nil {
return err
}
2017-07-24 13:44:49 +02:00
rel := utils.MustRel(base, filename)
switch {
2017-07-24 13:44:49 +02:00
case info.IsDir() && s.Exclude(rel):
return filepath.SkipDir
2017-07-28 00:07:42 +02:00
case info.IsDir():
return nil
case s.Exclude(rel):
return nil
case strings.HasPrefix(rel, "_"):
return nil
}
2017-07-24 13:44:49 +02:00
defaultFrontmatter := s.config.GetFrontMatterDefaults("", rel)
d, err := pages.NewFile(s, filename, filepath.ToSlash(rel), defaultFrontmatter)
if err != nil {
return utils.WrapPathError(err, filename)
}
2017-07-24 13:44:49 +02:00
s.AddDocument(d, true)
2017-08-10 17:45:46 +02:00
if p, ok := d.(pages.Page); ok {
s.nonCollectionPages = append(s.nonCollectionPages, p)
}
return nil
2017-07-24 13:44:49 +02:00
})
}
2017-08-10 17:45:46 +02:00
// AddDocument adds a document to the site's fields.
2017-07-10 20:33:06 +02:00
// It ignores unpublished documents unless config.Unpublished is true.
func (s *Site) AddDocument(d pages.Document, output bool) {
if d.Published() || s.config.Unpublished {
s.docs = append(s.docs, d)
2017-06-23 21:27:13 +02:00
if output {
2017-07-10 20:33:06 +02:00
s.Routes[d.Permalink()] = d
2017-06-23 21:27:13 +02:00
}
}
}
// ReadCollections reads the pages of the collections named in the site configuration.
// It adds each collection's pages to the site map, and creates a template site variable for each collection.
func (s *Site) ReadCollections() error {
for name, data := range s.config.Collections {
2017-07-04 15:09:36 +02:00
c := collection.New(s, name, data)
s.Collections = append(s.Collections, c)
2017-07-03 17:48:06 +02:00
if err := c.ReadPages(); err != nil {
return err
}
for _, p := range c.Pages() {
2017-06-29 17:00:59 +02:00
s.AddDocument(p, c.Output())
}
}
return nil
}