Move all Kind constants to its own package

See #11256
This commit is contained in:
Bjørn Erik Pedersen
2023-07-28 10:53:47 +02:00
parent 5542f02fbc
commit b3cb6788b2
35 changed files with 386 additions and 323 deletions

View File

@@ -12,36 +12,3 @@
// limitations under the License.
package page
import "strings"
const (
KindPage = "page"
// The rest are node types; home page, sections etc.
KindHome = "home"
KindSection = "section"
// Note that before Hugo 0.73 these were confusingly named
// taxonomy (now: term)
// taxonomyTerm (now: taxonomy)
KindTaxonomy = "taxonomy"
KindTerm = "term"
)
var kindMap = map[string]string{
strings.ToLower(KindPage): KindPage,
strings.ToLower(KindHome): KindHome,
strings.ToLower(KindSection): KindSection,
strings.ToLower(KindTaxonomy): KindTaxonomy,
strings.ToLower(KindTerm): KindTerm,
// Legacy, pre v0.53.0.
"taxonomyterm": KindTaxonomy,
}
// GetKind gets the page kind given a string, empty if not found.
func GetKind(s string) string {
return kindMap[strings.ToLower(s)]
}

View File

@@ -1,37 +0,0 @@
// Copyright 2019 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 page
import (
"testing"
qt "github.com/frankban/quicktest"
)
func TestKind(t *testing.T) {
t.Parallel()
c := qt.New(t)
// Add tests for these constants to make sure they don't change
c.Assert(KindPage, qt.Equals, "page")
c.Assert(KindHome, qt.Equals, "home")
c.Assert(KindSection, qt.Equals, "section")
c.Assert(KindTaxonomy, qt.Equals, "taxonomy")
c.Assert(KindTerm, qt.Equals, "term")
c.Assert(GetKind("TAXONOMYTERM"), qt.Equals, KindTaxonomy)
c.Assert(GetKind("Taxonomy"), qt.Equals, KindTaxonomy)
c.Assert(GetKind("Page"), qt.Equals, KindPage)
c.Assert(GetKind("Home"), qt.Equals, KindHome)
c.Assert(GetKind("SEction"), qt.Equals, KindSection)
}

View File

@@ -21,6 +21,7 @@ import (
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/hugofs/glob"
"github.com/gohugoio/hugo/resources/kinds"
"github.com/mitchellh/mapstructure"
)
@@ -173,7 +174,7 @@ func decodePageMatcher(m any, v *PageMatcher) error {
if v.Kind != "" {
g, _ := glob.GetGlob(v.Kind)
found := false
for _, k := range kindMap {
for _, k := range kinds.AllKindsInPages {
if g.Match(k) {
found = true
break

View File

@@ -21,6 +21,7 @@ import (
"github.com/gohugoio/hugo/common/urls"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/output"
"github.com/gohugoio/hugo/resources/kinds"
)
const slash = "/"
@@ -147,7 +148,7 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) {
isUgly = true
}
if d.Kind != KindPage && d.URL == "" && len(d.Sections) > 0 {
if d.Kind != kinds.KindPage && d.URL == "" && len(d.Sections) > 0 {
if d.ExpandedPermalink != "" {
pagePath = pjoin(pagePath, d.ExpandedPermalink)
} else {
@@ -160,7 +161,7 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) {
pagePath = pjoin(pagePath, d.Type.Path)
}
if d.Kind != KindHome && d.URL != "" {
if d.Kind != kinds.KindHome && d.URL != "" {
pagePath = pjoin(pagePath, d.URL)
if d.Addends != "" {
@@ -200,7 +201,7 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) {
}
}
} else if d.Kind == KindPage {
} else if d.Kind == kinds.KindPage {
if d.ExpandedPermalink != "" {
pagePath = pjoin(pagePath, d.ExpandedPermalink)
@@ -307,7 +308,7 @@ func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) {
// if page URL is explicitly set in frontmatter,
// preserve its value without sanitization
if d.Kind != KindPage || d.URL == "" {
if d.Kind != kinds.KindPage || d.URL == "" {
// Note: MakePathSanitized will lower case the path if
// disablePathToLower isn't set.
pagePath = d.PathSpec.MakePathSanitized(pagePath)

View File

@@ -20,6 +20,7 @@ import (
"testing"
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/resources/kinds"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/output"
@@ -47,31 +48,31 @@ func TestPageTargetPath(t *testing.T) {
d page.TargetPathDescriptor
expected page.TargetPaths
}{
{"JSON home", page.TargetPathDescriptor{Kind: page.KindHome, Type: output.JSONFormat}, page.TargetPaths{TargetFilename: "/index.json", SubResourceBaseTarget: "", Link: "/index.json"}},
{"AMP home", page.TargetPathDescriptor{Kind: page.KindHome, Type: output.AMPFormat}, page.TargetPaths{TargetFilename: "/amp/index.html", SubResourceBaseTarget: "/amp", Link: "/amp/"}},
{"HTML home", page.TargetPathDescriptor{Kind: page.KindHome, BaseName: "_index", Type: output.HTMLFormat}, page.TargetPaths{TargetFilename: "/index.html", SubResourceBaseTarget: "", Link: "/"}},
{"Netlify redirects", page.TargetPathDescriptor{Kind: page.KindHome, BaseName: "_index", Type: noExtDelimFormat}, page.TargetPaths{TargetFilename: "/_redirects", SubResourceBaseTarget: "", Link: "/_redirects"}},
{"JSON home", page.TargetPathDescriptor{Kind: kinds.KindHome, Type: output.JSONFormat}, page.TargetPaths{TargetFilename: "/index.json", SubResourceBaseTarget: "", Link: "/index.json"}},
{"AMP home", page.TargetPathDescriptor{Kind: kinds.KindHome, Type: output.AMPFormat}, page.TargetPaths{TargetFilename: "/amp/index.html", SubResourceBaseTarget: "/amp", Link: "/amp/"}},
{"HTML home", page.TargetPathDescriptor{Kind: kinds.KindHome, BaseName: "_index", Type: output.HTMLFormat}, page.TargetPaths{TargetFilename: "/index.html", SubResourceBaseTarget: "", Link: "/"}},
{"Netlify redirects", page.TargetPathDescriptor{Kind: kinds.KindHome, BaseName: "_index", Type: noExtDelimFormat}, page.TargetPaths{TargetFilename: "/_redirects", SubResourceBaseTarget: "", Link: "/_redirects"}},
{"HTML section list", page.TargetPathDescriptor{
Kind: page.KindSection,
Kind: kinds.KindSection,
Sections: []string{"sect1"},
BaseName: "_index",
Type: output.HTMLFormat,
}, page.TargetPaths{TargetFilename: "/sect1/index.html", SubResourceBaseTarget: "/sect1", Link: "/sect1/"}},
{"HTML taxonomy term", page.TargetPathDescriptor{
Kind: page.KindTerm,
Kind: kinds.KindTerm,
Sections: []string{"tags", "hugo"},
BaseName: "_index",
Type: output.HTMLFormat,
}, page.TargetPaths{TargetFilename: "/tags/hugo/index.html", SubResourceBaseTarget: "/tags/hugo", Link: "/tags/hugo/"}},
{"HTML taxonomy", page.TargetPathDescriptor{
Kind: page.KindTaxonomy,
Kind: kinds.KindTaxonomy,
Sections: []string{"tags"},
BaseName: "_index",
Type: output.HTMLFormat,
}, page.TargetPaths{TargetFilename: "/tags/index.html", SubResourceBaseTarget: "/tags", Link: "/tags/"}},
{
"HTML page", page.TargetPathDescriptor{
Kind: page.KindPage,
Kind: kinds.KindPage,
Dir: "/a/b",
BaseName: "mypage",
Sections: []string{"a"},
@@ -81,7 +82,7 @@ func TestPageTargetPath(t *testing.T) {
{
"HTML page with index as base", page.TargetPathDescriptor{
Kind: page.KindPage,
Kind: kinds.KindPage,
Dir: "/a/b",
BaseName: "index",
Sections: []string{"a"},
@@ -91,7 +92,7 @@ func TestPageTargetPath(t *testing.T) {
{
"HTML page with special chars", page.TargetPathDescriptor{
Kind: page.KindPage,
Kind: kinds.KindPage,
Dir: "/a/b",
BaseName: "My Page!",
Type: output.HTMLFormat,
@@ -105,7 +106,7 @@ func TestPageTargetPath(t *testing.T) {
}, page.TargetPaths{TargetFilename: "/sect1/index.xml", SubResourceBaseTarget: "/sect1", Link: "/sect1/index.xml"}},
{
"AMP page", page.TargetPathDescriptor{
Kind: page.KindPage,
Kind: kinds.KindPage,
Dir: "/a/b/c",
BaseName: "myamp",
Type: output.AMPFormat,
@@ -113,7 +114,7 @@ func TestPageTargetPath(t *testing.T) {
},
{
"AMP page with URL with suffix", page.TargetPathDescriptor{
Kind: page.KindPage,
Kind: kinds.KindPage,
Dir: "/sect/",
BaseName: "mypage",
URL: "/some/other/url.xhtml",
@@ -122,7 +123,7 @@ func TestPageTargetPath(t *testing.T) {
},
{
"JSON page with URL without suffix", page.TargetPathDescriptor{
Kind: page.KindPage,
Kind: kinds.KindPage,
Dir: "/sect/",
BaseName: "mypage",
URL: "/some/other/path/",
@@ -131,7 +132,7 @@ func TestPageTargetPath(t *testing.T) {
},
{
"JSON page with URL without suffix and no trailing slash", page.TargetPathDescriptor{
Kind: page.KindPage,
Kind: kinds.KindPage,
Dir: "/sect/",
BaseName: "mypage",
URL: "/some/other/path",
@@ -140,7 +141,7 @@ func TestPageTargetPath(t *testing.T) {
},
{
"HTML page with URL without suffix and no trailing slash", page.TargetPathDescriptor{
Kind: page.KindPage,
Kind: kinds.KindPage,
Dir: "/sect/",
BaseName: "mypage",
URL: "/some/other/path",
@@ -149,7 +150,7 @@ func TestPageTargetPath(t *testing.T) {
},
{
"HTML page with URL containing double hyphen", page.TargetPathDescriptor{
Kind: page.KindPage,
Kind: kinds.KindPage,
Dir: "/sect/",
BaseName: "mypage",
URL: "/some/other--url/",
@@ -158,7 +159,7 @@ func TestPageTargetPath(t *testing.T) {
},
{
"HTML page with expanded permalink", page.TargetPathDescriptor{
Kind: page.KindPage,
Kind: kinds.KindPage,
Dir: "/a/b",
BaseName: "mypage",
ExpandedPermalink: "/2017/10/my-title/",
@@ -167,7 +168,7 @@ func TestPageTargetPath(t *testing.T) {
},
{
"Paginated HTML home", page.TargetPathDescriptor{
Kind: page.KindHome,
Kind: kinds.KindHome,
BaseName: "_index",
Type: output.HTMLFormat,
Addends: "page/3",
@@ -175,7 +176,7 @@ func TestPageTargetPath(t *testing.T) {
},
{
"Paginated Taxonomy terms list", page.TargetPathDescriptor{
Kind: page.KindTerm,
Kind: kinds.KindTerm,
BaseName: "_index",
Sections: []string{"tags", "hugo"},
Type: output.HTMLFormat,
@@ -184,7 +185,7 @@ func TestPageTargetPath(t *testing.T) {
},
{
"Regular page with addend", page.TargetPathDescriptor{
Kind: page.KindPage,
Kind: kinds.KindPage,
Dir: "/a/b",
BaseName: "mypage",
Addends: "c/d/e",
@@ -207,8 +208,8 @@ func TestPageTargetPath(t *testing.T) {
expected := test.expected
// TODO(bep) simplify
if test.d.Kind == page.KindPage && test.d.BaseName == test.d.Type.BaseName {
} else if test.d.Kind == page.KindHome && test.d.Type.Path != "" {
if test.d.Kind == kinds.KindPage && test.d.BaseName == test.d.Type.BaseName {
} else if test.d.Kind == kinds.KindHome && test.d.Type.Path != "" {
} else if test.d.Type.MediaType.FirstSuffix.Suffix != "" && (!strings.HasPrefix(expected.TargetFilename, "/index") || test.d.Addends != "") && test.d.URL == "" && isUgly {
expected.TargetFilename = strings.Replace(expected.TargetFilename,
"/"+test.d.Type.BaseName+"."+test.d.Type.MediaType.FirstSuffix.Suffix,
@@ -250,12 +251,12 @@ func TestPageTargetPathPrefix(t *testing.T) {
}{
{
"URL set, prefix both, no force",
page.TargetPathDescriptor{Kind: page.KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: false, PrefixFilePath: "pf", PrefixLink: "pl"},
page.TargetPathDescriptor{Kind: kinds.KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: false, PrefixFilePath: "pf", PrefixLink: "pl"},
page.TargetPaths{TargetFilename: "/mydir/my.json", SubResourceBaseTarget: "/mydir", SubResourceBaseLink: "/mydir", Link: "/mydir/my.json"},
},
{
"URL set, prefix both, force",
page.TargetPathDescriptor{Kind: page.KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: true, PrefixFilePath: "pf", PrefixLink: "pl"},
page.TargetPathDescriptor{Kind: kinds.KindPage, Type: output.JSONFormat, URL: "/mydir/my.json", ForcePrefix: true, PrefixFilePath: "pf", PrefixLink: "pl"},
page.TargetPaths{TargetFilename: "/pf/mydir/my.json", SubResourceBaseTarget: "/pf/mydir", SubResourceBaseLink: "/pl/mydir", Link: "/pl/mydir/my.json"},
},
}

View File

@@ -27,6 +27,7 @@ import (
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/resources/kinds"
)
// PermalinkExpander holds permalin mappings per section.
@@ -398,16 +399,16 @@ func (l PermalinkExpander) toSliceFunc(cut string) func(s []string) []string {
}
var permalinksKindsSuppurt = []string{KindPage, KindSection, KindTaxonomy, KindTerm}
var permalinksKindsSuppurt = []string{kinds.KindPage, kinds.KindSection, kinds.KindTaxonomy, kinds.KindTerm}
// DecodePermalinksConfig decodes the permalinks configuration in the given map
func DecodePermalinksConfig(m map[string]any) (map[string]map[string]string, error) {
permalinksConfig := make(map[string]map[string]string)
permalinksConfig[KindPage] = make(map[string]string)
permalinksConfig[KindSection] = make(map[string]string)
permalinksConfig[KindTaxonomy] = make(map[string]string)
permalinksConfig[KindTerm] = make(map[string]string)
permalinksConfig[kinds.KindPage] = make(map[string]string)
permalinksConfig[kinds.KindSection] = make(map[string]string)
permalinksConfig[kinds.KindTaxonomy] = make(map[string]string)
permalinksConfig[kinds.KindTerm] = make(map[string]string)
config := maps.CleanConfigStringMap(m)
for k, v := range config {
@@ -417,8 +418,8 @@ func DecodePermalinksConfig(m map[string]any) (map[string]map[string]string, err
// key = '...'
// To sucessfully be backward compatible, "default" patterns need to be set for both page and term
permalinksConfig[KindPage][k] = v
permalinksConfig[KindTerm][k] = v
permalinksConfig[kinds.KindPage][k] = v
permalinksConfig[kinds.KindTerm][k] = v
case maps.Params:
// [permalinks.key]