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