Skip to content

Commit

Permalink
Complex table rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Sep 3, 2024
1 parent 0e88302 commit e66ea28
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
1 change: 0 additions & 1 deletion pkg/tfgen/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,6 @@ func (p *tfMarkdownParser) parse(tfMarkdown []byte) (entityDocs, error) {
goldmark.WithParserOptions(parser.WithASTTransformers(
util.Prioritized(exampleTransformer, 2000),
)),
goldmark.WithRenderer(parse.RenderMarkdown()),
).Convert(tfMarkdown, &out)
if err != nil {
return entityDocs{}, err
Expand Down
1 change: 0 additions & 1 deletion pkg/tfgen/docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,6 @@ func TestReformatExamples(t *testing.T) {
goldmark.WithParserOptions(parser.WithASTTransformers(
util.Prioritized(exampleTransformer{}, 2000),
)),
goldmark.WithRenderer(parse.RenderMarkdown()),
).Convert([]byte(input), &out)
require.NoError(t, err)

Expand Down
24 changes: 18 additions & 6 deletions pkg/tfgen/parse/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package parse
import (
"bytes"
"fmt"
"strings"

"github.com/olekukonko/tablewriter"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
Expand Down Expand Up @@ -140,12 +141,15 @@ func (t tableRenderer) RegisterFuncs(r renderer.NodeRendererFuncRegisterer) {
})
}

func (t tableRenderer) render(writer util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
writer.WriteRune('\n')
func (t tableRenderer) render(
writer util.BufWriter, source []byte, n ast.Node, entering bool,
) (ast.WalkStatus, error) {
_, err := writer.WriteRune('\n')
contract.AssertNoErrorf(err, "impossible")
var inHeader bool
header := make([]string, 0, len(n.(*extast.Table).Alignments))
var rows [][]string
err := ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
err = ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
switch n := n.(type) {
case *extast.Table:
return ast.WalkContinue, nil
Expand All @@ -160,14 +164,22 @@ func (t tableRenderer) render(writer util.BufWriter, source []byte, n ast.Node,
case *extast.TableCell:
if entering {
var cell bytes.Buffer
err := t.r.Render(&cell, source, n)
b := ast.NewTextBlock()
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
b.AppendChild(b, c)
}

err := t.r.Render(&cell, source, b)
if err != nil {
return ast.WalkStop, err
}

content := strings.TrimSpace(cell.String())

if inHeader {
header = append(header, string(n.Text(source)))
header = append(header, content)
} else {
rows[len(rows)-1] = append(rows[len(rows)-1], string(n.Text(source)))
rows[len(rows)-1] = append(rows[len(rows)-1], content)
}
}
return ast.WalkSkipChildren, nil
Expand Down
15 changes: 15 additions & 0 deletions pkg/tfgen/parse/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ func TestRenderTable(t *testing.T) {
|------|------|
| r1c1 | r1c2 |
| r2c1 | r2c2 |
`),
},
{
name: "with-in table effects",
input: `
| t1 | *t2* |
|------|------|
| __r1c1__ | r1c2 |
| r2c1 | r2c2 |
`,
expected: autogold.Expect(`
| t1 | *t2* |
|----------|------|
| **r1c1** | r1c2 |
| r2c1 | r2c2 |
`),
},
}
Expand Down

0 comments on commit e66ea28

Please sign in to comment.