mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Refactored added RenderThingOrDefault and tests
Signed-off-by: Noah Campbell <noahcampbell@gmail.com> Conflicts: hugolib/site.go
This commit is contained in:
parent
1de1992664
commit
1bb00b8c19
2 changed files with 63 additions and 10 deletions
|
@ -447,13 +447,9 @@ func (s *Site) RenderAliases() error {
|
||||||
|
|
||||||
func (s *Site) RenderPages() error {
|
func (s *Site) RenderPages() error {
|
||||||
for i, _ := range s.Pages {
|
for i, _ := range s.Pages {
|
||||||
content, err := s.RenderThing(s.Pages[i], s.Pages[i].Layout())
|
content, err := s.RenderThingOrDefault(s.Pages[i], s.Pages[i].Layout(), "_default/single.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var err2 error
|
return err
|
||||||
content, err2 = s.RenderThing(s.Pages[i], "_default/single.html")
|
|
||||||
if err2 != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
s.Pages[i].RenderedContent = content
|
s.Pages[i].RenderedContent = content
|
||||||
}
|
}
|
||||||
|
@ -549,11 +545,11 @@ func (s *Site) RenderLists() error {
|
||||||
n.Data["Pages"] = data
|
n.Data["Pages"] = data
|
||||||
layout := "indexes/" + section + ".html"
|
layout := "indexes/" + section + ".html"
|
||||||
|
|
||||||
x, err := s.RenderThing(n, layout)
|
content, err := s.RenderThingOrDefault(n, layout, "_default/index.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.WritePublic(section+"/index.html", x.Bytes())
|
s.WritePublic(section+"/index.html", content.Bytes())
|
||||||
|
|
||||||
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
|
if a := s.Tmpl.Lookup("rss.xml"); a != nil {
|
||||||
// XML Feed
|
// XML Feed
|
||||||
|
@ -639,6 +635,18 @@ func (s *Site) RenderThing(d interface{}, layout string) (*bytes.Buffer, error)
|
||||||
return buffer, err
|
return buffer, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Site) RenderThingOrDefault(d interface{}, layout string, defaultLayout string) (*bytes.Buffer, error) {
|
||||||
|
content, err := s.RenderThing(d, layout)
|
||||||
|
if err != nil {
|
||||||
|
var err2 error
|
||||||
|
content, err2 = s.RenderThing(d, defaultLayout)
|
||||||
|
if err2 == nil {
|
||||||
|
return content, err2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return content, err
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Site) NewXMLBuffer() *bytes.Buffer {
|
func (s *Site) NewXMLBuffer() *bytes.Buffer {
|
||||||
header := "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
|
header := "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>\n"
|
||||||
return bytes.NewBufferString(header)
|
return bytes.NewBufferString(header)
|
||||||
|
|
|
@ -2,6 +2,7 @@ package hugolib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"bytes"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -104,8 +105,8 @@ func TestRenderThing(t *testing.T) {
|
||||||
t.Fatalf("Unable to add template")
|
t.Fatalf("Unable to add template")
|
||||||
}
|
}
|
||||||
|
|
||||||
html, err := s.RenderThing(p, templateName)
|
html, err2 := s.RenderThing(p, templateName)
|
||||||
if err != nil {
|
if err2 != nil {
|
||||||
t.Errorf("Unable to render html: %s", err)
|
t.Errorf("Unable to render html: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,3 +115,47 @@ func TestRenderThing(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRenderThingOrDefault(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
content string
|
||||||
|
missing bool
|
||||||
|
template string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{PAGE_SIMPLE_TITLE, true, TEMPLATE_TITLE, "simple template"},
|
||||||
|
{PAGE_SIMPLE_TITLE, true, TEMPLATE_FUNC, "simple-template"},
|
||||||
|
{PAGE_SIMPLE_TITLE, false, TEMPLATE_TITLE, "simple template"},
|
||||||
|
{PAGE_SIMPLE_TITLE, false, TEMPLATE_FUNC, "simple-template"},
|
||||||
|
}
|
||||||
|
|
||||||
|
s := new(Site)
|
||||||
|
s.prepTemplates()
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
p, err := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error parsing buffer: %s", err)
|
||||||
|
}
|
||||||
|
templateName := fmt.Sprintf("default%d", i)
|
||||||
|
err = s.addTemplate(templateName, test.template)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unable to add template")
|
||||||
|
}
|
||||||
|
|
||||||
|
var html *bytes.Buffer
|
||||||
|
var err2 error
|
||||||
|
if test.missing {
|
||||||
|
html, err2 = s.RenderThingOrDefault(p, "missing", templateName)
|
||||||
|
} else {
|
||||||
|
html, err2 = s.RenderThingOrDefault(p, templateName, "missing_default")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err2 != nil {
|
||||||
|
t.Errorf("Unable to render html: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if string(html.Bytes()) != test.expected {
|
||||||
|
t.Errorf("Content does not match. Expected '%s', got '%s'", test.expected, html)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
|
Loading…
Reference in a new issue