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