Initialize paragraph buffer in ParagraphElement

This commit is contained in:
Christian Muehlhaeuser
2019-12-02 15:22:30 +01:00
parent 011c602e64
commit bbd7dfecce
2 changed files with 7 additions and 6 deletions
+3 -6
View File
@@ -98,9 +98,7 @@ func (tr *TermRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf
_, _ = tr.document.Write([]byte(e.Entering))
}
if node.Type == bf.Paragraph {
tr.paragraph = &bytes.Buffer{}
}
// each paragraph gets rendered into a separate buffer
if tr.paragraph != nil {
writeTo = tr.paragraph
}
@@ -113,6 +111,8 @@ func (tr *TermRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf
}
}
} else {
// if we're finished rendering the entire document,
// flush to the real writer
if node.Type == bf.Document {
writeTo = w
}
@@ -125,9 +125,6 @@ func (tr *TermRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf
}
}
if node.Type == bf.Paragraph {
tr.paragraph = nil
}
if e.Exiting != "" {
_, _ = tr.document.Write([]byte(e.Exiting))
}
+4
View File
@@ -1,6 +1,7 @@
package gold
import (
"bytes"
"io"
"github.com/muesli/reflow"
@@ -11,6 +12,8 @@ type ParagraphElement struct {
}
func (e *ParagraphElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error {
tr.paragraph = &bytes.Buffer{}
pre := "\n"
if node.Prev == nil || (node.Parent != nil && node.Parent.Type == bf.Item) {
pre = ""
@@ -48,5 +51,6 @@ func (e *ParagraphElement) Finish(w io.Writer, node *bf.Node, tr *TermRenderer)
_, err := iw.Write(reflow.ReflowBytes(tr.paragraph.Bytes(), tr.WordWrap-int(indent)))
tr.paragraph.Reset()
tr.paragraph = nil
return err
}