/
rundown_renderer.go
77 lines (59 loc) · 2 KB
/
rundown_renderer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Package renderer renders the given AST to certain formats.
package glamour
import (
"bytes"
"io"
"github.com/elseano/rundown/pkg/renderer"
goldast "github.com/yuin/goldmark/ast"
goldrenderer "github.com/yuin/goldmark/renderer"
)
type GlamourRenderer struct {
actualRenderer goldrenderer.Renderer
Section string
Context *renderer.Context
}
// NewRenderer returns a new Renderer with given options.
func NewGlamourRenderer(actualRenderer goldrenderer.Renderer, context *renderer.Context) *GlamourRenderer {
r := &GlamourRenderer{
actualRenderer: actualRenderer,
Context: context,
}
return r
}
func (r *GlamourRenderer) AddOptions(opts ...goldrenderer.Option) {
// for _, opt := range opts {
// opt.SetConfig(r.config)
// }
}
// WalkJump indicates that the walker should jump to the next node.
const WalkJump goldast.WalkStatus = 100
type JumpError struct {
ToNode goldast.Node
ReturnToNode goldast.Node
}
func (e JumpError) Error() string {
return "Jump compatible NodeWalker required"
}
type GlamourFlushNode struct{ goldast.BaseBlock }
func (n *GlamourFlushNode) Kind() goldast.NodeKind { return goldast.KindDocument }
// Render will individually render the child (block) nodes.
// This is required as Glamour buffers block node renders until the end of the document, which
// means execution blocks are run before any output is seen.
func (r *GlamourRenderer) Render(w io.Writer, source []byte, n goldast.Node) error {
if doc, ok := n.(*goldast.Document); ok {
startingNode := doc.FirstChild()
for child := startingNode; child != nil; child = child.NextSibling() {
outputBuffer := &bytes.Buffer{}
err := r.actualRenderer.Render(outputBuffer, source, child)
if err != nil {
return err
}
// Glamour pads lines so they're the full width of the terminal. We don't want that, so trim it all.
for _, line := range bytes.Split(outputBuffer.Bytes(), []byte("\n")) {
w.Write(bytes.TrimRight(line, " "))
w.Write([]byte("\n"))
}
}
}
return nil
}