permit backslash-escaped vertical bars in tables

This commit is contained in:
Russ Ross 2011-09-13 16:23:24 -06:00
parent 583b3c5e1d
commit b97990f1bb
2 changed files with 19 additions and 7 deletions

View File

@ -662,11 +662,20 @@ func (p *parser) table(out *bytes.Buffer, data []byte) int {
return i return i
} }
// check if the specified position is preceeded 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) tableHeader(out *bytes.Buffer, data []byte) (size int, columns []int) { func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns []int) {
i := 0 i := 0
colCount := 1 colCount := 1
for i = 0; data[i] != '\n'; i++ { for i = 0; data[i] != '\n'; i++ {
if data[i] == '|' { if data[i] == '|' && !isBackslashEscaped(data, i) {
colCount++ colCount++
} }
} }
@ -683,7 +692,7 @@ func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns
if data[0] == '|' { if data[0] == '|' {
colCount-- colCount--
} }
if i > 2 && data[i-1] == '|' { if i > 2 && data[i-1] == '|' && !isBackslashEscaped(data, i-1) {
colCount-- colCount--
} }
@ -695,7 +704,7 @@ func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns
return return
} }
if data[i] == '|' { if data[i] == '|' && !isBackslashEscaped(data, i) {
i++ i++
} }
for data[i] == ' ' { for data[i] == ' ' {
@ -732,7 +741,7 @@ func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns
// not a valid column // not a valid column
return return
case data[i] == '|': case data[i] == '|' && !isBackslashEscaped(data, i):
// marker found, now skip past trailing whitespace // marker found, now skip past trailing whitespace
col++ col++
i++ i++
@ -745,7 +754,7 @@ func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns
return return
} }
case data[i] != '|' && col+1 < colCount: case (data[i] != '|' || isBackslashEscaped(data, i)) && col+1 < colCount:
// something else found where marker was required // something else found where marker was required
return return
@ -771,7 +780,7 @@ func (p *parser) tableRow(out *bytes.Buffer, data []byte, columns []int) {
i, col := 0, 0 i, col := 0, 0
var rowWork bytes.Buffer var rowWork bytes.Buffer
if data[i] == '|' { if data[i] == '|' && !isBackslashEscaped(data, i) {
i++ i++
} }
@ -782,7 +791,7 @@ func (p *parser) tableRow(out *bytes.Buffer, data []byte, columns []int) {
cellStart := i cellStart := i
for data[i] != '|' && data[i] != '\n' { for (data[i] != '|' || isBackslashEscaped(data, i)) && data[i] != '\n' {
i++ i++
} }

View File

@ -686,6 +686,9 @@ func TestTable(t *testing.T) {
"a| b|c | d | e\n---|---|---|---|---\nf| g|h | i |j\n", "a| b|c | d | e\n---|---|---|---|---\nf| g|h | i |j\n",
"<table>\n<thead>\n<tr>\n<td>a</td>\n<td>b</td>\n<td>c</td>\n<td>d</td>\n<td>e</td>\n</tr>\n</thead>\n\n" + "<table>\n<thead>\n<tr>\n<td>a</td>\n<td>b</td>\n<td>c</td>\n<td>d</td>\n<td>e</td>\n</tr>\n</thead>\n\n" +
"<tbody>\n<tr>\n<td>f</td>\n<td>g</td>\n<td>h</td>\n<td>i</td>\n<td>j</td>\n</tr>\n</tbody>\n</table>\n", "<tbody>\n<tr>\n<td>f</td>\n<td>g</td>\n<td>h</td>\n<td>i</td>\n<td>j</td>\n</tr>\n</tbody>\n</table>\n",
"a|b\\|c|d\n---|---|---\nf|g\\|h|i\n",
"<table>\n<thead>\n<tr>\n<td>a</td>\n<td>b|c</td>\n<td>d</td>\n</tr>\n</thead>\n\n<tbody>\n<tr>\n<td>f</td>\n<td>g|h</td>\n<td>i</td>\n</tr>\n</tbody>\n</table>\n",
} }
doTestsBlock(t, tests, EXTENSION_TABLES) doTestsBlock(t, tests, EXTENSION_TABLES)
} }