github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/hugolib/site_output.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  	"fmt"
    18  	"strings"
    19  
    20  	"github.com/gohugoio/hugo/output"
    21  	"github.com/gohugoio/hugo/resources/page"
    22  	"github.com/spf13/cast"
    23  )
    24  
    25  func createDefaultOutputFormats(allFormats output.Formats) map[string]output.Formats {
    26  	rssOut, rssFound := allFormats.GetByName(output.RSSFormat.Name)
    27  	htmlOut, _ := allFormats.GetByName(output.HTMLFormat.Name)
    28  	robotsOut, _ := allFormats.GetByName(output.RobotsTxtFormat.Name)
    29  	sitemapOut, _ := allFormats.GetByName(output.SitemapFormat.Name)
    30  
    31  	defaultListTypes := output.Formats{htmlOut}
    32  	if rssFound {
    33  		defaultListTypes = append(defaultListTypes, rssOut)
    34  	}
    35  
    36  	m := map[string]output.Formats{
    37  		page.KindPage:     {htmlOut},
    38  		page.KindHome:     defaultListTypes,
    39  		page.KindSection:  defaultListTypes,
    40  		page.KindTerm:     defaultListTypes,
    41  		page.KindTaxonomy: defaultListTypes,
    42  		// Below are for consistency. They are currently not used during rendering.
    43  		kindSitemap:   {sitemapOut},
    44  		kindRobotsTXT: {robotsOut},
    45  		kind404:       {htmlOut},
    46  	}
    47  
    48  	// May be disabled
    49  	if rssFound {
    50  		m[kindRSS] = output.Formats{rssOut}
    51  	}
    52  
    53  	return m
    54  }
    55  
    56  func createSiteOutputFormats(allFormats output.Formats, outputs map[string]any, rssDisabled bool) (map[string]output.Formats, error) {
    57  	defaultOutputFormats := createDefaultOutputFormats(allFormats)
    58  
    59  	if outputs == nil {
    60  		return defaultOutputFormats, nil
    61  	}
    62  
    63  	outFormats := make(map[string]output.Formats)
    64  
    65  	if len(outputs) == 0 {
    66  		return outFormats, nil
    67  	}
    68  
    69  	seen := make(map[string]bool)
    70  
    71  	for k, v := range outputs {
    72  		k = getKind(k)
    73  		if k == "" {
    74  			// Invalid kind
    75  			continue
    76  		}
    77  		var formats output.Formats
    78  		vals := cast.ToStringSlice(v)
    79  		for _, format := range vals {
    80  			f, found := allFormats.GetByName(format)
    81  			if !found {
    82  				if rssDisabled && strings.EqualFold(format, "RSS") {
    83  					// This is legacy behaviour. We used to have both
    84  					// a RSS page kind and output format.
    85  					continue
    86  				}
    87  				return nil, fmt.Errorf("failed to resolve output format %q from site config", format)
    88  			}
    89  			formats = append(formats, f)
    90  		}
    91  
    92  		// This effectively prevents empty outputs entries for a given Kind.
    93  		// We need at least one.
    94  		if len(formats) > 0 {
    95  			seen[k] = true
    96  			outFormats[k] = formats
    97  		}
    98  	}
    99  
   100  	// Add defaults for the entries not provided by the user.
   101  	for k, v := range defaultOutputFormats {
   102  		if !seen[k] {
   103  			outFormats[k] = v
   104  		}
   105  	}
   106  
   107  	return outFormats, nil
   108  }