Skip to content

Commit

Permalink
Revert "tpl: Rework to handle both text and HTML templates"
Browse files Browse the repository at this point in the history
Will have to take another stab at this ...

This reverts commit 5c5efa0.

Closes gohugoio#3260
  • Loading branch information
bep committed Apr 2, 2017
1 parent c97dae4 commit 7eb71ee
Show file tree
Hide file tree
Showing 31 changed files with 777 additions and 1,186 deletions.
6 changes: 3 additions & 3 deletions deps/deps.go
Expand Up @@ -20,7 +20,7 @@ type Deps struct {
Log *jww.Notepad `json:"-"`

// The templates to use.
Tmpl tpl.TemplateHandler `json:"-"`
Tmpl tpl.Template `json:"-"`

// The file systems to use.
Fs *hugofs.Fs `json:"-"`
Expand All @@ -40,7 +40,7 @@ type Deps struct {
Language *helpers.Language

templateProvider ResourceProvider
WithTemplate func(templ tpl.TemplateHandler) error `json:"-"`
WithTemplate func(templ tpl.Template) error `json:"-"`

translationProvider ResourceProvider
}
Expand Down Expand Up @@ -158,7 +158,7 @@ type DepsCfg struct {

// Template handling.
TemplateProvider ResourceProvider
WithTemplate func(templ tpl.TemplateHandler) error
WithTemplate func(templ tpl.Template) error

// i18n handling.
TranslationProvider ResourceProvider
Expand Down
27 changes: 9 additions & 18 deletions hugolib/alias.go
Expand Up @@ -22,8 +22,6 @@ import (
"runtime"
"strings"

"github.com/spf13/hugo/tpl"

jww "github.com/spf13/jwalterweatherman"

"github.com/spf13/hugo/helpers"
Expand All @@ -37,19 +35,18 @@ const (
var defaultAliasTemplates *template.Template

func init() {
//TODO(bep) consolidate
defaultAliasTemplates = template.New("")
template.Must(defaultAliasTemplates.New("alias").Parse(alias))
template.Must(defaultAliasTemplates.New("alias-xhtml").Parse(aliasXHtml))
}

type aliasHandler struct {
t tpl.TemplateHandler
Templates *template.Template
log *jww.Notepad
allowRoot bool
}

func newAliasHandler(t tpl.TemplateHandler, l *jww.Notepad, allowRoot bool) aliasHandler {
func newAliasHandler(t *template.Template, l *jww.Notepad, allowRoot bool) aliasHandler {
return aliasHandler{t, l, allowRoot}
}

Expand All @@ -59,19 +56,12 @@ func (a aliasHandler) renderAlias(isXHTML bool, permalink string, page *Page) (i
t = "alias-xhtml"
}

var templ *tpl.TemplateAdapter

if a.t != nil {
templ = a.t.Lookup("alias.html")
template := defaultAliasTemplates
if a.Templates != nil {
template = a.Templates
t = "alias.html"
}

if templ == nil {
def := defaultAliasTemplates.Lookup(t)
if def != nil {
templ = &tpl.TemplateAdapter{def}
}

}
data := struct {
Permalink string
Page *Page
Expand All @@ -81,7 +71,7 @@ func (a aliasHandler) renderAlias(isXHTML bool, permalink string, page *Page) (i
}

buffer := new(bytes.Buffer)
err := templ.Execute(buffer, data)
err := template.ExecuteTemplate(buffer, t, data)
if err != nil {
return nil, err
}
Expand All @@ -93,7 +83,8 @@ func (s *Site) writeDestAlias(path, permalink string, p *Page) (err error) {
}

func (s *Site) publishDestAlias(allowRoot bool, path, permalink string, p *Page) (err error) {
handler := newAliasHandler(s.Tmpl, s.Log, allowRoot)

handler := newAliasHandler(s.Tmpl.Lookup("alias.html"), s.Log, allowRoot)

isXHTML := strings.HasSuffix(path, ".xhtml")

Expand Down
8 changes: 4 additions & 4 deletions hugolib/embedded_shortcodes_test.go
Expand Up @@ -335,8 +335,8 @@ func TestShortcodeTweet(t *testing.T) {
th = testHelper{cfg, fs, t}
)

withTemplate := func(templ tpl.TemplateHandler) error {
templ.(tpl.TemplateTestMocker).SetFuncs(tweetFuncMap)
withTemplate := func(templ tpl.Template) error {
templ.Funcs(tweetFuncMap)
return nil
}

Expand Down Expand Up @@ -390,8 +390,8 @@ func TestShortcodeInstagram(t *testing.T) {
th = testHelper{cfg, fs, t}
)

withTemplate := func(templ tpl.TemplateHandler) error {
templ.(tpl.TemplateTestMocker).SetFuncs(instagramFuncMap)
withTemplate := func(templ tpl.Template) error {
templ.Funcs(instagramFuncMap)
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions hugolib/hugo_sites.go
Expand Up @@ -127,11 +127,11 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) {
return newHugoSites(cfg, sites...)
}

func (s *Site) withSiteTemplates(withTemplates ...func(templ tpl.TemplateHandler) error) func(templ tpl.TemplateHandler) error {
return func(templ tpl.TemplateHandler) error {
templ.LoadTemplates(s.PathSpec.GetLayoutDirPath(), "")
func (s *Site) withSiteTemplates(withTemplates ...func(templ tpl.Template) error) func(templ tpl.Template) error {
return func(templ tpl.Template) error {
templ.LoadTemplates(s.PathSpec.GetLayoutDirPath())
if s.PathSpec.ThemeSet() {
templ.LoadTemplates(s.PathSpec.GetThemeDir()+"/layouts", "theme")
templ.LoadTemplatesWithPrefix(s.PathSpec.GetThemeDir()+"/layouts", "theme")
}

for _, wt := range withTemplates {
Expand Down
3 changes: 1 addition & 2 deletions hugolib/page.go
Expand Up @@ -1384,14 +1384,13 @@ func (p *Page) prepareLayouts() error {
if p.Kind == KindPage {
if !p.IsRenderable() {
self := "__" + p.UniqueID()
err := p.s.Tmpl.AddLateTemplate(self, string(p.Content))
_, err := p.s.Tmpl.GetClone().New(self).Parse(string(p.Content))
if err != nil {
return err
}
p.selfLayout = self
}
}

return nil
}

Expand Down
24 changes: 2 additions & 22 deletions hugolib/page_output.go
Expand Up @@ -110,29 +110,9 @@ func (p *PageOutput) Render(layout ...string) template.HTML {
l, err := p.layouts(layout...)
if err != nil {
helpers.DistinctErrorLog.Printf("in .Render: Failed to resolve layout %q for page %q", layout, p.pathOrTitle())
return ""
return template.HTML("")
}

for _, layout := range l {
templ := p.s.Tmpl.Lookup(layout)
if templ == nil {
// This is legacy from when we had only one output format and
// HTML templates only. Some have references to layouts without suffix.
// We default to good old HTML.
templ = p.s.Tmpl.Lookup(layout + ".html")
}
if templ != nil {
res, err := templ.ExecuteToString(p)
if err != nil {
helpers.DistinctErrorLog.Printf("in .Render: Failed to execute template %q for page %q", layout, p.pathOrTitle())
return template.HTML("")
}
return template.HTML(res)
}
}

return ""

return p.s.Tmpl.ExecuteTemplateToHTML(p, l...)
}

func (p *Page) Render(layout ...string) template.HTML {
Expand Down
16 changes: 9 additions & 7 deletions hugolib/shortcode.go
Expand Up @@ -177,7 +177,7 @@ var isInnerShortcodeCache = struct {
// to avoid potential costly look-aheads for closing tags we look inside the template itself
// we could change the syntax to self-closing tags, but that would make users cry
// the value found is cached
func isInnerShortcode(t tpl.TemplateExecutor) (bool, error) {
func isInnerShortcode(t *template.Template) (bool, error) {
isInnerShortcodeCache.RLock()
m, ok := isInnerShortcodeCache.m[t.Name()]
isInnerShortcodeCache.RUnlock()
Expand All @@ -188,7 +188,10 @@ func isInnerShortcode(t tpl.TemplateExecutor) (bool, error) {

isInnerShortcodeCache.Lock()
defer isInnerShortcodeCache.Unlock()
match, _ := regexp.MatchString("{{.*?\\.Inner.*?}}", t.Tree())
if t.Tree == nil {
return false, errors.New("Template failed to compile")
}
match, _ := regexp.MatchString("{{.*?\\.Inner.*?}}", t.Tree.Root.String())
isInnerShortcodeCache.m[t.Name()] = match

return match, nil
Expand Down Expand Up @@ -395,6 +398,8 @@ Loop:
case tScName:
sc.name = currItem.val
tmpl := getShortcodeTemplate(sc.name, p.s.Tmpl)
{
}
if tmpl == nil {
return sc, fmt.Errorf("Unable to locate template for shortcode %q in page %q", sc.name, p.Path())
}
Expand Down Expand Up @@ -565,10 +570,7 @@ func replaceShortcodeTokens(source []byte, prefix string, replacements map[strin
return source, nil
}

func getShortcodeTemplate(name string, t tpl.TemplateHandler) *tpl.TemplateAdapter {
isInnerShortcodeCache.RLock()
defer isInnerShortcodeCache.RUnlock()

func getShortcodeTemplate(name string, t tpl.Template) *template.Template {
if x := t.Lookup("shortcodes/" + name + ".html"); x != nil {
return x
}
Expand All @@ -578,7 +580,7 @@ func getShortcodeTemplate(name string, t tpl.TemplateHandler) *tpl.TemplateAdapt
return t.Lookup("_internal/shortcodes/" + name + ".html")
}

func renderShortcodeWithPage(tmpl tpl.Template, data *ShortcodeWithPage) string {
func renderShortcodeWithPage(tmpl *template.Template, data *ShortcodeWithPage) string {
buffer := bp.GetBuffer()
defer bp.PutBuffer(buffer)

Expand Down

0 comments on commit 7eb71ee

Please sign in to comment.