2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2017-03-07 08:20:39 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2017-03-22 06:34:17 -04:00
|
|
|
"fmt"
|
2020-02-19 02:38:46 -05:00
|
|
|
"strings"
|
2017-03-07 08:20:39 -05:00
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/output"
|
2023-07-28 04:53:47 -04:00
|
|
|
"github.com/gohugoio/hugo/resources/kinds"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/cast"
|
2017-03-07 08:20:39 -05:00
|
|
|
)
|
|
|
|
|
2020-06-16 09:43:50 -04:00
|
|
|
func createDefaultOutputFormats(allFormats output.Formats) map[string]output.Formats {
|
2019-09-10 05:26:34 -04:00
|
|
|
rssOut, rssFound := allFormats.GetByName(output.RSSFormat.Name)
|
2018-03-10 05:45:29 -05:00
|
|
|
htmlOut, _ := allFormats.GetByName(output.HTMLFormat.Name)
|
|
|
|
robotsOut, _ := allFormats.GetByName(output.RobotsTxtFormat.Name)
|
|
|
|
sitemapOut, _ := allFormats.GetByName(output.SitemapFormat.Name)
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
defaultListTypes := output.Formats{htmlOut}
|
|
|
|
if rssFound {
|
|
|
|
defaultListTypes = append(defaultListTypes, rssOut)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := map[string]output.Formats{
|
2023-07-28 04:53:47 -04:00
|
|
|
kinds.KindPage: {htmlOut},
|
|
|
|
kinds.KindHome: defaultListTypes,
|
|
|
|
kinds.KindSection: defaultListTypes,
|
|
|
|
kinds.KindTerm: defaultListTypes,
|
|
|
|
kinds.KindTaxonomy: defaultListTypes,
|
2019-08-05 04:19:55 -04:00
|
|
|
// Below are for consistency. They are currently not used during rendering.
|
2023-07-28 04:53:47 -04:00
|
|
|
kinds.KindSitemap: {sitemapOut},
|
|
|
|
kinds.KindRobotsTXT: {robotsOut},
|
|
|
|
kinds.Kind404: {htmlOut},
|
2018-03-10 05:45:29 -05:00
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
// May be disabled
|
|
|
|
if rssFound {
|
2023-07-28 04:53:47 -04:00
|
|
|
m[kinds.KindRSS] = output.Formats{rssOut}
|
2019-09-10 05:26:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
2018-03-10 05:45:29 -05:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func createSiteOutputFormats(allFormats output.Formats, outputs map[string]any, rssDisabled bool) (map[string]output.Formats, error) {
|
2020-06-16 09:43:50 -04:00
|
|
|
defaultOutputFormats := createDefaultOutputFormats(allFormats)
|
2018-03-10 05:45:29 -05:00
|
|
|
|
2020-06-16 09:43:50 -04:00
|
|
|
if outputs == nil {
|
2018-03-10 05:45:29 -05:00
|
|
|
return defaultOutputFormats, nil
|
2017-03-22 06:34:17 -04:00
|
|
|
}
|
2017-03-07 08:20:39 -05:00
|
|
|
|
2017-03-22 06:34:17 -04:00
|
|
|
outFormats := make(map[string]output.Formats)
|
2017-03-07 08:20:39 -05:00
|
|
|
|
2018-08-28 16:51:44 -04:00
|
|
|
if len(outputs) == 0 {
|
2017-03-22 06:34:17 -04:00
|
|
|
return outFormats, nil
|
|
|
|
}
|
2017-03-07 08:20:39 -05:00
|
|
|
|
2018-03-10 05:45:29 -05:00
|
|
|
seen := make(map[string]bool)
|
|
|
|
|
2017-03-22 06:34:17 -04:00
|
|
|
for k, v := range outputs {
|
2023-07-28 04:53:47 -04:00
|
|
|
k = kinds.GetKindAny(k)
|
2019-08-05 04:19:55 -04:00
|
|
|
if k == "" {
|
|
|
|
// Invalid kind
|
|
|
|
continue
|
|
|
|
}
|
2017-03-22 06:34:17 -04:00
|
|
|
var formats output.Formats
|
|
|
|
vals := cast.ToStringSlice(v)
|
|
|
|
for _, format := range vals {
|
2017-04-04 03:12:33 -04:00
|
|
|
f, found := allFormats.GetByName(format)
|
2017-03-22 06:34:17 -04:00
|
|
|
if !found {
|
2020-02-19 02:38:46 -05:00
|
|
|
if rssDisabled && strings.EqualFold(format, "RSS") {
|
|
|
|
// This is legacy behaviour. We used to have both
|
|
|
|
// a RSS page kind and output format.
|
|
|
|
continue
|
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
return nil, fmt.Errorf("failed to resolve output format %q from site config", format)
|
2017-03-22 06:34:17 -04:00
|
|
|
}
|
|
|
|
formats = append(formats, f)
|
2017-03-07 08:20:39 -05:00
|
|
|
}
|
|
|
|
|
2018-03-10 05:45:29 -05:00
|
|
|
// This effectively prevents empty outputs entries for a given Kind.
|
|
|
|
// We need at least one.
|
2017-03-22 06:34:17 -04:00
|
|
|
if len(formats) > 0 {
|
2018-03-10 05:45:29 -05:00
|
|
|
seen[k] = true
|
2017-03-22 06:34:17 -04:00
|
|
|
outFormats[k] = formats
|
|
|
|
}
|
|
|
|
}
|
2017-03-09 13:19:29 -05:00
|
|
|
|
2018-03-10 05:45:29 -05:00
|
|
|
// Add defaults for the entries not provided by the user.
|
|
|
|
for k, v := range defaultOutputFormats {
|
|
|
|
if !seen[k] {
|
|
|
|
outFormats[k] = v
|
2017-03-22 06:34:17 -04:00
|
|
|
}
|
|
|
|
}
|
2017-03-09 13:19:29 -05:00
|
|
|
|
2017-03-22 06:34:17 -04:00
|
|
|
return outFormats, nil
|
|
|
|
}
|