shithub: hugo

Download patch

ref: c7c6b47ba8bb098cf9fac778f7818afba40a1e2f
parent: 08fa2b112407c1fe180e6c02dd8fe63735608336
author: Bjørn Erik Pedersen <[email protected]>
date: Sun Mar 19 11:25:32 EDT 2017

hubolib: Pick layout per output format

--- 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 @@
 
 const (
 	alias      = "<!DOCTYPE html><html><head><title>{{ .Permalink }}</title><link rel=\"canonical\" href=\"{{ .Permalink }}\"/><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /><meta http-equiv=\"refresh\" content=\"0; url={{ .Permalink }}\" /></head></html>"
-	aliasXHtml = "<!DOCTYPE html><html xmlns=\"http://wwa.w3.org/1999/xhtml\"><head><title>{{ .Permalink }}</title><link rel=\"canonical\" href=\"{{ .Permalink }}\"/><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /><meta http-equiv=\"refresh\" content=\"0; url={{ .Permalink }}\" /></head></html>"
+	aliasXHtml = "<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>{{ .Permalink }}</title><link rel=\"canonical\" href=\"{{ .Permalink }}\"/><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /><meta http-equiv=\"refresh\" content=\"0; url={{ .Permalink }}\" /></head></html>"
 )
 
 var defaultAliasTemplates *template.Template
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -656,23 +656,6 @@
 	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 @@
 	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) 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 @@
 			if err != nil {
 				return err
 			}
-			layouts = append(layouts, self)
-		} else {
-			layouts = append(layouts, p.layouts()...)
+			p.layoutsCalculated = []string{self}
 		}
-		p.layoutsCalculated = layouts
 	}
 	return nil
 }
--- 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"
@@ -80,4 +81,31 @@
 		panic(err)
 	}
 	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...)
 }
--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -547,12 +547,6 @@
 	}
 }
 
-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 @@
 		checkPageContent(t, p, normalizeExpected(ext, "<p>Simple Page</p>\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")
 	}
 
--- a/hugolib/site_render.go
+++ b/hugolib/site_render.go
@@ -147,10 +147,11 @@
 			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
 			}