diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-08-07 14:03:03 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-09-06 18:32:17 +0300 |
commit | 2079a23dd89734cea39e523faf46e44201151279 (patch) | |
tree | 5b5163a12cf874863eea889685d65a3c5b56515c /hugolib | |
parent | 596e0e98e4483d2a0a709412a338ceddb6538757 (diff) |
Make it possible to configure Blackfroday per language
See #2309
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/config.go | 2 | ||||
-rw-r--r-- | hugolib/embedded_shortcodes_test.go | 8 | ||||
-rw-r--r-- | hugolib/hugo_sites.go | 14 | ||||
-rw-r--r-- | hugolib/hugo_sites_test.go | 18 | ||||
-rw-r--r-- | hugolib/multilingual.go | 29 | ||||
-rw-r--r-- | hugolib/node.go | 37 | ||||
-rw-r--r-- | hugolib/page.go | 21 | ||||
-rw-r--r-- | hugolib/pageSort_test.go | 4 | ||||
-rw-r--r-- | hugolib/page_permalink_test.go | 4 | ||||
-rw-r--r-- | hugolib/page_test.go | 2 | ||||
-rw-r--r-- | hugolib/pagination_test.go | 4 | ||||
-rw-r--r-- | hugolib/shortcode.go | 3 | ||||
-rw-r--r-- | hugolib/shortcode_test.go | 231 | ||||
-rw-r--r-- | hugolib/site.go | 25 | ||||
-rw-r--r-- | hugolib/translations.go | 15 |
15 files changed, 255 insertions, 162 deletions
diff --git a/hugolib/config.go b/hugolib/config.go index 285e3567a..c4292e81b 100644 --- a/hugolib/config.go +++ b/hugolib/config.go @@ -94,7 +94,7 @@ func loadDefaultSettings() { viper.SetDefault("NewContentEditor", "") viper.SetDefault("Paginate", 10) viper.SetDefault("PaginatePath", "page") - viper.SetDefault("Blackfriday", helpers.NewBlackfriday()) + viper.SetDefault("Blackfriday", helpers.NewBlackfriday(viper.GetViper())) viper.SetDefault("RSSUri", "index.xml") viper.SetDefault("SectionPagesMenu", "") viper.SetDefault("DisablePathToLower", false) diff --git a/hugolib/embedded_shortcodes_test.go b/hugolib/embedded_shortcodes_test.go index e668ff4c8..cebef0b8b 100644 --- a/hugolib/embedded_shortcodes_test.go +++ b/hugolib/embedded_shortcodes_test.go @@ -15,7 +15,6 @@ package hugolib import ( "fmt" - "html/template" "net/url" "os" "path/filepath" @@ -55,10 +54,9 @@ func doTestShortcodeCrossrefs(t *testing.T, relative bool) { templ := tpl.New() p, _ := pageFromString(simplePageWithURL, path) - p.Node.Site = &SiteInfo{ - rawAllPages: &(Pages{p}), - BaseURL: template.URL(helpers.SanitizeURLKeepTrailingSlash(baseURL)), - } + p.Node.Site = newSiteInfoDefaultLanguage( + helpers.SanitizeURLKeepTrailingSlash(baseURL), + p) output, err := HandleShortcodes(in, p, templ) diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index b41334fdc..8fd2a63b9 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -39,7 +39,7 @@ type HugoSites struct { // NewHugoSites creates a new collection of sites given the input sites, building // a language configuration based on those. -func NewHugoSites(sites ...*Site) (*HugoSites, error) { +func newHugoSites(sites ...*Site) (*HugoSites, error) { langConfig, err := newMultiLingualFromSites(sites...) if err != nil { @@ -55,7 +55,7 @@ func NewHugoSitesFromConfiguration() (*HugoSites, error) { if err != nil { return nil, err } - return NewHugoSites(sites...) + return newHugoSites(sites...) } func createSitesFromConfig() ([]*Site, error) { @@ -461,7 +461,7 @@ func buildAndRenderSite(s *Site, additionalTemplates ...string) error { // Convenience func used in tests to build a single site/language. func doBuildSite(s *Site, render bool, additionalTemplates ...string) error { - sites, err := NewHugoSites(s) + sites, err := newHugoSites(s) if err != nil { return err } @@ -490,7 +490,7 @@ func newHugoSitesFromSourceAndLanguages(input []source.ByteSource, languages Lan Language: languages[0], } if len(languages) == 1 { - return NewHugoSites(first) + return newHugoSites(first) } sites := make([]*Site, len(languages)) @@ -499,7 +499,7 @@ func newHugoSitesFromSourceAndLanguages(input []source.ByteSource, languages Lan sites[i] = &Site{Language: languages[i]} } - return NewHugoSites(sites...) + return newHugoSites(sites...) } @@ -507,3 +507,7 @@ func newHugoSitesFromSourceAndLanguages(input []source.ByteSource, languages Lan func newHugoSitesFromLanguages(languages Languages) (*HugoSites, error) { return newHugoSitesFromSourceAndLanguages(nil, languages) } + +func newHugoSitesDefaultLanguage() (*HugoSites, error) { + return newHugoSitesFromSourceAndLanguages(nil, Languages{newDefaultLanguage()}) +} diff --git a/hugolib/hugo_sites_test.go b/hugolib/hugo_sites_test.go index 158b65124..9ccfed054 100644 --- a/hugolib/hugo_sites_test.go +++ b/hugolib/hugo_sites_test.go @@ -137,6 +137,12 @@ func TestMultiSitesBuild(t *testing.T) { require.NotNil(t, frTags["frtag1"]) readDestination(t, "public/fr/plaques/frtag1/index.html") readDestination(t, "public/en/tags/tag1/index.html") + + // Check Blackfriday config + assert.True(t, strings.Contains(string(doc1fr.Content), "«"), string(doc1fr.Content)) + assert.False(t, strings.Contains(string(doc1en.Content), "«"), string(doc1en.Content)) + assert.True(t, strings.Contains(string(doc1en.Content), "“"), string(doc1en.Content)) + } func TestMultiSitesRebuild(t *testing.T) { @@ -326,7 +332,6 @@ title = "Norsk" // Watching does not work with in-memory fs, so we trigger a reload manually require.NoError(t, viper.ReadInConfig()) - err = sites.Build(BuildCfg{CreateSitesFromConfig: true}) if err != nil { @@ -370,7 +375,10 @@ paginate = 2 DefaultContentLanguage = "fr" [permalinks] - other = "/somewhere/else/:filename" +other = "/somewhere/else/:filename" + +[blackfriday] +angledQuotes = true [Taxonomies] tag = "tags" @@ -379,6 +387,8 @@ tag = "tags" [Languages.en] weight = 10 title = "English" +[Languages.en.blackfriday] +angledQuotes = false [Languages.fr] weight = 20 @@ -441,7 +451,7 @@ tags: publishdate: "2000-01-01" --- # doc1 -*some content* +*some "content"* NOTE: slug should be used as URL `)}, {filepath.FromSlash("sect/doc1.fr.md"), []byte(`--- @@ -452,7 +462,7 @@ plaques: publishdate: "2000-01-04" --- # doc1 -*quelque contenu* +*quelque "contenu"* NOTE: should be in the 'en' Page's 'Translations' field. NOTE: date is after "doc3" `)}, diff --git a/hugolib/multilingual.go b/hugolib/multilingual.go index cc9f607f2..9c5342f99 100644 --- a/hugolib/multilingual.go +++ b/hugolib/multilingual.go @@ -25,9 +25,14 @@ func NewLanguage(lang string) *Language { return &Language{Lang: lang, params: make(map[string]interface{})} } -// TODO(bep) multilingo func newDefaultLanguage() *Language { - return NewLanguage("en") + defaultLang := viper.GetString("DefaultContentLanguage") + + if defaultLang == "" { + defaultLang = "en" + } + + return NewLanguage(defaultLang) } type Languages []*Language @@ -74,16 +79,18 @@ func newMultiLingualFromSites(sites ...*Site) (*Multilingual, error) { languages[i] = s.Language } - defaultLang := viper.GetString("DefaultContentLanguage") + return &Multilingual{Languages: languages, DefaultLang: newDefaultLanguage()}, nil - if defaultLang == "" { - defaultLang = "en" - } - - return &Multilingual{Languages: languages, DefaultLang: NewLanguage(defaultLang)}, nil +} +func newMultiLingualDefaultLanguage() *Multilingual { + return newMultiLingualForLanguage(newDefaultLanguage()) } +func newMultiLingualForLanguage(language *Language) *Multilingual { + languages := Languages{language} + return &Multilingual{Languages: languages, DefaultLang: language} +} func (ml *Multilingual) enabled() bool { return len(ml.Languages) > 1 } @@ -92,6 +99,7 @@ func (l *Language) Params() map[string]interface{} { l.paramsInit.Do(func() { // Merge with global config. // TODO(bep) consider making this part of a constructor func. + globalParams := viper.GetStringMap("Params") for k, v := range globalParams { if _, ok := l.params[k]; !ok { @@ -116,6 +124,9 @@ func (l *Language) GetStringMapString(key string) map[string]string { } func (l *Language) Get(key string) interface{} { + if l == nil { + panic("language not set") + } key = strings.ToLower(key) if v, ok := l.params[key]; ok { return v @@ -159,7 +170,7 @@ func toSortedLanguages(l map[string]interface{}) (Languages, error) { } // Put all into the Params map - // TODO(bep) reconsile with the type handling etc. from other params handlers. + // TODO(bep) ml reconsile with the type handling etc. from other params handlers. language.SetParam(loki, v) } diff --git a/hugolib/node.go b/hugolib/node.go index db1d16631..773573c3c 100644 --- a/hugolib/node.go +++ b/hugolib/node.go @@ -22,6 +22,8 @@ import ( "sync" "time" + jww "github.com/spf13/jwalterweatherman" + "github.com/spf13/hugo/helpers" "github.com/spf13/cast" @@ -45,8 +47,9 @@ type Node struct { paginatorInit sync.Once scratch *Scratch - language *Language - lang string // TODO(bep) multilingo + language *Language + languageInit sync.Once + lang string // TODO(bep) multilingo translations Nodes translationsInit sync.Once @@ -191,6 +194,7 @@ func (n *Node) Scratch() *Scratch { // TODO(bep) multilingo consolidate. See Page. func (n *Node) Language() *Language { + n.initLanguage() return n.language } @@ -204,6 +208,31 @@ func (n *Node) Lang() string { return n.lang } +func (n *Node) initLanguage() { + n.languageInit.Do(func() { + pageLang := n.lang + ml := n.Site.multilingual + if ml == nil { + panic("Multilanguage not set") + } + if pageLang == "" { + n.language = ml.DefaultLang + return + } + + language := ml.Language(pageLang) + + if language == nil { + // TODO(bep) ml + // This may or may not be serious. It can be a file named stefano.chiodino.md. + jww.WARN.Printf("Page language (if it is that) not found in multilang setup: %s.", pageLang) + language = ml.DefaultLang + } + + n.language = language + }) +} + func (n *Node) LanguagePrefix() string { return n.Site.LanguagePrefix } @@ -261,7 +290,7 @@ func (n *Node) addMultilingualWebPrefix(outfile string) string { hadSlashSuffix := strings.HasSuffix(outfile, "/") lang := n.Lang() - if lang == "" || !n.Site.Multilingual { + if lang == "" || !n.Site.IsMultiLingual() { return outfile } outfile = "/" + path.Join(lang, outfile) @@ -273,7 +302,7 @@ func (n *Node) addMultilingualWebPrefix(outfile string) string { func (n *Node) addMultilingualFilesystemPrefix(outfile string) string { lang := n.Lang() - if lang == "" || !n.Site.Multilingual { + if lang == "" || !n.Site.IsMultiLingual() { return outfile } return string(filepath.Separator) + filepath.Join(lang, outfile) diff --git a/hugolib/page.go b/hugolib/page.go index 493e5b512..99aa0db16 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -319,7 +319,8 @@ func (p *Page) setAutoSummary() error { return nil } -func (p *Page) renderBytes(content []byte) []byte { +// TODO(bep) ml not used??? +func (p *Page) _renderBytes(content []byte) []byte { var fn helpers.LinkResolverFunc var fileFn helpers.FileResolverFunc if p.getRenderingConfig().SourceRelativeLinksEval { @@ -331,8 +332,10 @@ func (p *Page) renderBytes(content []byte) []byte { } } return helpers.RenderBytes( - &helpers.RenderingContext{Content: content, PageFmt: p.determineMarkupType(), - DocumentID: p.UniqueID(), Config: p.getRenderingConfig(), LinkResolver: fn, FileResolver: fileFn}) + &helpers.RenderingContext{ + Content: content, PageFmt: p.determineMarkupType(), + ConfigProvider: p.Language(), + DocumentID: p.UniqueID(), Config: p.getRenderingConfig(), LinkResolver: fn, FileResolver: fileFn}) } func (p *Page) renderContent(content []byte) []byte { @@ -346,16 +349,20 @@ func (p *Page) renderContent(content []byte) []byte { return p.Node.Site.SourceRelativeLinkFile(ref, p) } } - return helpers.RenderBytes(&helpers.RenderingContext{Content: content, RenderTOC: true, PageFmt: p.determineMarkupType(), - DocumentID: p.UniqueID(), Config: p.getRenderingConfig(), LinkResolver: fn, FileResolver: fileFn}) + return helpers.RenderBytes(&helpers.RenderingContext{ + Content: content, RenderTOC: true, PageFmt: p.determineMarkupType(), + ConfigProvider: p.Language(), + DocumentID: p.UniqueID(), Config: p.getRenderingConfig(), LinkResolver: fn, FileResolver: fileFn}) } func (p *Page) getRenderingConfig() *helpers.Blackfriday { p.renderingConfigInit.Do(func() { pageParam := cast.ToStringMap(p.GetParam("blackfriday")) - - p.renderingConfig = helpers.NewBlackfriday() + if p.Language() == nil { + panic(fmt.Sprintf("nil language for %s with source lang %s", p.BaseFileName(), p.lang)) + } + p.renderingConfig = helpers.NewBlackfriday(p.Language()) if err := mapstructure.Decode(pageParam, p.renderingConfig); err != nil { jww.FATAL.Printf("Failed to get rendering config for %s:\n%s", p.BaseFileName(), err.Error()) } diff --git a/hugolib/pageSort_test.go b/hugolib/pageSort_test.go index 7f0e89091..1ed99f318 100644 --- a/hugolib/pageSort_test.go +++ b/hugolib/pageSort_test.go @@ -141,9 +141,7 @@ func createSortTestPages(num int) Pages { Section: "z", URL: fmt.Sprintf("http://base/x/y/p%d.html", i), }, - Site: &SiteInfo{ - BaseURL: "http://base/", - }, + Site: newSiteInfoDefaultLanguage("http://base/"), }, Source: Source{File: *source.NewFile(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i)))}, } diff --git a/hugolib/page_permalink_test.go b/hugolib/page_permalink_test.go index a47bad85e..d185bebab 100644 --- a/hugolib/page_permalink_test.go +++ b/hugolib/page_permalink_test.go @@ -69,9 +69,7 @@ func TestPermalink(t *testing.T) { Section: "z", URL: test.url, }, - Site: &SiteInfo{ - BaseURL: test.base, - }, + Site: newSiteInfoDefaultLanguage(string(test.base)), }, Source: Source{File: *source.NewFile(filepath.FromSlash(test.file))}, } diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 6927e6e82..da46151ed 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -1117,7 +1117,7 @@ func TestPagePaths(t *testing.T) { for _, test := range tests { p, _ := NewPageFrom(strings.NewReader(test.content), filepath.FromSlash(test.path)) - p.Node.Site = &SiteInfo{} + p.Node.Site = newSiteInfoDefaultLanguage("") if test.hasPermalink { p.Node.Site.Permalinks = siteParmalinksSetting diff --git a/hugolib/pagination_test.go b/hugolib/pagination_test.go index b67f5dce5..df2094d63 100644 --- a/hugolib/pagination_test.go +++ b/hugolib/pagination_test.go @@ -460,9 +460,7 @@ func createTestPages(num int) Pages { Section: "z", URL: fmt.Sprintf("http://base/x/y/p%d.html", i), }, - Site: &SiteInfo{ - BaseURL: "http://base/", - }, + Site: newSiteInfoDefaultLanguage("http://base/"), }, Source: Source{File: *source.NewFile(filepath.FromSlash(fmt.Sprintf("/x/y/p%d.md", i)))}, } diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go index b63ba4a49..52cd6893b 100644 --- a/hugolib/shortcode.go +++ b/hugolib/shortcode.go @@ -242,7 +242,8 @@ func renderShortcode(sc shortcode, parent *ShortcodeWithPage, p *Page, t tpl.Tem if sc.doMarkup { newInner := helpers.RenderBytes(&helpers.RenderingContext{ Content: []byte(inner), PageFmt: p.determineMarkupType(), - DocumentID: p.UniqueID(), Config: p.getRenderingConfig()}) + ConfigProvider: p.Language(), + DocumentID: p.UniqueID(), Config: p.getRenderingConfig()}) // If the type is “unknown” or “markdown”, we assume the markdown // generation has been performed. Given the input: `a line`, markdown diff --git a/hugolib/shortcode_test.go b/hugolib/shortcode_test.go index dd16bff07..1b5c6c847 100644 --- a/hugolib/shortcode_test.go +++ b/hugolib/shortcode_test.go @@ -28,29 +28,59 @@ import ( "github.com/spf13/hugo/target" "github.com/spf13/hugo/tpl" "github.com/spf13/viper" + "github.com/stretchr/testify/require" ) +// TODO(bep) remove func pageFromString(in, filename string) (*Page, error) { return NewPageFrom(strings.NewReader(in), filename) } -func CheckShortCodeMatch(t *testing.T, input, expected string, template tpl.Template) { - CheckShortCodeMatchAndError(t, input, expected, template, false) +func CheckShortCodeMatch(t *testing.T, input, expected string, withTemplate func(templ tpl.Template) error) { + CheckShortCodeMatchAndError(t, input, expected, withTemplate, false) } -func CheckShortCodeMatchAndError(t *testing.T, input, expected string, template tpl.Template, expectError bool) { +func CheckShortCodeMatchAndError(t *testing.T, input, expected string, withTemplate func(templ tpl.Template) error, expectError bool) { + testCommonResetState() - p, _ := pageFromString(simplePage, "simple.md") - output, err := HandleShortcodes(input, p, template) + // Need some front matter, see https://github.com/spf13/hugo/issues/2337 + contentFile := `--- +title: "Title" +--- +` + input + + writeSource(t, "content/simple.md", contentFile) + + h, err := newHugoSitesDefaultLanguage() + + if err != nil { + t.Fatalf("Failed to create sites: %s", err) + } + + cfg := BuildCfg{SkipRender: true, withTemplate: withTemplate} + + err = h.Build(cfg) if err != nil && !expectError { - t.Fatalf("Shortcode rendered error %s. Expected: %q, Got: %q", err, expected, output) + t.Fatalf("Shortcode rendered error %s.", err) } if err == nil && expectError { t.Fatalf("No error from shortcode") } + require.Len(t, h.Sites[0].Pages, 1) + + output := strings.TrimSpace(string(h.Sites[0].Pages[0].Content)) + if strings.HasPrefix(output, "<p>") { + output = output[3:] + } + if strings.HasSuffix(output, "</p>") { + output = output[:len(output)-4] + } + + expected = strings.TrimSpace(expected) + if output != expected { t.Fatalf("Shortcode render didn't match. got \n%q but expected \n%q", output, expected) } @@ -86,115 +116,123 @@ func TestShortcodeGoFuzzReports(t *testing.T) { } func TestNonSC(t *testing.T) { - tem := tpl.New() + // notice the syntax diff from 0.12, now comment delims must be added - CheckShortCodeMatch(t, "{{%/* movie 47238zzb */%}}", "{{% movie 47238zzb %}}", tem) + CheckShortCodeMatch(t, "{{%/* movie 47238zzb */%}}", "{{% movie 47238zzb %}}", nil) } // Issue #929 func TestHyphenatedSC(t *testing.T) { - tem := tpl.New() - tem.AddInternalShortcode("hyphenated-video.html", `Playing Video {{ .Get 0 }}`) + wt := func(tem tpl.Template) error { + tem.AddInternalShortcode("hyphenated-video.html", `Playing Video {{ .Get 0 }}`) + return nil + } - CheckShortCodeMatch(t, "{{< hyphenated-video 47238zzb >}}", "Playing Video 47238zzb", tem) + CheckShortCodeMatch(t, "{{< hyphenated-video 47238zzb >}}", "Playing Video 47238zzb", wt) } // Issue #1753 func TestNoTrailingNewline(t *testing.T) { - tem := tpl.New() - tem.AddInternalShortcode("a.html", `{{ .Get 0 }}`) + wt := func(tem tpl.Template) error { + tem.AddInternalShortcode("a.html", `{{ .Get 0 }}`) + return nil + } - CheckShortCodeMatch(t, "ab{{< a c >}}d", "abcd", tem) + CheckShortCodeMatch(t, "ab{{< a c >}}d", "abcd", wt) } func TestPositionalParamSC(t *testing.T) { - tem := tpl.New() - tem.AddInternalShortcode("video.html", `Playing Video {{ .Get 0 }}`) + wt := func(tem tpl.Template) error { + tem.AddInternalShortcode("video.html", `Playing Video {{ .Get 0 }}`) + return nil + } - 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) + CheckShortCodeMatch(t, "{{< video 47238zzb >}}", "Playing Video 47238zzb", wt) + CheckShortCodeMatch(t, "{{< video 47238zzb 132 >}}", "Playing Video 47238zzb", wt) + CheckShortCodeMatch(t, "{{<video 47238zzb>}}", "Playing Video 47238zzb", wt) + CheckShortCodeMatch(t, "{{<video 47238zzb >}}", "Playing Video 47238zzb", wt) + CheckShortCodeMatch(t, "{{< video 47238zzb >}}", "Playing Video 47238zzb", wt) } func TestPositionalParamIndexOutOfBounds(t *testing.T) { - tem := tpl.New() - tem.AddInternalShortcode("video.html", `Playing Video {{ .Get 1 }}`) - CheckShortCodeMatch(t, "{{< video 47238zzb >}}", "Playing Video error: index out of range for positional param at position 1", tem) + wt := func(tem tpl.Template) error { + tem.AddInternalShortcode("video.html", `Playing Video {{ .Get 1 }}`) + return nil + } + CheckShortCodeMatch(t, "{{< video 47238zzb >}}", "Playing Video error: index out of range for positional param at position 1", wt) } // some repro issues for panics in Go Fuzz testing -func TestShortcodeGoFuzzRepros(t *testing.T) { - tt := tpl.New() - tt.AddInternalShortcode("inner.html", `Shortcode... {{ with .Get 0 }}{{ . }}{{ end }}-- {{ with .Get 1 }}{{ . }}{{ end }}- {{ with .Inner }}{{ . }}{{ end }}`) - // Issue #1337 - CheckShortCodeMatchAndError(t, "{{%inner\"\"\"\"=\"\"", "", tt, true) -} 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) + wt := func(tem tpl.Template) error { + tem.AddInternalShortcode("img.html", `<img{{ with .Get "src" }} src="{{.}}"{{end}}{{with .Get "class"}} class="{{.}}"{{end}}>`) + return nil + } + CheckShortCodeMatch(t, `{{< img src="one" >}}`, `<img src="one">`, wt) + CheckShortCodeMatch(t, `{{< img class="aspen" >}}`, `<img class="aspen">`, wt) + CheckShortCodeMatch(t, `{{< img src= "one" >}}`, `<img src="one">`, wt) + CheckShortCodeMatch(t, `{{< img src ="one" >}}`, `<img src="one">`, wt) + CheckShortCodeMatch(t, `{{< img src = "one" >}}`, `<img src="one">`, wt) + CheckShortCodeMatch(t, `{{< img src = "one" class = "aspen grove" >}}`, `<img src="one" class="aspen grove">`, wt) } // 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>`) - + wt := func(tem tpl.Template) error { + 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>`) + return nil + } CheckShortCodeMatch(t, `{{% acc %}}{{% div %}}d1{{% /div %}}{{% div2 %}}d2{{% /div2 %}}{{% /acc %}}`, - "<div class=\"acc\"><div >d1</div><div >d2</div>\n</div>", tem) + "<div class=\"acc\"><div >d1</div><div >d2</div>\n</div>", wt) } func TestIsNamedParamsSC(t *testing.T) { - tem := tpl.New() - tem.AddInternalShortcode("byposition.html", `<div id="{{ .Get 0 }}">`) - tem.AddInternalShortcode("byname.html", `<div id="{{ .Get "id" }}">`) - tem.AddInternalShortcode("ifnamedparams.html", `<div id="{{ if .IsNamedParams }}{{ .Get "id" }}{{ else }}{{ .Get 0 }}{{end}}">`) - - CheckShortCodeMatch(t, `{{< ifnamedparams id="name" >}}`, `<div id="name">`, tem) - CheckShortCodeMatch(t, `{{< ifnamedparams position >}}`, `<div id="position">`, tem) - CheckShortCodeMatch(t, `{{< byname id="name" >}}`, `<div id="name">`, tem) - CheckShortCodeMatch(t, `{{< byname position >}}`, `<div id="error: cannot access positional params by string name">`, tem) - CheckShortCodeMatch(t, `{{< byposition position >}}`, `<div id="position">`, tem) - CheckShortCodeMatch(t, `{{< byposition id="name" >}}`, `<div id="error: cannot access named params by position">`, tem) + wt := func(tem tpl.Template) error { + tem.AddInternalShortcode("byposition.html", `<div id="{{ .Get 0 }}">`) + tem.AddInternalShortcode("byname.html", `<div id="{{ .Get "id" }}">`) + tem.AddInternalShortcode("ifnamedparams.html", `<div id="{{ if .IsNamedParams }}{{ .Get "id" }}{{ else }}{{ .Get 0 }}{{end}}">`) + return nil + } + CheckShortCodeMatch(t, `{{< ifnamedparams id="name" >}}`, `<div id="name">`, wt) + CheckShortCodeMatch(t, `{{< ifnamedparams position >}}`, `<div id="position">`, wt) + CheckShortCodeMatch(t, `{{< byname id="name" >}}`, `<div id="name">`, wt) + CheckShortCodeMatch(t, `{{< byname position >}}`, `<div id="error: cannot access positional params by string name">`, wt) + CheckShortCodeMatch(t, `{{< byposition position >}}`, `<div id="position">`, wt) + CheckShortCodeMatch(t, `{{< byposition id="name" >}}`, `<div id="error: cannot access named params by position">`, wt) } 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) + wt := func(tem tpl.Template) error { + tem.AddInternalShortcode("inside.html", `<div{{with .Get "class"}} class="{{.}}"{{end}}>{{ .Inner }}</div>`)< |