permit backslash-escaped vertical bars in tables
Russ Ross russ@dixie.edu
Tue, 13 Sep 2011 16:23:24 -0600
2 files changed,
19 insertions(+),
7 deletions(-)
M
block.go
→
block.go
@@ -662,11 +662,20 @@
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) { i := 0 colCount := 1 for i = 0; data[i] != '\n'; i++ { - if data[i] == '|' { + if data[i] == '|' && !isBackslashEscaped(data, i) { colCount++ } }@@ -683,7 +692,7 @@ // column count ignores pipes at beginning or end of line
if data[0] == '|' { colCount-- } - if i > 2 && data[i-1] == '|' { + if i > 2 && data[i-1] == '|' && !isBackslashEscaped(data, i-1) { colCount-- }@@ -695,7 +704,7 @@ if i >= len(data) {
return } - if data[i] == '|' { + if data[i] == '|' && !isBackslashEscaped(data, i) { i++ } for data[i] == ' ' {@@ -732,7 +741,7 @@ case dashes < 3:
// not a valid column return - case data[i] == '|': + case data[i] == '|' && !isBackslashEscaped(data, i): // marker found, now skip past trailing whitespace col++ i++@@ -745,7 +754,7 @@ if col >= colCount && data[i] != '\n' {
return } - case data[i] != '|' && col+1 < colCount: + case (data[i] != '|' || isBackslashEscaped(data, i)) && col+1 < colCount: // something else found where marker was required return@@ -771,7 +780,7 @@ func (p *parser) tableRow(out *bytes.Buffer, data []byte, columns []int) {
i, col := 0, 0 var rowWork bytes.Buffer - if data[i] == '|' { + if data[i] == '|' && !isBackslashEscaped(data, i) { i++ }@@ -782,7 +791,7 @@ }
cellStart := i - for data[i] != '|' && data[i] != '\n' { + for (data[i] != '|' || isBackslashEscaped(data, i)) && data[i] != '\n' { i++ }
M
block_test.go
→
block_test.go
@@ -686,6 +686,9 @@
"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" + "<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) }