Skip to content

Commit

Permalink
feat(renderer): table columns as headers
Browse files Browse the repository at this point in the history
Fixes bytesparadise#1094

Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
  • Loading branch information
xcoulon committed Oct 8, 2022
1 parent f537bdf commit 118e354
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 7 deletions.
193 changes: 193 additions & 0 deletions pkg/parser/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
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 @@ -592,6 +592,48 @@ var _ = Describe("tables", func() {
</tr>
</tbody>
</table>
`
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 := `<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Dir (X,Y,Z)</th>
<th class="tableblock halign-right valign-top">Num Cells</th>
<th class="tableblock halign-right valign-top">Size</th>
</tr>
</thead>
<tbody>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock">X</p></th>
<td class="tableblock halign-right valign-top"><p class="tableblock">10</p></td>
<td class="tableblock halign-right valign-top"><p class="tableblock">0.1</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock">Y</p></th>
<td class="tableblock halign-right valign-top"><p class="tableblock">5</p></td>
<td class="tableblock halign-right valign-top"><p class="tableblock">0.2</p></td>
</tr>
<tr>
<th class="tableblock halign-left valign-top"><p class="tableblock">Z</p></th>
<td class="tableblock halign-right valign-top"><p class="tableblock">10</p></td>
<td class="tableblock halign-right valign-top"><p class="tableblock">0.1</p></td>
</tr>
</tbody>
</table>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})
Expand Down
6 changes: 5 additions & 1 deletion pkg/renderer/sgml/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 118e354

Please sign in to comment.