-
Notifications
You must be signed in to change notification settings - Fork 0
/
printers.go
54 lines (45 loc) · 1.01 KB
/
printers.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
package notion
import (
"bytes"
"fmt"
"github.com/tmc/notion/notiontypes"
)
type vimPrinter struct {
buf *bytes.Buffer
indent string
indentBy string
}
func (v *vimPrinter) W(s string) {
v.w(s)
v.w("\n")
}
func (v *vimPrinter) w(s string) {
v.buf.WriteString(v.indent)
v.buf.WriteString(s)
}
func (v *vimPrinter) print(block *notiontypes.Block) error {
v.W(fmt.Sprintf("%v %v %v {{{", block.Title, block.Type, block.ID))
v.incIndent()
//spew.Dump(block)
for _, b := range block.InlineContent {
v.W(b.Text)
}
for _, b := range block.Content {
v.print(b)
}
v.decIndent()
v.W("}}}")
return nil
}
func (v *vimPrinter) incIndent() {
v.indent += v.indentBy
}
func (v *vimPrinter) decIndent() {
v.indent = string(v.indent[:len(v.indent)-len(v.indentBy)])
}
// PrintAsVim renders a notion block as a vim block.
func PrintAsVim(block *notiontypes.Block, indent string) ([]byte, error) {
v := &vimPrinter{buf: new(bytes.Buffer), indentBy: indent}
err := v.print(block)
return v.buf.Bytes(), err
}