mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Reducing scope of RenderThing*
This commit is contained in:
parent
ff8b52758d
commit
2176d2c197
2 changed files with 24 additions and 21 deletions
|
@ -376,7 +376,7 @@ func (s *Site) RenderPages() (err error) {
|
||||||
layout = p.Layout()
|
layout = p.Layout()
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := s.RenderThingOrDefault(p, layout, "_default/single.html")
|
content, err := s.renderThingOrDefault(p, layout, "_default/single.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -402,7 +402,7 @@ func (s *Site) RenderIndexes() error {
|
||||||
n.Data[singular] = o
|
n.Data[singular] = o
|
||||||
n.Data["Pages"] = o
|
n.Data["Pages"] = o
|
||||||
layout := "indexes/" + singular + ".html"
|
layout := "indexes/" + singular + ".html"
|
||||||
x, err := s.RenderThing(n, layout)
|
x, err := s.renderThing(n, layout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -444,7 +444,7 @@ func (s *Site) RenderIndexesIndexes() (err error) {
|
||||||
n.Data["Index"] = s.Indexes[plural]
|
n.Data["Index"] = s.Indexes[plural]
|
||||||
n.Data["OrderedIndex"] = s.Info.Indexes[plural]
|
n.Data["OrderedIndex"] = s.Info.Indexes[plural]
|
||||||
|
|
||||||
x, err := s.RenderThing(n, layout)
|
x, err := s.renderThing(n, layout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -469,7 +469,7 @@ func (s *Site) RenderLists() error {
|
||||||
n.Data["Pages"] = data
|
n.Data["Pages"] = data
|
||||||
layout := "indexes/" + section + ".html"
|
layout := "indexes/" + section + ".html"
|
||||||
|
|
||||||
content, err := s.RenderThingOrDefault(n, layout, "_default/index.html")
|
content, err := s.renderThingOrDefault(n, layout, "_default/index.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -508,7 +508,7 @@ func (s *Site) RenderHomePage() error {
|
||||||
n.Data["Pages"] = s.Pages[:9]
|
n.Data["Pages"] = s.Pages[:9]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
x, err := s.RenderThing(n, "index.html")
|
x, err := s.renderThing(n, "index.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -534,7 +534,7 @@ func (s *Site) RenderHomePage() error {
|
||||||
n.Url = helpers.Urlize("404.html")
|
n.Url = helpers.Urlize("404.html")
|
||||||
n.Title = "404 Page not found"
|
n.Title = "404 Page not found"
|
||||||
n.Permalink = permalink(s, "404.html")
|
n.Permalink = permalink(s, "404.html")
|
||||||
x, err := s.RenderThing(n, "404.html")
|
x, err := s.renderThing(n, "404.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -575,7 +575,7 @@ func (s *Site) NewNode() *Node {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Site) RenderThing(d interface{}, layout string) (*bytes.Buffer, error) {
|
func (s *Site) renderThing(d interface{}, layout string) (*bytes.Buffer, error) {
|
||||||
if s.Tmpl.Lookup(layout) == nil {
|
if s.Tmpl.Lookup(layout) == nil {
|
||||||
return nil, fmt.Errorf("Layout not found: %s", layout)
|
return nil, fmt.Errorf("Layout not found: %s", layout)
|
||||||
}
|
}
|
||||||
|
@ -584,11 +584,11 @@ 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) {
|
func (s *Site) renderThingOrDefault(d interface{}, layout string, defaultLayout string) (*bytes.Buffer, error) {
|
||||||
content, err := s.RenderThing(d, layout)
|
content, err := s.renderThing(d, layout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var err2 error
|
var err2 error
|
||||||
content, err2 = s.RenderThing(d, defaultLayout)
|
content, err2 = s.renderThing(d, defaultLayout)
|
||||||
if err2 == nil {
|
if err2 == nil {
|
||||||
return content, err2
|
return content, err2
|
||||||
}
|
}
|
||||||
|
@ -623,9 +623,12 @@ func (s *Site) WritePublic(path string, content io.Reader) (err error) {
|
||||||
&transform.NavActive{Section: "tbd"},
|
&transform.NavActive{Section: "tbd"},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
final := new(bytes.Buffer)
|
reader, writer := io.Pipe()
|
||||||
s.Transformer.Apply(final, content)
|
go func() {
|
||||||
return s.Target.Publish(path, final)
|
s.Transformer.Apply(writer, content)
|
||||||
|
writer.Close()
|
||||||
|
}()
|
||||||
|
return s.Target.Publish(path, reader)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Site) WriteAlias(path string, permalink template.HTML) (err error) {
|
func (s *Site) WriteAlias(path string, permalink template.HTML) (err error) {
|
||||||
|
|
|
@ -46,11 +46,11 @@ func pageMust(p *Page, err error) *Page {
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDegenerateRenderThingMissingTemplate(t *testing.T) {
|
func TestDegeneraterenderThingMissingTemplate(t *testing.T) {
|
||||||
p, _ := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
|
p, _ := ReadFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
|
||||||
s := new(Site)
|
s := new(Site)
|
||||||
s.prepTemplates()
|
s.prepTemplates()
|
||||||
_, err := s.RenderThing(p, "foobar")
|
_, err := s.renderThing(p, "foobar")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Expected err to be returned when missing the template.")
|
t.Errorf("Expected err to be returned when missing the template.")
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ func TestAddInvalidTemplate(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func matchRender(t *testing.T, s *Site, p *Page, tmplName string, expected string) {
|
func matchRender(t *testing.T, s *Site, p *Page, tmplName string, expected string) {
|
||||||
content, err := s.RenderThing(p, tmplName)
|
content, err := s.renderThing(p, tmplName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to render template.")
|
t.Fatalf("Unable to render template.")
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ func matchRender(t *testing.T, s *Site, p *Page, tmplName string, expected strin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRenderThing(t *testing.T) {
|
func TestrenderThing(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
content string
|
content string
|
||||||
template string
|
template string
|
||||||
|
@ -103,7 +103,7 @@ func TestRenderThing(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Content = template.HTML(p.Content)
|
p.Content = template.HTML(p.Content)
|
||||||
html, err2 := s.RenderThing(p, templateName)
|
html, err2 := s.renderThing(p, templateName)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
t.Errorf("Unable to render html: %s", err)
|
t.Errorf("Unable to render html: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -114,7 +114,7 @@ func TestRenderThing(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRenderThingOrDefault(t *testing.T) {
|
func TestrenderThingOrDefault(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
content string
|
content string
|
||||||
missing bool
|
missing bool
|
||||||
|
@ -144,9 +144,9 @@ func TestRenderThingOrDefault(t *testing.T) {
|
||||||
var html *bytes.Buffer
|
var html *bytes.Buffer
|
||||||
var err2 error
|
var err2 error
|
||||||
if test.missing {
|
if test.missing {
|
||||||
html, err2 = s.RenderThingOrDefault(p, "missing", templateName)
|
html, err2 = s.renderThingOrDefault(p, "missing", templateName)
|
||||||
} else {
|
} else {
|
||||||
html, err2 = s.RenderThingOrDefault(p, templateName, "missing_default")
|
html, err2 = s.renderThingOrDefault(p, templateName, "missing_default")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
|
|
Loading…
Reference in a new issue