Move writing next to rendering
authorNoah Campbell <noahcampbell@gmail.com>
Wed, 2 Oct 2013 02:58:15 +0000 (19:58 -0700)
committerNoah Campbell <noahcampbell@gmail.com>
Tue, 8 Oct 2013 16:40:40 +0000 (18:40 +0200)
All render(Thing) calls have a WritePublic call directly after it.  This
refactor creates one function that makes the specific call sequence.

hugolib/site.go
hugolib/site_test.go

index d9e9db0d9f9e2601cff8beecd7c18e027b852838..2b3a6774a87c674534f6157a6f796994ff4db601 100644 (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)
index 62b96ff80c46360ee8467dd8afddf3e52d0f318a..cca0540a7f67ba2d06599d44517a1b7588fc0015 100644 (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"])
                }
        }
 }