layout = p.Layout()
}
- content, err := s.renderThingOrDefault(p, layout, "_default/single.html")
- if err != nil {
- return err
- }
- err = s.WritePublic(p.TargetPath(), content)
+ err := s.render(p, layout, "_default/single.html", p.TargetPath())
if err != nil {
return err
}
n.Data[singular] = o
n.Data["Pages"] = o
layout := "indexes/" + singular + ".html"
- x, err := s.renderThing(n, layout)
- if err != nil {
- return err
- }
var base string
base = plural + "/" + k
- err = s.WritePublic(base+".html", x)
+ err := s.render(n, layout, "_default/indexes.html", base+".html")
if err != nil {
return err
}
n.Data["Index"] = s.Indexes[plural]
n.Data["OrderedIndex"] = s.Info.Indexes[plural]
- x, err := s.renderThing(n, layout)
- if err != nil {
- return err
- }
-
- err = s.WritePublic(plural+"/index.html", x)
+ err := s.render(n, layout, "_default/indexesindexes.html", plural+"/index.html")
if err != nil {
return err
}
n.Data["Pages"] = data
layout := "indexes/" + section + ".html"
- content, err := s.renderThingOrDefault(n, layout, "_default/index.html")
- if err != nil {
- return err
- }
- err = s.WritePublic(section, content)
+ err := s.render(n, layout, "_default/index.html", section)
if err != nil {
return err
}
n.Data["Pages"] = s.Pages[:9]
}
}
- x, err := s.renderThing(n, "index.html")
- if err != nil {
- return err
- }
- err = s.WritePublic("/", x)
+ err := s.render(n, "index.html", "_default/index.html", "/")
if err != nil {
return err
}
n.Permalink = permalink(s, "index.xml")
y := s.NewXMLBuffer()
s.Tmpl.ExecuteTemplate(y, "rss.xml", n)
- err = s.WritePublic(".xml", y)
+ err = s.WritePublic("index.xml", y)
if err != nil {
return err
}
n.Url = helpers.Urlize("404.html")
n.Title = "404 Page not found"
n.Permalink = permalink(s, "404.html")
- x, err := s.renderThing(n, "404.html")
- if err != nil {
- return err
- }
- err = s.WritePublic("404.html", x)
- if err != nil {
- return err
- }
+ return s.render(n, "404.html", "_default/404.html", "404.html")
}
return nil
}
}
+func (s *Site) render(d interface{}, layout, defaultLayout, out string) (err error) {
+ r, err := s.renderThingOrDefault(d, layout, defaultLayout)
+ if err != nil {
+ return err
+ }
+
+ err = s.WritePublic(out, r)
+ if err != nil {
+ return err
+ }
+ return
+}
+
func (s *Site) renderThing(d interface{}, layout string) (*bytes.Buffer, error) {
if s.Tmpl.Lookup(layout) == nil {
return nil, fmt.Errorf("Layout not found: %s", layout)
}
}
-func TestrenderThingOrDefault(t *testing.T) {
+func HTML(in string) string {
+ return fmt.Sprintf("<html><head></head><body>%s</body></html>", in)
+}
+
+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"},
+ {PAGE_SIMPLE_TITLE, true, TEMPLATE_TITLE, HTML("simple template")},
+ {PAGE_SIMPLE_TITLE, true, TEMPLATE_FUNC, HTML("simple-template")},
+ {PAGE_SIMPLE_TITLE, false, TEMPLATE_TITLE, HTML("simple template")},
+ {PAGE_SIMPLE_TITLE, false, TEMPLATE_FUNC, HTML("simple-template")},
}
- s := new(Site)
+ files := make(map[string][]byte)
+ target := &target.InMemoryTarget{Files: files}
+ s := &Site{
+ Target: target,
+ }
s.prepTemplates()
for i, test := range tests {
t.Fatalf("Unable to add template")
}
- var html *bytes.Buffer
var err2 error
if test.missing {
- html, err2 = s.renderThingOrDefault(p, "missing", templateName)
+ err2 = s.render(p, "missing", templateName, "out")
} else {
- html, err2 = s.renderThingOrDefault(p, templateName, "missing_default")
+ err2 = s.render(p, templateName, "missing_default", "out")
}
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)
+ if string(files["out"]) != test.expected {
+ t.Errorf("Content does not match. Expected '%s', got '%s'", test.expected, files["out"])
}
}
}