Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Dec 6, 2019
1 parent 7c9358e commit c59d053
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
7 changes: 4 additions & 3 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,15 +380,16 @@ func (p *pageState) createRenderHooks(f output.Format) (*hooks.Render, error) {
layoutDescriptor := p.getLayoutDescriptor()
layoutDescriptor.RenderingHook = true
layoutDescriptor.LayoutOverride = false
layoutDescriptor.Layout = ""

layoutDescriptor.Kind = "render-link"
linkLayouts, err := p.s.layoutHandler.For(layoutDescriptor, f)
linkLayouts, err := p.s.templateLookupHandler.LayoutHandler.For(layoutDescriptor, f)
if err != nil {
return nil, err
}

layoutDescriptor.Kind = "render-image"
imageLayouts, err := p.s.layoutHandler.For(layoutDescriptor, f)
imageLayouts, err := p.s.templateLookupHandler.LayoutHandler.For(layoutDescriptor, f)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -466,7 +467,7 @@ func (p *pageState) getLayouts(layouts ...string) ([]string, error) {
layoutDescriptor.LayoutOverride = true
}

return p.s.layoutHandler.For(layoutDescriptor, f)
return p.s.templateLookupHandler.LayoutHandler.For(layoutDescriptor, f)
}

// This is serialized
Expand Down
6 changes: 3 additions & 3 deletions hugolib/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ type Site struct {
Sections Taxonomy
Info SiteInfo

layoutHandler *output.LayoutHandler
templateLookupHandler *tpl.TemplateLookupHandler

language *langs.Language

Expand Down Expand Up @@ -323,7 +323,7 @@ func (s *Site) isEnabled(kind string) bool {
// reset returns a new Site prepared for rebuild.
func (s *Site) reset() *Site {
return &Site{Deps: s.Deps,
layoutHandler: output.NewLayoutHandler(),
templateLookupHandler: tpl.NewTemplateLookupHandler(),
disabledKinds: s.disabledKinds,
titleFunc: s.titleFunc,
relatedDocsHandler: s.relatedDocsHandler.Clone(),
Expand Down Expand Up @@ -438,7 +438,7 @@ func newSite(cfg deps.DepsCfg) (*Site, error) {

s := &Site{
PageCollections: c,
layoutHandler: output.NewLayoutHandler(),
templateLookupHandler: tpl.NewTemplateLookupHandler(),
language: cfg.Language,
disabledKinds: disabledKinds,
titleFunc: titleFunc,
Expand Down
51 changes: 51 additions & 0 deletions tpl/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"path/filepath"
"regexp"
"strings"
"sync"
"time"

"github.com/gohugoio/hugo/output"
Expand Down Expand Up @@ -304,3 +305,53 @@ type TemplateFuncsGetter interface {
type TemplateTestMocker interface {
SetFuncs(funcMap map[string]interface{})
}

type TemplateLookupHandler struct {
mu sync.RWMutex
cache map[layoutCacheKey]Template
LayoutHandler *output.LayoutHandler
}

func NewTemplateLookupHandler() *TemplateLookupHandler {
return &TemplateLookupHandler{
LayoutHandler: output.NewLayoutHandler(),
cache: make(map[layoutCacheKey]Template),
}
}

func (t *TemplateLookupHandler) GetOrLookup(d output.LayoutDescriptor, f output.Format, lookup func(name string) (Template, bool)) (templ Template, found bool, err error) {
key := layoutCacheKey{d: d, f: f.Name}

t.mu.RLock()
templ, found = t.cache[key]
t.mu.RUnlock()

if found {
return
}

t.mu.Lock()
defer t.mu.Unlock()

var layouts []string
layouts, err = t.LayoutHandler.For(d, f)
if err != nil {
return
}

for _, l := range layouts {
templ, found = lookup(l)
if found {
t.cache[key] = templ
}
return
}

return

}

type layoutCacheKey struct {
d output.LayoutDescriptor
f string
}

0 comments on commit c59d053

Please sign in to comment.