Skip to content

Commit

Permalink
fix(renderer): move the content of hello macro template into a string…
Browse files Browse the repository at this point in the history
… variable
  • Loading branch information
odknt committed May 14, 2019
1 parent da0167a commit 19f16ab
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
13 changes: 0 additions & 13 deletions pkg/renderer/html5/_macros/hello

This file was deleted.

54 changes: 30 additions & 24 deletions pkg/renderer/html5/user_macro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,12 @@ import (
. "github.com/onsi/ginkgo"
)

// an example for find and define user macro
func findMacroTemplate() []renderer.Option {
t := texttemplate.New("user macro")
t.Funcs(texttemplate.FuncMap{
"escape": html.EscapeString,
})
tmpl := texttemplate.Must(t.ParseGlob("_macros/*"))
tmpls := tmpl.Templates()
opts := make([]renderer.Option, len(tmpls))
for i, tt := range tmpls {
opts[i] = renderer.DefineMacro(tt.Name(), tt)
}
return opts
}
var macroOption renderer.Option
var helloMacroTmpl *texttemplate.Template

var _ = Describe("user macros", func() {

Context("user macros", func() {
opts := findMacroTemplate()

It("undefined macro block", func() {

actualContent := "hello::[]"
Expand All @@ -45,7 +31,7 @@ var _ = Describe("user macros", func() {
<span>hello world</span>
</div>
</div>`
verify(GinkgoT(), expectedResult, actualContent, opts...)
verify(GinkgoT(), expectedResult, actualContent, renderer.DefineMacro(helloMacroTmpl.Name(), helloMacroTmpl))
})

It("user macro block with attribute", func() {
Expand All @@ -56,7 +42,7 @@ var _ = Describe("user macros", func() {
<span>hello world!!!!</span>
</div>
</div>`
verify(GinkgoT(), expectedResult, actualContent, opts...)
verify(GinkgoT(), expectedResult, actualContent, renderer.DefineMacro(helloMacroTmpl.Name(), helloMacroTmpl))
})

It("user macro block with value", func() {
Expand All @@ -67,7 +53,7 @@ var _ = Describe("user macros", func() {
<span>hello John Doe</span>
</div>
</div>`
verify(GinkgoT(), expectedResult, actualContent, opts...)
verify(GinkgoT(), expectedResult, actualContent, renderer.DefineMacro(helloMacroTmpl.Name(), helloMacroTmpl))
})

It("user macro block with value and attributes", func() {
Expand All @@ -78,7 +64,7 @@ var _ = Describe("user macros", func() {
<span>Hi John Doe!!</span>
</div>
</div>`
verify(GinkgoT(), expectedResult, actualContent, opts...)
verify(GinkgoT(), expectedResult, actualContent, renderer.DefineMacro(helloMacroTmpl.Name(), helloMacroTmpl))
})

It("undefined inline macro", func() {
Expand All @@ -96,7 +82,7 @@ var _ = Describe("user macros", func() {
expectedResult := `<div class="paragraph">
<p>AAA <span>hello world</span></p>
</div>`
verify(GinkgoT(), expectedResult, actualContent, opts...)
verify(GinkgoT(), expectedResult, actualContent, renderer.DefineMacro(helloMacroTmpl.Name(), helloMacroTmpl))
})

It("inline macro with attribute", func() {
Expand All @@ -105,7 +91,7 @@ var _ = Describe("user macros", func() {
expectedResult := `<div class="paragraph">
<p>AAA <span>hello world!!!!!</span></p>
</div>`
verify(GinkgoT(), expectedResult, actualContent, opts...)
verify(GinkgoT(), expectedResult, actualContent, renderer.DefineMacro(helloMacroTmpl.Name(), helloMacroTmpl))
})

It("inline macro with value", func() {
Expand All @@ -114,7 +100,7 @@ var _ = Describe("user macros", func() {
expectedResult := `<div class="paragraph">
<p>AAA <span>hello John Doe</span></p>
</div>`
verify(GinkgoT(), expectedResult, actualContent, opts...)
verify(GinkgoT(), expectedResult, actualContent, renderer.DefineMacro(helloMacroTmpl.Name(), helloMacroTmpl))
})

It("inline macro with value and attributes", func() {
Expand All @@ -123,8 +109,28 @@ var _ = Describe("user macros", func() {
expectedResult := `<div class="paragraph">
<p>AAA <span>Hi John Doe!!</span></p>
</div>`
verify(GinkgoT(), expectedResult, actualContent, opts...)
verify(GinkgoT(), expectedResult, actualContent, renderer.DefineMacro(helloMacroTmpl.Name(), helloMacroTmpl))
})

})
})

func init() {
t := texttemplate.New("hello")
t.Funcs(texttemplate.FuncMap{
"escape": html.EscapeString,
})
helloMacroTmpl = texttemplate.Must(t.Parse(`{{- if eq .Kind "block" -}}
<div class="helloblock">
<div class="content">
{{end -}}
<span>
{{- if .Attributes.Has "prefix"}}{{escape (.Attributes.GetAsString "prefix")}} {{else}}hello {{end -}}
{{- if ne .Value ""}}{{escape .Value}}{{else}}world{{- end -}}
{{- escape (.Attributes.GetAsString "suffix") -}}
</span>
{{- if eq .Kind "block"}}
</div>
</div>
{{- end -}}`))
}

0 comments on commit 19f16ab

Please sign in to comment.