From: Bjørn Erik Pedersen Date: Thu, 16 Mar 2017 07:32:14 +0000 (+0100) Subject: Rename OutputType to OutputFormat X-Git-Tag: v0.20~94 X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=4c2abe00;p=brevno-suite%2Fhugo Rename OutputType to OutputFormat --- diff --git a/hugolib/hugo_sites_build.go b/hugolib/hugo_sites_build.go index ce7271cd..5a008a51 100644 --- a/hugolib/hugo_sites_build.go +++ b/hugolib/hugo_sites_build.go @@ -176,8 +176,8 @@ func (h *HugoSites) assemble(config *BuildCfg) error { for _, s := range h.Sites { for _, p := range s.Pages { // May have been set in front matter - if len(p.outputTypes) == 0 { - p.outputTypes = s.defaultOutputDefinitions.ForKind(p.Kind) + if len(p.outputFormats) == 0 { + p.outputFormats = s.defaultOutputDefinitions.ForKind(p.Kind) } } s.assembleMenus() diff --git a/hugolib/page.go b/hugolib/page.go index 8d8bd2be..99e3309b 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -198,10 +198,10 @@ type Page struct { lang string - // The output types this page will be rendered to. - outputTypes output.Types + // The output formats this page will be rendered to. + outputFormats output.Formats - // This is the PageOutput that represents the first item in outputTypes. + // This is the PageOutput that represents the first item in outputFormats. // Use with care, as there are potential for inifinite loops. mainPageOutput *PageOutput @@ -792,7 +792,7 @@ func (p *Page) Extension() string { return p.extension } // - // TODO(bep) return p.outputType.MediaType.Suffix + // TODO(bep) return MediaType.Suffix // TODO(bep) remove this config option => return p.s.Cfg.GetString("defaultExtension") @@ -952,13 +952,13 @@ func (p *Page) update(f interface{}) error { case "outputs": outputs := cast.ToStringSlice(v) if len(outputs) > 0 { - // Output types are exlicitly set in front matter, use those. - outTypes, err := output.GetTypes(outputs...) + // Output formats are exlicitly set in front matter, use those. + outFormats, err := output.GetTypes(outputs...) if err != nil { - p.s.Log.ERROR.Printf("Failed to resolve output types: %s", err) + p.s.Log.ERROR.Printf("Failed to resolve output formats: %s", err) } else { - p.outputTypes = outTypes - p.Params[loki] = outTypes + p.outputFormats = outFormats + p.Params[loki] = outFormats } } diff --git a/hugolib/page_output.go b/hugolib/page_output.go index 88386a6d..90dbc963 100644 --- a/hugolib/page_output.go +++ b/hugolib/page_output.go @@ -31,11 +31,11 @@ type PageOutput struct { // Keep this to create URL/path variations, i.e. paginators. targetPathDescriptor targetPathDescriptor - outputType output.Type + outputFormat output.Format } func (p *PageOutput) targetPath(addends ...string) (string, error) { - tp, err := p.createTargetPath(p.outputType, addends...) + tp, err := p.createTargetPath(p.outputFormat, addends...) if err != nil { return "", err } @@ -43,13 +43,13 @@ func (p *PageOutput) targetPath(addends ...string) (string, error) { } -func newPageOutput(p *Page, createCopy bool, outputType output.Type) (*PageOutput, error) { +func newPageOutput(p *Page, createCopy bool, f output.Format) (*PageOutput, error) { if createCopy { p.initURLs() p = p.copy() } - td, err := p.createTargetPathDescriptor(outputType) + td, err := p.createTargetPathDescriptor(f) if err != nil { return nil, err @@ -57,7 +57,7 @@ func newPageOutput(p *Page, createCopy bool, outputType output.Type) (*PageOutpu return &PageOutput{ Page: p, - outputType: outputType, + outputFormat: f, targetPathDescriptor: td, }, nil } @@ -65,7 +65,7 @@ func newPageOutput(p *Page, createCopy bool, outputType output.Type) (*PageOutpu // copy creates a copy of this PageOutput with the lazy sync.Once vars reset // so they will be evaluated again, for word count calculations etc. func (p *PageOutput) copy() *PageOutput { - c, err := newPageOutput(p.Page, true, p.outputType) + c, err := newPageOutput(p.Page, true, p.outputFormat) if err != nil { panic(err) } diff --git a/hugolib/page_paths.go b/hugolib/page_paths.go index 1347102b..209ae14e 100644 --- a/hugolib/page_paths.go +++ b/hugolib/page_paths.go @@ -36,7 +36,7 @@ import ( type targetPathDescriptor struct { PathSpec *helpers.PathSpec - Type output.Type + Type output.Format Kind string Sections []string @@ -66,10 +66,10 @@ type targetPathDescriptor struct { UglyURLs bool } -// createTargetPathDescriptor adapts a Page and the given output.Type into +// createTargetPathDescriptor adapts a Page and the given output.Format into // a targetPathDescriptor. This descriptor can then be used to create paths // and URLs for this Page. -func (p *Page) createTargetPathDescriptor(t output.Type) (targetPathDescriptor, error) { +func (p *Page) createTargetPathDescriptor(t output.Format) (targetPathDescriptor, error) { d := targetPathDescriptor{ PathSpec: p.s.PathSpec, Type: t, @@ -107,9 +107,9 @@ func (p *Page) createTargetPathDescriptor(t output.Type) (targetPathDescriptor, } // createTargetPath creates the target filename for this Page for the given -// output.Type. Some additional URL parts can also be provided, the typical +// output.Format. Some additional URL parts can also be provided, the typical // use case being pagination. -func (p *Page) createTargetPath(t output.Type, addends ...string) (string, error) { +func (p *Page) createTargetPath(t output.Format, addends ...string) (string, error) { d, err := p.createTargetPathDescriptor(t) if err != nil { return "", nil @@ -205,20 +205,20 @@ func createTargetPath(d targetPathDescriptor) string { func (p *Page) createRelativePermalink() string { - if len(p.outputTypes) == 0 { + if len(p.outputFormats) == 0 { panic(fmt.Sprintf("Page %q missing output format(s)", p.Title)) } // Choose the main output format. In most cases, this will be HTML. - outputType := p.outputTypes[0] - tp, err := p.createTargetPath(outputType) + outFormat := p.outputFormats[0] + tp, err := p.createTargetPath(outFormat) if err != nil { p.s.Log.ERROR.Printf("Failed to create permalink for page %q: %s", p.FullFilePath(), err) return "" } - tp = strings.TrimSuffix(tp, outputType.BaseFilename()) + tp = strings.TrimSuffix(tp, outFormat.BaseFilename()) return p.s.PathSpec.URLizeFilename(tp) } diff --git a/hugolib/site.go b/hugolib/site.go index dbd0ea7c..2c5725b0 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -1661,7 +1661,7 @@ func (s *Site) kindFromSections(sections []string) string { } func (s *Site) layouts(p *PageOutput) []string { - return s.layoutHandler.For(p.layoutIdentifier, "", p.outputType) + return s.layoutHandler.For(p.layoutIdentifier, "", p.outputFormat) } func (s *Site) preparePages() error { @@ -1806,7 +1806,7 @@ func (s *Site) renderAndWriteXML(name string, dest string, d interface{}, layout } -func (s *Site) renderAndWritePage(tp output.Type, name string, dest string, d interface{}, layouts ...string) error { +func (s *Site) renderAndWritePage(f output.Format, name string, dest string, d interface{}, layouts ...string) error { renderBuffer := bp.GetBuffer() defer bp.PutBuffer(renderBuffer) @@ -1878,7 +1878,7 @@ Your rendered home page is blank: /index.html is zero-length } - if err = w.writeDestPage(tp, dest, outBuffer); err != nil { + if err = w.writeDestPage(f, dest, outBuffer); err != nil { return err } @@ -2061,7 +2061,7 @@ func (s *Site) newNodePage(typ string) *Page { Data: make(map[string]interface{}), Site: &s.Info, s: s} - p.outputTypes = p.s.defaultOutputDefinitions.ForKind(typ) + p.outputFormats = p.s.defaultOutputDefinitions.ForKind(typ) p.layoutIdentifier = pageLayoutIdentifier{p} return p diff --git a/hugolib/site_output.go b/hugolib/site_output.go index 53d3f979..6b27bb1a 100644 --- a/hugolib/site_output.go +++ b/hugolib/site_output.go @@ -29,11 +29,11 @@ type siteOutputDefinition struct { // Comma separated list (for now). ExcludedKinds string - Outputs []output.Type + Outputs []output.Format } -func (defs siteOutputDefinitions) ForKind(kind string) []output.Type { - var result []output.Type +func (defs siteOutputDefinitions) ForKind(kind string) []output.Format { + var result []output.Format for _, def := range defs { if def.ExcludedKinds == "" || !strings.Contains(def.ExcludedKinds, kind) { @@ -49,7 +49,7 @@ func createSiteOutputDefinitions(cfg config.Provider) siteOutputDefinitions { var defs siteOutputDefinitions // All have HTML - defs = append(defs, siteOutputDefinition{ExcludedKinds: "", Outputs: []output.Type{output.HTMLType}}) + defs = append(defs, siteOutputDefinition{ExcludedKinds: "", Outputs: []output.Format{output.HTMLType}}) // TODO(bep) output deprecate rssURI rssBase := cfg.GetString("rssURI") @@ -63,7 +63,7 @@ func createSiteOutputDefinitions(cfg config.Provider) siteOutputDefinitions { rssType.BaseName = rssBase // Some have RSS - defs = append(defs, siteOutputDefinition{ExcludedKinds: "page", Outputs: []output.Type{rssType}}) + defs = append(defs, siteOutputDefinition{ExcludedKinds: "page", Outputs: []output.Format{rssType}}) return defs } diff --git a/hugolib/site_output_test.go b/hugolib/site_output_test.go index 6ad53c0f..3e5df6a6 100644 --- a/hugolib/site_output_test.go +++ b/hugolib/site_output_test.go @@ -32,10 +32,10 @@ func TestDefaultOutputDefinitions(t *testing.T) { tests := []struct { name string kind string - want []output.Type + want []output.Format }{ - {"RSS not for regular pages", KindPage, []output.Type{output.HTMLType}}, - {"Home Sweet Home", KindHome, []output.Type{output.HTMLType, output.RSSType}}, + {"RSS not for regular pages", KindPage, []output.Format{output.HTMLType}}, + {"Home Sweet Home", KindHome, []output.Format{output.HTMLType, output.RSSType}}, } for _, tt := range tests { @@ -88,7 +88,7 @@ outputs: ["json"] require.NotNil(t, home) - require.Len(t, home.outputTypes, 1) + require.Len(t, home.outputFormats, 1) // TODO(bep) output assert template/text diff --git a/hugolib/site_render.go b/hugolib/site_render.go index 27fbf7dd..c59522b8 100644 --- a/hugolib/site_render.go +++ b/hugolib/site_render.go @@ -65,10 +65,10 @@ func pageRenderer(s *Site, pages <-chan *Page, results chan<- error, wg *sync.Wa var mainPageOutput *PageOutput for page := range pages { - for i, outputType := range page.outputTypes { - pageOutput, err := newPageOutput(page, i > 0, outputType) + for i, outFormat := range page.outputFormats { + pageOutput, err := newPageOutput(page, i > 0, outFormat) if err != nil { - s.Log.ERROR.Printf("Failed to create output page for type %q for page %q: %s", outputType.Name, page, err) + s.Log.ERROR.Printf("Failed to create output page for type %q for page %q: %s", outFormat.Name, page, err) continue } if i == 0 { @@ -85,7 +85,7 @@ func pageRenderer(s *Site, pages <-chan *Page, results chan<- error, wg *sync.Wa layouts = s.layouts(pageOutput) } - switch pageOutput.outputType.Name { + switch pageOutput.outputFormat.Name { case "RSS": if err := s.renderRSS(pageOutput); err != nil { @@ -94,13 +94,13 @@ func pageRenderer(s *Site, pages <-chan *Page, results chan<- error, wg *sync.Wa default: targetPath, err := pageOutput.targetPath() if err != nil { - s.Log.ERROR.Printf("Failed to create target path for output %q for page %q: %s", outputType.Name, page, err) + s.Log.ERROR.Printf("Failed to create target path for output %q for page %q: %s", outFormat.Name, page, err) continue } s.Log.DEBUG.Printf("Render %s to %q with layouts %q", pageOutput.Kind, targetPath, layouts) - if err := s.renderAndWritePage(outputType, "page "+pageOutput.FullFilePath(), targetPath, pageOutput, layouts...); err != nil { + if err := s.renderAndWritePage(outFormat, "page "+pageOutput.FullFilePath(), targetPath, pageOutput, layouts...); err != nil { results <- err } @@ -149,7 +149,7 @@ func (s *Site) renderPaginator(p *PageOutput) error { addend := fmt.Sprintf("/%s/%d", paginatePath, pageNumber) targetPath, _ := p.targetPath(addend) - if err := s.renderAndWritePage(p.outputType, pagerNode.Title, + if err := s.renderAndWritePage(p.outputFormat, pagerNode.Title, targetPath, pagerNode, p.layouts()...); err != nil { return err } diff --git a/hugolib/site_writer.go b/hugolib/site_writer.go index 976f5def..ba28c28d 100644 --- a/hugolib/site_writer.go +++ b/hugolib/site_writer.go @@ -27,7 +27,7 @@ import ( ) // We may find some abstractions/interface(s) here once we star with -// "Multiple Output Types". +// "Multiple Output Formats". type siteWriter struct { langDir string publishDir string @@ -40,8 +40,8 @@ type siteWriter struct { log *jww.Notepad } -func (w siteWriter) targetPathPage(tp output.Type, src string) (string, error) { - dir, err := w.baseTargetPathPage(tp, src) +func (w siteWriter) targetPathPage(f output.Format, src string) (string, error) { + dir, err := w.baseTargetPathPage(f, src) if err != nil { return "", err } @@ -51,7 +51,7 @@ func (w siteWriter) targetPathPage(tp output.Type, src string) (string, error) { return dir, nil } -func (w siteWriter) baseTargetPathPage(tp output.Type, src string) (string, error) { +func (w siteWriter) baseTargetPathPage(f output.Format, src string) (string, error) { if src == helpers.FilePathSeparator { return "index.html", nil } @@ -178,7 +178,7 @@ func filename(f string) string { return f[:len(f)-len(ext)] } -func (w siteWriter) writeDestPage(tp output.Type, path string, reader io.Reader) error { +func (w siteWriter) writeDestPage(f output.Format, path string, reader io.Reader) error { w.log.DEBUG.Println("creating page:", path) path, _ = w.targetPathFile(path) // TODO(bep) output remove this file ... targetPath, err := w.targetPathPage(tp, path) diff --git a/hugolib/site_writer_test.go b/hugolib/site_writer_test.go index a17dae9f..0831c073 100644 --- a/hugolib/site_writer_test.go +++ b/hugolib/site_writer_test.go @@ -127,9 +127,9 @@ func _TestTargetPathUglyURLs(t *testing.T) { w := siteWriter{log: newErrorLogger(), uglyURLs: true} tests := []struct { - outputType output.Type - content string - expected string + outFormat output.Format + content string + expected string }{ {output.HTMLType, "foo.html", "foo.html"}, {output.HTMLType, "/", "index.html"}, @@ -139,7 +139,7 @@ func _TestTargetPathUglyURLs(t *testing.T) { } for i, test := range tests { - dest, err := w.targetPathPage(test.outputType, filepath.FromSlash(test.content)) + dest, err := w.targetPathPage(test.outFormat, filepath.FromSlash(test.content)) if err != nil { t.Fatalf(" [%d] targetPathPage returned an unexpected err: %s", i, err) } diff --git a/output/layout.go b/output/layout.go index 1d2cbeed..d9f1c7a4 100644 --- a/output/layout.go +++ b/output/layout.go @@ -60,7 +60,7 @@ indexes/indexes.NAME.SUFFIX indexes/indexes.SUFFIX ` ) -func (l *LayoutHandler) For(id LayoutIdentifier, layoutOverride string, tp Type) []string { +func (l *LayoutHandler) For(id LayoutIdentifier, layoutOverride string, f Format) []string { var layouts []string layout := id.PageLayout() @@ -72,15 +72,15 @@ func (l *LayoutHandler) For(id LayoutIdentifier, layoutOverride string, tp Type) switch id.PageKind() { // TODO(bep) move the Kind constants some common place. case "home": - layouts = resolveTemplate(layoutsHome, id, tp) + layouts = resolveTemplate(layoutsHome, id, f) case "section": - layouts = resolveTemplate(layoutsSection, id, tp) + layouts = resolveTemplate(layoutsSection, id, f) case "taxonomy": - layouts = resolveTemplate(layoutTaxonomy, id, tp) + layouts = resolveTemplate(layoutTaxonomy, id, f) case "taxonomyTerm": - layouts = resolveTemplate(layoutTaxonomyTerm, id, tp) + layouts = resolveTemplate(layoutTaxonomyTerm, id, f) case "page": - layouts = regularPageLayouts(id.PageType(), layout, tp) + layouts = regularPageLayouts(id.PageType(), layout, f) } if l.hasTheme { @@ -112,10 +112,10 @@ func (l *LayoutHandler) For(id LayoutIdentifier, layoutOverride string, tp Type) return layouts } -func resolveTemplate(templ string, id LayoutIdentifier, tp Type) []string { +func resolveTemplate(templ string, id LayoutIdentifier, f Format) []string { return strings.Fields(replaceKeyValues(templ, - "SUFFIX", tp.MediaType.Suffix, - "NAME", strings.ToLower(tp.Name), + "SUFFIX", f.MediaType.Suffix, + "NAME", strings.ToLower(f.Name), "SECTION", id.PageSection())) } @@ -124,13 +124,13 @@ func replaceKeyValues(s string, oldNew ...string) string { return replacer.Replace(s) } -func regularPageLayouts(types string, layout string, tp Type) (layouts []string) { +func regularPageLayouts(types string, layout string, f Format) (layouts []string) { if layout == "" { layout = "single" } - suffix := tp.MediaType.Suffix - name := strings.ToLower(tp.Name) + suffix := f.MediaType.Suffix + name := strings.ToLower(f.Name) if types != "" { t := strings.Split(types, "/") diff --git a/output/layout_test.go b/output/layout_test.go index 8f851b89..8b71bbe3 100644 --- a/output/layout_test.go +++ b/output/layout_test.go @@ -44,7 +44,7 @@ func (l testLayoutIdentifier) PageSection() string { return l.pageSection } -var ampType = Type{ +var ampType = Format{ Name: "AMP", MediaType: media.HTMLType, BaseName: "index", @@ -57,7 +57,7 @@ func TestLayout(t *testing.T) { li testLayoutIdentifier hasTheme bool layoutOverride string - tp Type + tp Format expect []string }{ {"Home", testLayoutIdentifier{"home", "", "", ""}, true, "", ampType, diff --git a/output/outputFormat.go b/output/outputFormat.go new file mode 100644 index 00000000..8c99aa13 --- /dev/null +++ b/output/outputFormat.go @@ -0,0 +1,120 @@ +// 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" + "strings" + + "github.com/spf13/hugo/media" +) + +var ( + // An ordered list of built-in output formats + // See https://www.ampproject.org/learn/overview/ + AMPType = Format{ + Name: "AMP", + MediaType: media.HTMLType, + BaseName: "index", + Path: "amp", + } + + CSSType = Format{ + Name: "CSS", + MediaType: media.CSSType, + BaseName: "styles", + } + + HTMLType = Format{ + Name: "HTML", + MediaType: media.HTMLType, + BaseName: "index", + } + + JSONType = Format{ + Name: "JSON", + MediaType: media.JSONType, + BaseName: "index", + IsPlainText: true, + } + + RSSType = Format{ + Name: "RSS", + MediaType: media.RSSType, + BaseName: "index", + NoUgly: true, + } +) + +var builtInTypes = map[string]Format{ + strings.ToLower(AMPType.Name): AMPType, + strings.ToLower(CSSType.Name): CSSType, + strings.ToLower(HTMLType.Name): HTMLType, + strings.ToLower(JSONType.Name): JSONType, + strings.ToLower(RSSType.Name): RSSType, +} + +type Formats []Format + +// Format represents an output represenation, usually to a file on disk. +type Format struct { + // The Name is used as an identifier. Internal output formats (i.e. HTML and RSS) + // can be overridden by providing a new definition for those types. + Name string + + MediaType media.Type + + // 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 +} + +func GetType(key string) (Format, bool) { + found, ok := builtInTypes[key] + if !ok { + found, ok = builtInTypes[strings.ToLower(key)] + } + return found, ok +} + +// TODO(bep) outputs rewamp on global config? +func GetTypes(keys ...string) (Formats, error) { + var types []Format + + for _, key := range keys { + tpe, ok := GetType(key) + if !ok { + return types, fmt.Errorf("OutputFormat with key %q not found", key) + } + types = append(types, tpe) + } + + return types, nil +} + +func (t Format) BaseFilename() string { + return t.BaseName + "." + t.MediaType.Suffix +} diff --git a/output/outputFormat_test.go b/output/outputFormat_test.go new file mode 100644 index 00000000..3eb56d8d --- /dev/null +++ b/output/outputFormat_test.go @@ -0,0 +1,43 @@ +// 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/spf13/hugo/media" + "github.com/stretchr/testify/require" +) + +func TestDefaultTypes(t *testing.T) { + require.Equal(t, "HTML", HTMLType.Name) + require.Equal(t, media.HTMLType, HTMLType.MediaType) + require.Empty(t, HTMLType.Path) + require.False(t, HTMLType.IsPlainText) + + require.Equal(t, "RSS", RSSType.Name) + require.Equal(t, media.RSSType, RSSType.MediaType) + require.Empty(t, RSSType.Path) + require.False(t, RSSType.IsPlainText) + require.True(t, RSSType.NoUgly) +} + +func TestGetType(t *testing.T) { + tp, _ := GetType("html") + require.Equal(t, HTMLType, tp) + tp, _ = GetType("HTML") + require.Equal(t, HTMLType, tp) + _, found := GetType("FOO") + require.False(t, found) +} diff --git a/output/outputType.go b/output/outputType.go deleted file mode 100644 index ad66b4b6..00000000 --- a/output/outputType.go +++ /dev/null @@ -1,120 +0,0 @@ -// 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" - "strings" - - "github.com/spf13/hugo/media" -) - -var ( - // An ordered list of built-in output formats - // See https://www.ampproject.org/learn/overview/ - AMPType = Type{ - Name: "AMP", - MediaType: media.HTMLType, - BaseName: "index", - Path: "amp", - } - - CSSType = Type{ - Name: "CSS", - MediaType: media.CSSType, - BaseName: "styles", - } - - HTMLType = Type{ - Name: "HTML", - MediaType: media.HTMLType, - BaseName: "index", - } - - JSONType = Type{ - Name: "JSON", - MediaType: media.JSONType, - BaseName: "index", - IsPlainText: true, - } - - RSSType = Type{ - Name: "RSS", - MediaType: media.RSSType, - BaseName: "index", - NoUgly: true, - } -) - -var builtInTypes = map[string]Type{ - strings.ToLower(AMPType.Name): AMPType, - strings.ToLower(CSSType.Name): CSSType, - strings.ToLower(HTMLType.Name): HTMLType, - strings.ToLower(JSONType.Name): JSONType, - strings.ToLower(RSSType.Name): RSSType, -} - -type Types []Type - -// Type represents an output represenation, usually to a file on disk. -type Type struct { - // The Name is used as an identifier. Internal output types (i.e. HTML and RSS) - // can be overridden by providing a new definition for those types. - Name string - - MediaType media.Type - - // 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 -} - -func GetType(key string) (Type, bool) { - found, ok := builtInTypes[key] - if !ok { - found, ok = builtInTypes[strings.ToLower(key)] - } - return found, ok -} - -// TODO(bep) outputs rewamp on global config? -func GetTypes(keys ...string) (Types, error) { - var types []Type - - for _, key := range keys { - tpe, ok := GetType(key) - if !ok { - return types, fmt.Errorf("OutputType with key %q not found", key) - } - types = append(types, tpe) - } - - return types, nil -} - -func (t Type) BaseFilename() string { - return t.BaseName + "." + t.MediaType.Suffix -} diff --git a/output/outputType_test.go b/output/outputType_test.go deleted file mode 100644 index 3eb56d8d..00000000 --- a/output/outputType_test.go +++ /dev/null @@ -1,43 +0,0 @@ -// 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/spf13/hugo/media" - "github.com/stretchr/testify/require" -) - -func TestDefaultTypes(t *testing.T) { - require.Equal(t, "HTML", HTMLType.Name) - require.Equal(t, media.HTMLType, HTMLType.MediaType) - require.Empty(t, HTMLType.Path) - require.False(t, HTMLType.IsPlainText) - - require.Equal(t, "RSS", RSSType.Name) - require.Equal(t, media.RSSType, RSSType.MediaType) - require.Empty(t, RSSType.Path) - require.False(t, RSSType.IsPlainText) - require.True(t, RSSType.NoUgly) -} - -func TestGetType(t *testing.T) { - tp, _ := GetType("html") - require.Equal(t, HTMLType, tp) - tp, _ = GetType("HTML") - require.Equal(t, HTMLType, tp) - _, found := GetType("FOO") - require.False(t, found) -}