diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-05-06 20:15:28 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-05-13 22:44:15 +0300 |
commit | af72db806f2c1c0bf1dfe5832275c41eeba89906 (patch) | |
tree | a1bc9c7d09836073a811a1b395b7756727fdb210 /hugolib/shortcode_test.go | |
parent | e951d65771ca299aa899e91bfe00411a5ada8f19 (diff) |
hugolib: Handle shortcode per output format
This commit allows shortcode per output format, a typical use case would be the special AMP media tags.
Note that this will only re-render the "overridden" shortcodes and only in pages where these are used, so performance in the normal case should not suffer.
Closes #3220
Diffstat (limited to 'hugolib/shortcode_test.go')
-rw-r--r-- | hugolib/shortcode_test.go | 164 |
1 files changed, 163 insertions, 1 deletions
diff --git a/hugolib/shortcode_test.go b/hugolib/shortcode_test.go index 3d1922462..2c1e887c9 100644 --- a/hugolib/shortcode_test.go +++ b/hugolib/shortcode_test.go @@ -22,6 +22,14 @@ import ( "strings" "testing" + jww "github.com/spf13/jwalterweatherman" + + "github.com/spf13/afero" + + "github.com/spf13/hugo/output" + + "github.com/spf13/hugo/media" + "github.com/spf13/hugo/deps" "github.com/spf13/hugo/helpers" "github.com/spf13/hugo/source" @@ -353,7 +361,7 @@ func TestExtractShortcodes(t *testing.T) { return nil }) - s := newShortcodeHandler() + s := newShortcodeHandler(p) content, err := s.extractShortcodes(this.input, p) if b, ok := this.expect.(bool); ok && !b { @@ -563,6 +571,150 @@ tags: } +func TestShortcodeMultipleOutputFormats(t *testing.T) { + t.Parallel() + + siteConfig := ` +baseURL = "http://example.com/blog" + +paginate = 1 + +disableKinds = ["section", "taxonomy", "taxonomyTerm", "RSS", "sitemap", "robotsTXT", "404"] + +[outputs] +home = [ "HTML", "AMP", "Calendar" ] +page = [ "HTML", "AMP", "JSON" ] + +` + + pageTemplate := `--- +title: "%s" +--- +# Doc + +{{< myShort >}} +{{< noExt >}} +{{%% onlyHTML %%}} + +{{< myInner >}}{{< myShort >}}{{< /myInner >}} + +` + + pageTemplateCSVOnly := `--- +title: "%s" +outputs: ["CSV"] +--- +# Doc + +CSV: {{< myShort >}} +` + + pageTemplateShortcodeNotFound := `--- +title: "%s" +outputs: ["CSV"] +--- +# Doc + +NotFound: {{< thisDoesNotExist >}} +` + + mf := afero.NewMemMapFs() + + th, h := newTestSitesFromConfig(t, mf, siteConfig, + "layouts/_default/single.html", `Single HTML: {{ .Title }}|{{ .Content }}`, + "layouts/_default/single.json", `Single JSON: {{ .Title }}|{{ .Content }}`, + "layouts/_default/single.csv", `Single CSV: {{ .Title }}|{{ .Content }}`, + "layouts/index.html", `Home HTML: {{ .Title }}|{{ .Content }}`, + "layouts/index.amp.html", `Home AMP: {{ .Title }}|{{ .Content }}`, + "layouts/index.ics", `Home Calendar: {{ .Title }}|{{ .Content }}`, + "layouts/shortcodes/myShort.html", `ShortHTML`, + "layouts/shortcodes/myShort.amp.html", `ShortAMP`, + "layouts/shortcodes/myShort.csv", `ShortCSV`, + "layouts/shortcodes/myShort.ics", `ShortCalendar`, + "layouts/shortcodes/myShort.json", `ShortJSON`, + "layouts/shortcodes/noExt", `ShortNoExt`, + "layouts/shortcodes/onlyHTML.html", `ShortOnlyHTML`, + "layouts/shortcodes/myInner.html", `myInner:--{{- .Inner -}}--`, + ) + + fs := th.Fs + + writeSource(t, fs, "content/_index.md", fmt.Sprintf(pageTemplate, "Home")) + writeSource(t, fs, "content/sect/mypage.md", fmt.Sprintf(pageTemplate, "Single")) + writeSource(t, fs, "content/sect/mycsvpage.md", fmt.Sprintf(pageTemplateCSVOnly, "Single CSV")) + writeSource(t, fs, "content/sect/notfound.md", fmt.Sprintf(pageTemplateShortcodeNotFound, "Single CSV")) + + require.NoError(t, h.Build(BuildCfg{})) + require.Len(t, h.Sites, 1) + + s := h.Sites[0] + home := s.getPage(KindHome) + require.NotNil(t, home) + require.Len(t, home.outputFormats, 3) + + th.assertFileContent("public/index.html", + "Home HTML", + "ShortHTML", + "ShortNoExt", + "ShortOnlyHTML", + "myInner:--ShortHTML--", + ) + + th.assertFileContent("public/amp/index.html", + "Home AMP", + "ShortAMP", + "ShortNoExt", + "ShortOnlyHTML", + "myInner:--ShortAMP--", + ) + + th.assertFileContent("public/index.ics", + "Home Calendar", + "ShortCalendar", + "ShortNoExt", + "ShortOnlyHTML", + "myInner:--ShortCalendar--", + ) + + th.assertFileContent("public/sect/mypage/index.html", + "Single HTML", + "ShortHTML", + "ShortNoExt", + "ShortOnlyHTML", + "myInner:--ShortHTML--", + ) + + th.assertFileContent("public/sect/mypage/index.json", + "Single JSON", + "ShortJSON", + "ShortNoExt", + "ShortOnlyHTML", + "myInner:--ShortJSON--", + ) + + th.assertFileContent("public/amp/sect/mypage/index.html", + // No special AMP template + "Single HTML", + "ShortAMP", + "ShortNoExt", + "ShortOnlyHTML", + "myInner:--ShortAMP--", + ) + + th.assertFileContent("public/sect/mycsvpage/index.csv", + "Single CSV", + "ShortCSV", + ) + + th.assertFileContent("public/sect/notfound/index.csv", + "NotFound:", + "thisDoesNotExist", + ) + + require.Equal(t, uint64(1), s.Log.LogCountForLevel(jww.LevelError)) + +} + func collectAndSortShortcodes(shortcodes map[string]shortcode) []string { var asArray []string @@ -681,3 +833,13 @@ func TestReplaceShortcodeTokens(t *testing.T) { } } + +func TestScKey(t *testing.T) { + require.Equal(t, scKey{Suffix: "xml", ShortcodePlaceholder: "ABCD"}, + newScKey(media.XMLType, "ABCD")) + require.Equal(t, scKey{Suffix: "html", OutputFormat: "AMP", ShortcodePlaceholder: "EFGH"}, + newScKeyFromOutputFormat(output.AMPFormat, "EFGH")) + require.Equal(t, scKey{Suffix: "html", ShortcodePlaceholder: "IJKL"}, + newDefaultScKey("IJKL")) + +} |