hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

pagemeta.go (2824B)

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