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"
|
2017-03-07 08:20:39 -05:00
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
"github.com/gohugoio/hugo/output"
|
2019-01-02 06:33:26 -05:00
|
|
|
"github.com/gohugoio/hugo/resources/page"
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/cast"
|
2017-03-07 08:20:39 -05:00
|
|
|
)
|
|
|
|
|
2018-03-10 05:45:29 -05:00
|
|
|
func createDefaultOutputFormats(allFormats output.Formats, cfg config.Provider) map[string]output.Formats {
|
|
|
|
rssOut, _ := allFormats.GetByName(output.RSSFormat.Name)
|
|
|
|
htmlOut, _ := allFormats.GetByName(output.HTMLFormat.Name)
|
|
|
|
robotsOut, _ := allFormats.GetByName(output.RobotsTxtFormat.Name)
|
|
|
|
sitemapOut, _ := allFormats.GetByName(output.SitemapFormat.Name)
|
|
|
|
|
|
|
|
return map[string]output.Formats{
|
2019-01-02 06:33:26 -05:00
|
|
|
page.KindPage: {htmlOut},
|
|
|
|
page.KindHome: {htmlOut, rssOut},
|
|
|
|
page.KindSection: {htmlOut, rssOut},
|
|
|
|
page.KindTaxonomy: {htmlOut, rssOut},
|
|
|
|
page.KindTaxonomyTerm: {htmlOut, rssOut},
|
2019-08-05 04:19:55 -04:00
|
|
|
// Below are for consistency. They are currently not used during rendering.
|
2018-10-23 17:18:24 -04:00
|
|
|
kindRSS: {rssOut},
|
|
|
|
kindSitemap: {sitemapOut},
|
|
|
|
kindRobotsTXT: {robotsOut},
|
|
|
|
kind404: {htmlOut},
|
2018-03-10 05:45:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-04 03:12:33 -04:00
|
|
|
func createSiteOutputFormats(allFormats output.Formats, cfg config.Provider) (map[string]output.Formats, error) {
|
2018-03-10 05:45:29 -05:00
|
|
|
defaultOutputFormats := createDefaultOutputFormats(allFormats, cfg)
|
|
|
|
|
2017-03-22 06:34:17 -04:00
|
|
|
if !cfg.IsSet("outputs") {
|
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
|
|
|
|
2017-03-22 06:34:17 -04:00
|
|
|
outputs := cfg.GetStringMap("outputs")
|
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 {
|
2019-08-05 04:19:55 -04:00
|
|
|
k = getKind(k)
|
|
|
|
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 {
|
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
|
2017-03-09 13:19:29 -05:00
|
|
|
|
2017-03-22 06:34:17 -04:00
|
|
|
}
|