1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 16:47:50 +01:00
gojekyll/site/site.go

216 lines
5.5 KiB
Go
Raw Permalink Normal View History

2017-07-04 15:09:36 +02:00
package site
import (
2017-06-24 19:30:01 +02:00
"fmt"
"path/filepath"
2017-07-05 17:18:32 +02:00
"sync"
2017-06-19 20:44:34 +02:00
2017-07-04 15:09:36 +02:00
"github.com/osteele/gojekyll/collection"
"github.com/osteele/gojekyll/config"
2017-06-22 17:02:32 +02:00
"github.com/osteele/gojekyll/pages"
2017-06-30 18:53:34 +02:00
"github.com/osteele/gojekyll/plugins"
2017-08-18 17:07:01 +02:00
"github.com/osteele/gojekyll/renderers"
2017-07-09 22:17:20 +02:00
"github.com/osteele/gojekyll/utils"
2017-07-12 13:10:52 +02:00
"github.com/osteele/liquid"
)
// Site is a Jekyll site.
type Site struct {
2017-07-04 15:09:36 +02:00
Collections []*collection.Collection
2017-09-03 18:18:17 +02:00
Routes map[string]Document // URL path -> Document; only for output pages
2017-06-29 17:00:59 +02:00
2017-08-24 18:25:38 +02:00
cfg config.Config
data map[string]interface{} // from _data files
flags config.Flags // command-line flags, override config files
2017-09-01 15:10:07 +02:00
plugins []string // initially cfg.Plugins, but plugins can modify this this
2017-08-24 18:25:38 +02:00
themeDir string // absolute path to theme directory
2017-09-03 18:18:17 +02:00
docs []Document // all documents, whether or not they are output
nonCollectionPages []Page
2017-08-18 17:07:01 +02:00
renderer *renderers.Manager
renderOnce sync.Once
drop map[string]interface{} // cached drop value
dropOnce sync.Once
}
2017-09-03 18:18:17 +02:00
// Document is in package pages.
type Document = pages.Document
// Page is in package pages.
type Page = pages.Page
2017-07-01 05:10:58 +02:00
// SourceDir returns the site source directory.
2017-08-24 18:25:38 +02:00
func (s *Site) SourceDir() string { return s.cfg.Source }
2017-07-01 05:10:58 +02:00
// DestDir returns the site destination directory.
func (s *Site) DestDir() string {
2017-08-24 18:25:38 +02:00
if filepath.IsAbs(s.cfg.Destination) {
return s.cfg.Destination
}
2017-08-24 18:25:38 +02:00
return filepath.Join(s.cfg.Source, s.cfg.Destination)
}
2017-07-13 03:17:11 +02:00
// OutputDocs returns a list of output pages.
2017-09-03 18:18:17 +02:00
func (s *Site) OutputDocs() []Document {
out := make([]Document, 0, len(s.Routes))
2017-06-29 01:00:01 +02:00
for _, p := range s.Routes {
out = append(out, p)
}
2017-06-29 04:46:19 +02:00
return out
2017-06-29 01:00:01 +02:00
}
// Pages returns all the pages, output or not.
2017-09-03 18:18:17 +02:00
func (s *Site) Pages() (out []Page) {
for _, d := range s.docs {
2017-09-03 18:18:17 +02:00
if p, ok := d.(Page); ok {
out = append(out, p)
}
}
return
}
2017-06-22 16:37:31 +02:00
2017-08-29 21:19:08 +02:00
// Posts is part of the plugins.Site interface.
2017-09-03 18:18:17 +02:00
func (s *Site) Posts() []Page {
2017-08-29 21:19:08 +02:00
for _, c := range s.Collections {
if c.Name == "posts" {
return c.Pages()
}
}
return nil
}
2017-07-04 15:09:36 +02:00
// AbsDir is in the collection.Site interface.
2017-07-02 18:09:15 +02:00
func (s *Site) AbsDir() string {
d, err := filepath.Abs(s.SourceDir())
if err != nil {
panic(err)
}
return d
}
2017-07-04 15:09:36 +02:00
// Config is in the collection.Site interface.
2017-07-03 17:48:06 +02:00
func (s *Site) Config() *config.Config {
2017-08-24 18:25:38 +02:00
return &s.cfg
2017-07-01 20:55:50 +02:00
}
2017-07-03 16:39:55 +02:00
// Site is in the pages.RenderingContext interface.
func (s *Site) Site() interface{} {
return s
}
2017-07-01 01:37:31 +02:00
// PathPrefix is in the page.Container interface.
2017-06-22 16:37:31 +02:00
func (s *Site) PathPrefix() string { return "" }
2017-07-04 15:09:36 +02:00
// New creates a new site record, initialized with the site defaults.
func New(flags config.Flags) *Site {
2017-08-24 18:25:38 +02:00
s := &Site{cfg: config.Default(), flags: flags}
s.cfg.ApplyFlags(flags)
2017-07-01 20:55:50 +02:00
return s
}
2017-06-29 00:44:39 +02:00
// SetAbsoluteURL overrides the loaded configuration.
// The server uses this.
func (s *Site) SetAbsoluteURL(url string) {
2017-08-24 18:25:38 +02:00
s.cfg.AbsoluteURL = url
s.cfg.Set("url", url)
2017-07-03 16:39:55 +02:00
if s.drop != nil {
s.drop["url"] = url
2017-06-29 17:00:59 +02:00
}
2017-06-29 00:44:39 +02:00
}
// FilenameURLs returns a map of site-relative pathnames to URL paths
2017-06-24 20:00:19 +02:00
func (s *Site) FilenameURLs() map[string]string {
urls := map[string]string{}
for _, page := range s.Pages() {
2017-09-02 19:53:50 +02:00
urls[utils.MustRel(s.SourceDir(), page.Source())] = page.URL()
2017-06-24 20:00:19 +02:00
}
return urls
}
2017-06-13 23:19:05 +02:00
// KeepFile returns a boolean indicating that clean should leave the file in the destination directory.
2017-07-02 01:42:48 +02:00
func (s *Site) KeepFile(filename string) bool {
2017-08-24 18:25:38 +02:00
return utils.SearchStrings(s.cfg.KeepFiles, filename)
}
2017-07-01 23:46:18 +02:00
// FilePathPage returns a Page, give a file path relative to site source directory.
2017-09-03 18:18:17 +02:00
func (s *Site) FilePathPage(rel string) (Document, bool) {
// This looks wasteful. If it shows up as a hotspot, you know what to do.
2017-06-29 01:00:01 +02:00
for _, p := range s.Routes {
2017-09-02 19:53:50 +02:00
if p.Source() != "" {
if r, err := filepath.Rel(s.SourceDir(), p.Source()); err == nil {
2017-07-24 15:32:57 +02:00
if r == rel {
return p, true
}
}
}
}
2017-06-22 16:37:31 +02:00
return nil, false
}
2017-07-01 23:46:18 +02: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 {
2017-09-02 19:53:50 +02:00
return p.URL(), true
}
2017-07-01 23:46:18 +02:00
return "", false
}
2017-08-18 17:07:01 +02:00
// RendererManager returns the rendering manager.
func (s *Site) RendererManager() renderers.Renderers {
if s.renderer == nil {
panic(fmt.Errorf("uninitialized rendering manager"))
2017-06-24 19:30:01 +02:00
}
2017-08-18 17:07:01 +02:00
return s.renderer
2017-06-24 19:30:01 +02:00
}
2017-07-12 13:10:52 +02:00
// TemplateEngine is part of the plugins.Site interface.
func (s *Site) TemplateEngine() *liquid.Engine {
2017-08-18 17:07:01 +02:00
return s.renderer.TemplateEngine()
2017-07-12 13:10:52 +02:00
}
2017-08-18 17:07:01 +02:00
// initializeRenderers initializes the rendering manager
func (s *Site) initializeRenderers() (err error) {
options := renderers.Options{
2017-07-01 23:46:18 +02:00
RelativeFilenameToURL: s.FilenameURLPath,
2017-07-24 15:32:57 +02:00
ThemeDir: s.themeDir,
}
2017-08-24 18:25:38 +02:00
s.renderer, err = renderers.New(s.cfg, options)
2017-07-09 04:47:50 +02:00
if err != nil {
2017-07-10 00:19:22 +02:00
return err
2017-06-30 18:53:34 +02:00
}
2017-08-18 17:07:01 +02:00
engine := s.renderer.TemplateEngine()
2017-07-09 04:47:50 +02:00
return s.runHooks(func(p plugins.Plugin) error {
return p.ConfigureTemplateEngine(engine)
})
2017-06-24 19:30:01 +02:00
}
2017-09-02 18:10:35 +02:00
// HasLayout is in the plugins.Site interface.
func (s *Site) HasLayout(ln string) bool {
l, err := s.renderer.FindLayout(ln, nil)
return err == nil && l != nil
}
2017-07-24 15:32:57 +02: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
}
}
2017-08-24 18:25:38 +02:00
return utils.MustRel(s.cfg.Source, path)
2017-07-24 15:32:57 +02:00
}
2017-06-22 16:37:31 +02:00
// URLPage returns the page that will be served at URL
2017-09-03 18:18:17 +02:00
func (s *Site) URLPage(urlpath string) (p Document, found bool) {
2017-06-29 01:00:01 +02:00
p, found = s.Routes[urlpath]
if !found {
2017-06-29 01:00:01 +02:00
p, found = s.Routes[filepath.Join(urlpath, "index.html")]
}
if !found {
2017-06-29 01:00:01 +02:00
p, found = s.Routes[filepath.Join(urlpath, "index.htm")]
}
return
}