/
gotext.go
55 lines (48 loc) · 1.46 KB
/
gotext.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
package processors
import (
"fmt"
"html"
"io"
"github.com/linuxerwang/goats-html/util"
)
type GoTextProcessor struct {
BaseProcessor
text string
}
func (i *GoTextProcessor) Process(writer io.Writer, ctx *TagContext) {
// i.text is the (possiblly) merged text, its white space handling logic:
// - If before normalizing it has leading white space,
// it should have one leading space after normalizing.
// - If before normalizing it has trailing white space,
// it should have one trailing space after normalizing.
// - For all white spaces in the middle, reduces consecutive white spaces to one space.
//
hasLeadingSpace := util.HasLeadingSpace(i.text)
hasTrailingSpace := util.HasTrailingSpace(i.text)
text := util.NormalizeText(i.text)
if text != "" {
if hasLeadingSpace {
text = " " + text
}
if hasTrailingSpace {
text = text + " "
}
if ctx.AutoEscape {
text = html.EscapeString(text)
}
switch ctx.OutputFormat {
case "go":
io.WriteString(writer, fmt.Sprintf("__impl.WriteString(\"%s\")\n", text))
case "closure":
id := ctx.NextId()
io.WriteString(writer, fmt.Sprintf("var __text_node_%d = goog.dom.createTextNode(\"%s\");\n", id, text))
io.WriteString(writer, fmt.Sprintf("goog.dom.appendChild(__tag_stack[__tag_stack.length-1], __text_node_%d);\n", id))
}
}
// go text is a terminal processor.
}
func NewTextProcessor(text string) *GoTextProcessor {
return &GoTextProcessor{
text: text,
}
}