-
Notifications
You must be signed in to change notification settings - Fork 49
/
formatter.go
51 lines (44 loc) · 1.16 KB
/
formatter.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
package ace
import "bytes"
import "strings"
// File represents a file.
type outputFormatter interface {
OpeningElement(*bytes.Buffer, element) (int, error)
ClosingElement(*bytes.Buffer, element) (int, error)
WritingTextValue(*bytes.Buffer, element) (int, error)
}
type Formatter struct {
indent string
}
func newFormatter(indent string) outputFormatter {
f := &Formatter{
indent: indent,
}
return f
}
func (f *Formatter) OpeningElement(bf *bytes.Buffer, e element) (int, error) {
if e.IsControlElement() {
return 0, nil
}
base := e.Base()
if base.parent != nil && base.parent.IsBlockElement() {
return f.writeIndent(bf, base.ln.indent)
}
return 0, nil
}
func (f *Formatter) ClosingElement(bf *bytes.Buffer, e element) (int, error) {
if e.IsBlockElement() {
return f.writeIndent(bf, e.Base().ln.indent)
}
return 0, nil
}
func (f *Formatter) WritingTextValue(bf *bytes.Buffer, e element) (int, error) {
if e.IsBlockElement() {
return f.writeIndent(bf, e.Base().ln.indent+1)
}
return 0, nil
}
func (f *Formatter) writeIndent(bf *bytes.Buffer, depth int) (int, error) {
indent := "\n" + strings.Repeat(f.indent, depth)
return bf.WriteString(indent)
}