-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiece.go
181 lines (144 loc) · 3.59 KB
/
piece.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package dhtml
import (
"fmt"
"strings"
)
// HtmlPiece is set of one or several html elements (or no elements at all). Could be tags, complex elements, text content etc.
// Every HtmlPiece as an element itself (so it can be rendered as HTML).
type HtmlPiece struct {
list []ElementI // added elements, tags or another pieces
tagList TagList // cached rendered contents
}
// force interfaces implementation declaring fake variable
var _ ElementI = (*HtmlPiece)(nil)
var _ fmt.Stringer = (*HtmlPiece)(nil)
// If firstElement is HtmlPiece, return it.
// Else create new HtmlPiece and add firstElement to its contents.
func Piece(firstElement any) *HtmlPiece {
switch v := firstElement.(type) {
case HtmlPiece:
return &v
case *HtmlPiece:
return v
default:
return NewHtmlPiece().AppendElement(AnyToElement(firstElement))
}
}
// Actual Constructor
func NewHtmlPiece() *HtmlPiece {
return &HtmlPiece{
list: make([]ElementI, 0),
}
}
// Adds something to the piece: another piece, ElemenetI, any string, Stringer or other value.
func (p *HtmlPiece) Append(v ...any) *HtmlPiece {
for _, v := range v {
if v == nil || v == "" {
//nothing to append
continue
}
switch v := v.(type) {
case HtmlPiece:
p.AppendPiece(&v)
case *HtmlPiece:
p.AppendPiece(v)
case ElementI:
p.AppendElement(v)
default:
p.AppendElement(AnyToElement(v))
}
}
return p
}
// Adds single element
func (p *HtmlPiece) AppendElement(e ElementI) *HtmlPiece {
p.list = append(p.list, e)
return p
}
// Adds another piece elements to this one
func (p *HtmlPiece) AppendPiece(another_piece *HtmlPiece) *HtmlPiece {
if another_piece.IsEmpty() {
//nothing to add
return p
}
p.list = append(p.list, another_piece.list...)
return p
}
// Adds text element to piece
func (p *HtmlPiece) AppendText(text string) *HtmlPiece {
p.list = append(p.list, Text(text))
return p
}
// Format and add text element
func (p *HtmlPiece) Textf(format string, a ...any) *HtmlPiece {
p.list = append(p.list, Textf(format, a...))
return p
}
// Returns true if piece has no anything added to it
func (p *HtmlPiece) IsEmpty() bool {
return p.Len() == 0
}
// Elements count
func (p *HtmlPiece) Len() int {
return len(p.list)
}
// Remove all contents and cached rendered tags.
func (p *HtmlPiece) Clear() *HtmlPiece {
p.list = make([]ElementI, 0)
p.tagList = make(TagList, 0) //clear cache
return p
}
// Calls f function for each element.
func (p *HtmlPiece) Walk(f ElementWalkFunc, args ...any) {
if len(p.tagList) > 0 {
// already rendered
for _, e := range p.tagList {
f(e, args...)
}
} else {
for _, e := range p.list {
f(e, args...)
}
}
}
// Calls f function for each element with recursion.
func (p *HtmlPiece) WalkR(f ElementWalkFunc, args ...any) {
p.Walk(func(e ElementI, args ...any) {
f(e, args...)
//and dive deeper
switch v := e.(type) {
case *HtmlPiece:
v.WalkR(f, args...)
case *Tag:
v.WalkR(f, args...)
}
})
}
// ElementI implementation
func (p *HtmlPiece) GetTags() TagList {
//p.tagList - cached tags if it was already rendered
if len(p.tagList) == 0 {
for _, element := range p.list {
p.tagList = append(p.tagList, element.GetTags()...)
}
}
return p.tagList
}
// Render everything to string as HTML
func (p *HtmlPiece) String() string {
var sb strings.Builder
for _, tag := range p.GetTags() {
sb.WriteString(tag.String())
}
return sb.String()
}
// Render just text content only
func (p *HtmlPiece) RawString() string {
var sb strings.Builder
for _, tag := range p.GetTags() {
if tag.kind == tagKindText {
sb.WriteString(tag.text)
}
}
return sb.String()
}