Skip to content

Commit

Permalink
feature(renderer): Example blocks should support custom captions
Browse files Browse the repository at this point in the history
This introduces custom captions to example blocks, and it works
just like asciidoctor, including the global example-caption
and counter:example-number attributes.
  • Loading branch information
gdamore authored and xcoulon committed Jul 15, 2020
1 parent 099fe89 commit 5dd3224
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 2 deletions.
19 changes: 18 additions & 1 deletion pkg/renderer/sgml/delimited_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,18 +306,34 @@ func (r *sgmlRenderer) renderExampleBlock(ctx *renderer.Context, b types.Delimit
return r.renderAdmonitionBlock(ctx, b)
}
result := &strings.Builder{}
caption := &strings.Builder{}

// default, example block
number := ctx.GetAndIncrementExampleBlockCounter()
number := 0
elements := b.Elements
content, err := r.renderElements(ctx, elements)
if err != nil {
return "", errors.Wrap(err, "unable to render example block content")
}

c, ok := b.Attributes.GetAsString(types.AttrCaption)
if !ok {
c, _ = ctx.Attributes.GetAsString(types.AttrExampleCaption)
if c != "" {
c += " {counter:example-number}. "
}
}
// TODO: Replace this hack when we have attribute substitution fully working
if strings.Contains(c, "{counter:example-number}") {
number = ctx.GetAndIncrementExampleBlockCounter()
c = strings.ReplaceAll(c, "{counter:example-number}", strconv.Itoa(number))
}
caption.WriteString(c)
err = r.exampleBlock.Execute(result, struct {
Context *renderer.Context
ID sanitized
Title sanitized
Caption string
Roles sanitized
ExampleNumber int
Content sanitized
Expand All @@ -326,6 +342,7 @@ func (r *sgmlRenderer) renderExampleBlock(ctx *renderer.Context, b types.Delimit
Context: ctx,
ID: r.renderElementID(b.Attributes),
Title: r.renderElementTitle(b.Attributes),
Caption: caption.String(),
Roles: r.renderElementRoles(b.Attributes),
ExampleNumber: number,
Content: sanitized(content),
Expand Down
2 changes: 1 addition & 1 deletion pkg/renderer/sgml/html5/delimited_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (

exampleBlockTmpl = `<div {{ if .ID }}id="{{ .ID }}" {{ end }}` +
"class=\"exampleblock{{ if .Roles }} {{ .Roles }}{{ end }}\">\n" +
"{{ if .Title }}<div class=\"title\">Example {{ .ExampleNumber }}. {{ .Title }}</div>\n{{ end }}" +
"{{ if .Title }}<div class=\"title\">{{ .Caption }}{{ .Title }}</div>\n{{ end }}" +
"<div class=\"content\">\n" +
"{{ .Content }}" +
"</div>\n" +
Expand Down
58 changes: 58 additions & 0 deletions pkg/renderer/sgml/html5/delimited_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,64 @@ foo
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("example block with custom caption and title", func() {
source := `[caption="Caption A. "]
.example block title
====
foo
====`
expected := `<div class="exampleblock">
<div class="title">Caption A. example block title</div>
<div class="content">
<div class="paragraph">
<p>foo</p>
</div>
</div>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})
It("example block with custom global caption and title", func() {
source := `:example-caption: Caption
.example block title
====
foo
====`
expected := `<div class="exampleblock">
<div class="title">Caption 1. example block title</div>
<div class="content">
<div class="paragraph">
<p>foo</p>
</div>
</div>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})
It("example block with suppressed caption and title", func() {
source := `:example-caption!:
.example block title
====
foo
====`
expected := `<div class="exampleblock">
<div class="title">example block title</div>
<div class="content">
<div class="paragraph">
<p>foo</p>
</div>
</div>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

})

Context("admonition blocks", func() {
Expand Down
58 changes: 58 additions & 0 deletions pkg/renderer/sgml/xhtml5/delimited_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,64 @@ foo
`
Expect(RenderXHTML(source)).To(MatchHTML(expected))
})

It("example block with custom caption and title", func() {
source := `[caption="Caption A. "]
.example block title
====
foo
====`
expected := `<div class="exampleblock">
<div class="title">Caption A. example block title</div>
<div class="content">
<div class="paragraph">
<p>foo</p>
</div>
</div>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})
It("example block with custom global caption and title", func() {
source := `:example-caption: Caption
.example block title
====
foo
====`
expected := `<div class="exampleblock">
<div class="title">Caption 1. example block title</div>
<div class="content">
<div class="paragraph">
<p>foo</p>
</div>
</div>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})
It("example block with suppressed caption and title", func() {
source := `:example-caption!:
.example block title
====
foo
====`
expected := `<div class="exampleblock">
<div class="title">example block title</div>
<div class="content">
<div class="paragraph">
<p>foo</p>
</div>
</div>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

})

Context("admonition blocks", func() {
Expand Down
2 changes: 2 additions & 0 deletions pkg/types/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ const (
AttrPositional3 = "@3"
// AttrVersionLabel labels the version number in the document
AttrVersionLabel = "version-label"
// AttrExampleCaption is the example caption
AttrExampleCaption = "example-caption"
// AttrFigureCaption is the figure (image) caption
AttrFigureCaption = "figure-caption"
// AttrTableCaption is the table caption
Expand Down
1 change: 1 addition & 0 deletions pkg/types/predefined_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func init() {
"two-semicolons": ";",
"cpp": "C++",
AttrVersionLabel: "version",
AttrExampleCaption: "Example",
AttrTableCaption: "Table",
AttrFigureCaption: "Figure",
AttrCautionCaption: "Caution",
Expand Down

0 comments on commit 5dd3224

Please sign in to comment.