summaryrefslogtreecommitdiffstats
path: root/output
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-03-06 11:20:30 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-03-27 15:43:56 +0200
commit3bb52bf7bff72457481acabfe8268915e23a7d23 (patch)
treecfa554352b52d468769b0247b94d5d545a19773b /output
parentd3d2c63dd98ea2339ae31501f04346cfb93be6c6 (diff)
output: Move layout logic to the output package
Diffstat (limited to 'output')
-rw-r--r--output/layout.go85
-rw-r--r--output/layout_test.go68
2 files changed, 153 insertions, 0 deletions
diff --git a/output/layout.go b/output/layout.go
new file mode 100644
index 000000000..7646caf48
--- /dev/null
+++ b/output/layout.go
@@ -0,0 +1,85 @@
+// 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"
+)
+
+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 Layout struct {
+}
+
+// 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 *Layout) For(id LayoutIdentifier, tp Type) []string {
+ var layouts []string
+
+ 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(), id.PageLayout())
+ }
+
+ for _, l := range layouts {
+ layouts = append(layouts, "theme/"+l)
+ }
+
+ 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
+}
diff --git a/output/layout_test.go b/output/layout_test.go
new file mode 100644
index 000000000..897b451c9
--- /dev/null
+++ b/output/layout_test.go
@@ -0,0 +1,68 @@
+// 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 (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+type testLayoutIdentifier struct {
+ pageKind string
+ pageSection string
+ pageLayout string
+ pageType string
+}
+
+func (l testLayoutIdentifier) PageKind() string {
+ return l.pageKind
+}
+
+func (l testLayoutIdentifier) PageLayout() string {
+ return l.pageLayout
+}
+
+func (l testLayoutIdentifier) PageType() string {
+ return l.pageType
+}
+
+func (l testLayoutIdentifier) PageSection() string {
+ return l.pageSection
+}
+
+func TestLayout(t *testing.T) {
+ l := &Layout{}
+
+ for _, this := range []struct {
+ li testLayoutIdentifier
+ tp Type
+ expect []string
+ }{
+ {testLayoutIdentifier{"home", "", "", ""}, HTMLType, []string{"index.html", "_default/list.html", "theme/index.html", "theme/_default/list.html"}},
+ {testLayoutIdentifier{"section", "sect1", "", ""}, HTMLType, []string{"section/sect1.html", "sect1/list.html"}},
+ {testLayoutIdentifier{"taxonomy", "tag", "", ""}, HTMLType, []string{"taxonomy/tag.html", "indexes/tag.html"}},
+ {testLayoutIdentifier{"taxonomyTerm", "categories", "", ""}, HTMLType, []string{"taxonomy/categories.terms.html", "_default/terms.html"}},
+ {testLayoutIdentifier{"page", "", "", ""}, HTMLType, []string{"_default/single.html", "theme/_default/single.html"}},
+ {testLayoutIdentifier{"page", "", "mylayout", ""}, HTMLType, []string{"_default/mylayout.html"}},
+ {testLayoutIdentifier{"page", "", "mylayout", "myttype"}, HTMLType, []string{"myttype/mylayout.html", "_default/mylayout.html"}},
+ {testLayoutIdentifier{"page", "", "mylayout", "myttype/mysubtype"}, HTMLType, []string{"myttype/mysubtype/mylayout.html", "myttype/mylayout.html", "_default/mylayout.html"}},
+ } {
+ layouts := l.For(this.li, this.tp)
+ require.NotNil(t, layouts)
+ require.True(t, len(layouts) >= len(this.expect))
+ // Not checking the complete list for now ...
+ require.Equal(t, this.expect, layouts[:len(this.expect)])
+ }
+}