From 3d9314f0a49bd32c1e47530fa769bd3c5c9694c6 Mon Sep 17 00:00:00 2001 From: Timur Demin Date: Sat, 7 Nov 2020 19:27:55 +0500 Subject: [PATCH] Make blockquote read stuff not byte-by-byte --- internal/gemini/renderer.go | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/internal/gemini/renderer.go b/internal/gemini/renderer.go index a21798b..b3ce9c5 100644 --- a/internal/gemini/renderer.go +++ b/internal/gemini/renderer.go @@ -3,17 +3,18 @@ package gemini import ( + "bufio" + "bytes" "io" "github.com/gomarkdown/markdown/ast" ) var ( - lineBreak = []byte{'\n'} - lineBreakByte byte = 0x0a - space = []byte{' '} - linkPrefix = []byte("=> ") - quotePrefix = []byte("> ") + lineBreak = []byte{'\n'} + space = []byte{' '} + linkPrefix = []byte("=> ") + quotePrefix = []byte("> ") ) // Renderer implements markdown.Renderer. @@ -75,23 +76,18 @@ func (r Renderer) blockquote(w io.Writer, node *ast.BlockQuote, entering bool) { // TODO: Renderer.blockquote: needs support for subnode rendering; // ideally to be merged with paragraph if entering { - w.Write(quotePrefix) if para, ok := node.Children[0].(*ast.Paragraph); ok { for _, subnode := range para.Children { if l := subnode.AsLeaf(); l != nil { - // TODO: Renderer.blockquote: rendering by byte is asking - // for optimizations - for _, b := range l.Literal { - w.Write([]byte{b}) - if b == lineBreakByte { - w.Write(quotePrefix) - } + reader := bufio.NewScanner(bytes.NewBuffer(l.Literal)) + for reader.Scan() { + w.Write(quotePrefix) + w.Write(reader.Bytes()) + w.Write(lineBreak) } } } } - } else { - w.Write(lineBreak) } }