-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_writer.go
158 lines (126 loc) · 2.81 KB
/
output_writer.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package generator
import (
"bufio"
"fmt"
"html"
"io"
"strings"
)
type outputWriter struct {
w io.Writer
indentation int
instrs []Instruction
}
func (w *outputWriter) add(i Instruction) {
w.instrs = append(w.instrs, i)
}
func (w *outputWriter) indent(delta int) {
w.indentation += delta
}
func (w *outputWriter) writeIndentation() {
w.add(&InstructionIndentation{
Depth: w.indentation,
})
}
func (w *outputWriter) Close() error {
var litBuf strings.Builder
for i := 0; i < len(w.instrs); i++ {
inst := w.instrs[i]
switch inst := inst.(type) {
case *InstructionLiteral:
litBuf.WriteString(inst.String)
continue
case *InstructionIndentation:
if i == len(w.instrs)-1 {
break
}
_, nextIsLit := w.instrs[i+1].(*InstructionLiteral)
if nextIsLit {
continue
}
}
if litBuf.Len() > 0 {
writeLiteral(w.w, litBuf.String())
litBuf.Reset()
}
inst.WriteTo(w.w)
}
return nil
}
func (w *outputWriter) WriteFileHeader(pkg string, imports []string) {
w.add(&InstructionFileHeader{
Package: pkg,
Imports: imports,
})
}
func (w *outputWriter) WriteFuncHeader(name string, args []string) {
w.add(&InstructionWriteFuncHeader{
Name: name,
Args: args,
})
w.indent(1)
}
func (w *outputWriter) WriteLiteralUnescaped(str string) {
w.writeIndentation()
w.add(&InstructionLiteral{String: str})
}
func (w *outputWriter) WriteLiteralUnescapedf(format string, a ...any) {
w.WriteLiteralUnescaped(fmt.Sprintf(format, a...))
}
func (w *outputWriter) WriteLiteralEscaped(str string) {
w.WriteLiteralUnescaped(html.EscapeString(str))
}
func (w *outputWriter) WriteLiteralEscapedf(format string, a ...any) {
w.WriteLiteralEscaped(fmt.Sprintf(format, a...))
}
func (w *outputWriter) WriteGoUnescaped(str string) {
w.writeIndentation()
w.add(&InstructionGo{
Value: str,
HTMLEscape: false,
})
}
func (w *outputWriter) WriteGoEscaped(str string) {
w.writeIndentation()
w.add(&InstructionGo{
Value: str,
HTMLEscape: true,
})
}
func (w *outputWriter) WriteStatementStart(indent bool, keyword string, arg string) {
if indent {
w.writeIndentation()
}
w.add(&InstructionStatementStart{
Keyword: keyword,
Arg: arg,
})
w.indent(1)
}
func (w *outputWriter) WriteVariable(name, typ, value string) {
w.writeIndentation()
w.add(&InstructionVariable{
Name: name,
Type: typ,
Value: value,
})
}
func (w *outputWriter) WriteBlockStart() {
w.writeIndentation()
w.add(&InstructionBlockStart{})
w.indent(1)
}
func (w *outputWriter) WriteBlockEnd(newLine bool) {
w.indent(-1)
w.writeIndentation()
w.add(&InstructionBlockEnd{Newline: newLine})
}
func (w *outputWriter) WriteGoBlock(contents string) {
sc := bufio.NewScanner(strings.NewReader(contents))
for sc.Scan() {
w.writeIndentation()
w.add(&InstructionGoLine{
Content: sc.Bytes(),
})
}
}