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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
package parser
import "github.com/gomarkdown/markdown/ast"
// check if the specified position is preceded by an odd number of backslashes
func isBackslashEscaped(data []byte, i int) bool {
backslashes := 0
for i-backslashes-1 >= 0 && data[i-backslashes-1] == '\\' {
backslashes++
}
return backslashes&1 == 1
}
func (p *Parser) tableRow(data []byte, columns []ast.CellAlignFlags, header bool) {
p.AddBlock(&ast.TableRow{})
col := 0
i := skipChar(data, 0, '|')
n := len(data)
colspans := 0 // keep track of total colspan in this row.
for col = 0; col < len(columns) && i < n; col++ {
colspan := 0
i = skipChar(data, i, ' ')
cellStart := i
// If we are in a codespan we should discount any | we see, check for that here and skip ahead.
if isCode, _ := codeSpan(p, data[i:], 0); isCode > 0 {
i += isCode - 1
}
for i < n && (data[i] != '|' || isBackslashEscaped(data, i)) && data[i] != '\n' {
i++
}
cellEnd := i
// skip the end-of-cell marker, possibly taking us past end of buffer
// each _extra_ | means a colspan
for i < len(data) && data[i] == '|' && !isBackslashEscaped(data, i) {
i++
colspan++
}
// only colspan > 1 make sense.
if colspan < 2 {
colspan = 0
}
for cellEnd > cellStart && cellEnd-1 < n && data[cellEnd-1] == ' ' {
cellEnd--
}
block := &ast.TableCell{
IsHeader: header,
Align: columns[col],
ColSpan: colspan,
}
block.Content = data[cellStart:cellEnd]
if cellStart == cellEnd && colspans > 0 {
// an empty cell that we should ignore, it exists because of colspan
colspans--
} else {
p.AddBlock(block)
}
if colspan > 0 {
colspans += colspan - 1
}
}
// pad it out with empty columns to get the right number
for ; col < len(columns); col++ {
block := &ast.TableCell{
IsHeader: header,
Align: columns[col],
}
p.AddBlock(block)
}
// silently ignore rows with too many cells
}
// tableFooter parses the (optional) table footer.
func (p *Parser) tableFooter(data []byte) bool {
colCount := 1
// ignore up to 3 spaces
n := len(data)
i := skipCharN(data, 0, ' ', 3)
for ; i < n && data[i] != '\n'; i++ {
// If we are in a codespan we should discount any | we see, check for that here and skip ahead.
if isCode, _ := codeSpan(p, data[i:], 0); isCode > 0 {
i += isCode - 1
}
if data[i] == '|' && !isBackslashEscaped(data, i) {
colCount++
continue
}
// remaining data must be the = character
if data[i] != '=' {
return false
}
}
// doesn't look like a table footer
if colCount == 1 {
return false
}
p.AddBlock(&ast.TableFooter{})
return true
}
// tableHeaders parses the header. If recognized it will also add a table.
func (p *Parser) tableHeader(data []byte, doRender bool) (size int, columns []ast.CellAlignFlags, table ast.Node) {
i := 0
colCount := 1
headerIsUnderline := true
headerIsWithEmptyFields := true
for i = 0; i < len(data) && data[i] != '\n'; i++ {
// If we are in a codespan we should discount any | we see, check for that here and skip ahead.
if isCode, _ := codeSpan(p, data[i:], 0); isCode > 0 {
i += isCode - 1
}
if data[i] == '|' && !isBackslashEscaped(data, i) {
colCount++
}
if data[i] != '-' && data[i] != ' ' && data[i] != ':' && data[i] != '|' {
headerIsUnderline = false
}
if data[i] != ' ' && data[i] != '|' {
headerIsWithEmptyFields = false
}
}
// doesn't look like a table header
if colCount == 1 {
return
}
// include the newline in the data sent to tableRow
j := skipCharN(data, i, '\n', 1)
header := data[:j]
// column count ignores pipes at beginning or end of line
if data[0] == '|' {
colCount--
}
{
tmp := header
// remove whitespace from the end
for len(tmp) > 0 {
lastIdx := len(tmp) - 1
if tmp[lastIdx] == '\n' || tmp[lastIdx] == ' ' {
tmp = tmp[:lastIdx]
} else {
break
}
}
n := len(tmp)
if n > 2 && tmp[n-1] == '|' && !isBackslashEscaped(tmp, n-1) {
colCount--
}
}
// if the header looks like a underline, then we omit the header
// and parse the first line again as underline
if headerIsUnderline && !headerIsWithEmptyFields {
header = nil
i = 0
} else {
i++ // move past newline
}
columns = make([]ast.CellAlignFlags, colCount)
// move on to the header underline
if i >= len(data) {
return
}
if data[i] == '|' && !isBackslashEscaped(data, i) {
i++
}
i = skipChar(data, i, ' ')
// each column header is of form: / *:?-+:? *|/ with # dashes + # colons >= 3
// and trailing | optional on last column
col := 0
n := len(data)
for i < n && data[i] != '\n' {
dashes := 0
if data[i] == ':' {
i++
columns[col] |= ast.TableAlignmentLeft
dashes++
}
for i < n && data[i] == '-' {
i++
dashes++
}
if i < n && data[i] == ':' {
i++
columns[col] |= ast.TableAlignmentRight
dashes++
}
for i < n && data[i] == ' ' {
i++
}
if i == n {
return
}
// end of column test is messy
switch {
case dashes < 1:
// not a valid column
return
case data[i] == '|' && !isBackslashEscaped(data, i):
// marker found, now skip past trailing whitespace
col++
i++
for i < n && data[i] == ' ' {
i++
}
// trailing junk found after last column
if col >= colCount && i < len(data) && data[i] != '\n' {
return
}
case (data[i] != '|' || isBackslashEscaped(data, i)) && col+1 < colCount:
// something else found where marker was required
return
case data[i] == '\n':
// marker is optional for the last column
col++
default:
// trailing junk found after last column
return
}
}
if col != colCount {
return
}
if doRender {
table = &ast.Table{}
p.AddBlock(table)
if header != nil {
p.AddBlock(&ast.TableHeader{})
p.tableRow(header, columns, true)
}
}
size = skipCharN(data, i, '\n', 1)
return
}
/*
Table:
Name | Age | Phone
------|-----|---------
Bob | 31 | 555-1234
Alice | 27 | 555-4321
*/
func (p *Parser) table(data []byte) int {
i, columns, table := p.tableHeader(data, true)
if i == 0 {
return 0
}
p.AddBlock(&ast.TableBody{})
for i < len(data) {
pipes, rowStart := 0, i
for ; i < len(data) && data[i] != '\n'; i++ {
if data[i] == '|' {
pipes++
}
}
if pipes == 0 {
i = rowStart
break
}
// include the newline in data sent to tableRow
i = skipCharN(data, i, '\n', 1)
if p.tableFooter(data[rowStart:i]) {
continue
}
p.tableRow(data[rowStart:i], columns, false)
}
if captionContent, id, consumed := p.caption(data[i:], []byte(captionTable)); consumed > 0 {
caption := &ast.Caption{}
p.Inline(caption, captionContent)
// Some switcheroo to re-insert the parsed table as a child of the captionfigure.
figure := &ast.CaptionFigure{}
figure.HeadingID = id
table2 := &ast.Table{}
// Retain any block level attributes.
table2.AsContainer().Attribute = table.AsContainer().Attribute
children := table.GetChildren()
ast.RemoveFromTree(table)
table2.SetChildren(children)
ast.AppendChild(figure, table2)
ast.AppendChild(figure, caption)
p.addChild(figure)
p.Finalize(figure)
i += consumed
}
return i
}
|