Move writing next to rendering

All render(Thing) calls have a WritePublic call directly after it.  This
refactor creates one function that makes the specific call sequence.
This commit is contained in:
Noah Campbell 2013-10-01 19:58:15 -07:00
parent 2176d2c197
commit 19cb6c7819
2 changed files with 38 additions and 46 deletions

View file

@ -376,11 +376,7 @@ func (s *Site) RenderPages() (err error) {
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
}
@ -402,14 +398,10 @@ func (s *Site) RenderIndexes() error {
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
}
@ -444,12 +436,7 @@ func (s *Site) RenderIndexesIndexes() (err error) {
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
}
@ -469,11 +456,7 @@ func (s *Site) RenderLists() error {
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
}
@ -508,11 +491,7 @@ func (s *Site) RenderHomePage() error {
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
}
@ -524,7 +503,7 @@ func (s *Site) RenderHomePage() error {
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
}
@ -534,14 +513,7 @@ func (s *Site) RenderHomePage() error {
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
@ -575,6 +547,19 @@ func (s *Site) NewNode() *Node {
}
}
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)

View file

@ -114,20 +114,28 @@ func TestrenderThing(t *testing.T) {
}
}
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 {
@ -141,20 +149,19 @@ func TestrenderThingOrDefault(t *testing.T) {
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"])
}
}
}