1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
// Copyright 2017-present The Hugo Authors. All rights reserved.
//
// 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://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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package output
import (
"fmt"
"path"
"strings"
)
// LayoutIdentifier is used to pick the correct layout for a piece of content.
type LayoutIdentifier interface {
PageType() string
PageSection() string // TODO(bep) name
PageKind() string
PageLayout() string
}
// Layout calculates the layout template to use to render a given output type.
// TODO(bep) output improve names
type LayoutHandler struct {
hasTheme bool
}
func NewLayoutHandler(hasTheme bool) *LayoutHandler {
return &LayoutHandler{hasTheme: hasTheme}
}
// TODO(bep) output theme layouts
var (
layoutsHome = "index.html _default/list.html"
layoutsSection = "section/SECTION.html SECTION/list.html _default/section.html _default/list.html indexes/SECTION.html _default/indexes.html"
layoutTaxonomy = "taxonomy/SECTION.html indexes/SECTION.html _default/taxonomy.html _default/list.html"
layoutTaxonomyTerm = "taxonomy/SECTION.terms.html _default/terms.html indexes/indexes.html"
)
func (l *LayoutHandler) For(id LayoutIdentifier, layoutOverride string, tp Type) []string {
var layouts []string
layout := id.PageLayout()
if layoutOverride != "" {
layout = layoutOverride
}
switch id.PageKind() {
// TODO(bep) move the Kind constants some common place.
case "home":
layouts = strings.Fields(layoutsHome)
case "section":
layouts = strings.Fields(strings.Replace(layoutsSection, "SECTION", id.PageSection(), -1))
case "taxonomy":
layouts = strings.Fields(strings.Replace(layoutTaxonomy, "SECTION", id.PageSection(), -1))
case "taxonomyTerm":
layouts = strings.Fields(strings.Replace(layoutTaxonomyTerm, "SECTION", id.PageSection(), -1))
case "page":
layouts = regularPageLayouts(id.PageType(), layout)
}
if l.hasTheme {
layoutsWithThemeLayouts := []string{}
// First place all non internal templates
for _, t := range layouts {
if !strings.HasPrefix(t, "_internal/") {
layoutsWithThemeLayouts = append(layoutsWithThemeLayouts, t)
}
}
// Then place theme templates with the same names
for _, t := range layouts {
if !strings.HasPrefix(t, "_internal/") {
layoutsWithThemeLayouts = append(layoutsWithThemeLayouts, "theme/"+t)
}
}
// Lastly place internal templates
for _, t := range layouts {
if strings.HasPrefix(t, "_internal/") {
layoutsWithThemeLayouts = append(layoutsWithThemeLayouts, t)
}
}
return layoutsWithThemeLayouts
}
return layouts
}
func regularPageLayouts(types string, layout string) (layouts []string) {
if layout == "" {
layout = "single"
}
if types != "" {
t := strings.Split(types, "/")
// Add type/layout.html
for i := range t {
search := t[:len(t)-i]
layouts = append(layouts, fmt.Sprintf("%s/%s.html", strings.ToLower(path.Join(search...)), layout))
}
}
// Add _default/layout.html
layouts = append(layouts, fmt.Sprintf("_default/%s.html", layout))
return
}
|