2017-07-04 09:09:36 -04:00
|
|
|
package site
|
2017-06-10 15:38:09 -04:00
|
|
|
|
|
|
|
import (
|
2017-06-24 13:30:01 -04:00
|
|
|
"fmt"
|
2017-06-10 15:38:09 -04:00
|
|
|
"path/filepath"
|
2017-07-27 18:07:42 -04:00
|
|
|
"regexp"
|
2017-07-05 11:18:32 -04:00
|
|
|
"sync"
|
2017-06-19 14:44:34 -04:00
|
|
|
|
2017-07-04 09:09:36 -04:00
|
|
|
"github.com/osteele/gojekyll/collection"
|
2017-06-22 15:21:47 -04:00
|
|
|
"github.com/osteele/gojekyll/config"
|
2017-06-22 11:02:32 -04:00
|
|
|
"github.com/osteele/gojekyll/pages"
|
2017-06-24 14:00:19 -04:00
|
|
|
"github.com/osteele/gojekyll/pipelines"
|
2017-06-30 12:53:34 -04:00
|
|
|
"github.com/osteele/gojekyll/plugins"
|
2017-07-09 16:17:20 -04:00
|
|
|
"github.com/osteele/gojekyll/utils"
|
2017-07-12 07:10:52 -04:00
|
|
|
"github.com/osteele/liquid"
|
2017-06-10 15:38:09 -04:00
|
|
|
)
|
|
|
|
|
2017-06-13 11:00:24 -04:00
|
|
|
// Site is a Jekyll site.
|
|
|
|
type Site struct {
|
2017-07-04 09:09:36 -04:00
|
|
|
Collections []*collection.Collection
|
2017-07-05 12:44:38 -04:00
|
|
|
Routes map[string]pages.Document // URL path -> Document, only for output pages
|
2017-06-29 11:00:59 -04:00
|
|
|
|
2017-08-10 11:18:36 -04:00
|
|
|
config config.Config
|
|
|
|
data map[string]interface{}
|
|
|
|
flags config.Flags
|
|
|
|
themeDir string
|
|
|
|
|
2017-08-10 11:45:46 -04:00
|
|
|
docs []pages.Document // all documents, whether or not they are output
|
|
|
|
nonCollectionPages []pages.Page
|
2017-08-10 11:18:36 -04:00
|
|
|
|
|
|
|
pipeline *pipelines.Pipeline
|
|
|
|
renderOnce sync.Once
|
|
|
|
|
|
|
|
drop map[string]interface{} // cached drop value
|
|
|
|
dropOnce sync.Once
|
2017-06-13 11:00:24 -04:00
|
|
|
}
|
|
|
|
|
2017-06-30 23:10:58 -04:00
|
|
|
// SourceDir returns the site source directory.
|
2017-06-30 20:00:38 -04:00
|
|
|
func (s *Site) SourceDir() string { return s.config.Source }
|
|
|
|
|
2017-06-30 23:10:58 -04:00
|
|
|
// DestDir returns the site destination directory.
|
2017-06-30 20:00:38 -04:00
|
|
|
func (s *Site) DestDir() string {
|
|
|
|
if filepath.IsAbs(s.config.Destination) {
|
|
|
|
return s.config.Destination
|
|
|
|
}
|
|
|
|
return filepath.Join(s.config.Source, s.config.Destination)
|
|
|
|
}
|
|
|
|
|
2017-07-12 21:17:11 -04:00
|
|
|
// OutputDocs returns a list of output pages.
|
|
|
|
func (s *Site) OutputDocs() []pages.Document {
|
2017-06-29 10:13:25 -04:00
|
|
|
out := make([]pages.Document, 0, len(s.Routes))
|
2017-06-28 19:00:01 -04:00
|
|
|
for _, p := range s.Routes {
|
|
|
|
out = append(out, p)
|
|
|
|
}
|
2017-06-28 22:46:19 -04:00
|
|
|
return out
|
2017-06-28 19:00:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pages returns all the pages, output or not.
|
2017-07-05 12:44:38 -04:00
|
|
|
func (s *Site) Pages() (out []pages.Page) {
|
|
|
|
for _, d := range s.docs {
|
|
|
|
if p, ok := d.(pages.Page); ok {
|
|
|
|
out = append(out, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-06-22 10:37:31 -04:00
|
|
|
|
2017-07-04 09:09:36 -04:00
|
|
|
// AbsDir is in the collection.Site interface.
|
2017-07-02 12:09:15 -04:00
|
|
|
func (s *Site) AbsDir() string {
|
|
|
|
d, err := filepath.Abs(s.SourceDir())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2017-07-04 09:09:36 -04:00
|
|
|
// Config is in the collection.Site interface.
|
2017-07-03 11:48:06 -04:00
|
|
|
func (s *Site) Config() *config.Config {
|
|
|
|
return &s.config
|
2017-07-01 14:55:50 -04:00
|
|
|
}
|
|
|
|
|
2017-07-08 22:47:50 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-03 10:39:55 -04:00
|
|
|
// Site is in the pages.RenderingContext interface.
|
|
|
|
func (s *Site) Site() interface{} {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2017-06-30 19:37:31 -04:00
|
|
|
// PathPrefix is in the page.Container interface.
|
2017-06-22 10:37:31 -04:00
|
|
|
func (s *Site) PathPrefix() string { return "" }
|
|
|
|
|
2017-07-04 09:09:36 -04:00
|
|
|
// New creates a new site record, initialized with the site defaults.
|
|
|
|
func New(flags config.Flags) *Site {
|
2017-07-01 14:55:50 -04:00
|
|
|
s := &Site{config: config.Default(), flags: flags}
|
2017-07-01 15:03:36 -04:00
|
|
|
s.config.ApplyFlags(flags)
|
2017-07-01 14:55:50 -04:00
|
|
|
return s
|
2017-06-13 08:54:35 -04:00
|
|
|
}
|
|
|
|
|
2017-06-28 18:44:39 -04:00
|
|
|
// SetAbsoluteURL overrides the loaded configuration.
|
|
|
|
// The server uses this.
|
|
|
|
func (s *Site) SetAbsoluteURL(url string) {
|
|
|
|
s.config.AbsoluteURL = url
|
|
|
|
s.config.Variables["url"] = url
|
2017-07-03 10:39:55 -04:00
|
|
|
if s.drop != nil {
|
|
|
|
s.drop["url"] = url
|
2017-06-29 11:00:59 -04:00
|
|
|
}
|
2017-06-28 18:44:39 -04:00
|
|
|
}
|
|
|
|
|
2017-07-11 19:23:42 -04:00
|
|
|
// FilenameURLs returns a map of site-relative pathnames to URL paths
|
2017-06-24 14:00:19 -04:00
|
|
|
func (s *Site) FilenameURLs() map[string]string {
|
|
|
|
urls := map[string]string{}
|
|
|
|
for _, page := range s.Pages() {
|
2017-07-11 19:23:42 -04:00
|
|
|
urls[utils.MustRel(s.SourceDir(), page.SourcePath())] = page.Permalink()
|
2017-06-24 14:00:19 -04:00
|
|
|
}
|
|
|
|
return urls
|
|
|
|
}
|
|
|
|
|
2017-06-13 17:19:05 -04:00
|
|
|
// KeepFile returns a boolean indicating that clean should leave the file in the destination directory.
|
2017-07-01 19:42:48 -04:00
|
|
|
func (s *Site) KeepFile(filename string) bool {
|
2017-07-09 16:17:20 -04:00
|
|
|
return utils.SearchStrings(s.config.KeepFiles, filename)
|
2017-06-13 11:00:24 -04:00
|
|
|
}
|
|
|
|
|
2017-07-01 17:46:18 -04:00
|
|
|
// FilePathPage returns a Page, give a file path relative to site source directory.
|
2017-07-11 19:23:42 -04:00
|
|
|
func (s *Site) FilePathPage(rel string) (pages.Document, bool) {
|
|
|
|
// This looks wasteful. If it shows up as a hotspot, you know what to do.
|
2017-06-28 19:00:01 -04:00
|
|
|
for _, p := range s.Routes {
|
2017-07-24 09:32:57 -04:00
|
|
|
if p.SourcePath() != "" {
|
|
|
|
if r, err := filepath.Rel(s.SourceDir(), p.SourcePath()); err == nil {
|
|
|
|
if r == rel {
|
|
|
|
return p, true
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 23:50:30 -04:00
|
|
|
}
|
|
|
|
}
|
2017-06-22 10:37:31 -04:00
|
|
|
return nil, false
|
2017-06-16 23:50:30 -04:00
|
|
|
}
|
|
|
|
|
2017-07-01 17:46:18 -04:00
|
|
|
// 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
|
2017-06-13 08:55:15 -04:00
|
|
|
}
|
2017-07-01 17:46:18 -04:00
|
|
|
return "", false
|
2017-06-13 08:55:15 -04:00
|
|
|
}
|
|
|
|
|
2017-06-24 13:30:01 -04:00
|
|
|
// RenderingPipeline returns the rendering pipeline.
|
2017-06-24 14:00:19 -04:00
|
|
|
func (s *Site) RenderingPipeline() pipelines.PipelineInterface {
|
2017-06-24 13:30:01 -04:00
|
|
|
if s.pipeline == nil {
|
|
|
|
panic(fmt.Errorf("uninitialized rendering pipeline"))
|
|
|
|
}
|
|
|
|
return s.pipeline
|
|
|
|
}
|
|
|
|
|
2017-07-12 07:10:52 -04:00
|
|
|
// TemplateEngine is part of the plugins.Site interface.
|
|
|
|
func (s *Site) TemplateEngine() *liquid.Engine {
|
|
|
|
return s.pipeline.TemplateEngine()
|
|
|
|
}
|
|
|
|
|
2017-06-29 11:00:59 -04:00
|
|
|
// initializeRenderingPipeline initializes the rendering pipeline
|
|
|
|
func (s *Site) initializeRenderingPipeline() (err error) {
|
2017-06-29 07:41:14 -04:00
|
|
|
options := pipelines.PipelineOptions{
|
2017-07-01 17:46:18 -04:00
|
|
|
RelativeFilenameToURL: s.FilenameURLPath,
|
2017-07-24 09:32:57 -04:00
|
|
|
ThemeDir: s.themeDir,
|
2017-06-29 07:41:14 -04:00
|
|
|
}
|
|
|
|
s.pipeline, err = pipelines.NewPipeline(s.config, options)
|
2017-07-08 22:47:50 -04:00
|
|
|
if err != nil {
|
2017-07-09 18:19:22 -04:00
|
|
|
return err
|
2017-06-30 12:53:34 -04:00
|
|
|
}
|
2017-07-08 22:47:50 -04:00
|
|
|
engine := s.pipeline.TemplateEngine()
|
|
|
|
return s.runHooks(func(p plugins.Plugin) error {
|
|
|
|
return p.ConfigureTemplateEngine(engine)
|
|
|
|
})
|
2017-06-24 13:30:01 -04:00
|
|
|
}
|
|
|
|
|
2017-07-24 09:32:57 -04:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2017-06-30 19:37:31 -04:00
|
|
|
// OutputExt is in the page.Container interface.
|
2017-07-24 09:32:57 -04:00
|
|
|
func (s *Site) OutputExt(path string) string {
|
|
|
|
return s.config.OutputExt(path)
|
2017-06-24 14:00:19 -04:00
|
|
|
}
|
|
|
|
|
2017-06-22 10:37:31 -04:00
|
|
|
// URLPage returns the page that will be served at URL
|
2017-06-29 10:13:25 -04:00
|
|
|
func (s *Site) URLPage(urlpath string) (p pages.Document, found bool) {
|
2017-06-28 19:00:01 -04:00
|
|
|
p, found = s.Routes[urlpath]
|
2017-06-16 23:50:30 -04:00
|
|
|
if !found {
|
2017-06-28 19:00:01 -04:00
|
|
|
p, found = s.Routes[filepath.Join(urlpath, "index.html")]
|
2017-06-16 23:50:30 -04:00
|
|
|
}
|
|
|
|
if !found {
|
2017-06-28 19:00:01 -04:00
|
|
|
p, found = s.Routes[filepath.Join(urlpath, "index.htm")]
|
2017-06-16 23:50:30 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-27 18:07:42 -04:00
|
|
|
var excludeFileRE = regexp.MustCompile(`^[#~]|^\..|~$`)
|
|
|
|
|
2017-07-26 12:47:31 -04:00
|
|
|
// Exclude returns a boolean indicating that the site configuration excludes a file or directory.
|
2017-07-27 18:07:42 -04:00
|
|
|
// 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
|
2017-06-13 11:00:24 -04:00
|
|
|
}
|
2017-07-27 18:07:42 -04:00
|
|
|
return false
|
2017-06-13 11:00:24 -04:00
|
|
|
}
|