forked from bytesparadise/libasciidoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parser/renderer): add user macro feature (bytesparadise#347)
Fixes bytesparadise#334
- Loading branch information
Showing
10 changed files
with
24,995 additions
and
22,651 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package parser_test | ||
|
||
import ( | ||
"github.com/bytesparadise/libasciidoc/pkg/parser" | ||
"github.com/bytesparadise/libasciidoc/pkg/types" | ||
. "github.com/onsi/ginkgo" | ||
) | ||
|
||
var _ = Describe("user macros", func() { | ||
|
||
Context("user macros", func() { | ||
|
||
It("user block macro", func() { | ||
actualContent := "git::some/url.git[key1=value1,key2=value2]" | ||
expectedResult := types.UserMacro{ | ||
Kind: types.BlockMacro, | ||
Name: "git", | ||
Value: "some/url.git", | ||
Attributes: types.ElementAttributes{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
}, | ||
RawText: "git::some/url.git[key1=value1,key2=value2]", | ||
} | ||
verifyWithPreprocessing(GinkgoT(), expectedResult, actualContent, parser.Entrypoint("DocumentBlock")) | ||
}) | ||
|
||
It("inline user macro", func() { | ||
actualContent := "repository: git:some/url.git[key1=value1,key2=value2]" | ||
expectedResult := types.Paragraph{ | ||
Attributes: types.ElementAttributes{}, | ||
Lines: []types.InlineElements{ | ||
{ | ||
types.StringElement{ | ||
Content: "repository: ", | ||
}, | ||
types.UserMacro{ | ||
Kind: types.InlineMacro, | ||
Name: "git", | ||
Value: "some/url.git", | ||
Attributes: types.ElementAttributes{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
}, | ||
RawText: "git:some/url.git[key1=value1,key2=value2]", | ||
}, | ||
}, | ||
}, | ||
} | ||
verifyWithPreprocessing(GinkgoT(), expectedResult, actualContent, parser.Entrypoint("DocumentBlock")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package html5 | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/bytesparadise/libasciidoc/pkg/renderer" | ||
"github.com/bytesparadise/libasciidoc/pkg/types" | ||
) | ||
|
||
func renderUserMacro(ctx *renderer.Context, um types.UserMacro) ([]byte, error) { | ||
buf := bytes.NewBuffer([]byte{}) | ||
macro, err := ctx.MacroTemplate(um.Name) | ||
if err != nil { | ||
if um.Kind == types.BlockMacro { | ||
// fallback to paragraph | ||
p, _ := types.NewParagraph([]interface{}{ | ||
types.InlineElements{ | ||
types.StringElement{Content: um.RawText}, | ||
}, | ||
}, nil) | ||
return renderParagraph(ctx, p) | ||
} | ||
// fallback to render raw text | ||
_, err = buf.WriteString(um.RawText) | ||
} else { | ||
err = macro.Execute(buf, um) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package html5_test | ||
|
||
import ( | ||
"html" | ||
texttemplate "text/template" | ||
|
||
"github.com/bytesparadise/libasciidoc/pkg/renderer" | ||
. "github.com/onsi/ginkgo" | ||
) | ||
|
||
var helloMacroTmpl *texttemplate.Template | ||
|
||
var _ = Describe("user macros", func() { | ||
|
||
Context("user macros", func() { | ||
It("undefined macro block", func() { | ||
|
||
actualContent := "hello::[]" | ||
expectedResult := `<div class="paragraph"> | ||
<p>hello::[]</p> | ||
</div>` | ||
verify(GinkgoT(), expectedResult, actualContent) | ||
}) | ||
|
||
It("user macro block", func() { | ||
|
||
actualContent := "hello::[]" | ||
expectedResult := `<div class="helloblock"> | ||
<div class="content"> | ||
<span>hello world</span> | ||
</div> | ||
</div>` | ||
verify(GinkgoT(), expectedResult, actualContent, renderer.DefineMacro(helloMacroTmpl.Name(), helloMacroTmpl)) | ||
}) | ||
|
||
It("user macro block with attribute", func() { | ||
|
||
actualContent := `hello::[suffix="!!!!"]` | ||
expectedResult := `<div class="helloblock"> | ||
<div class="content"> | ||
<span>hello world!!!!</span> | ||
</div> | ||
</div>` | ||
verify(GinkgoT(), expectedResult, actualContent, renderer.DefineMacro(helloMacroTmpl.Name(), helloMacroTmpl)) | ||
}) | ||
|
||
It("user macro block with value", func() { | ||
|
||
actualContent := `hello::John Doe[]` | ||
expectedResult := `<div class="helloblock"> | ||
<div class="content"> | ||
<span>hello John Doe</span> | ||
</div> | ||
</div>` | ||
verify(GinkgoT(), expectedResult, actualContent, renderer.DefineMacro(helloMacroTmpl.Name(), helloMacroTmpl)) | ||
}) | ||
|
||
It("user macro block with value and attributes", func() { | ||
|
||
actualContent := `hello::John Doe[prefix="Hi ",suffix="!!"]` | ||
expectedResult := `<div class="helloblock"> | ||
<div class="content"> | ||
<span>Hi John Doe!!</span> | ||
</div> | ||
</div>` | ||
verify(GinkgoT(), expectedResult, actualContent, renderer.DefineMacro(helloMacroTmpl.Name(), helloMacroTmpl)) | ||
}) | ||
|
||
It("undefined inline macro", func() { | ||
|
||
actualContent := "hello:[]" | ||
expectedResult := `<div class="paragraph"> | ||
<p>hello:[]</p> | ||
</div>` | ||
verify(GinkgoT(), expectedResult, actualContent) | ||
}) | ||
|
||
It("inline macro", func() { | ||
|
||
actualContent := "AAA hello:[]" | ||
expectedResult := `<div class="paragraph"> | ||
<p>AAA <span>hello world</span></p> | ||
</div>` | ||
verify(GinkgoT(), expectedResult, actualContent, renderer.DefineMacro(helloMacroTmpl.Name(), helloMacroTmpl)) | ||
}) | ||
|
||
It("inline macro with attribute", func() { | ||
|
||
actualContent := `AAA hello:[suffix="!!!!!"]` | ||
expectedResult := `<div class="paragraph"> | ||
<p>AAA <span>hello world!!!!!</span></p> | ||
</div>` | ||
verify(GinkgoT(), expectedResult, actualContent, renderer.DefineMacro(helloMacroTmpl.Name(), helloMacroTmpl)) | ||
}) | ||
|
||
It("inline macro with value", func() { | ||
|
||
actualContent := `AAA hello:John Doe[]` | ||
expectedResult := `<div class="paragraph"> | ||
<p>AAA <span>hello John Doe</span></p> | ||
</div>` | ||
verify(GinkgoT(), expectedResult, actualContent, renderer.DefineMacro(helloMacroTmpl.Name(), helloMacroTmpl)) | ||
}) | ||
|
||
It("inline macro with value and attributes", func() { | ||
|
||
actualContent := `AAA hello:John Doe[prefix="Hi ",suffix="!!"]` | ||
expectedResult := `<div class="paragraph"> | ||
<p>AAA <span>Hi John Doe!!</span></p> | ||
</div>` | ||
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 -}}`)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.