From 118e3542f3f6b154449aa312274981ef77316852 Mon Sep 17 00:00:00 2001 From: Xavier Coulon Date: Sat, 8 Oct 2022 07:00:46 +0200 Subject: [PATCH] feat(renderer): table columns as headers Fixes #1094 Signed-off-by: Xavier Coulon --- pkg/parser/table_test.go | 193 ++++++++++++++++++++++++++ pkg/renderer/sgml/html5/table_test.go | 42 ++++++ pkg/renderer/sgml/table.go | 6 +- pkg/types/types.go | 14 +- 4 files changed, 248 insertions(+), 7 deletions(-) diff --git a/pkg/parser/table_test.go b/pkg/parser/table_test.go index 2ae11b77..4280f226 100644 --- a/pkg/parser/table_test.go +++ b/pkg/parser/table_test.go @@ -1010,6 +1010,199 @@ var _ = Describe("tables", func() { } Expect(ParseDocument(source)).To(MatchDocument(expected)) }) + It("with header col option", func() { + source := `[cols="h,>,>",options="header"] +|=== +|Dir (X,Y,Z) |Num Cells |Size +|X |10 |0.1 +|Y |5 |0.2 +|Z |10 |0.1 +|===` + expected := &types.Document{ + Elements: []interface{}{ + &types.Table{ + Attributes: types.Attributes{ + types.AttrCols: []interface{}{ + &types.TableColumn{ + Multiplier: 1, + HAlign: types.HAlignDefault, + VAlign: types.VAlignDefault, + Weight: 1, + Style: types.HeaderStyle, + }, + &types.TableColumn{ + Multiplier: 1, + HAlign: types.HAlignRight, + VAlign: types.VAlignDefault, + Weight: 1, + }, + &types.TableColumn{ + Multiplier: 1, + HAlign: types.HAlignRight, + VAlign: types.VAlignDefault, + Weight: 1, + }, + }, + types.AttrOptions: types.Options{"header"}, + }, + Header: &types.TableRow{ + Cells: []*types.TableCell{ + { + Elements: []interface{}{ + &types.Paragraph{ + Elements: []interface{}{ + &types.StringElement{ + Content: "Dir (X,Y,Z)", + }, + }, + }, + }, + }, + { + Elements: []interface{}{ + &types.Paragraph{ + Elements: []interface{}{ + &types.StringElement{ + Content: "Num Cells", + }, + }, + }, + }, + }, + { + Elements: []interface{}{ + &types.Paragraph{ + Elements: []interface{}{ + &types.StringElement{ + Content: "Size", + }, + }, + }, + }, + }, + }, + }, + Rows: []*types.TableRow{ + { + Cells: []*types.TableCell{ + { + // Format: string(types.HeaderStyle), + Elements: []interface{}{ + &types.Paragraph{ + Elements: []interface{}{ + &types.StringElement{ + Content: "X", + }, + }, + }, + }, + }, + { + Elements: []interface{}{ + &types.Paragraph{ + Elements: []interface{}{ + &types.StringElement{ + Content: "10", + }, + }, + }, + }, + }, + { + Elements: []interface{}{ + &types.Paragraph{ + Elements: []interface{}{ + &types.StringElement{ + Content: "0.1", + }, + }, + }, + }, + }, + }, + }, + { + Cells: []*types.TableCell{ + { + // Format: string(types.HeaderStyle), + Elements: []interface{}{ + &types.Paragraph{ + Elements: []interface{}{ + &types.StringElement{ + Content: "Y", + }, + }, + }, + }, + }, + { + Elements: []interface{}{ + &types.Paragraph{ + Elements: []interface{}{ + &types.StringElement{ + Content: "5", + }, + }, + }, + }, + }, + { + Elements: []interface{}{ + &types.Paragraph{ + Elements: []interface{}{ + &types.StringElement{ + Content: "0.2", + }, + }, + }, + }, + }, + }, + }, + { + Cells: []*types.TableCell{ + { + // Format: string(types.HeaderStyle), + Elements: []interface{}{ + &types.Paragraph{ + Elements: []interface{}{ + &types.StringElement{ + Content: "Z", + }, + }, + }, + }, + }, + { + Elements: []interface{}{ + &types.Paragraph{ + Elements: []interface{}{ + &types.StringElement{ + Content: "10", + }, + }, + }, + }, + }, + { + Elements: []interface{}{ + &types.Paragraph{ + Elements: []interface{}{ + &types.StringElement{ + Content: "0.1", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + } + Expect(ParseDocument(source)).To(MatchDocument(expected)) + }) It("with header and footer options", func() { source := `[%header%footer,cols="2,2,1"] diff --git a/pkg/renderer/sgml/html5/table_test.go b/pkg/renderer/sgml/html5/table_test.go index 212dada0..e8738f3e 100644 --- a/pkg/renderer/sgml/html5/table_test.go +++ b/pkg/renderer/sgml/html5/table_test.go @@ -592,6 +592,48 @@ var _ = Describe("tables", func() { +` + Expect(RenderHTML(source)).To(MatchHTML(expected)) + }) + It("with header col option", func() { + source := `[cols="h,>,>",options="header"] +|=== +|Dir (X,Y,Z) |Num Cells |Size +|X |10 |0.1 +|Y |5 |0.2 +|Z |10 |0.1 +|===` + expected := ` +++++ + + + + + + + + + + + + + + + + + + + + + + + + +
Dir (X,Y,Z)Num CellsSize

X

10

0.1

Y

5

0.2

Z

10

0.1

` Expect(RenderHTML(source)).To(MatchHTML(expected)) }) diff --git a/pkg/renderer/sgml/table.go b/pkg/renderer/sgml/table.go index e2acec29..75819f47 100644 --- a/pkg/renderer/sgml/table.go +++ b/pkg/renderer/sgml/table.go @@ -261,7 +261,11 @@ func (r *sgmlRenderer) renderTableCell(ctx *context, cell *types.TableCell, col } buff.WriteString(renderedElement) } - return r.execute(r.tableCell, struct { + tmpl := r.tableCell + if col.Style == types.HeaderStyle { + tmpl = r.tableHeaderCell + } + return r.execute(tmpl, struct { Context *context Content string Cell *types.TableCell diff --git a/pkg/types/types.go b/pkg/types/types.go index e2914408..784b4814 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -3915,17 +3915,19 @@ func (t *Table) Reference(refs ElementReferences) { type HAlign string const ( - HAlignLeft HAlign = "<" - HAlignRight HAlign = ">" - HAlignCenter HAlign = "^" + HAlignDefault HAlign = HAlignLeft + HAlignLeft HAlign = "<" // default + HAlignRight HAlign = ">" + HAlignCenter HAlign = "^" ) type VAlign string const ( - VAlignTop VAlign = "<" - VAlignBottom VAlign = ">" - VAlignMiddle VAlign = "^" + VAlignDefault VAlign = VAlignTop + VAlignTop VAlign = "<" // default + VAlignBottom VAlign = ">" + VAlignMiddle VAlign = "^" ) type ContentStyle string