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

107
resources/kinds/kinds.go Normal file
View File

@@ -0,0 +1,107 @@
// Copyright 2023 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 kinds
import (
"sort"
"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"
// The following are (currently) temporary nodes,
// i.e. nodes we create just to render in isolation.
KindRSS = "rss"
KindSitemap = "sitemap"
KindRobotsTXT = "robotstxt"
Kind404 = "404"
)
var (
// This is all the kinds we can expect to find in .Site.Pages.
AllKindsInPages []string
// This is all the kinds, including the temporary ones.
AllKinds []string
)
func init() {
for k := range kindMapMain {
AllKindsInPages = append(AllKindsInPages, k)
AllKinds = append(AllKinds, k)
}
for k := range kindMapTemporary {
AllKinds = append(AllKinds, k)
}
// Sort the slices for determinism.
sort.Strings(AllKindsInPages)
sort.Strings(AllKinds)
}
var kindMapMain = map[string]string{
KindPage: KindPage,
KindHome: KindHome,
KindSection: KindSection,
KindTaxonomy: KindTaxonomy,
KindTerm: KindTerm,
// Legacy, pre v0.53.0.
"taxonomyterm": KindTaxonomy,
}
var kindMapTemporary = map[string]string{
KindRSS: KindRSS,
KindSitemap: KindSitemap,
KindRobotsTXT: KindRobotsTXT,
Kind404: Kind404,
}
// GetKindMain gets the page kind given a string, empty if not found.
// Note that this will not return any temporary kinds (e.g. robotstxt).
func GetKindMain(s string) string {
return kindMapMain[strings.ToLower(s)]
}
// GetKindAny gets the page kind given a string, empty if not found.
func GetKindAny(s string) string {
if pkind := GetKindMain(s); pkind != "" {
return pkind
}
return kindMapTemporary[strings.ToLower(s)]
}
// IsDeprecated returns whether the given kind is deprecated.
func IsDeprecated(s string) bool {
s = strings.ToLower(s)
switch s {
case "taxonomyterm":
return true
default:
return false
}
}

View File

@@ -1,4 +1,4 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
// Copyright 2023 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.
@@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package page
package kinds
import (
"testing"
@@ -29,9 +29,12 @@ func TestKind(t *testing.T) {
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)
c.Assert(GetKindMain("TAXONOMYTERM"), qt.Equals, KindTaxonomy)
c.Assert(GetKindMain("Taxonomy"), qt.Equals, KindTaxonomy)
c.Assert(GetKindMain("Page"), qt.Equals, KindPage)
c.Assert(GetKindMain("Home"), qt.Equals, KindHome)
c.Assert(GetKindMain("SEction"), qt.Equals, KindSection)
c.Assert(GetKindAny("Page"), qt.Equals, KindPage)
c.Assert(GetKindAny("Robotstxt"), qt.Equals, KindRobotsTXT)
}

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

@@ -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]