| 1 |
package ast |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
|
| 7 |
gast "github.com/yuin/goldmark/ast" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
type Alignment int |
| 12 |
|
| 13 |
const ( |
| 14 |
|
| 15 |
AlignLeft Alignment = iota + 1 |
| 16 |
|
| 17 |
|
| 18 |
AlignRight |
| 19 |
|
| 20 |
|
| 21 |
AlignCenter |
| 22 |
|
| 23 |
|
| 24 |
AlignNone |
| 25 |
) |
| 26 |
|
| 27 |
func (a Alignment) String() string { |
| 28 |
switch a { |
| 29 |
case AlignLeft: |
| 30 |
return "left" |
| 31 |
case AlignRight: |
| 32 |
return "right" |
| 33 |
case AlignCenter: |
| 34 |
return "center" |
| 35 |
case AlignNone: |
| 36 |
return "none" |
| 37 |
} |
| 38 |
return "" |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
type Table struct { |
| 43 |
gast.BaseBlock |
| 44 |
|
| 45 |
|
| 46 |
Alignments []Alignment |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
func (n *Table) Dump(source []byte, level int) { |
| 51 |
gast.DumpHelper(n, source, level, nil, func(level int) { |
| 52 |
indent := strings.Repeat(" ", level) |
| 53 |
fmt.Printf("%sAlignments {\n", indent) |
| 54 |
for i, alignment := range n.Alignments { |
| 55 |
indent2 := strings.Repeat(" ", level+1) |
| 56 |
fmt.Printf("%s%s", indent2, alignment.String()) |
| 57 |
if i != len(n.Alignments)-1 { |
| 58 |
fmt.Println("") |
| 59 |
} |
| 60 |
} |
| 61 |
fmt.Printf("\n%s}\n", indent) |
| 62 |
}) |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
var KindTable = gast.NewNodeKind("Table") |
| 67 |
|
| 68 |
|
| 69 |
func (n *Table) Kind() gast.NodeKind { |
| 70 |
return KindTable |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
func NewTable() *Table { |
| 75 |
return &Table{ |
| 76 |
Alignments: []Alignment{}, |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
type TableRow struct { |
| 82 |
gast.BaseBlock |
| 83 |
Alignments []Alignment |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
func (n *TableRow) Dump(source []byte, level int) { |
| 88 |
gast.DumpHelper(n, source, level, nil, nil) |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
var KindTableRow = gast.NewNodeKind("TableRow") |
| 93 |
|
| 94 |
|
| 95 |
func (n *TableRow) Kind() gast.NodeKind { |
| 96 |
return KindTableRow |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
func NewTableRow(alignments []Alignment) *TableRow { |
| 101 |
return &TableRow{Alignments: alignments} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
type TableHeader struct { |
| 106 |
gast.BaseBlock |
| 107 |
Alignments []Alignment |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
var KindTableHeader = gast.NewNodeKind("TableHeader") |
| 112 |
|
| 113 |
|
| 114 |
func (n *TableHeader) Kind() gast.NodeKind { |
| 115 |
return KindTableHeader |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
func (n *TableHeader) Dump(source []byte, level int) { |
| 120 |
gast.DumpHelper(n, source, level, nil, nil) |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
func NewTableHeader(row *TableRow) *TableHeader { |
| 125 |
n := &TableHeader{} |
| 126 |
n.SetPos(row.Pos()) |
| 127 |
for c := row.FirstChild(); c != nil; { |
| 128 |
next := c.NextSibling() |
| 129 |
n.AppendChild(n, c) |
| 130 |
c = next |
| 131 |
} |
| 132 |
return n |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
type TableCell struct { |
| 137 |
gast.BaseBlock |
| 138 |
Alignment Alignment |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
func (n *TableCell) Dump(source []byte, level int) { |
| 143 |
gast.DumpHelper(n, source, level, nil, nil) |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
var KindTableCell = gast.NewNodeKind("TableCell") |
| 148 |
|
| 149 |
|
| 150 |
func (n *TableCell) Kind() gast.NodeKind { |
| 151 |
return KindTableCell |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
func NewTableCell() *TableCell { |
| 156 |
return &TableCell{ |
| 157 |
Alignment: AlignNone, |
| 158 |
} |
| 159 |
} |