1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 06:48:59 +01:00

Recognize $theme/{_layouts,_sass}

This commit is contained in:
Oliver Steele 2017-07-24 08:09:14 -04:00
parent a72426bb1b
commit a63729f6b2
4 changed files with 58 additions and 41 deletions

View File

@ -8,9 +8,32 @@ import (
"strings"
"github.com/osteele/gojekyll/frontmatter"
"github.com/osteele/gojekyll/templates"
"github.com/osteele/gojekyll/utils"
"github.com/osteele/liquid"
)
// ApplyLayout applies the named layout to the data.
func (p *Pipeline) ApplyLayout(name string, data []byte, e map[string]interface{}) ([]byte, error) {
for name != "" {
var lfm map[string]interface{}
tpl, err := p.FindLayout(name, &lfm)
if err != nil {
return nil, err
}
b := utils.MergeStringMaps(e, map[string]interface{}{
"content": string(data),
"layout": lfm,
})
data, err = tpl.Render(b)
if err != nil {
return nil, utils.WrapPathError(err, name)
}
name = templates.VariableMap(lfm).String("layout", "")
}
return data, nil
}
// FindLayout returns a template for the named layout.
func (p *Pipeline) FindLayout(base string, fm *map[string]interface{}) (tpl *liquid.Template, err error) {
// not cached, but the time here is negligible
@ -23,16 +46,18 @@ func (p *Pipeline) FindLayout(base string, fm *map[string]interface{}) (tpl *liq
content []byte
found bool
)
for _, ext := range exts {
// TODO respect layout config
filename = filepath.Join(p.LayoutsDir(), base+ext)
content, err = ioutil.ReadFile(filename)
if err == nil {
found = true
break
}
if !os.IsNotExist(err) {
return nil, err
loop:
for _, dir := range p.layoutDirs() {
for _, ext := range exts {
filename = filepath.Join(dir, base+ext)
content, err = ioutil.ReadFile(filename)
if err == nil {
found = true
break loop
}
if !os.IsNotExist(err) {
return nil, err
}
}
}
if !found {
@ -51,6 +76,10 @@ func (p *Pipeline) FindLayout(base string, fm *map[string]interface{}) (tpl *liq
}
// LayoutsDir returns the path to the layouts directory.
func (p *Pipeline) LayoutsDir() string {
return filepath.Join(p.SourceDir(), p.config.LayoutsDir)
func (p *Pipeline) layoutDirs() []string {
dirs := []string{filepath.Join(p.SourceDir(), p.config.LayoutsDir)}
if p.ThemeDir != "" {
dirs = append(dirs, filepath.Join(p.ThemeDir, "_layouts"))
}
return dirs
}

View File

@ -7,7 +7,6 @@ import (
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/filters"
"github.com/osteele/gojekyll/tags"
"github.com/osteele/gojekyll/templates"
"github.com/osteele/gojekyll/utils"
"github.com/osteele/liquid"
)
@ -31,6 +30,7 @@ type Pipeline struct {
// PipelineOptions configures a pipeline.
type PipelineOptions struct {
RelativeFilenameToURL tags.LinkTagHandler
ThemeDir string
}
// NewPipeline makes a rendering pipeline.
@ -90,27 +90,6 @@ func (p *Pipeline) renderTemplate(src []byte, b map[string]interface{}, filename
return out, err
}
// ApplyLayout applies the named layout to the data.
func (p *Pipeline) ApplyLayout(name string, data []byte, e map[string]interface{}) ([]byte, error) {
for name != "" {
var lfm map[string]interface{}
tpl, err := p.FindLayout(name, &lfm)
if err != nil {
return nil, err
}
b := utils.MergeStringMaps(e, map[string]interface{}{
"content": string(data),
"layout": lfm,
})
data, err = tpl.Render(b)
if err != nil {
return nil, utils.WrapPathError(err, name)
}
name = templates.VariableMap(lfm).String("layout", "")
}
return data, nil
}
func (p *Pipeline) makeLiquidEngine() *liquid.Engine {
engine := liquid.NewEngine()
filters.AddJekyllFilters(engine, &p.config)

View File

@ -19,8 +19,8 @@ import (
// CopySassFileIncludes copies sass partials into a temporary directory,
// removing initial underscores.
// TODO delete the temp directory when done
func (p *Pipeline) CopySassFileIncludes() error {
// TODO delete the temp directory when done?
// TODO use libsass.ImportsOption instead?
if p.sassTempDir == "" {
dir, err := ioutil.TempDir(os.TempDir(), "_sass")
@ -29,10 +29,20 @@ func (p *Pipeline) CopySassFileIncludes() error {
}
p.sassTempDir = dir
}
h := md5.New() // nolint: gas, noncrypto
src := filepath.Join(p.SourceDir(), "_sass")
dst := p.sassTempDir
if p.ThemeDir != "" {
if err := copySassFiles(filepath.Join(p.ThemeDir, "_sass"), p.sassTempDir, h); err != nil {
return err
}
}
if err := copySassFiles(filepath.Join(p.SourceDir(), "_sass"), p.sassTempDir, h); err != nil {
return err
}
p.sassHash = fmt.Sprintf("%x", h.Sum(nil))
return nil
}
func copySassFiles(src, dst string, h io.Writer) error {
err := filepath.Walk(src, func(from string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
@ -44,8 +54,7 @@ func (p *Pipeline) CopySassFileIncludes() error {
return err
}
defer in.Close() // nolint: errcheck
_, err = io.Copy(h, in)
if err != nil {
if _, err = io.Copy(h, in); err != nil {
return err
}
return utils.CopyFileContents(to, from, 0644)
@ -53,7 +62,6 @@ func (p *Pipeline) CopySassFileIncludes() error {
if os.IsNotExist(err) {
return nil
}
p.sassHash = fmt.Sprintf("%x", h.Sum(nil))
return err
}

View File

@ -163,6 +163,7 @@ func (s *Site) TemplateEngine() *liquid.Engine {
func (s *Site) initializeRenderingPipeline() (err error) {
options := pipelines.PipelineOptions{
RelativeFilenameToURL: s.FilenameURLPath,
ThemeDir: s.themeDir,
}
s.pipeline, err = pipelines.NewPipeline(s.config, options)
if err != nil {