From f8378658c0b0a9f073eade7aa8f652d94b594219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vytautas=20=C5=A0altenis?= Date: Mon, 9 Nov 2015 21:57:27 +0200 Subject: [PATCH] Fix rendering of tables Add proper capture groups to fix the order of bits and pieces. --- block.go | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/block.go b/block.go index 5b93a98..d768e6e 100644 --- a/block.go +++ b/block.go @@ -689,23 +689,25 @@ func (p *parser) table(data []byte) int { var body bytes.Buffer - for i < len(data) { - pipes, rowStart := 0, i - for ; data[i] != '\n'; i++ { - if data[i] == '|' { - pipes++ + body.Write(p.r.CaptureWrites(func() { + for i < len(data) { + pipes, rowStart := 0, i + for ; data[i] != '\n'; i++ { + if data[i] == '|' { + pipes++ + } } - } - if pipes == 0 { - i = rowStart - break - } + if pipes == 0 { + i = rowStart + break + } - // include the newline in data sent to tableRow - i++ - p.tableRow(&body, data[rowStart:i], columns, false) - } + // include the newline in data sent to tableRow + i++ + p.tableRow(data[rowStart:i], columns, false) + } + })) p.r.Table(header.Bytes(), body.Bytes(), columns) @@ -819,12 +821,14 @@ func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns return } - p.tableRow(out, header, columns, true) + out.Write(p.r.CaptureWrites(func() { + p.tableRow(header, columns, true) + })) size = i + 1 return } -func (p *parser) tableRow(out *bytes.Buffer, data []byte, columns []int, header bool) { +func (p *parser) tableRow(data []byte, columns []int, header bool) { i, col := 0, 0 var rowWork bytes.Buffer