-
Notifications
You must be signed in to change notification settings - Fork 7
/
indenter.go
124 lines (105 loc) · 3.63 KB
/
indenter.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package xmlwriter
// Indenter allows custom indenting strategies to be written for pretty
// printing the resultant XML.
//
// Writing one of these little nightmares is not for the faint of heart - it's
// a monumental pain to get indenting rules right. See StandardIndenter for a
// glimpse into the abyss.
//
type Indenter interface {
// Indent is called every time a node transitions from one State
// to another. This allows whitespace (or anything, really) to be
// injected in between any two pieces that the writer writes to the
// document.
Indent(w *Writer, last Event, next Event) error
// Wrap is called on every Text and CommentContent node - if you
// want your indenter to place text slabs into nicely formatted
// blocks, the logic goes here.
Wrap(content string) string
}
type indentLevel struct {
e Event
indents int
}
// StandardIndenter implements a primitive Indenter strategy for pretty
// printing the Writer's XML output.
//
// StandardIndenter is used by the WithIndent writer option:
// w := xmlwriter.Open(b, xmlwriter.WithIndent())
//
type StandardIndenter struct {
// Output whitespace control
IndentString string
depth int
stack []indentLevel
}
// NewStandardIndenter creates a StandardIndenter.
func NewStandardIndenter() *StandardIndenter {
si := &StandardIndenter{
IndentString: " ",
stack: make([]indentLevel, 0, initialNodeDepth),
}
si.stack = append(si.stack, indentLevel{})
return si
}
// Wrap satisfies the Indenter interface.
func (s *StandardIndenter) Wrap(content string) string {
return content
}
// Indent satisfies the Indenter interface.
func (s *StandardIndenter) Indent(w *Writer, last Event, next Event) error {
// fmt.Print(next.String())
isIndenting := (next.Node == ElemNode || next.Node == DTDNode ||
next.Node == DTDAttListNode)
isIndented := isIndenting
isIndentedState := next.State == StateOpen || next.State == StateEnded
lastIsIndenting := (last.Node == ElemNode || last.Node == DTDNode ||
last.Node == DTDAttListNode)
lastIsIndented := lastIsIndenting ||
last.Node == DTDEntityNode ||
last.Node == DTDAttrNode ||
last.Node == DTDElemNode ||
last.Node == NotationNode ||
(last.State == StateEnded && last.Node == CommentNode)
isInline := false
stackDepth := s.depth
if isIndenting {
if next.State == StateOpened {
s.stack = append(s.stack, indentLevel{e: last})
s.depth++
} else if next.State == StateEnded {
isInline = s.stack[s.depth].indents == 0
s.depth--
s.stack = s.stack[:len(s.stack)-1]
}
} else if isIndentedState {
isIndented = next.Node == DTDEntityNode ||
next.Node == DTDAttrNode ||
next.Node == DTDElemNode ||
next.Node == NotationNode ||
(next.State == StateOpen && next.Node == CommentNode)
}
// fmt.Printf(" indent:%d lii:%v lid:%v ii:%v id:%v ", s.depth, lastIsIndenting, lastIsIndented, isIndenting, isIndented)
// fmt.Printf(" %d %v ", stackDepth, isInline)
pairIsIndented := ((lastIsIndenting && isIndented) ||
(lastIsIndented && isIndenting) ||
(isIndented && lastIsIndented)) &&
last.Node != DocNode
if pairIsIndented && isIndentedState {
isEmptyElem := (next.Node == ElemNode && next.State == StateEnded && next.Children == 0)
isInlineCloser := (next.State == StateEnded && isInline)
if !isEmptyElem && !isInlineCloser {
if next.State != StateEnded {
s.stack[stackDepth].indents++
}
w.printer.WriteString(w.NewlineString)
// strings.Join massacres the heap
for i := 0; i < s.depth; i++ {
w.printer.WriteString(s.IndentString)
}
}
} else if next.Node == DocNode && next.State == StateEnded {
w.printer.WriteString(w.NewlineString)
}
return w.printer.cachedWriteError()
}