From f091fc23edfa912ae3e6e2d3a80d65432db6e35e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 7 Mar 2017 14:20:39 +0100 Subject: [PATCH] hugolib: Add basic setup for output def per Kind --- hugolib/hugo_sites.go | 7 +----- hugolib/site.go | 4 +++ hugolib/site_output.go | 50 +++++++++++++++++++++++++++++++++++++ hugolib/site_output_test.go | 42 +++++++++++++++++++++++++++++++ hugolib/site_render.go | 3 +-- output/outputType.go | 11 ++++++++ 6 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 hugolib/site_output.go create mode 100644 hugolib/site_output_test.go diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index 3cd44e46d..0ffc286d6 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -548,12 +548,7 @@ func (s *Site) preparePagesForRender(cfg *BuildCfg) { p.Content = helpers.BytesToHTML(workContentCopy) } - // TODO(bep) output this is temporary - if p.IsNode() { - p.outputTypes = outputTypesWithRSS - } else { - p.outputTypes = outputTypesHTML - } + p.outputTypes = defaultOutputDefinitions.ForKind(p.Kind) //analyze for raw stats p.analyzePage() diff --git a/hugolib/site.go b/hugolib/site.go index b3825a9bb..2a9db1abe 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -1642,6 +1642,10 @@ func (s *Site) kindFromSections(sections []string) string { return KindSection } +func (s *Site) layouts(p *PageOutput) []string { + return s.layoutHandler.For(p.layoutIdentifier, "", p.outputType) +} + func (s *Site) preparePages() error { var errors []error diff --git a/hugolib/site_output.go b/hugolib/site_output.go new file mode 100644 index 000000000..7f6fa2d4a --- /dev/null +++ b/hugolib/site_output.go @@ -0,0 +1,50 @@ +// 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 hugolib + +import ( + "strings" + + "github.com/spf13/hugo/output" +) + +var defaultOutputDefinitions = siteOutputDefinitions{ + // All have HTML + siteOutputDefinition{ExcludedKinds: "", Outputs: []output.Type{output.HTMLType}}, + // Some have RSS + siteOutputDefinition{ExcludedKinds: "page", Outputs: []output.Type{output.RSSType}}, +} + +type siteOutputDefinitions []siteOutputDefinition + +type siteOutputDefinition struct { + // What Kinds of pages are excluded in this definition. + // A blank strings means NONE. + // Comma separated list (for now). + ExcludedKinds string + + Outputs []output.Type +} + +func (defs siteOutputDefinitions) ForKind(kind string) []output.Type { + var result []output.Type + + for _, def := range defs { + if def.ExcludedKinds == "" || !strings.Contains(def.ExcludedKinds, kind) { + result = append(result, def.Outputs...) + } + } + + return result +} diff --git a/hugolib/site_output_test.go b/hugolib/site_output_test.go new file mode 100644 index 000000000..e67818fb1 --- /dev/null +++ b/hugolib/site_output_test.go @@ -0,0 +1,42 @@ +// 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 hugolib + +import ( + "reflect" + "testing" + + "github.com/spf13/hugo/output" +) + +func TestDefaultOutputDefinitions(t *testing.T) { + defs := defaultOutputDefinitions + + tests := []struct { + name string + kind string + want []output.Type + }{ + {"RSS not for regular pages", KindPage, []output.Type{output.HTMLType}}, + {"Home Sweet Home", KindHome, []output.Type{output.HTMLType, output.RSSType}}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := defs.ForKind(tt.kind); !reflect.DeepEqual(got, tt.want) { + t.Errorf("siteOutputDefinitions.ForKind(%v) = %v, want %v", tt.kind, got, tt.want) + } + }) + } +} diff --git a/hugolib/site_render.go b/hugolib/site_render.go index 54cba59bc..466e01ffb 100644 --- a/hugolib/site_render.go +++ b/hugolib/site_render.go @@ -73,7 +73,7 @@ func pageRenderer(s *Site, pages <-chan *Page, results chan<- error, wg *sync.Wa // TODO(bep) output layouts = pageOutput.layoutsCalculated } else { - layouts = s.layoutHandler.For(pageOutput.layoutIdentifier, "", pageOutput.outputType) + layouts = s.layouts(pageOutput) } switch pageOutput.outputType { @@ -87,7 +87,6 @@ func pageRenderer(s *Site, pages <-chan *Page, results chan<- error, wg *sync.Wa results <- err } - // Taxonomy terms have no page set to paginate, so skip that for now. if pageOutput.IsNode() { if err := s.renderPaginator(pageOutput); err != nil { results <- err diff --git a/output/outputType.go b/output/outputType.go index 222494b09..cf5fff76e 100644 --- a/output/outputType.go +++ b/output/outputType.go @@ -21,11 +21,13 @@ var ( HTMLType = Type{ Name: "HTML", MediaType: media.HTMLType, + BaseName: "index", } RSSType = Type{ Name: "RSS", MediaType: media.RSSType, + BaseName: "index", } ) @@ -42,7 +44,16 @@ type Type struct { // Must be set to a value when there are two or more conflicting mediatype for the same resource. Path string + // The base output file name used when not using "ugly URLs", defaults to "index". + BaseName string + + // The protocol to use, i.e. "webcal://". Defaults to the protocol of the baseURL. + Protocol string + // IsPlainText decides whether to use text/template or html/template // as template parser. IsPlainText bool + + // Enable to ignore the global uglyURLs setting. + NoUgly bool }