Debian Codegraph @

resources/page/pagemeta/pagemeta.go

raw
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 pagemeta
15
16 import (
17 "github.com/mitchellh/mapstructure"
18 )
19
20 const (
21 Never = "never"
22 Always = "always"
23 ListLocally = "local"
24 Link = "link"
25 )
26
27 var DefaultBuildConfig = BuildConfig{
28 List: Always,
29 Render: Always,
30 PublishResources: true,
31 }
32
33 // BuildConfig holds configuration options about how to handle a Page in Hugo's
34 // build process.
35 type BuildConfig struct {
36 // Whether to add it to any of the page collections.
37 // Note that the page can always be found with .Site.GetPage.
38 // Valid values: never, always, local.
39 // Setting it to 'local' means they will be available via the local
40 // page collections, e.g. $section.Pages.
41 // Note: before 0.57.2 this was a bool, so we accept those too.
42 List string
43
44 // Whether to render it.
45 // Valid values: never, always, link.
46 // The value link means it will not be rendered, but it will get a RelPermalink/Permalink.
47 // Note that before 0.76.0 this was a bool, so we accept those too.
48 Render string
49
50 // Whether to publish its resources. These will still be published on demand,
51 // but enabling this can be useful if the originals (e.g. images) are
52 // never used.
53 PublishResources bool
54 }
55
56 // Disable sets all options to their off value.
57 func (b *BuildConfig) Disable() {
58 b.List = Never
59 b.Render = Never
60 b.PublishResources = false
61 }
62
63 func (b BuildConfig) IsZero() bool {
64 return b == BuildConfig{}
65 }
66
67 func DecodeBuildConfig(m any) (BuildConfig, error) {
68 b := DefaultBuildConfig
69 if m == nil {
70 return b, nil
71 }
72
73 err := mapstructure.WeakDecode(m, &b)
74
75 // In 0.67.1 we changed the list attribute from a bool to a string (enum).
76 // Bool values will become 0 or 1.
77 switch b.List {
78 case "0":
79 b.List = Never
80 case "1":
81 b.List = Always
82 case Always, Never, ListLocally:
83 default:
84 b.List = Always
85 }
86
87 // In 0.76.0 we changed the Render from bool to a string.
88 switch b.Render {
89 case "0":
90 b.Render = Never
91 case "1":
92 b.Render = Always
93 case Always, Never, Link:
94 default:
95 b.Render = Always
96 }
97
98 return b, err
99 }