github.com/lyeb/hugo@v0.47.1/hugolib/site_output.go (about)

     1  // Copyright 2017-present 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  	"path"
    19  	"strings"
    20  
    21  	"github.com/gohugoio/hugo/config"
    22  	"github.com/gohugoio/hugo/helpers"
    23  	"github.com/gohugoio/hugo/output"
    24  	"github.com/spf13/cast"
    25  )
    26  
    27  func createDefaultOutputFormats(allFormats output.Formats, cfg config.Provider) map[string]output.Formats {
    28  	rssOut, _ := allFormats.GetByName(output.RSSFormat.Name)
    29  	htmlOut, _ := allFormats.GetByName(output.HTMLFormat.Name)
    30  	robotsOut, _ := allFormats.GetByName(output.RobotsTxtFormat.Name)
    31  	sitemapOut, _ := allFormats.GetByName(output.SitemapFormat.Name)
    32  
    33  	// TODO(bep) this mumbo jumbo is deprecated and should be removed, but there are tests that
    34  	// depends on this, so that will have to wait.
    35  	rssBase := cfg.GetString("rssURI")
    36  	if rssBase == "" || rssBase == "index.xml" {
    37  		rssBase = rssOut.BaseName
    38  	} else {
    39  		// Remove in Hugo 0.36.
    40  		helpers.Deprecated("Site config", "rssURI", "Set baseName in outputFormats.RSS", true)
    41  		// RSS has now a well defined media type, so strip any suffix provided
    42  		rssBase = strings.TrimSuffix(rssBase, path.Ext(rssBase))
    43  	}
    44  
    45  	rssOut.BaseName = rssBase
    46  
    47  	return map[string]output.Formats{
    48  		KindPage:         output.Formats{htmlOut},
    49  		KindHome:         output.Formats{htmlOut, rssOut},
    50  		KindSection:      output.Formats{htmlOut, rssOut},
    51  		KindTaxonomy:     output.Formats{htmlOut, rssOut},
    52  		KindTaxonomyTerm: output.Formats{htmlOut, rssOut},
    53  		// Below are for conistency. They are currently not used during rendering.
    54  		kindRSS:       output.Formats{rssOut},
    55  		kindSitemap:   output.Formats{sitemapOut},
    56  		kindRobotsTXT: output.Formats{robotsOut},
    57  		kind404:       output.Formats{htmlOut},
    58  	}
    59  
    60  }
    61  
    62  func createSiteOutputFormats(allFormats output.Formats, cfg config.Provider) (map[string]output.Formats, error) {
    63  	defaultOutputFormats := createDefaultOutputFormats(allFormats, cfg)
    64  
    65  	if !cfg.IsSet("outputs") {
    66  		return defaultOutputFormats, nil
    67  	}
    68  
    69  	outFormats := make(map[string]output.Formats)
    70  
    71  	outputs := cfg.GetStringMap("outputs")
    72  
    73  	if outputs == nil || len(outputs) == 0 {
    74  		return outFormats, nil
    75  	}
    76  
    77  	seen := make(map[string]bool)
    78  
    79  	for k, v := range outputs {
    80  		var formats output.Formats
    81  		vals := cast.ToStringSlice(v)
    82  		for _, format := range vals {
    83  			f, found := allFormats.GetByName(format)
    84  			if !found {
    85  				return nil, fmt.Errorf("Failed to resolve output format %q from site config", format)
    86  			}
    87  			formats = append(formats, f)
    88  		}
    89  
    90  		// This effectively prevents empty outputs entries for a given Kind.
    91  		// We need at least one.
    92  		if len(formats) > 0 {
    93  			seen[k] = true
    94  			outFormats[k] = formats
    95  		}
    96  	}
    97  
    98  	// Add defaults for the entries not provided by the user.
    99  	for k, v := range defaultOutputFormats {
   100  		if !seen[k] {
   101  			outFormats[k] = v
   102  		}
   103  	}
   104  
   105  	return outFormats, nil
   106  
   107  }