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
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
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

@ -0,0 +1,40 @@
// 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 (
"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(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)
}