diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-07-28 10:53:47 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-07-28 15:14:23 +0200 |
commit | b3cb6788b2634a89ae774895f345f082020b52d8 (patch) | |
tree | 08eadc21d9308510e332a4898d2c06516cb6e497 /resources/kinds | |
parent | 5542f02fbc4c9467a4338ee1ce2e741f480a0751 (diff) |
Move all Kind constants to its own package
See #11256
Diffstat (limited to 'resources/kinds')
-rw-r--r-- | resources/kinds/kinds.go | 107 | ||||
-rw-r--r-- | resources/kinds/kinds_test.go | 40 |
2 files changed, 147 insertions, 0 deletions
diff --git a/resources/kinds/kinds.go b/resources/kinds/kinds.go new file mode 100644 index 000000000..b1d1c18f4 --- /dev/null +++ b/resources/kinds/kinds.go @@ -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 + } +} diff --git a/resources/kinds/kinds_test.go b/resources/kinds/kinds_test.go new file mode 100644 index 000000000..c2868d617 --- /dev/null +++ b/resources/kinds/kinds_test.go @@ -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) +} |