1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-03 13:57:46 +01:00
gojekyll/site/read.go

116 lines
3.1 KiB
Go
Raw Normal View History

2017-07-04 15:09:36 +02:00
package site
import (
2017-07-01 01:37:31 +02:00
"io/ioutil"
"os"
"path/filepath"
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.
func FromDirectory(source string, flags config.Flags) (*Site, error) {
s := New(flags)
2017-07-01 01:37:31 +02:00
configPath := filepath.Join(source, "_config.yml")
bytes, err := ioutil.ReadFile(configPath)
switch {
case err != nil && os.IsNotExist(err):
// ok
case err != nil:
return nil, err
default:
err = config.Unmarshal(bytes, &s.config)
if err != nil {
return nil, err
}
s.ConfigFile = &configPath
}
s.config.Source = source
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-09 01:57:41 +02:00
// Read loads the site data and files. It doesn't load the configuration file; NewSiteFromDirectory did that.
func (s *Site) Read() error {
2017-07-09 04:47:50 +02:00
plugins.Install(s.config.Plugins, s)
2017-07-09 01:57:41 +02:00
if err := s.readDataFiles(); err != nil {
return err
}
2017-06-29 17:00:59 +02:00
if err := s.readFiles(); err != nil {
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-15 14:59:19 +02:00
// Reloaded returns a new site read the same source directory, configuration file, and load flags.
// It does not read the site files.
func (s *Site) Reloaded() (*Site, error) {
2017-07-04 15:09:36 +02:00
copy, err := FromDirectory(s.SourceDir(), s.flags)
if err != nil {
2017-07-05 17:18:32 +02:00
return nil, err
}
2017-07-09 01:57:41 +02:00
return copy, copy.Read()
}
2017-07-04 15:09:36 +02:00
// readFiles scans the source directory and creates pages and collection.
func (s *Site) readFiles() error {
s.Routes = make(map[string]pages.Document)
walkFn := func(filename string, info os.FileInfo, err error) error {
if err != nil {
return err
}
2017-07-09 22:17:20 +02:00
relname := utils.MustRel(s.SourceDir(), filename)
switch {
case info.IsDir() && s.Exclude(relname):
return filepath.SkipDir
case info.IsDir(), s.Exclude(relname):
return nil
}
2017-07-03 17:48:06 +02:00
defaultFrontmatter := s.config.GetFrontMatterDefaults("", relname)
2017-07-10 19:23:51 +02:00
p, err := pages.NewFile(s, filename, filepath.ToSlash(relname), defaultFrontmatter)
if err != nil {
return utils.WrapPathError(err, filename)
}
2017-06-29 17:00:59 +02:00
s.AddDocument(p, true)
return nil
}
if err := filepath.Walk(s.SourceDir(), walkFn); err != nil {
return err
}
return s.ReadCollections()
}
2017-07-10 20:33:06 +02:00
// AddDocument adds a document to the site structures.
// 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
}