1
0
mirror of https://github.com/danog/blackfriday.git synced 2024-11-26 20:14:43 +01:00

Redirect output to a capture buffer where necessary

Use CaptureWrites where output should go to a temp buffer instead of the
final output.
This commit is contained in:
Vytautas Šaltenis 2015-11-04 22:18:46 +02:00
parent 08233481ed
commit 91771dc3ef
2 changed files with 35 additions and 20 deletions

View File

@ -852,13 +852,14 @@ func (p *parser) tableRow(out *bytes.Buffer, data []byte, columns []int, header
cellEnd--
}
var cellWork bytes.Buffer
p.inline(&cellWork, data[cellStart:cellEnd])
cellWork := p.r.CaptureWrites(func() {
p.inline(data[cellStart:cellEnd])
})
if header {
p.r.TableHeaderCell(&rowWork, cellWork.Bytes(), columns[col])
p.r.TableHeaderCell(&rowWork, cellWork, columns[col])
} else {
p.r.TableCell(&rowWork, cellWork.Bytes(), columns[col])
p.r.TableCell(&rowWork, cellWork, columns[col])
}
}
@ -936,9 +937,9 @@ func (p *parser) quote(data []byte) int {
beg = end
}
var cooked bytes.Buffer
p.block(&cooked, raw.Bytes())
p.r.BlockQuote(cooked.Bytes())
p.r.BlockQuote(p.r.CaptureWrites(func() {
p.block(raw.Bytes())
}))
return end
}
@ -1223,18 +1224,26 @@ gatherlines:
if *flags&ListItemContainsBlock != 0 && *flags&ListTypeTerm == 0 {
// intermediate render of block item, except for definition term
if sublist > 0 {
p.block(&cooked, rawBytes[:sublist])
p.block(&cooked, rawBytes[sublist:])
cooked.Write(p.r.CaptureWrites(func() {
p.block(rawBytes[:sublist])
p.block(rawBytes[sublist:])
}))
} else {
p.block(&cooked, rawBytes)
cooked.Write(p.r.CaptureWrites(func() {
p.block(rawBytes)
}))
}
} else {
// intermediate render of inline item
if sublist > 0 {
p.inline(&cooked, rawBytes[:sublist])
p.block(&cooked, rawBytes[sublist:])
cooked.Write(p.r.CaptureWrites(func() {
p.inline(rawBytes[:sublist])
p.block(rawBytes[sublist:])
}))
} else {
p.inline(&cooked, rawBytes)
cooked.Write(p.r.CaptureWrites(func() {
p.inline(rawBytes)
}))
}
}

View File

@ -549,7 +549,9 @@ func link(p *parser, data []byte, offset int) int {
// links cannot contain other links, so turn off link parsing temporarily
insideLink := p.insideLink
p.insideLink = true
p.inline(&content, data[1:txtE])
content.Write(p.r.CaptureWrites(func() {
p.inline(data[1:txtE])
}))
p.insideLink = insideLink
}
}
@ -1099,9 +1101,10 @@ func helperEmphasis(p *parser, data []byte, c byte) int {
}
}
var work bytes.Buffer
p.inline(&work, data[:i])
p.r.Emphasis(work.Bytes())
work := p.r.CaptureWrites(func() {
p.inline(data[:i])
})
p.r.Emphasis(work)
return i + 1
}
}
@ -1121,7 +1124,9 @@ func helperDoubleEmphasis(p *parser, data []byte, c byte) int {
if i+1 < len(data) && data[i] == c && data[i+1] == c && i > 0 && !isspace(data[i-1]) {
var work bytes.Buffer
p.inline(&work, data[:i])
work.Write(p.r.CaptureWrites(func() {
p.inline(data[:i])
}))
if work.Len() > 0 {
// pick the right renderer
@ -1159,8 +1164,9 @@ func helperTripleEmphasis(p *parser, data []byte, offset int, c byte) int {
case i+2 < len(data) && data[i+1] == c && data[i+2] == c:
// triple symbol found
var work bytes.Buffer
p.inline(&work, data[:i])
work.Write(p.r.CaptureWrites(func() {
p.inline(data[:i])
}))
if work.Len() > 0 {
p.r.TripleEmphasis(work.Bytes())
}