github.com/neohugo/neohugo@v0.123.8/resources/kinds/kinds.go (about)

     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  	KindRSS          = "rss"
    38  	KindSitemap      = "sitemap"
    39  	KindSitemapIndex = "sitemapindex"
    40  	KindRobotsTXT    = "robotstxt"
    41  	KindStatus404    = "404"
    42  )
    43  
    44  var (
    45  	// This is all the kinds we can expect to find in .Site.Pages.
    46  	AllKindsInPages []string
    47  	// This is all the kinds, including the temporary ones.
    48  	AllKinds []string
    49  )
    50  
    51  func init() {
    52  	for k := range kindMapMain {
    53  		AllKindsInPages = append(AllKindsInPages, k)
    54  		AllKinds = append(AllKinds, k)
    55  	}
    56  
    57  	for k := range kindMapTemporary {
    58  		AllKinds = append(AllKinds, k)
    59  	}
    60  
    61  	// Sort the slices for determinism.
    62  	sort.Strings(AllKindsInPages)
    63  	sort.Strings(AllKinds)
    64  }
    65  
    66  var kindMapMain = map[string]string{
    67  	KindPage:     KindPage,
    68  	KindHome:     KindHome,
    69  	KindSection:  KindSection,
    70  	KindTaxonomy: KindTaxonomy,
    71  	KindTerm:     KindTerm,
    72  
    73  	// Legacy, pre v0.53.0.
    74  	"taxonomyterm": KindTaxonomy,
    75  }
    76  
    77  var kindMapTemporary = map[string]string{
    78  	KindRSS:       KindRSS,
    79  	KindSitemap:   KindSitemap,
    80  	KindRobotsTXT: KindRobotsTXT,
    81  	KindStatus404: KindStatus404,
    82  }
    83  
    84  // GetKindMain gets the page kind given a string, empty if not found.
    85  // Note that this will not return any temporary kinds (e.g. robotstxt).
    86  func GetKindMain(s string) string {
    87  	return kindMapMain[strings.ToLower(s)]
    88  }
    89  
    90  // GetKindAny gets the page kind given a string, empty if not found.
    91  func GetKindAny(s string) string {
    92  	if pkind := GetKindMain(s); pkind != "" {
    93  		return pkind
    94  	}
    95  	return kindMapTemporary[strings.ToLower(s)]
    96  }
    97  
    98  // IsBranch returns whether the given kind is a branch node.
    99  func IsBranch(kind string) bool {
   100  	switch kind {
   101  	case KindHome, KindSection, KindTaxonomy, KindTerm:
   102  		return true
   103  	default:
   104  		return false
   105  	}
   106  }
   107  
   108  // IsDeprecatedAndReplacedWith returns the new kind if the given kind is deprecated.
   109  func IsDeprecatedAndReplacedWith(s string) string {
   110  	s = strings.ToLower(s)
   111  
   112  	switch s {
   113  	case "taxonomyterm":
   114  		return KindTaxonomy
   115  	default:
   116  		return ""
   117  	}
   118  }