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