// Copyright 2019 The Hugo Authors. All rights reserved.
//
// 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 (
"fmt"
"html/template"
"strings"
"testing"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/common/hstrings"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/resources/kinds"
"github.com/spf13/afero"
"github.com/gohugoio/hugo/output"
)
func TestSiteWithPageOutputs(t *testing.T) {
for _, outputs := range [][]string{{"html", "json", "calendar"}, {"json"}} {
outputs := outputs
t.Run(fmt.Sprintf("%v", outputs), func(t *testing.T) {
t.Parallel()
doTestSiteWithPageOutputs(t, outputs)
})
}
}
func doTestSiteWithPageOutputs(t *testing.T, outputs []string) {
outputsStr := strings.Replace(fmt.Sprintf("%q", outputs), " ", ", ", -1)
siteConfig := `
baseURL = "http://example.com/blog"
paginate = 1
defaultContentLanguage = "en"
disableKinds = ["section", "term", "taxonomy", "RSS", "sitemap", "robotsTXT", "404"]
[Taxonomies]
tag = "tags"
category = "categories"
defaultContentLanguage = "en"
[languages]
[languages.en]
title = "Title in English"
languageName = "English"
weight = 1
[languages.nn]
languageName = "Nynorsk"
weight = 2
title = "Tittel på Nynorsk"
`
pageTemplate := `---
title: "%s"
outputs: %s
---
# Doc
{{< myShort >}}
{{< myOtherShort >}}
`
b := newTestSitesBuilder(t).WithConfigFile("toml", siteConfig)
b.WithI18n("en.toml", `
[elbow]
other = "Elbow"
`, "nn.toml", `
[elbow]
other = "Olboge"
`)
b.WithTemplates(
// Case issue partials #3333
"layouts/partials/GoHugo.html", `Go Hugo Partial`,
"layouts/_default/baseof.json", `START JSON:{{block "main" .}}default content{{ end }}:END JSON`,
"layouts/_default/baseof.html", `START HTML:{{block "main" .}}default content{{ end }}:END HTML`,
"layouts/shortcodes/myOtherShort.html", `OtherShort: {{ "
Hi!
" | safeHTML }}`,
"layouts/shortcodes/myShort.html", `ShortHTML`,
"layouts/shortcodes/myShort.json", `ShortJSON`,
"layouts/_default/list.json", `{{ define "main" }}
List JSON|{{ .Title }}|{{ .Content }}|Alt formats: {{ len .AlternativeOutputFormats -}}|
{{- range .AlternativeOutputFormats -}}
Alt Output: {{ .Name -}}|
{{- end -}}|
{{- range .OutputFormats -}}
Output/Rel: {{ .Name -}}/{{ .Rel }}|{{ .MediaType }}
{{- end -}}
{{ with .OutputFormats.Get "JSON" }}
{{ end }}
{{ .Site.Language.Lang }}: {{ T "elbow" -}}
{{ end }}
`,
"layouts/_default/list.html", `{{ define "main" }}
List HTML|{{.Title }}|
{{- with .OutputFormats.Get "HTML" -}}
{{- end -}}
{{ .Site.Language.Lang }}: {{ T "elbow" -}}
Partial Hugo 1: {{ partial "GoHugo.html" . }}
Partial Hugo 2: {{ partial "GoHugo" . -}}
Content: {{ .Content }}
Len Pages: {{ .Kind }} {{ len .Site.RegularPages }} Page Number: {{ .Paginator.PageNumber }}
{{ end }}
`,
"layouts/_default/single.html", `{{ define "main" }}{{ .Content }}{{ end }}`,
)
b.WithContent("_index.md", fmt.Sprintf(pageTemplate, "JSON Home", outputsStr))
b.WithContent("_index.nn.md", fmt.Sprintf(pageTemplate, "JSON Nynorsk Heim", outputsStr))
for i := 1; i <= 10; i++ {
b.WithContent(fmt.Sprintf("p%d.md", i), fmt.Sprintf(pageTemplate, fmt.Sprintf("Page %d", i), outputsStr))
}
b.Build(BuildCfg{})
s := b.H.Sites[0]
b.Assert(s.language.Lang, qt.Equals, "en")
home := s.getPageOldVersion(kinds.KindHome)
b.Assert(home, qt.Not(qt.IsNil))
lenOut := len(outputs)
b.Assert(len(home.OutputFormats()), qt.Equals, lenOut)
// There is currently always a JSON output to make it simpler ...
altFormats := lenOut - 1
hasHTML := hstrings.InSlice(outputs, "html")
b.AssertFileContent("public/index.json",
"List JSON",
fmt.Sprintf("Alt formats: %d", altFormats),
)
if hasHTML {
b.AssertFileContent("public/index.json",
"Alt Output: html",
"Output/Rel: json/alternate|",
"Output/Rel: html/canonical|",
"en: Elbow",
"ShortJSON",
"OtherShort:
Hi!
",
)
b.AssertFileContent("public/index.html",
// The HTML entity is a deliberate part of this test: The HTML templates are
// parsed with html/template.
`List HTML|JSON Home|`,
"en: Elbow",
"ShortHTML",
"OtherShort:
Hi!
",
"Len Pages: home 10",
)
b.AssertFileContent("public/page/2/index.html", "Page Number: 2")
b.Assert(b.CheckExists("public/page/2/index.json"), qt.Equals, false)
b.AssertFileContent("public/nn/index.html",
"List HTML|JSON Nynorsk Heim|",
"nn: Olboge")
} else {
b.AssertFileContent("public/index.json",
"Output/Rel: json/canonical|",
// JSON is plain text, so no need to safeHTML this and that
``,
"ShortJSON",
"OtherShort: