github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/resources/kinds/kinds.go (about)

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