Skip to content
Browse files

permit backslash-escaped vertical bars in tables

  • Loading branch information...
1 parent 583b3c5 commit b97990f1bba5a0a50de2574c1bbc3697374a44f0 Russ Ross committed Sep 13, 2011
Showing with 19 additions and 7 deletions.
  1. +16 −7 block.go
  2. +3 −0 block_test.go
View
23 block.go
@@ -662,11 +662,20 @@ func (p *parser) table(out *bytes.Buffer, data []byte) int {
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 @@ func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns
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 @@ func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns
return
}
- if data[i] == '|' {
+ if data[i] == '|' && !isBackslashEscaped(data, i) {
i++
}
for data[i] == ' ' {
@@ -732,7 +741,7 @@ func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns
// 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 @@ func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns
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 @@ func (p *parser) tableRow(out *bytes.Buffer, data []byte, columns []int) {
cellStart := i
- for data[i] != '|' && data[i] != '\n' {
+ for (data[i] != '|' || isBackslashEscaped(data, i)) && data[i] != '\n' {
i++
}
View
3 block_test.go
@@ -686,6 +686,9 @@ func TestTable(t *testing.T) {
"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)
}

0 comments on commit b97990f

Please sign in to comment.
Something went wrong with that request. Please try again.