hugolib, output: Fix robots.txt in multihost mode

Fixes #4193
This commit is contained in:
Bjørn Erik Pedersen 2018-01-25 10:36:53 +01:00
parent 1e27d05891
commit 4d912e2aad
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 21 additions and 16 deletions

View file

@ -19,11 +19,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/output" "github.com/gohugoio/hugo/output"
bp "github.com/gohugoio/hugo/bufferpool"
) )
// renderPages renders pages each corresponding to a markdown file. // renderPages renders pages each corresponding to a markdown file.
@ -368,26 +364,27 @@ func (s *Site) renderRobotsTXT() error {
return nil return nil
} }
n := s.newNodePage(kindRobotsTXT) p := s.newNodePage(kindRobotsTXT)
if err := n.initTargetPathDescriptor(); err != nil { if err := p.initTargetPathDescriptor(); err != nil {
return err return err
} }
n.Data["Pages"] = s.Pages p.Data["Pages"] = s.Pages
n.Pages = s.Pages p.Pages = s.Pages
rLayouts := []string{"robots.txt", "_default/robots.txt", "_internal/_default/robots.txt"} rLayouts := []string{"robots.txt", "_default/robots.txt", "_internal/_default/robots.txt"}
outBuffer := bp.GetBuffer()
defer bp.PutBuffer(outBuffer) pageOutput, err := newPageOutput(p, false, output.RobotsTxtFormat)
if err := s.renderForLayouts("robots", n, outBuffer, s.appendThemeTemplates(rLayouts)...); err != nil { if err != nil {
helpers.DistinctWarnLog.Println(err) return err
return nil
} }
if outBuffer.Len() == 0 { targetPath, err := pageOutput.targetPath()
return nil if err != nil {
s.Log.ERROR.Printf("Failed to create target path for page %q: %s", p, err)
} }
return s.publish(&s.PathSpec.ProcessingStats.Pages, "robots.txt", outBuffer) return s.renderAndWritePage(&s.PathSpec.ProcessingStats.Pages, "Robots Txt", targetPath, pageOutput, s.appendThemeTemplates(rLayouts)...)
} }
// renderAliases renders shell pages that simply have a redirect in the header. // renderAliases renders shell pages that simply have a redirect in the header.

View file

@ -125,6 +125,14 @@ var (
Rel: "alternate", Rel: "alternate",
} }
RobotsTxtFormat = Format{
Name: "ROBOTS",
MediaType: media.TextType,
BaseName: "robots",
IsPlainText: true,
Rel: "alternate",
}
RSSFormat = Format{ RSSFormat = Format{
Name: "RSS", Name: "RSS",
MediaType: media.RSSType, MediaType: media.RSSType,