From 7f3a6e070392c1f3b28f04d6c3d42e4d35835f72 Mon Sep 17 00:00:00 2001 From: Xavier Coulon Date: Thu, 16 Aug 2018 19:08:50 +0200 Subject: [PATCH] fix(renderer): fix table numbering when title is included (#166) fixes #166 Signed-off-by: Xavier Coulon --- pkg/renderer/context.go | 32 ++++++------ pkg/renderer/html5/table.go | 5 +- pkg/renderer/html5/table_test.go | 85 ++++++++++++++++++++++++++++++-- 3 files changed, 100 insertions(+), 22 deletions(-) diff --git a/pkg/renderer/context.go b/pkg/renderer/context.go index 8ce5b828..f20c321f 100644 --- a/pkg/renderer/context.go +++ b/pkg/renderer/context.go @@ -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 @@ -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() diff --git a/pkg/renderer/html5/table.go b/pkg/renderer/html5/table.go index 0b409da6..6249aefd 100644 --- a/pkg/renderer/html5/table.go +++ b/pkg/renderer/html5/table.go @@ -15,7 +15,7 @@ import ( var tableTmpl texttemplate.Template func init() { - tableTmpl = newTextTemplate("table", `{{ $ctx := .Context }}{{ with .Data }}{{ if .Lines }} + tableTmpl = newTextTemplate("table", `{{ $ctx := .Context }}{{ with .Data }}
{{ if .Lines }} {{ if .Title }} {{ end }}{{ $cellWidths := .CellWidths }}{{ range $index, $width := $cellWidths }}{{ includeNewline $ctx $index $cellWidths }}{{ end }} @@ -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, diff --git a/pkg/renderer/html5/table_test.go b/pkg/renderer/html5/table_test.go index 5d755b6a..ba9cf575 100644 --- a/pkg/renderer/html5/table_test.go +++ b/pkg/renderer/html5/table_test.go @@ -8,7 +8,7 @@ var _ = Describe("tables", func() { actualContent := `|=== | *foo* foo | _bar_ |===` - expectedResult := `
{{ .Title }}
+ expectedResult := `
@@ -27,7 +27,7 @@ var _ = Describe("tables", func() { actualContent := `|=== | *foo* foo | _bar_ | baz |===` - expectedResult := `
+ expectedResult := `
@@ -55,7 +55,7 @@ var _ = Describe("tables", func() { |Column 1, row 2 |Column 2, row 2 |===` - expectedResult := `
+ expectedResult := `
@@ -84,8 +84,85 @@ var _ = Describe("tables", func() { It("empty table ", func() { actualContent := `|=== |===` - expectedResult := `
Table 1. table title
+ expectedResult := `
` verify(GinkgoT(), expectedResult, actualContent) }) + + It("2 tables with 1 counter", func() { + actualContent := `|=== +| foo | bar +|=== + +.Title 2 +|=== +| foo | bar +|===` + expectedResult := ` ++++ + + + + + + +

foo

bar

+ + ++++ + + + + + + +
Table 1. Title 2

foo

bar

` + verify(GinkgoT(), expectedResult, actualContent) + }) + + It("2 tables with 2 counters", func() { + actualContent := `.Title 1 +|=== +| foo | bar +|=== + +.Title 2 +|=== +| foo | bar +|===` + expectedResult := ` + ++++ + + + + + + +
Table 1. Title 1

foo

bar

+ + ++++ + + + + + + +
Table 2. Title 2

foo

bar

` + verify(GinkgoT(), expectedResult, actualContent) + }) + })