本文整理汇总了Golang中github.com/spf13/hugo/tpl.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestShortcodeYoutube
func TestShortcodeYoutube(t *testing.T) {
for i, this := range []struct {
in, expected string
}{
{
`{{< youtube w7Ft2ymGmfc >}}`,
"(?s)^\n<div style=\".*?\">.*?<iframe src=\"//www.youtube.com/embed/w7Ft2ymGmfc\" style=\".*?\" allowfullscreen frameborder=\"0\">.*?</iframe>.*?</div>\n$",
},
// set class
{
`{{< youtube w7Ft2ymGmfc video>}}`,
"(?s)^\n<div class=\"video\">.*?<iframe src=\"//www.youtube.com/embed/w7Ft2ymGmfc\" allowfullscreen frameborder=\"0\">.*?</iframe>.*?</div>\n$",
},
// set class and autoplay (using named params)
{
`{{< youtube id="w7Ft2ymGmfc" class="video" autoplay="true" >}}`,
"(?s)^\n<div class=\"video\">.*?<iframe src=\"//www.youtube.com/embed/w7Ft2ymGmfc\\?autoplay=1\".*?allowfullscreen frameborder=\"0\">.*?</iframe>.*?</div>$",
},
} {
templ := tpl.New()
p, _ := pageFromString(simplePage, "simple.md")
output, err := HandleShortcodes(this.in, p, templ)
matched, err := regexp.MatchString(this.expected, output)
if err != nil {
t.Fatalf("[%d] Regexp error", i)
}
if !matched {
t.Errorf("[%d] unexpected rendering, got %s\n", i, output)
}
}
}
示例2: TestHighlight
func TestHighlight(t *testing.T) {
viper.Reset()
defer viper.Reset()
if !helpers.HasPygments() {
t.Skip("Skip test as Pygments is not installed")
}
viper.Set("PygmentsStyle", "bw")
viper.Set("PygmentsUseClasses", false)
templ := tpl.New()
code := `
{{< highlight java >}}
void do();
{{< /highlight >}}`
p, _ := pageFromString(simplePage, "simple.md")
output, err := HandleShortcodes(code, p, templ)
if err != nil {
t.Fatal("Handle shortcode error", err)
}
matched, err := regexp.MatchString("(?s)^\n<div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\">.*?void</span> do().*?</pre></div>\n$", output)
if err != nil {
t.Fatal("Regexp error", err)
}
if !matched {
t.Error("Hightlight mismatch, got\n", output)
}
}
示例3: TestShortcodeGoFuzzReports
func TestShortcodeGoFuzzReports(t *testing.T) {
tem := tpl.New()
tem.AddInternalShortcode("sc.html", `foo`)
p, _ := pageFromString(SIMPLE_PAGE, "simple.md")
for i, this := range []struct {
data string
expectErr bool
}{
{"{{</*/", true},
} {
output, err := HandleShortcodes(this.data, p, tem)
if this.expectErr && err == nil {
t.Errorf("[%d] should have errored", i)
}
if !this.expectErr && err != nil {
t.Errorf("[%d] should not have errored: %s", i, err)
}
if !this.expectErr && err == nil && len(output) == 0 {
t.Errorf("[%d] empty result", i)
}
}
}
示例4: TestShortcodeVimeo
func TestShortcodeVimeo(t *testing.T) {
for i, this := range []struct {
in, expected string
}{
{
`{{< vimeo 146022717 >}}`,
"(?s)^\n<div style=\".*?\">.*?<iframe src=\"//player.vimeo.com/video/146022717\" style=\".*?\" webkitallowfullscreen mozallowfullscreen allowfullscreen>.*?</iframe>.*?</div>\n$",
},
// set class
{
`{{< vimeo 146022717 video >}}`,
"(?s)^\n<div class=\"video\">.*?<iframe src=\"//player.vimeo.com/video/146022717\" webkitallowfullscreen mozallowfullscreen allowfullscreen>.*?</iframe>.*?</div>\n$",
},
// set class (using named params)
{
`{{< vimeo id="146022717" class="video" >}}`,
"(?s)^<div class=\"video\">.*?<iframe src=\"//player.vimeo.com/video/146022717\" webkitallowfullscreen mozallowfullscreen allowfullscreen>.*?</iframe>.*?</div>$",
},
} {
templ := tpl.New()
p, _ := pageFromString(simplePage, "simple.md")
output, err := HandleShortcodes(this.in, p, templ)
matched, err := regexp.MatchString(this.expected, output)
if err != nil {
t.Fatalf("[%d] Regexp error", i)
}
if !matched {
t.Errorf("[%d] unexpected rendering, got %s\n", i, output)
}
}
}
示例5: templatePrep
func templatePrep(s *Site) {
s.Tmpl = tpl.New()
s.Tmpl.LoadTemplates(s.absLayoutDir())
if s.hasTheme() {
s.Tmpl.LoadTemplatesWithPrefix(s.absThemeDir()+"/layouts", "theme")
}
}
示例6: doTestShortcodeCrossrefs
func doTestShortcodeCrossrefs(t *testing.T, relative bool) {
var refShortcode string
var expectedBase string
if relative {
refShortcode = "relref"
expectedBase = "/bar"
} else {
refShortcode = "ref"
expectedBase = baseURL
}
path := filepath.FromSlash("blog/post.md")
in := fmt.Sprintf(`{{< %s "%s" >}}`, refShortcode, path)
expected := fmt.Sprintf(`%s/simple/url/`, expectedBase)
templ := tpl.New()
p, _ := pageFromString(simplePageWithURL, path)
p.Node.Site = &SiteInfo{
Pages: &(Pages{p}),
BaseURL: template.URL(helpers.SanitizeURLKeepTrailingSlash(baseURL)),
}
output, err := HandleShortcodes(in, p, templ)
if err != nil {
t.Fatal("Handle shortcode error", err)
}
if output != expected {
t.Errorf("Got\n%q\nExpected\n%q", output, expected)
}
}
示例7: TestShortcodeTweet
func TestShortcodeTweet(t *testing.T) {
if testing.Short() {
t.Skip("skipping Twitter test in short mode.")
}
for i, this := range []struct {
in, expected string
}{
{
`{{< tweet 666616452582129664 >}}`,
"(?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>— 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>$",
},
} {
templ := tpl.New()
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)
matched, err := regexp.MatchString(this.expected, output)
if err != nil {
t.Fatalf("[%d] Regexp error", i)
}
if !matched {
t.Errorf("[%d] Hightlight mismatch, got %s\n", i, output)
}
}
}
示例8: TestShortcodeGist
func TestShortcodeGist(t *testing.T) {
for i, this := range []struct {
in, expected string
}{
{
`{{< gist spf13 7896402 >}}`,
"(?s)^<script src=\"//gist.github.com/spf13/7896402.js\"></script>$",
},
{
`{{< gist spf13 7896402 "img.html" >}}`,
"(?s)^<script src=\"//gist.github.com/spf13/7896402.js\\?file=img.html\"></script>$",
},
} {
templ := tpl.New()
p, _ := pageFromString(simplePage, "simple.md")
output, err := HandleShortcodes(this.in, p, templ)
matched, err := regexp.MatchString(this.expected, output)
if err != nil {
t.Fatalf("[%d] Regexp error", i)
}
if !matched {
t.Errorf("[%d] unexpected rendering, got %s\n", i, output)
}
}
}
示例9: TestInnerSC
func TestInnerSC(t *testing.T) {
tem := tpl.New()
tem.AddInternalShortcode("inside.html", `<div{{with .Get "class"}} class="{{.}}"{{end}}>{{ .Inner }}</div>`)
CheckShortCodeMatch(t, `{{< inside class="aspen" >}}`, `<div class="aspen"></div>`, tem)
CheckShortCodeMatch(t, `{{< inside class="aspen" >}}More Here{{< /inside >}}`, "<div class=\"aspen\">More Here</div>", tem)
CheckShortCodeMatch(t, `{{< inside >}}More Here{{< /inside >}}`, "<div>More Here</div>", tem)
}
示例10: TestNestedSC
func TestNestedSC(t *testing.T) {
tem := tpl.New()
tem.AddInternalShortcode("scn1.html", `<div>Outer, inner is {{ .Inner }}</div>`)
tem.AddInternalShortcode("scn2.html", `<div>SC2</div>`)
CheckShortCodeMatch(t, `{{% scn1 %}}{{% scn2 %}}{{% /scn1 %}}`, "<div>Outer, inner is <div>SC2</div>\n</div>", tem)
CheckShortCodeMatch(t, `{{< scn1 >}}{{% scn2 %}}{{< /scn1 >}}`, "<div>Outer, inner is <div>SC2</div></div>", tem)
}
示例11: TestParentShortcode
func TestParentShortcode(t *testing.T) {
tem := tpl.New()
tem.AddInternalShortcode("r1.html", `1: {{ .Get "pr1" }} {{ .Inner }}`)
tem.AddInternalShortcode("r2.html", `2: {{ .Parent.Get "pr1" }}{{ .Get "pr2" }} {{ .Inner }}`)
tem.AddInternalShortcode("r3.html", `3: {{ .Parent.Parent.Get "pr1" }}{{ .Parent.Get "pr2" }}{{ .Get "pr3" }} {{ .Inner }}`)
CheckShortCodeMatch(t, `{{< r1 pr1="p1" >}}1: {{< r2 pr2="p2" >}}2: {{< r3 pr3="p3" >}}{{< /r3 >}}{{< /r2 >}}{{< /r1 >}}`,
"1: p1 1: 2: p1p2 2: 3: p1p2p3 ", tem)
}
示例12: TestNestedNamedMissingParam
// Issue #2294
func TestNestedNamedMissingParam(t *testing.T) {
tem := tpl.New()
tem.AddInternalShortcode("acc.html", `<div class="acc">{{ .Inner }}</div>`)
tem.AddInternalShortcode("div.html", `<div {{with .Get "class"}} class="{{ . }}"{{ end }}>{{ .Inner }}</div>`)
tem.AddInternalShortcode("div2.html", `<div {{with .Get 0}} class="{{ . }}"{{ end }}>{{ .Inner }}</div>`)
CheckShortCodeMatch(t,
`{{% acc %}}{{% div %}}d1{{% /div %}}{{% div2 %}}d2{{% /div2 %}}{{% /acc %}}`,
"<div class=\"acc\"><div >d1</div><div >d2</div>\n</div>", tem)
}
示例13: TestPositionalParamSC
func TestPositionalParamSC(t *testing.T) {
tem := tpl.New()
tem.AddInternalShortcode("video.html", `Playing Video {{ .Get 0 }}`)
CheckShortCodeMatch(t, "{{< video 47238zzb >}}", "Playing Video 47238zzb", tem)
CheckShortCodeMatch(t, "{{< video 47238zzb 132 >}}", "Playing Video 47238zzb", tem)
CheckShortCodeMatch(t, "{{<video 47238zzb>}}", "Playing Video 47238zzb", tem)
CheckShortCodeMatch(t, "{{<video 47238zzb >}}", "Playing Video 47238zzb", tem)
CheckShortCodeMatch(t, "{{< video 47238zzb >}}", "Playing Video 47238zzb", tem)
}
示例14: TestInnerSCWithMarkdown
func TestInnerSCWithMarkdown(t *testing.T) {
tem := tpl.New()
tem.AddInternalShortcode("inside.html", `<div{{with .Get "class"}} class="{{.}}"{{end}}>{{ .Inner }}</div>`)
CheckShortCodeMatch(t, `{{% inside %}}
# More Here
[link](http://spf13.com) and text
{{% /inside %}}`, "<div><h1 id=\"more-here:bec3ed8ba720b9073ab75abcf3ba5d97\">More Here</h1>\n\n<p><a href=\"http://spf13.com\">link</a> and text</p>\n</div>", tem)
}
示例15: TestNamedParamSC
func TestNamedParamSC(t *testing.T) {
tem := tpl.New()
tem.AddInternalShortcode("img.html", `<img{{ with .Get "src" }} src="{{.}}"{{end}}{{with .Get "class"}} class="{{.}}"{{end}}>`)
CheckShortCodeMatch(t, `{{< img src="one" >}}`, `<img src="one">`, tem)
CheckShortCodeMatch(t, `{{< img class="aspen" >}}`, `<img class="aspen">`, tem)
CheckShortCodeMatch(t, `{{< img src= "one" >}}`, `<img src="one">`, tem)
CheckShortCodeMatch(t, `{{< img src ="one" >}}`, `<img src="one">`, tem)
CheckShortCodeMatch(t, `{{< img src = "one" >}}`, `<img src="one">`, tem)
CheckShortCodeMatch(t, `{{< img src = "one" class = "aspen grove" >}}`, `<img src="one" class="aspen grove">`, tem)
}