Skip to content

Commit

Permalink
fix(renderer): fix table numbering when title is included (#166)
Browse files Browse the repository at this point in the history
fixes #166 

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Aug 16, 2018
1 parent ae07458 commit 7f3a6e0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 22 deletions.
32 changes: 16 additions & 16 deletions pkg/renderer/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,6 @@ func (ctx *Context) IncludeBlankLine() bool {
return false
}

// const trimTrailingSpaces string = "trimTrailingSpaces"

// // SetTrimTrailingSpaces sets the rendering context to trim (or not) trailing spaces
// func (ctx *Context) SetTrimTrailingSpaces(b bool) {
// ctx.options[trimTrailingSpaces] = b
// }

// // TrimTrailingSpaces indicates if trailing spaces should be trimmed
// func (ctx *Context) TrimTrailingSpaces() bool {
// if b, found := ctx.options[trimTrailingSpaces].(bool); found {
// return b
// }
// // by default, do trim
// return true
// }

const withinDelimitedBlock string = "withinDelimitedBlock"

// SetWithinDelimitedBlock sets the rendering context to be within a delimited block
Expand Down Expand Up @@ -114,6 +98,22 @@ func (ctx *Context) WithinList() bool {
return false
}

const tableCounter = "tableCounter"

// GetAndIncrementTableCounter returns the current value for the table counter after internally incrementing it.
func (ctx *Context) GetAndIncrementTableCounter() int {
if _, found := ctx.options[tableCounter]; !found {
ctx.options[tableCounter] = 1
}
if c, ok := ctx.options[tableCounter].(int); ok {
ctx.options[tableCounter] = c + 1
return c
}
ctx.options[tableCounter] = 1
log.Warnf("table counter was set to a non-int value")
return 1
}

// Deadline wrapper implementation of context.Context.Deadline()
func (ctx *Context) Deadline() (deadline time.Time, ok bool) {
return ctx.context.Deadline()
Expand Down
5 changes: 3 additions & 2 deletions pkg/renderer/html5/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var tableTmpl texttemplate.Template

func init() {
tableTmpl = newTextTemplate("table", `{{ $ctx := .Context }}{{ with .Data }}<table class="tableblock frame-all grid-all spread">{{ if .Lines }}
tableTmpl = newTextTemplate("table", `{{ $ctx := .Context }}{{ with .Data }}<table class="tableblock frame-all grid-all stretch">{{ if .Lines }}
{{ if .Title }}<caption class="title">{{ .Title }}</caption>
{{ end }}<colgroup>
{{ $cellWidths := .CellWidths }}{{ range $index, $width := $cellWidths }}<col style="width: {{ $width }}%;">{{ includeNewline $ctx $index $cellWidths }}{{ end }}
Expand Down Expand Up @@ -59,7 +59,8 @@ func renderTable(ctx *renderer.Context, t types.Table) ([]byte, error) {
}
var title string
if titleAttr, ok := t.Attributes[types.AttrTitle].(string); ok {
title = fmt.Sprintf("Table 1. %s", titleAttr)
c := ctx.GetAndIncrementTableCounter()
title = fmt.Sprintf("Table %d. %s", c, titleAttr)
}
err := tableTmpl.Execute(result, ContextualPipeline{
Context: ctx,
Expand Down
85 changes: 81 additions & 4 deletions pkg/renderer/html5/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var _ = Describe("tables", func() {
actualContent := `|===
| *foo* foo | _bar_
|===`
expectedResult := `<table class="tableblock frame-all grid-all spread">
expectedResult := `<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
Expand All @@ -27,7 +27,7 @@ var _ = Describe("tables", func() {
actualContent := `|===
| *foo* foo | _bar_ | baz
|===`
expectedResult := `<table class="tableblock frame-all grid-all spread">
expectedResult := `<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
Expand Down Expand Up @@ -55,7 +55,7 @@ var _ = Describe("tables", func() {
|Column 1, row 2
|Column 2, row 2
|===`
expectedResult := `<table class="tableblock frame-all grid-all spread">
expectedResult := `<table class="tableblock frame-all grid-all stretch">
<caption class="title">Table 1. table title</caption>
<colgroup>
<col style="width: 50%;">
Expand Down Expand Up @@ -84,8 +84,85 @@ var _ = Describe("tables", func() {
It("empty table ", func() {
actualContent := `|===
|===`
expectedResult := `<table class="tableblock frame-all grid-all spread">
expectedResult := `<table class="tableblock frame-all grid-all stretch">
</table>`
verify(GinkgoT(), expectedResult, actualContent)
})

It("2 tables with 1 counter", func() {
actualContent := `|===
| foo | bar
|===
.Title 2
|===
| foo | bar
|===`
expectedResult := `<table class="tableblock frame-all grid-all stretch">
<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">Table 1. Title 2</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>`
verify(GinkgoT(), expectedResult, actualContent)
})

It("2 tables with 2 counters", func() {
actualContent := `.Title 1
|===
| foo | bar
|===
.Title 2
|===
| foo | bar
|===`
expectedResult := `<table class="tableblock frame-all grid-all stretch">
<caption class="title">Table 1. Title 1</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">Table 2. Title 2</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>`
verify(GinkgoT(), expectedResult, actualContent)
})

})

0 comments on commit 7f3a6e0

Please sign in to comment.