github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/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  	"sync"
    18  
    19  	"github.com/gohugoio/hugo/resources/page"
    20  )
    21  
    22  type pageData struct {
    23  	*pageState
    24  
    25  	dataInit sync.Once
    26  	data     page.Data
    27  }
    28  
    29  func (p *pageData) Data() any {
    30  	p.dataInit.Do(func() {
    31  		p.data = make(page.Data)
    32  
    33  		if p.Kind() == page.KindPage {
    34  			return
    35  		}
    36  
    37  		switch p.Kind() {
    38  		case page.KindTerm:
    39  			b := p.treeRef.n
    40  			name := b.viewInfo.name
    41  			termKey := b.viewInfo.termKey
    42  
    43  			taxonomy := p.s.Taxonomies()[name.plural].Get(termKey)
    44  
    45  			p.data[name.singular] = taxonomy
    46  			p.data["Singular"] = name.singular
    47  			p.data["Plural"] = name.plural
    48  			p.data["Term"] = b.viewInfo.term()
    49  		case page.KindTaxonomy:
    50  			b := p.treeRef.n
    51  			name := b.viewInfo.name
    52  
    53  			p.data["Singular"] = name.singular
    54  			p.data["Plural"] = name.plural
    55  			p.data["Terms"] = p.s.Taxonomies()[name.plural]
    56  			// keep the following just for legacy reasons
    57  			p.data["OrderedIndex"] = p.data["Terms"]
    58  			p.data["Index"] = p.data["Terms"]
    59  		}
    60  
    61  		// Assign the function to the map to make sure it is lazily initialized
    62  		p.data["pages"] = p.Pages
    63  	})
    64  
    65  	return p.data
    66  }