-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki-table.go
134 lines (109 loc) · 2.85 KB
/
wiki-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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package wikitable
import (
"bytes"
wikitable "github.com/movsb/goldmark-wiki-table/wiki-table"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
type WikiTable struct{}
func (t *WikiTable) Extend(md goldmark.Markdown) {
md.Parser().AddOptions(
parser.WithParagraphTransformers(
util.Prioritized(_NewParser(), 0),
),
)
md.Renderer().AddOptions(
renderer.WithNodeRenderers(
util.Prioritized(_NewRenderer(), 0),
),
)
}
var _wikiTable = &WikiTable{}
func New() *WikiTable {
return _wikiTable
}
type _WikiTableParser struct{}
var _ parser.ParagraphTransformer = (*_WikiTableParser)(nil)
func _NewParser() *_WikiTableParser {
return &_WikiTableParser{}
}
func (t *_WikiTableParser) Transform(node *ast.Paragraph, reader text.Reader, pc parser.Context) {
source := t.getSource(node, reader, pc)
if source == nil {
return
}
table, err := wikitable.Parse(bytes.NewReader(source))
if err != nil {
return
}
t.transform(node, table)
}
func (t *_WikiTableParser) getSource(node *ast.Paragraph, reader text.Reader, pc parser.Context) []byte {
values := []byte{}
lines := node.Lines()
line0 := lines.At(0)
text := reader.Value(line0)
if !bytes.Contains(text, []byte{'{', '|'}) {
return nil
}
values = append(values, text...)
// check if it has matching start and end.
numBrackets := 1
for i := 1; i < lines.Len(); i++ {
line := lines.At(i)
text := reader.Value(line)
if bytes.Contains(text, []byte{'{', '|'}) {
numBrackets++
} else if bytes.Contains(text, []byte{'|', '}'}) {
numBrackets--
}
values = append(values, text...)
}
if numBrackets != 0 {
return nil
}
// extraneous char
lastLine := reader.Value(lines.At(lines.Len() - 1))
if !bytes.Contains(lastLine, []byte{'|', '}'}) {
return nil
}
return values
}
func (t *_WikiTableParser) transform(node ast.Node, table *wikitable.Table) {
tableNode := &_TableNode{
table: table,
}
node.Parent().InsertAfter(node.Parent(), node, tableNode)
node.Parent().RemoveChild(node.Parent(), node)
}
var (
nodeKindTable = ast.NewNodeKind(`WikiTable`)
)
type _TableNode struct {
ast.BaseBlock
table *wikitable.Table
}
func (t *_TableNode) Kind() ast.NodeKind {
return nodeKindTable
}
func (t *_TableNode) Dump(source []byte, level int) {
ast.DumpHelper(t, source, level, nil, nil)
}
type _WikiTableRender struct{}
func _NewRenderer() *_WikiTableRender {
return &_WikiTableRender{}
}
func (t *_WikiTableRender) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(nodeKindTable, t.renderTable)
}
func (t *_WikiTableRender) renderTable(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
table := n.(*_TableNode).table
if entering {
table.Html(w)
}
return ast.WalkContinue, nil
}