github.com/neohugo/neohugo@v0.123.8/hugolib/page__data.go (about) 1 // Copyright 2019 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 hugolib 15 16 import ( 17 "strings" 18 "sync" 19 20 "github.com/neohugo/neohugo/resources/kinds" 21 "github.com/neohugo/neohugo/resources/page" 22 ) 23 24 type pageData struct { 25 *pageState 26 27 dataInit sync.Once 28 data page.Data 29 } 30 31 func (p *pageData) Data() any { 32 p.dataInit.Do(func() { 33 p.data = make(page.Data) 34 35 if p.Kind() == kinds.KindPage { 36 return 37 } 38 39 switch p.Kind() { 40 case kinds.KindTerm: 41 path := p.Path() 42 name := p.s.pageMap.cfg.getTaxonomyConfig(path) 43 term := p.s.Taxonomies()[name.plural].Get(strings.TrimPrefix(path, name.pluralTreeKey)) 44 p.data[name.singular] = term 45 p.data["Singular"] = name.singular 46 p.data["Plural"] = name.plural 47 p.data["Term"] = p.m.term 48 case kinds.KindTaxonomy: 49 viewCfg := p.s.pageMap.cfg.getTaxonomyConfig(p.Path()) 50 p.data["Singular"] = viewCfg.singular 51 p.data["Plural"] = viewCfg.plural 52 p.data["Terms"] = p.s.Taxonomies()[viewCfg.plural] 53 // keep the following just for legacy reasons 54 p.data["OrderedIndex"] = p.data["Terms"] 55 p.data["Index"] = p.data["Terms"] 56 } 57 58 // Assign the function to the map to make sure it is lazily initialized 59 p.data["pages"] = p.Pages 60 }) 61 62 return p.data 63 }