forked from mmarkdown/mmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
39 lines (34 loc) · 872 Bytes
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package markdown
import (
"bytes"
"github.com/gomarkdown/markdown/ast"
)
func (r *Renderer) tableColWidth(tab *ast.Table) []int {
cells := 0
ast.WalkFunc(tab, func(node ast.Node, entering bool) ast.WalkStatus {
switch node := node.(type) {
case *ast.TableRow:
cells = len(node.GetChildren())
break
}
return ast.GoToNext
})
width := make([]int, cells)
ast.WalkFunc(tab, func(node ast.Node, entering bool) ast.WalkStatus {
switch node := node.(type) {
case *ast.TableRow:
for col, cell := range node.GetChildren() {
buf := &bytes.Buffer{}
ast.WalkFunc(cell, func(node1 ast.Node, entering bool) ast.WalkStatus {
r.RenderNode(buf, node1, entering)
return ast.GoToNext
})
if l := buf.Len(); l > width[col] {
width[col] = l + 1 // space in beginning or end
}
}
}
return ast.GoToNext
})
return width
}