1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 21:27:48 +01:00
gojekyll/site/read.go

123 lines
3.3 KiB
Go
Raw Normal View History

2017-07-04 15:09:36 +02:00
package site
import (
"os"
"path/filepath"
2017-08-21 21:06:53 +02:00
"sort"
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-08-24 18:25:38 +02:00
if err := s.cfg.FromDirectory(dir); err != nil {
2017-09-01 14:43:04 +02:00
return nil, utils.WrapError(err, "reading site")
2017-07-01 01:37:31 +02:00
}
2017-08-24 18:25:38 +02:00
s.cfg.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-09-01 15:10:07 +02:00
if err := s.installPlugins(); err != nil {
return utils.WrapError(err, "initializing plugins")
}
2017-09-03 18:18:17 +02:00
s.Routes = make(map[string]Document)
2017-07-24 13:44:49 +02:00
if err := s.findTheme(); err != nil {
2017-09-01 14:43:04 +02:00
return utils.WrapError(err, "finding theme")
2017-07-24 13:44:49 +02:00
}
2017-07-09 01:57:41 +02:00
if err := s.readDataFiles(); err != nil {
2017-09-01 14:43:04 +02:00
return utils.WrapError(err, "reading data files")
2017-07-09 01:57:41 +02:00
}
2017-07-24 13:44:49 +02:00
if err := s.readThemeAssets(); err != nil {
2017-09-01 14:43:04 +02:00
return utils.WrapError(err, "reading theme assets")
2017-07-24 13:44:49 +02:00
}
if err := s.readFiles(s.SourceDir(), s.SourceDir()); err != nil {
2017-09-01 14:43:04 +02:00
return utils.WrapError(err, "reading files")
2017-07-24 13:44:49 +02:00
}
if err := s.ReadCollections(); err != nil {
2017-09-01 14:43:04 +02:00
return utils.WrapError(err, "reading collections")
}
2017-08-18 17:07:01 +02:00
if err := s.initializeRenderers(); err != nil {
2017-09-01 14:43:04 +02:00
return utils.WrapError(err, "initializing renderers")
2017-07-15 14:59:19 +02:00
}
2017-09-02 18:10:35 +02:00
for _, p := range s.Pages() {
err := s.runHooks(func(h plugins.Plugin) error {
return h.PostInitPage(s, p)
})
if err != nil {
return err
}
}
2017-09-02 19:57:28 +02:00
return s.runHooks(func(p plugins.Plugin) error { return p.PostReadSite(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-08-24 18:25:38 +02:00
defaultFrontmatter := s.cfg.GetFrontMatterDefaults("", rel)
2017-07-24 13:44:49 +02:00
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-09-03 18:18:17 +02:00
if p, ok := d.(Page); ok {
2017-08-10 17:45:46 +02:00
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.
2017-09-03 18:18:17 +02:00
func (s *Site) AddDocument(d Document, output bool) {
2017-08-24 18:25:38 +02:00
if d.Published() || s.cfg.Unpublished {
2017-07-10 20:33:06 +02:00
s.docs = append(s.docs, d)
2017-06-23 21:27:13 +02:00
if output {
2017-09-02 19:53:50 +02:00
s.Routes[d.URL()] = 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.
2017-08-21 21:06:53 +02:00
func (s *Site) ReadCollections() (err error) {
var cols []*collection.Collection
2017-08-24 18:25:38 +02:00
for name, data := range s.cfg.Collections {
2017-07-04 15:09:36 +02:00
c := collection.New(s, name, data)
2017-08-21 21:06:53 +02:00
cols = append(cols, c)
err = c.ReadPages()
if err != nil {
break
}
for _, p := range c.Pages() {
2017-06-29 17:00:59 +02:00
s.AddDocument(p, c.Output())
}
}
2017-08-21 21:06:53 +02:00
sort.Slice(cols, func(i, j int) bool {
return cols[i].Name < cols[j].Name
})
s.Collections = cols
return nil
}