all: Refactor to nonglobal template handling

Updates #2701
This commit is contained in:
Bjørn Erik Pedersen
2017-01-10 01:36:59 +01:00
committed by GitHub
parent 4ea4359ac1
commit d6000a208c
17 changed files with 255 additions and 209 deletions

View File

@@ -19,16 +19,17 @@ import (
"html/template"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"io/ioutil"
"log"
"path/filepath"
"github.com/spf13/hugo/tpl"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/tpl"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
@@ -106,9 +107,8 @@ void do();
"(?s)^\n<div class=\"highlight\" style=\"background: #f0f0f0\"><pre style=\"line-height: 125%\">.*?void</span>.*?do</span>.*?().*?</pre></div>\n$",
},
} {
templ := tpl.New(logger)
p, _ := pageFromString(simplePage, "simple.md")
output, err := HandleShortcodes(this.in, p, templ)
output, err := HandleShortcodes(this.in, p)
if err != nil {
t.Fatalf("[%d] Handle shortcode error", i)
@@ -150,9 +150,8 @@ func TestShortcodeFigure(t *testing.T) {
"(?s)^\n<figure >.*?<img src=\"/img/hugo-logo.png\" />.*?<figcaption>.*?<p>.*?<a href=\"/img/hugo-logo.png\">.*?Hugo logo.*?</a>.*?</p>.*?</figcaption>.*?</figure>\n$",
},
} {
templ := tpl.New(logger)
p, _ := pageFromString(simplePage, "simple.md")
output, err := HandleShortcodes(this.in, p, templ)
output, err := HandleShortcodes(this.in, p)
matched, err := regexp.MatchString(this.expected, output)
@@ -175,9 +174,8 @@ func TestShortcodeSpeakerdeck(t *testing.T) {
"(?s)^<script async class='speakerdeck-embed' data-id='4e8126e72d853c0060001f97'.*?>.*?</script>$",
},
} {
templ := tpl.New(logger)
p, _ := pageFromString(simplePage, "simple.md")
output, err := HandleShortcodes(this.in, p, templ)
output, err := HandleShortcodes(this.in, p)
matched, err := regexp.MatchString(this.expected, output)
@@ -210,9 +208,8 @@ func TestShortcodeYoutube(t *testing.T) {
"(?s)^\n<div class=\"video\">.*?<iframe src=\"//www.youtube.com/embed/w7Ft2ymGmfc\\?autoplay=1\".*?allowfullscreen frameborder=\"0\">.*?</iframe>.*?</div>$",
},
} {
templ := tpl.New(logger)
p, _ := pageFromString(simplePage, "simple.md")
output, err := HandleShortcodes(this.in, p, templ)
output, err := HandleShortcodes(this.in, p)
matched, err := regexp.MatchString(this.expected, output)
@@ -245,9 +242,8 @@ func TestShortcodeVimeo(t *testing.T) {
"(?s)^<div class=\"video\">.*?<iframe src=\"//player.vimeo.com/video/146022717\" webkitallowfullscreen mozallowfullscreen allowfullscreen>.*?</iframe>.*?</div>$",
},
} {
templ := tpl.New(logger)
p, _ := pageFromString(simplePage, "simple.md")
output, err := HandleShortcodes(this.in, p, templ)
output, err := HandleShortcodes(this.in, p)
matched, err := regexp.MatchString(this.expected, output)
@@ -274,9 +270,8 @@ func TestShortcodeGist(t *testing.T) {
"(?s)^<script src=\"//gist.github.com/spf13/7896402.js\\?file=img.html\"></script>$",
},
} {
templ := tpl.New(logger)
p, _ := pageFromString(simplePage, "simple.md")
output, err := HandleShortcodes(this.in, p, templ)
output, err := HandleShortcodes(this.in, p)
matched, err := regexp.MatchString(this.expected, output)
@@ -313,13 +308,14 @@ func TestShortcodeTweet(t *testing.T) {
},
}
templ := tpl.New(logger)
templ.Lookup("").Funcs(tweetFuncMap)
p, _ := pageFromString(simplePage, "simple.md", func(templ tpl.Template) error {
templ.Funcs(tweetFuncMap)
return nil
})
p, _ := pageFromString(simplePage, "simple.md")
cacheFileID := viper.GetString("cacheDir") + url.QueryEscape("https://api.twitter.com/1/statuses/oembed.json?id=666616452582129664")
defer os.Remove(cacheFileID)
output, err := HandleShortcodes(this.in, p, templ)
output, err := HandleShortcodes(this.in, p)
matched, err := regexp.MatchString(this.expected, output)
@@ -353,7 +349,7 @@ func TestShortcodeInstagram(t *testing.T) {
},
} {
// overload getJSON to return mock API response from Instagram
tweetFuncMap := template.FuncMap{
instagramFuncMap := template.FuncMap{
"getJSON": func(urlParts ...string) interface{} {
var v interface{}
err := json.Unmarshal([]byte(this.resp), &v)
@@ -365,13 +361,14 @@ func TestShortcodeInstagram(t *testing.T) {
},
}
templ := tpl.New(logger)
templ.Lookup("").Funcs(tweetFuncMap)
p, _ := pageFromString(simplePage, "simple.md", func(templ tpl.Template) error {
templ.Funcs(instagramFuncMap)
return nil
})
p, _ := pageFromString(simplePage, "simple.md")
cacheFileID := viper.GetString("cacheDir") + url.QueryEscape("https://api.instagram.com/oembed/?url=https://instagram.com/p/BMokmydjG-M/&hidecaption="+this.hidecaption)
defer os.Remove(cacheFileID)
output, err := HandleShortcodes(this.in, p, templ)
output, err := HandleShortcodes(this.in, p)
if err != nil {
t.Fatalf("[%d] Failed to render shortcodes", i)