From c7c6b47ba8bb098cf9fac778f7818afba40a1e2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 19 Mar 2017 15:25:32 +0100 Subject: [PATCH] hubolib: Pick layout per output format --- hugolib/alias.go | 4 ++-- hugolib/page.go | 28 +--------------------------- hugolib/page_output.go | 28 ++++++++++++++++++++++++++++ hugolib/page_test.go | 11 ----------- hugolib/site_render.go | 3 ++- 5 files changed, 33 insertions(+), 41 deletions(-) diff --git a/hugolib/alias.go b/hugolib/alias.go index 63f0bb996..7120e6086 100644 --- a/hugolib/alias.go +++ b/hugolib/alias.go @@ -3,7 +3,7 @@ // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// http://wwa.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, @@ -29,7 +29,7 @@ import ( const ( alias = "{{ .Permalink }}" - aliasXHtml = "{{ .Permalink }}" + aliasXHtml = "{{ .Permalink }}" ) var defaultAliasTemplates *template.Template diff --git a/hugolib/page.go b/hugolib/page.go index 2f21eb313..f477e6a14 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -656,23 +656,6 @@ func (p *Page) Section() string { return p.Source.Section() } -func (p *Page) layouts(layouts ...string) []string { - // TODO(bep) output the logic here needs to be redone. - if len(layouts) == 0 && len(p.layoutsCalculated) > 0 { - return p.layoutsCalculated - } - - layoutOverride := "" - if len(layouts) > 0 { - layoutOverride = layouts[0] - } - - return p.s.layoutHandler.For( - p.layoutDescriptor, - layoutOverride, - output.HTMLType) -} - // TODO(bep) consolidate and test these KindHome switches (see other layouts methods)s // rssLayouts returns RSS layouts to use for the RSS version of this page, nil // if no RSS should be rendered. @@ -1285,11 +1268,6 @@ func (p *Page) Menus() PageMenus { return p.pageMenus } -func (p *Page) Render(layout ...string) template.HTML { - l := p.layouts(layout...) - return p.s.Tmpl.ExecuteTemplateToHTML(p, l...) -} - func (p *Page) determineMarkupType() string { // Try markup explicitly set in the frontmatter p.Markup = helpers.GuessType(p.Markup) @@ -1420,7 +1398,6 @@ func (p *Page) FullFilePath() string { func (p *Page) prepareLayouts() error { // TODO(bep): Check the IsRenderable logic. if p.Kind == KindPage { - var layouts []string if !p.IsRenderable() { // TODO(bep) output self := "__" + p.UniqueID() @@ -1428,11 +1405,8 @@ func (p *Page) prepareLayouts() error { if err != nil { return err } - layouts = append(layouts, self) - } else { - layouts = append(layouts, p.layouts()...) + p.layoutsCalculated = []string{self} } - p.layoutsCalculated = layouts } return nil } diff --git a/hugolib/page_output.go b/hugolib/page_output.go index de51a4401..fa9a45190 100644 --- a/hugolib/page_output.go +++ b/hugolib/page_output.go @@ -14,6 +14,7 @@ package hugolib import ( + "html/template" "sync" "github.com/spf13/hugo/output" @@ -81,3 +82,30 @@ func (p *PageOutput) copy() *PageOutput { } return c } + +func (p *PageOutput) layouts(layouts ...string) []string { + // TODO(bep) output the logic here needs to be redone. + if len(layouts) == 0 && len(p.layoutsCalculated) > 0 { + return p.layoutsCalculated + } + + layoutOverride := "" + if len(layouts) > 0 { + layoutOverride = layouts[0] + } + + return p.s.layoutHandler.For( + p.layoutDescriptor, + layoutOverride, + p.outputFormat) +} + +func (p *PageOutput) Render(layout ...string) template.HTML { + l := p.layouts(layout...) + return p.s.Tmpl.ExecuteTemplateToHTML(p, l...) +} + +// TODO(bep) output +func (p *Page) Render(layout ...string) template.HTML { + return p.mainPageOutput.Render(layout...) +} diff --git a/hugolib/page_test.go b/hugolib/page_test.go index ac615ab89..d23918603 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -547,12 +547,6 @@ func checkPageType(t *testing.T, page *Page, pageType string) { } } -func checkPageLayout(t *testing.T, page *Page, layout ...string) { - if !listEqual(page.layouts(), layout) { - t.Fatalf("Page layout is:\n%s. Expected:\n%s", page.layouts(), layout) - } -} - func checkPageDate(t *testing.T, page *Page, time time.Time) { if page.Date != time { t.Fatalf("Page date is: %s. Expected: %s", page.Date, time) @@ -659,11 +653,6 @@ func TestCreateNewPage(t *testing.T) { checkPageContent(t, p, normalizeExpected(ext, "

Simple Page

\n")) checkPageSummary(t, p, "Simple Page") checkPageType(t, p, "page") - checkPageLayout(t, p, - "page/single.html.html", "page/single.html", - "_default/single.html.html", "_default/single.html", - "theme/page/single.html.html", "theme/page/single.html", - "theme/_default/single.html.html", "theme/_default/single.html") checkTruncation(t, p, false, "simple short page") } diff --git a/hugolib/site_render.go b/hugolib/site_render.go index e31b6ada9..6ab67c408 100644 --- a/hugolib/site_render.go +++ b/hugolib/site_render.go @@ -147,10 +147,11 @@ func (s *Site) renderPaginator(p *PageOutput) error { pageNumber := i + 1 addend := fmt.Sprintf("/%s/%d", paginatePath, pageNumber) targetPath, _ := p.targetPath(addend) + layouts := p.layouts() if err := s.renderAndWritePage( pagerNode.Title, - targetPath, pagerNode, p.layouts()...); err != nil { + targetPath, pagerNode, layouts...); err != nil { return err }