Skip to content

Commit

Permalink
compat(renderer): table-caption and table-number handling wrong
Browse files Browse the repository at this point in the history
The code integrated to handle customizable table-caption had two
bugs.  First the table-caption should not include the number -
asciidoctor automatically suffixes that.  Second, the counter is
called table-number, not table-counter.
  • Loading branch information
gdamore authored and xcoulon committed Jul 15, 2020
1 parent 5c0d5ca commit 099fe89
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
42 changes: 42 additions & 0 deletions pkg/renderer/sgml/html5/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,48 @@ var _ = Describe("tables", func() {
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("2 tables with custom caption label", func() {
source := `:table-caption: Chart
.First
|===
| foo | bar
|===
.Second
|===
| foo | bar
|===`
expected := `<table class="tableblock frame-all grid-all stretch">
<caption class="title">Chart 1. First</caption>
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">foo</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">bar</p></td>
</tr>
</tbody>
</table>
<table class="tableblock frame-all grid-all stretch">
<caption class="title">Chart 2. Second</caption>
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">foo</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">bar</p></td>
</tr>
</tbody>
</table>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("autowidth ", func() {
source := "[%autowidth]\n|===\n|==="
expected := `<table class="tableblock frame-all grid-all fit-content">
Expand Down
9 changes: 7 additions & 2 deletions pkg/renderer/sgml/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,20 @@ func (r *sgmlRenderer) renderTable(ctx *renderer.Context, t types.Table) (string
c, ok := t.Attributes.GetAsString(types.AttrCaption)
if !ok {
c, _ = ctx.Attributes.GetAsString(types.AttrTableCaption)
if c != "" {
// We always append the figure number, unless the caption is disabled.
// This is for asciidoctor compatibility.
c += " {counter:table-number}. "
}
}

// TODO: This is a very primitive and incomplete replacement of the counter attribute only.
// This should be removed when attribute values are allowed to contain attributes.
// Also this expansion should be limited to just singly quoted strings in the Attribute list,
// or the default. Ultimately this should all be done long before it gets into the renderer.
if strings.Contains(c, "{counter:table-counter}") {
if strings.Contains(c, "{counter:table-number}") {
number = ctx.GetAndIncrementTableCounter()
c = strings.ReplaceAll(c, "{counter:table-counter}", strconv.Itoa(number))
c = strings.ReplaceAll(c, "{counter:table-number}", strconv.Itoa(number))
}
caption.WriteString(c)
}
Expand Down
42 changes: 42 additions & 0 deletions pkg/renderer/sgml/xhtml5/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,48 @@ var _ = Describe("tables", func() {
Expect(RenderXHTML(source)).To(MatchHTML(expected))
})

It("2 tables with custom caption label", func() {
source := `:table-caption: Chart
.First
|===
| foo | bar
|===
.Second
|===
| foo | bar
|===`
expected := `<table class="tableblock frame-all grid-all stretch">
<caption class="title">Chart 1. First</caption>
<colgroup>
<col style="width: 50%;"/>
<col style="width: 50%;"/>
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">foo</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">bar</p></td>
</tr>
</tbody>
</table>
<table class="tableblock frame-all grid-all stretch">
<caption class="title">Chart 2. Second</caption>
<colgroup>
<col style="width: 50%;"/>
<col style="width: 50%;"/>
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">foo</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">bar</p></td>
</tr>
</tbody>
</table>
`
Expect(RenderXHTML(source)).To(MatchHTML(expected))
})

It("2 tables with 2 counters", func() {
source := `.Title 1
|===
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/predefined_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func init() {
"two-semicolons": ";",
"cpp": "C++",
AttrVersionLabel: "version",
AttrTableCaption: "Table {counter:table-counter}. ",
AttrTableCaption: "Table",
AttrFigureCaption: "Figure",
AttrCautionCaption: "Caution",
AttrImportantCaption: "Important",
Expand Down

0 comments on commit 099fe89

Please sign in to comment.