1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 10:19:03 +01:00
gojekyll/sites/site.go

192 lines
5.1 KiB
Go
Raw Normal View History

package sites
import (
2017-06-24 19:30:01 +02:00
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
2017-06-19 20:44:34 +02:00
"github.com/osteele/gojekyll/collections"
"github.com/osteele/gojekyll/config"
2017-06-17 02:11:52 +02:00
"github.com/osteele/gojekyll/helpers"
2017-06-30 18:53:34 +02:00
"github.com/osteele/gojekyll/liquid"
2017-06-22 17:02:32 +02:00
"github.com/osteele/gojekyll/pages"
2017-06-24 20:00:19 +02:00
"github.com/osteele/gojekyll/pipelines"
2017-06-30 18:53:34 +02:00
"github.com/osteele/gojekyll/plugins"
"github.com/osteele/gojekyll/templates"
)
// Site is a Jekyll site.
type Site struct {
2017-06-29 13:27:43 +02:00
ConfigFile *string
Source string
Destination string
Collections []*collections.Collection
2017-06-29 17:00:59 +02:00
// Variables templates.VariableMap
Routes map[string]pages.Document // URL path -> Page, only for output pages
config config.Config
data map[string]interface{}
2017-06-30 18:53:34 +02:00
pipeline *pipelines.Pipeline
2017-06-29 17:00:59 +02:00
pages []pages.Document // all pages, output or not
preparedToRender bool
siteVariables templates.VariableMap
}
2017-06-29 01:00:01 +02:00
// OutputPages returns a list of output pages.
func (s *Site) OutputPages() []pages.Document {
out := make([]pages.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.
func (s *Site) Pages() []pages.Document { return s.pages }
2017-06-22 16:37:31 +02:00
// PathPrefix returns the relative directory prefix.
func (s *Site) PathPrefix() string { return "" }
2017-06-16 04:31:36 +02:00
// NewSite creates a new site record, initialized with the site defaults.
2017-06-13 18:00:14 +02:00
func NewSite() *Site {
return &Site{config: config.Default()}
}
// NewSiteFromDirectory reads the configuration file, if it exists.
func NewSiteFromDirectory(source string) (*Site, error) {
s := NewSite()
configPath := filepath.Join(source, "_config.yml")
2017-06-13 18:00:14 +02:00
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
}
2017-06-13 18:00:14 +02:00
s.Source = filepath.Join(source, s.config.Source)
s.ConfigFile = &configPath
}
s.Destination = filepath.Join(s.Source, s.config.Destination)
return s, nil
}
2017-06-29 00:44:39 +02: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-06-29 17:00:59 +02:00
if s.siteVariables != nil {
s.siteVariables["url"] = url
}
2017-06-29 00:44:39 +02:00
}
2017-06-24 20:00:19 +02:00
// FilenameURLs returns a map of relative filenames to URL paths
func (s *Site) FilenameURLs() map[string]string {
urls := map[string]string{}
for _, page := range s.Pages() {
urls[page.SiteRelPath()] = page.Permalink()
}
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.
func (s *Site) KeepFile(path string) bool {
// TODO
return false
}
2017-06-22 16:37:31 +02:00
// RelPathPage returns a Page, give a file path relative to site source directory.
func (s *Site) RelPathPage(relpath string) (pages.Document, bool) {
2017-06-29 01:00:01 +02:00
for _, p := range s.Routes {
2017-06-22 16:37:31 +02:00
if p.SiteRelPath() == relpath {
return p, true
}
}
2017-06-22 16:37:31 +02:00
return nil, false
}
2017-06-24 20:00:19 +02:00
// RelativeFilenameToURL returns a page's relative URL, give a file path relative to the site source directory.
2017-06-24 19:30:01 +02:00
func (s *Site) RelativeFilenameToURL(relpath string) (string, bool) {
2017-06-22 16:37:31 +02:00
var url string
p, found := s.RelPathPage(relpath)
if found {
url = p.Permalink()
}
2017-06-22 16:37:31 +02:00
return url, found
}
2017-06-24 19:30:01 +02:00
// RenderingPipeline returns the rendering pipeline.
2017-06-24 20:00:19 +02:00
func (s *Site) RenderingPipeline() pipelines.PipelineInterface {
2017-06-24 19:30:01 +02:00
if s.pipeline == nil {
panic(fmt.Errorf("uninitialized rendering pipeline"))
}
return s.pipeline
}
2017-06-30 18:53:34 +02:00
type pluginContext struct {
engine liquid.Engine
}
// Engine is in the PluginContext interface.
func (c pluginContext) TemplateEngine() liquid.Engine { return c.engine }
2017-06-29 17:00:59 +02:00
// initializeRenderingPipeline initializes the rendering pipeline
func (s *Site) initializeRenderingPipeline() (err error) {
options := pipelines.PipelineOptions{
SourceDir: s.Source,
RelativeFilenameToURL: s.RelativeFilenameToURL,
}
s.pipeline, err = pipelines.NewPipeline(s.config, options)
2017-06-30 18:53:34 +02:00
ctx := pluginContext{s.pipeline.TemplateEngine()}
for _, name := range s.config.Plugins {
if !plugins.Install(name, ctx) {
fmt.Printf("warning: gojekyll does not emulate the %s plugin.\n", name)
}
}
2017-06-29 13:27:43 +02:00
return
2017-06-24 19:30:01 +02:00
}
2017-06-24 20:00:19 +02:00
// OutputExt returns the output extension.
func (s *Site) OutputExt(pathname string) string {
return s.config.OutputExt(pathname)
}
2017-06-22 16:37:31 +02:00
// URLPage returns the page that will be served at URL
func (s *Site) URLPage(urlpath string) (p pages.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
}
2017-06-13 23:19:05 +02:00
// Exclude returns a boolean indicating that the site excludes a file.
func (s *Site) Exclude(path string) bool {
// TODO exclude based on glob, not exact match
inclusionMap := helpers.StringArrayToMap(s.config.Include)
exclusionMap := helpers.StringArrayToMap(s.config.Exclude)
base := filepath.Base(path)
switch {
2017-06-13 18:38:06 +02:00
case inclusionMap[path]:
return false
case path == ".":
return false
case exclusionMap[path]:
return true
case strings.HasPrefix(base, "."), strings.HasPrefix(base, "_"):
return true
default:
return false
}
}