-
Notifications
You must be signed in to change notification settings - Fork 13
/
site.go
234 lines (204 loc) · 5.95 KB
/
site.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package site
import (
"fmt"
"path/filepath"
"regexp"
"sync"
"github.com/osteele/gojekyll/collection"
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/pages"
"github.com/osteele/gojekyll/plugins"
"github.com/osteele/gojekyll/renderers"
"github.com/osteele/gojekyll/utils"
"github.com/osteele/liquid"
)
// Site is a Jekyll site.
type Site struct {
Collections []*collection.Collection
Routes map[string]pages.Document // URL path -> Document, only for output pages
config config.Config
data map[string]interface{}
flags config.Flags
themeDir string
docs []pages.Document // all documents, whether or not they are output
nonCollectionPages []pages.Page
renderer *renderers.Manager
renderOnce sync.Once
drop map[string]interface{} // cached drop value
dropOnce sync.Once
}
// SourceDir returns the site source directory.
func (s *Site) SourceDir() string { return s.config.Source }
// DestDir returns the site destination directory.
func (s *Site) DestDir() string {
if filepath.IsAbs(s.config.Destination) {
return s.config.Destination
}
return filepath.Join(s.config.Source, s.config.Destination)
}
// OutputDocs returns a list of output pages.
func (s *Site) OutputDocs() []pages.Document {
out := make([]pages.Document, 0, len(s.Routes))
for _, p := range s.Routes {
out = append(out, p)
}
return out
}
// Pages returns all the pages, output or not.
func (s *Site) Pages() (out []pages.Page) {
for _, d := range s.docs {
if p, ok := d.(pages.Page); ok {
out = append(out, p)
}
}
return
}
// AbsDir is in the collection.Site interface.
func (s *Site) AbsDir() string {
d, err := filepath.Abs(s.SourceDir())
if err != nil {
panic(err)
}
return d
}
// Config is in the collection.Site interface.
func (s *Site) Config() *config.Config {
return &s.config
}
func (s *Site) runHooks(h func(plugins.Plugin) error) error {
for _, name := range s.config.Plugins {
p, ok := plugins.Lookup(name)
if ok {
if err := h(p); err != nil {
return err
}
}
}
return nil
}
// Site is in the pages.RenderingContext interface.
func (s *Site) Site() interface{} {
return s
}
// PathPrefix is in the page.Container interface.
func (s *Site) PathPrefix() string { return "" }
// New creates a new site record, initialized with the site defaults.
func New(flags config.Flags) *Site {
s := &Site{config: config.Default(), flags: flags}
s.config.ApplyFlags(flags)
return s
}
// SetAbsoluteURL overrides the loaded configuration.
// The server uses this.
func (s *Site) SetAbsoluteURL(url string) {
s.config.AbsoluteURL = url
s.config.Variables["url"] = url
if s.drop != nil {
s.drop["url"] = url
}
}
// FilenameURLs returns a map of site-relative pathnames to URL paths
func (s *Site) FilenameURLs() map[string]string {
urls := map[string]string{}
for _, page := range s.Pages() {
urls[utils.MustRel(s.SourceDir(), page.SourcePath())] = page.Permalink()
}
return urls
}
// KeepFile returns a boolean indicating that clean should leave the file in the destination directory.
func (s *Site) KeepFile(filename string) bool {
return utils.SearchStrings(s.config.KeepFiles, filename)
}
// FilePathPage returns a Page, give a file path relative to site source directory.
func (s *Site) FilePathPage(rel string) (pages.Document, bool) {
// This looks wasteful. If it shows up as a hotspot, you know what to do.
for _, p := range s.Routes {
if p.SourcePath() != "" {
if r, err := filepath.Rel(s.SourceDir(), p.SourcePath()); err == nil {
if r == rel {
return p, true
}
}
}
}
return nil, false
}
// FilenameURLPath returns a page's URL path, give a relative file path relative to the site source directory.
func (s *Site) FilenameURLPath(relpath string) (string, bool) {
if p, found := s.FilePathPage(relpath); found {
return p.Permalink(), true
}
return "", false
}
// RendererManager returns the rendering manager.
func (s *Site) RendererManager() renderers.Renderers {
if s.renderer == nil {
panic(fmt.Errorf("uninitialized rendering manager"))
}
return s.renderer
}
// TemplateEngine is part of the plugins.Site interface.
func (s *Site) TemplateEngine() *liquid.Engine {
return s.renderer.TemplateEngine()
}
// initializeRenderers initializes the rendering manager
func (s *Site) initializeRenderers() (err error) {
options := renderers.Options{
RelativeFilenameToURL: s.FilenameURLPath,
ThemeDir: s.themeDir,
}
s.renderer, err = renderers.New(s.config, options)
if err != nil {
return err
}
engine := s.renderer.TemplateEngine()
return s.runHooks(func(p plugins.Plugin) error {
return p.ConfigureTemplateEngine(engine)
})
}
// RelativePath is in the page.Container interface.
func (s *Site) RelativePath(path string) string {
if s.themeDir != "" {
if rel, err := filepath.Rel(s.themeDir, path); err == nil {
return rel
}
}
return utils.MustRel(s.config.Source, path)
}
// OutputExt is in the page.Container interface.
func (s *Site) OutputExt(path string) string {
return s.config.OutputExt(path)
}
// URLPage returns the page that will be served at URL
func (s *Site) URLPage(urlpath string) (p pages.Document, found bool) {
p, found = s.Routes[urlpath]
if !found {
p, found = s.Routes[filepath.Join(urlpath, "index.html")]
}
if !found {
p, found = s.Routes[filepath.Join(urlpath, "index.htm")]
}
return
}
var excludeFileRE = regexp.MustCompile(`^[#~]|^\..|~$`)
// Exclude returns a boolean indicating that the site configuration excludes a file or directory.
// It does not exclude top-level _underscore files and directories.
func (s *Site) Exclude(siteRel string) bool {
for siteRel != "." {
dir, base := filepath.Dir(siteRel), filepath.Base(siteRel)
switch {
case utils.MatchList(s.config.Include, siteRel):
return false
case utils.MatchList(s.config.Exclude, siteRel):
return true
case dir != "." && base[0] == '_':
return true
default:
if excludeFileRE.MatchString(base) {
return true
}
}
siteRel = dir
}
return false
}