tpl: Make getJSON/getCVS accept non-string args
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 10 Oct 2019 08:18:30 +0000 (10:18 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 10 Oct 2019 11:30:39 +0000 (13:30 +0200)
This broke for the Twitter simple shortcode now that Shortcodes accepts typed arguments.

Fixes #6382

hugolib/embedded_shortcodes_test.go
hugolib/testhelpers_test.go
tpl/data/data.go
tpl/data/data_test.go

index b169cd74e5cdc7ed87ff85da4da6716dc856fb91..b34a767ca92a9d2a2ba1ccd8b683ce8fc4b81287 100644 (file)
@@ -280,9 +280,25 @@ func TestShortcodeTweet(t *testing.T) {
        t.Parallel()
 
        for i, this := range []struct {
+               privacy            map[string]interface{}
                in, resp, expected string
        }{
                {
+                       map[string]interface{}{
+                               "twitter": map[string]interface{}{
+                                       "simple": true,
+                               },
+                       },
+                       `{{< tweet 666616452582129664 >}}`,
+                       `{"url":"https:\/\/twitter.com\/spf13\/status\/666616452582129664","author_name":"Steve Francia","author_url":"https:\/\/twitter.com\/spf13","html":"\u003Cblockquote class=\"twitter-tweet\"\u003E\u003Cp lang=\"en\" dir=\"ltr\"\u003EHugo 0.15 will have 30%+ faster render times thanks to this commit \u003Ca href=\"https:\/\/t.co\/FfzhM8bNhT\"\u003Ehttps:\/\/t.co\/FfzhM8bNhT\u003C\/a\u003E  \u003Ca href=\"https:\/\/twitter.com\/hashtag\/gohugo?src=hash\"\u003E#gohugo\u003C\/a\u003E \u003Ca href=\"https:\/\/twitter.com\/hashtag\/golang?src=hash\"\u003E#golang\u003C\/a\u003E \u003Ca href=\"https:\/\/t.co\/ITbMNU2BUf\"\u003Ehttps:\/\/t.co\/ITbMNU2BUf\u003C\/a\u003E\u003C\/p\u003E&mdash; Steve Francia (@spf13) \u003Ca href=\"https:\/\/twitter.com\/spf13\/status\/666616452582129664\"\u003ENovember 17, 2015\u003C\/a\u003E\u003C\/blockquote\u003E\n\u003Cscript async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"\u003E\u003C\/script\u003E","width":550,"height":null,"type":"rich","cache_age":"3153600000","provider_name":"Twitter","provider_url":"https:\/\/twitter.com","version":"1.0"}`,
+                       `.twitter-tweet a`,
+               },
+               {
+                       map[string]interface{}{
+                               "twitter": map[string]interface{}{
+                                       "simple": false,
+                               },
+                       },
                        `{{< tweet 666616452582129664 >}}`,
                        `{"url":"https:\/\/twitter.com\/spf13\/status\/666616452582129664","author_name":"Steve Francia","author_url":"https:\/\/twitter.com\/spf13","html":"\u003Cblockquote class=\"twitter-tweet\"\u003E\u003Cp lang=\"en\" dir=\"ltr\"\u003EHugo 0.15 will have 30%+ faster render times thanks to this commit \u003Ca href=\"https:\/\/t.co\/FfzhM8bNhT\"\u003Ehttps:\/\/t.co\/FfzhM8bNhT\u003C\/a\u003E  \u003Ca href=\"https:\/\/twitter.com\/hashtag\/gohugo?src=hash\"\u003E#gohugo\u003C\/a\u003E \u003Ca href=\"https:\/\/twitter.com\/hashtag\/golang?src=hash\"\u003E#golang\u003C\/a\u003E \u003Ca href=\"https:\/\/t.co\/ITbMNU2BUf\"\u003Ehttps:\/\/t.co\/ITbMNU2BUf\u003C\/a\u003E\u003C\/p\u003E&mdash; Steve Francia (@spf13) \u003Ca href=\"https:\/\/twitter.com\/spf13\/status\/666616452582129664\"\u003ENovember 17, 2015\u003C\/a\u003E\u003C\/blockquote\u003E\n\u003Cscript async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"\u003E\u003C\/script\u003E","width":550,"height":null,"type":"rich","cache_age":"3153600000","provider_name":"Twitter","provider_url":"https:\/\/twitter.com","version":"1.0"}`,
                        `(?s)^<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Hugo 0.15 will have 30%. faster render times thanks to this commit <a href="https://t.co/FfzhM8bNhT">https://t.co/FfzhM8bNhT</a>  <a href="https://twitter.com/hashtag/gohugo.src=hash">#gohugo</a> <a href="https://twitter.com/hashtag/golang.src=hash">#golang</a> <a href="https://t.co/ITbMNU2BUf">https://t.co/ITbMNU2BUf</a></p>&mdash; Steve Francia .@spf13. <a href="https://twitter.com/spf13/status/666616452582129664">November 17, 2015</a></blockquote>.*?<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>`,
@@ -290,7 +306,7 @@ func TestShortcodeTweet(t *testing.T) {
        } {
                // overload getJSON to return mock API response from Twitter
                tweetFuncMap := template.FuncMap{
-                       "getJSON": func(urlParts ...string) interface{} {
+                       "getJSON": func(urlParts ...interface{}) interface{} {
                                var v interface{}
                                err := json.Unmarshal([]byte(this.resp), &v)
                                if err != nil {
@@ -306,6 +322,8 @@ func TestShortcodeTweet(t *testing.T) {
                        th      = newTestHelper(cfg, fs, t)
                )
 
+               cfg.Set("privacy", this.privacy)
+
                withTemplate := func(templ tpl.TemplateHandler) error {
                        templ.(tpl.TemplateTestMocker).SetFuncs(tweetFuncMap)
                        return nil
index 353cc9ec6208590bdd938b90908e6970bb89acfc..2865010f04f6b1bd0f6c8f0c5ceef1b4f1c145d5 100644 (file)
@@ -753,7 +753,11 @@ func (th testHelper) assertFileContentRegexp(filename string, matches ...string)
        for _, match := range matches {
                match = th.replaceDefaultContentLanguageValue(match)
                r := regexp.MustCompile(match)
-               th.Assert(r.MatchString(content), qt.Equals, true)
+               matches := r.MatchString(content)
+               if !matches {
+                       fmt.Println(content)
+               }
+               th.Assert(matches, qt.Equals, true)
        }
 }
 
index 15f03929435c55947715d2dd60f9e0ff431c768a..f64ba01274b7cf265f6de8b87b86bd5c0e160608 100644 (file)
@@ -23,6 +23,8 @@ import (
        "net/http"
        "strings"
 
+       "github.com/spf13/cast"
+
        "github.com/gohugoio/hugo/cache/filecache"
        "github.com/gohugoio/hugo/deps"
        _errors "github.com/pkg/errors"
@@ -54,8 +56,8 @@ type Namespace struct {
 // The data separator can be a comma, semi-colon, pipe, etc, but only one character.
 // If you provide multiple parts for the URL they will be joined together to the final URL.
 // GetCSV returns nil or a slice slice to use in a short code.
-func (ns *Namespace) GetCSV(sep string, urlParts ...string) (d [][]string, err error) {
-       url := strings.Join(urlParts, "")
+func (ns *Namespace) GetCSV(sep string, urlParts ...interface{}) (d [][]string, err error) {
+       url := joinURL(urlParts)
        cache := ns.cacheGetCSV
 
        unmarshal := func(b []byte) (bool, error) {
@@ -93,9 +95,9 @@ func (ns *Namespace) GetCSV(sep string, urlParts ...string) (d [][]string, err e
 // GetJSON expects one or n-parts of a URL to a resource which can either be a local or a remote one.
 // If you provide multiple parts they will be joined together to the final URL.
 // GetJSON returns nil or parsed JSON to use in a short code.
-func (ns *Namespace) GetJSON(urlParts ...string) (interface{}, error) {
+func (ns *Namespace) GetJSON(urlParts ...interface{}) (interface{}, error) {
        var v interface{}
-       url := strings.Join(urlParts, "")
+       url := joinURL(urlParts)
        cache := ns.cacheGetJSON
 
        req, err := http.NewRequest("GET", url, nil)
@@ -122,6 +124,10 @@ func (ns *Namespace) GetJSON(urlParts ...string) (interface{}, error) {
        return v, nil
 }
 
+func joinURL(urlParts []interface{}) string {
+       return strings.Join(cast.ToStringSlice(urlParts), "")
+}
+
 // parseCSV parses bytes of CSV data into a slice slice string or an error
 func parseCSV(c []byte, sep string) ([][]string, error) {
        if len(sep) != 1 {
index 04575707ca45367c131658051eebc54ec4703f8f..8bd4edc98e05bf4783aa46e7c0c19292fc01e73a 100644 (file)
@@ -204,6 +204,12 @@ func TestGetJSON(t *testing.T) {
        }
 }
 
+func TestJoinURL(t *testing.T) {
+       t.Parallel()
+       c := qt.New(t)
+       c.Assert(joinURL([]interface{}{"https://foo?id=", 32}), qt.Equals, "https://foo?id=32")
+}
+
 func TestParseCSV(t *testing.T) {
        t.Parallel()
        c := qt.New(t)