| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package kinds |
| 15 |
|
| 16 |
import ( |
| 17 |
"sort" |
| 18 |
"strings" |
| 19 |
) |
| 20 |
|
| 21 |
const ( |
| 22 |
KindPage = "page" |
| 23 |
|
| 24 |
// The rest are node types; home page, sections etc. |
| 25 |
|
| 26 |
KindHome = "home" |
| 27 |
KindSection = "section" |
| 28 |
|
| 29 |
// Note that before Hugo 0.73 these were confusingly named |
| 30 |
// taxonomy (now: term) |
| 31 |
// taxonomyTerm (now: taxonomy) |
| 32 |
KindTaxonomy = "taxonomy" |
| 33 |
KindTerm = "term" |
| 34 |
|
| 35 |
// The following are (currently) temporary nodes, |
| 36 |
// i.e. nodes we create just to render in isolation. |
| 37 |
KindTemporary = "temporary" |
| 38 |
KindRSS = "rss" |
| 39 |
KindSitemap = "sitemap" |
| 40 |
KindSitemapIndex = "sitemapindex" |
| 41 |
KindRobotsTXT = "robotstxt" |
| 42 |
KindStatus404 = "404" |
| 43 |
) |
| 44 |
|
| 45 |
var ( |
| 46 |
// This is all the kinds we can expect to find in .Site.Pages. |
| 47 |
AllKindsInPages []string |
| 48 |
// This is all the kinds, including the temporary ones. |
| 49 |
AllKinds []string |
| 50 |
) |
| 51 |
|
| 52 |
func init() { |
| 53 |
for k := range kindMapMain { |
| 54 |
AllKindsInPages = append(AllKindsInPages, k) |
| 55 |
AllKinds = append(AllKinds, k) |
| 56 |
} |
| 57 |
|
| 58 |
for k := range kindMapTemporary { |
| 59 |
AllKinds = append(AllKinds, k) |
| 60 |
} |
| 61 |
|
| 62 |
// Sort the slices for determinism. |
| 63 |
sort.Strings(AllKindsInPages) |
| 64 |
sort.Strings(AllKinds) |
| 65 |
} |
| 66 |
|
| 67 |
var kindMapMain = map[string]string{ |
| 68 |
KindPage: KindPage, |
| 69 |
KindHome: KindHome, |
| 70 |
KindSection: KindSection, |
| 71 |
KindTaxonomy: KindTaxonomy, |
| 72 |
KindTerm: KindTerm, |
| 73 |
|
| 74 |
// Legacy, pre v0.53.0. |
| 75 |
"taxonomyterm": KindTaxonomy, |
| 76 |
} |
| 77 |
|
| 78 |
var kindMapTemporary = map[string]string{ |
| 79 |
KindRSS: KindRSS, |
| 80 |
KindSitemap: KindSitemap, |
| 81 |
KindRobotsTXT: KindRobotsTXT, |
| 82 |
KindStatus404: KindStatus404, |
| 83 |
} |
| 84 |
|
| 85 |
// GetKindMain gets the page kind given a string, empty if not found. |
| 86 |
// Note that this will not return any temporary kinds (e.g. robotstxt). |
| 87 |
func GetKindMain(s string) string { |
| 88 |
return kindMapMain[strings.ToLower(s)] |
| 89 |
} |
| 90 |
|
| 91 |
// GetKindAny gets the page kind given a string, empty if not found. |
| 92 |
func GetKindAny(s string) string { |
| 93 |
if pkind := GetKindMain(s); pkind != "" { |
| 94 |
return pkind |
| 95 |
} |
| 96 |
return kindMapTemporary[strings.ToLower(s)] |
| 97 |
} |
| 98 |
|
| 99 |
// IsBranch returns whether the given kind is a branch node. |
| 100 |
func IsBranch(kind string) bool { |
| 101 |
switch kind { |
| 102 |
case KindHome, KindSection, KindTaxonomy, KindTerm: |
| 103 |
return true |
| 104 |
default: |
| 105 |
return false |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// IsDeprecatedAndReplacedWith returns the new kind if the given kind is deprecated. |
| 110 |
func IsDeprecatedAndReplacedWith(s string) string { |
| 111 |
s = strings.ToLower(s) |
| 112 |
|
| 113 |
switch s { |
| 114 |
case "taxonomyterm": |
| 115 |
return KindTaxonomy |
| 116 |
default: |
| 117 |
return "" |
| 118 |
} |
| 119 |
} |