Render each paragraph individually and reflow text only inside paragraphs

This commit is contained in:
Christian Muehlhaeuser
2019-11-28 02:23:29 +01:00
parent a601b62b90
commit 59b8ecde6d
3 changed files with 26 additions and 12 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ require (
github.com/logrusorgru/aurora v0.0.0-20191017060258-dc85c304c434
github.com/mattn/go-isatty v0.0.10
github.com/microcosm-cc/bluemonday v1.0.2
github.com/muesli/go-wordwrap v1.0.1-0.20191125090158-10bc5504e2a8
github.com/muesli/reflow v0.0.0-20191128011458-f4f606856be2
github.com/olekukonko/tablewriter v0.0.3
github.com/rakyll/statik v0.1.6
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
+2 -2
View File
@@ -50,8 +50,8 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/muesli/go-wordwrap v1.0.1-0.20191125090158-10bc5504e2a8 h1:pkJGPjqhekH0hq/lJABlh5EhzU7eU/+VdFq9ZMOae2E=
github.com/muesli/go-wordwrap v1.0.1-0.20191125090158-10bc5504e2a8/go.mod h1:isHWaQQfvQ38DUiq7ugbuPZowGJ+9gPmACGc3tDUN4M=
github.com/muesli/reflow v0.0.0-20191128011458-f4f606856be2 h1:XvNcEIUJQ6oZic3FIGK2u1V8pF+IeREHttlW8ySBvMM=
github.com/muesli/reflow v0.0.0-20191128011458-f4f606856be2/go.mod h1:w8tGa//Xq1g6PFmbPclEY93QDv0cnBObujYoEYX/8oI=
github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E=
github.com/olekukonko/tablewriter v0.0.3 h1:i0LBnzgiChAWHJYTQAZJDOgf8MNxAVYZJ2m63SIDimI=
github.com/olekukonko/tablewriter v0.0.3/go.mod h1:YZeBtGzYYEsCHp2LST/u/0NDwGkRoBtmn1cIWCJiS6M=
+23 -9
View File
@@ -1,6 +1,7 @@
package gold
import (
"bytes"
"encoding/json"
"fmt"
"io"
@@ -10,6 +11,7 @@ import (
"os"
"github.com/microcosm-cc/bluemonday"
"github.com/muesli/reflow"
bf "gopkg.in/russross/blackfriday.v2"
)
@@ -18,9 +20,12 @@ var (
)
type TermRenderer struct {
BaseURL string
style map[StyleType]*ElementStyle
table TableElement
BaseURL string
WordWrap int
style map[StyleType]*ElementStyle
table TableElement
paragraph bytes.Buffer
}
func Render(in string, stylePath string) ([]byte, error) {
@@ -85,16 +90,20 @@ func (tr *TermRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf
e := tr.NewElement(node)
if entering {
if e.Entering != "" {
fmt.Fprintf(w, "%s", e.Entering)
if node.Type == bf.Paragraph {
w.Write(tr.paragraph.Bytes())
tr.paragraph.Reset()
}
if isChild(node) {
return bf.GoToNext
}
if e.Entering != "" {
fmt.Fprintf(&tr.paragraph, "%s", e.Entering)
}
if e.Renderer != nil {
err := e.Renderer.Render(w, node, tr)
err := e.Renderer.Render(&tr.paragraph, node, tr)
if err != nil {
fmt.Println(err)
return bf.Terminate
@@ -102,7 +111,7 @@ func (tr *TermRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf
}
} else {
if e.Finisher != nil {
err := e.Finisher.Finish(w, node, tr)
err := e.Finisher.Finish(&tr.paragraph, node, tr)
if err != nil {
fmt.Println(err)
return bf.Terminate
@@ -110,7 +119,12 @@ func (tr *TermRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf
}
if e.Exiting != "" {
fmt.Fprintf(w, "%s", e.Exiting)
fmt.Fprintf(&tr.paragraph, "%s", e.Exiting)
}
if node.Type == bf.Paragraph {
w.Write(reflow.ReflowBytes(tr.paragraph.Bytes(), tr.WordWrap))
tr.paragraph.Reset()
}
}