-
Notifications
You must be signed in to change notification settings - Fork 14
/
read.go
122 lines (115 loc) · 3.3 KB
/
read.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package site
import (
"os"
"path/filepath"
"sort"
"strings"
"github.com/osteele/gojekyll/collection"
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/plugins"
"github.com/osteele/gojekyll/utils"
)
// FromDirectory reads the configuration file, if it exists.
func FromDirectory(dir string, flags config.Flags) (*Site, error) {
s := New(flags)
if err := s.cfg.FromDirectory(dir); err != nil {
return nil, utils.WrapError(err, "reading site")
}
s.cfg.ApplyFlags(s.flags)
return s, nil
}
// Read loads the site data and files.
func (s *Site) Read() error {
if err := s.installPlugins(); err != nil {
return utils.WrapError(err, "initializing plugins")
}
s.Routes = make(map[string]Document)
if err := s.findTheme(); err != nil {
return utils.WrapError(err, "finding theme")
}
if err := s.readDataFiles(); err != nil {
return utils.WrapError(err, "reading data files")
}
if err := s.readThemeAssets(); err != nil {
return utils.WrapError(err, "reading theme assets")
}
if err := s.readFiles(s.SourceDir(), s.SourceDir()); err != nil {
return utils.WrapError(err, "reading files")
}
if err := s.ReadCollections(); err != nil {
return utils.WrapError(err, "reading collections")
}
if err := s.initializeRenderers(); err != nil {
return utils.WrapError(err, "initializing renderers")
}
for _, p := range s.Pages() {
err := s.runHooks(func(h plugins.Plugin) error {
return h.PostInitPage(s, p)
})
if err != nil {
return err
}
}
return s.runHooks(func(p plugins.Plugin) error { return p.PostReadSite(s) })
}
// readFiles scans the source directory and creates pages and collection.
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
}
rel := utils.MustRel(base, filename)
switch {
case info.IsDir() && s.Exclude(rel):
return filepath.SkipDir
case info.IsDir():
return nil
case s.Exclude(rel):
return nil
case strings.HasPrefix(rel, "_"):
return nil
}
defaultFrontmatter := s.cfg.GetFrontMatterDefaults("", rel)
d, err := pages.NewFile(s, filename, filepath.ToSlash(rel), defaultFrontmatter)
if err != nil {
return utils.WrapPathError(err, filename)
}
s.AddDocument(d, true)
if p, ok := d.(Page); ok {
s.nonCollectionPages = append(s.nonCollectionPages, p)
}
return nil
})
}
// AddDocument adds a document to the site's fields.
// It ignores unpublished documents unless config.Unpublished is true.
func (s *Site) AddDocument(d Document, output bool) {
if d.Published() || s.cfg.Unpublished {
s.docs = append(s.docs, d)
if output {
s.Routes[d.URL()] = d
}
}
}
// 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() (err error) {
var cols []*collection.Collection
for name, data := range s.cfg.Collections {
c := collection.New(s, name, data)
cols = append(cols, c)
err = c.ReadPages()
if err != nil {
break
}
for _, p := range c.Pages() {
s.AddDocument(p, c.Output())
}
}
sort.Slice(cols, func(i, j int) bool {
return cols[i].Name < cols[j].Name
})
s.Collections = cols
return nil
}