1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-26 21:14:48 +01:00

Reduce reliance on global site variable

This commit is contained in:
Oliver Steele 2017-06-16 16:44:09 -04:00
parent 5e6de82bbf
commit 76db93cab6
9 changed files with 71 additions and 66 deletions

View File

@ -17,7 +17,7 @@ func (s *Site) Clean() error {
return err
case info.IsDir():
return nil
case site.KeepFile(path):
case s.KeepFile(path):
return nil
case options.dryRun:
fmt.Println("rm", path)

View File

@ -15,7 +15,7 @@ type Collection struct {
Pages []Page
}
func makeCollection(s *Site, name string, d VariableMap) *Collection {
func NewCollection(s *Site, name string, d VariableMap) *Collection {
return &Collection{
Site: s,
Name: name,
@ -24,6 +24,21 @@ func makeCollection(s *Site, name string, d VariableMap) *Collection {
}
}
// 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() error {
for name, d := range s.config.Collections {
c := NewCollection(s, name, d)
s.Collections = append(s.Collections, c)
if c.Output { // TODO always read the pages; just don't build them / include them in routes
if err := c.ReadPages(); err != nil {
return err
}
}
}
return nil
}
// PageTemplateObjects returns an array of page objects, for use as the template variable
// value of the collection.
func (c *Collection) PageTemplateObjects() (d []VariableMap) {
@ -68,7 +83,7 @@ func (c *Collection) ReadPages() error {
case info.IsDir():
return nil
}
p, err := ReadPage(site, rel, defaults)
p, err := ReadPage(c.Site, rel, defaults)
switch {
case err != nil:
return err

View File

@ -49,7 +49,7 @@ func (p *DynamicPage) applyLayout(frontMatter VariableMap, body []byte) ([]byte,
if layoutName == "" {
break
}
template, err := site.FindLayout(layoutName, &frontMatter)
template, err := p.site.FindLayout(layoutName, &frontMatter)
if err != nil {
return nil, err
}

27
main.go
View File

@ -62,9 +62,14 @@ func main() {
// Load the site specified at destination into the site global, and print the common banner settings.
loadSite := func() error {
if err := site.ReadConfiguration(source, destination); err != nil {
s, err := NewSiteFromDirectory(source)
if err != nil {
return err
}
if destination != "" {
s.Destination = destination
}
site = s
if site.ConfigFile != nil {
printPathSetting(configurationFileLabel, *site.ConfigFile)
} else {
@ -76,12 +81,12 @@ func main() {
}
// Given a subcommand function, load the site and then call the subcommand.
withSite := func(cmd func(c *cli.Context) error) func(c *cli.Context) error {
withSite := func(cmd func(*cli.Context, *Site) error) func(*cli.Context) error {
return func(c *cli.Context) error {
if err := loadSite(); err != nil {
return cli.NewExitError(err, 1)
}
if err := cmd(c); err != nil {
if err := cmd(c, site); err != nil {
return cli.NewExitError(err, 1)
}
return nil
@ -132,7 +137,7 @@ func main() {
_ = app.Run(os.Args)
}
func buildCommand(c *cli.Context) error {
func buildCommand(c *cli.Context, site *Site) error {
printPathSetting("Destination:", site.Destination)
printSetting("Generating...", "")
count, err := site.Build()
@ -144,12 +149,12 @@ func buildCommand(c *cli.Context) error {
return nil
}
func serveCommand(c *cli.Context) error {
func serveCommand(c *cli.Context, site *Site) error {
return server()
}
func dataCommand(c *cli.Context) error {
p, err := cliPage(c)
func dataCommand(c *cli.Context, site *Site) error {
p, err := cliPage(c, site)
if err != nil {
return err
}
@ -167,7 +172,7 @@ func dataCommand(c *cli.Context) error {
return nil
}
func routesCommand(c *cli.Context) error {
func routesCommand(c *cli.Context, site *Site) error {
printSetting("Routes:", "")
urls := []string{}
for u, p := range site.Paths {
@ -182,8 +187,8 @@ func routesCommand(c *cli.Context) error {
return nil
}
func renderCommand(c *cli.Context) error {
page, err := cliPage(c)
func renderCommand(c *cli.Context, site *Site) error {
page, err := cliPage(c, site)
if err != nil {
return err
}
@ -195,7 +200,7 @@ func renderCommand(c *cli.Context) error {
// If path starts with /, it's a URL path. Else it's a file path relative
// to the site source directory.
func cliPage(c *cli.Context) (page Page, err error) {
func cliPage(c *cli.Context, site *Site) (page Page, err error) {
path := "/"
if c.NArg() > 0 {
path = c.Args().Get(0)

View File

@ -8,7 +8,7 @@ import (
// IsMarkdown returns a boolean indicating whether the file is a Markdown file, according to the current project.
func (s *Site) IsMarkdown(path string) bool {
ext := filepath.Ext(path)
return site.MarkdownExtensions()[strings.TrimLeft(ext, ".")]
return s.MarkdownExtensions()[strings.TrimLeft(ext, ".")]
}
// MarkdownExtensions returns a set of markdown extension, without the final dots.

24
page.go
View File

@ -68,16 +68,17 @@ func ReadPage(site *Site, rel string, defaults VariableMap) (p Page, err error)
frontMatter: defaults,
}
if string(magic) == "---\n" {
p, err = readDynamicPage(fields, rel)
p, err = NewDynamicPage(fields)
if err != nil {
return
}
} else {
p = &StaticPage{fields}
}
if p != nil {
// Compute this after creating the page, in order to pick up the front matter.
err := p.initPermalink()
if err != nil {
return nil, err
}
// Compute this after creating the page, in order to pick up the front matter.
err = p.initPermalink()
if err != nil {
return
}
return
}
@ -117,7 +118,7 @@ func (p *pageFields) DebugVariables() VariableMap {
// Source returns the file path of the page source.
func (p *pageFields) Source() string {
return filepath.Join(site.Source, p.path)
return filepath.Join(p.site.Source, p.path)
}
// StaticPage is a static page.
@ -142,8 +143,9 @@ type DynamicPage struct {
// Static returns a bool indicating that the page is a not static page.
func (p *DynamicPage) Static() bool { return false }
func readDynamicPage(fields pageFields, rel string) (p *DynamicPage, err error) {
data, err := ioutil.ReadFile(filepath.Join(site.Source, rel))
// NewDynamicPage reads the front matter from a file to create a new DynamicPage.
func NewDynamicPage(fields pageFields) (p *DynamicPage, err error) {
data, err := ioutil.ReadFile(filepath.Join(fields.site.Source, fields.path))
if err != nil {
return
}
@ -234,7 +236,7 @@ func (p *DynamicPage) TemplateObject() VariableMap {
func (p *DynamicPage) TemplateVariables() VariableMap {
return VariableMap{
"page": p.TemplateObject(),
"site": site.Variables,
"site": p.site.Variables,
}
}

View File

@ -14,7 +14,7 @@ func TestExpandPermalinkPattern(t *testing.T) {
testPermalinkPattern := func(pattern, path string, data VariableMap) (string, error) {
vs := MergeVariableMaps(data, VariableMap{"permalink": pattern})
p := pageFields{nil, path, "", vs}
p := pageFields{site, path, "", vs}
return p.expandPermalink()
}

14
sass.go
View File

@ -22,7 +22,7 @@ func (p *DynamicPage) writeSass(w io.Writer, data []byte) error {
if err != nil {
return err
}
err = comp.Option(libsass.IncludePaths(site.SassIncludePaths()))
err = comp.Option(libsass.IncludePaths(p.site.SassIncludePaths()))
if err != nil {
log.Fatal(err)
}
@ -34,16 +34,16 @@ func (p *DynamicPage) writeSass(w io.Writer, data []byte) error {
// TODO delete the temp directory when done
func (s *Site) CopySassFileIncludes() {
// TODO use libsass.ImportsOption instead?
if site.sassTempDir == "" {
if s.sassTempDir == "" {
d, err := ioutil.TempDir(os.TempDir(), "_sass")
if err != nil {
panic(err)
}
site.sassTempDir = d
s.sassTempDir = d
}
src := filepath.Join(s.Source, "_sass")
dst := site.sassTempDir
dst := s.sassTempDir
err := filepath.Walk(src, func(from string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
@ -62,9 +62,9 @@ func (s *Site) CopySassFileIncludes() {
// SassIncludePaths returns an array of sass include directories.
func (s *Site) SassIncludePaths() []string {
if site.sassTempDir == "" {
site.CopySassFileIncludes()
if s.sassTempDir == "" {
s.CopySassFileIncludes()
}
s.CopySassFileIncludes()
return []string{site.sassTempDir}
return []string{s.sassTempDir}
}

45
site.go
View File

@ -79,27 +79,25 @@ func NewSite() *Site {
return s
}
// ReadConfiguration reads the configuration file, if it exists.
func (s *Site) ReadConfiguration(source, dest string) error {
// NewSiteFromDirectory reads the configuration file, if it exists.
func NewSiteFromDirectory(source string) (*Site, error) {
s := NewSite()
configPath := filepath.Join(source, "_config.yml")
bytes, err := ioutil.ReadFile(configPath)
switch {
case err == nil:
if err = site.readConfigBytes(bytes); err != nil {
return err
case err != nil && os.IsNotExist(err):
// ok
case err != nil:
return nil, err
default:
if err = s.readConfigBytes(bytes); err != nil {
return nil, err
}
s.Source = filepath.Join(source, s.config.Source)
s.Destination = filepath.Join(s.Source, s.config.Destination)
s.ConfigFile = &configPath
if dest != "" {
site.Destination = dest
}
return nil
case os.IsNotExist(err):
return nil
default:
return err
}
s.Destination = filepath.Join(s.Source, s.config.Destination)
return s, nil
}
func (s *Site) readConfigBytes(bytes []byte) error {
@ -170,7 +168,7 @@ func (s *Site) ReadFiles() error {
case info.IsDir(), s.Exclude(rel):
return nil
}
p, err := ReadPage(site, rel, defaults)
p, err := ReadPage(s, rel, defaults)
if err != nil {
return err
}
@ -183,28 +181,13 @@ func (s *Site) ReadFiles() error {
if err := filepath.Walk(s.Source, walkFn); err != nil {
return err
}
if err := s.readCollections(); err != nil {
if err := s.ReadCollections(); err != nil {
return err
}
s.initTemplateAttributes()
return nil
}
// readCollections scans the file system for collections. It adds each collection's
// pages to the site map, and creates a template site variable for each collection.
func (s *Site) readCollections() error {
for name, d := range s.config.Collections {
c := makeCollection(s, name, d)
s.Collections = append(s.Collections, c)
if c.Output { // TODO always read the pages; just don't build them / include them in routes
if err := c.ReadPages(); err != nil {
return err
}
}
}
return nil
}
func (s *Site) initTemplateAttributes() {
// TODO site: {pages, posts, related_posts, static_files, html_pages, html_files, collections, data, documents, categories.CATEGORY, tags.TAG}
s.Variables = MergeVariableMaps(s.Variables, VariableMap{