-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.go
365 lines (323 loc) · 7.01 KB
/
content.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package plugin
import (
"fmt"
"slices"
"strings"
"sync"
)
type idStore struct {
id uint32
sync.Mutex
}
func (i *idStore) next() uint32 {
i.Lock()
defer i.Unlock()
i.id++
return i.id
}
type LocationEffect int
const (
LocationEffectUnspecified LocationEffect = iota
LocationEffectBefore
LocationEffectAfter
)
type Location struct {
Index uint32
Effect LocationEffect
}
type ContentResult struct {
Location *Location
Content Content
}
type Content interface {
setID(id uint32)
setMeta(meta *ContentMeta)
AsData() Data
ID() uint32
AsJQData() Data
Meta() *ContentMeta
Print() string
}
type ContentEmpty struct {
id uint32
meta *ContentMeta
}
func (n *ContentEmpty) setID(id uint32) {
n.id = id
}
func (n *ContentEmpty) setMeta(meta *ContentMeta) {
n.meta = meta
}
func (n *ContentEmpty) AsData() Data {
return MapData{
"type": StringData("empty"),
"id": NumberData(n.id),
"meta": n.meta.AsData(),
}
}
func (n *ContentEmpty) ID() uint32 {
return n.id
}
func (n *ContentEmpty) Meta() *ContentMeta {
return n.meta
}
func (n *ContentEmpty) Print() string {
return ""
}
func (n *ContentEmpty) AsJQData() Data {
return n.AsData()
}
type ContentSection struct {
*idStore
id uint32
Children []Content
meta *ContentMeta
}
// Print returns the content tree as a string.
func (c *ContentSection) Print() string {
list := make([]string, len(c.Children))
for i, child := range c.Children {
list[i] = child.Print()
}
return strings.Join(list, "\n\n")
}
// Add content to the content tree.
func (c *ContentSection) Add(content Content, loc *Location) error {
return addContent(c, content, loc)
}
func addContent(parent *ContentSection, child Content, loc *Location) error {
if parent.idStore == nil {
parent.idStore = &idStore{}
}
if section, ok := child.(*ContentSection); ok {
section.idStore = parent.idStore
}
if loc == nil {
child.setID(parent.next())
parent.Children = append(parent.Children, child)
return nil
}
if loc.Effect != LocationEffectUnspecified {
child.setID(parent.next())
} else {
child.setID(loc.Index)
}
foundIdx := slices.IndexFunc(parent.Children, func(c Content) bool {
return c.ID() == loc.Index
})
if foundIdx > -1 {
switch loc.Effect {
case LocationEffectBefore:
parent.Children = append(parent.Children[:foundIdx], append([]Content{child}, parent.Children[foundIdx:]...)...)
case LocationEffectAfter:
parent.Children = append(parent.Children[:foundIdx+1], append([]Content{child}, parent.Children[foundIdx+1:]...)...)
default:
parent.Children[foundIdx] = child
}
return nil
}
for _, c := range parent.Children {
section, ok := c.(*ContentSection)
if !ok {
continue
}
err := addContent(section, child, loc)
if err == ErrContentLocationNotFound {
continue
} else if err != nil {
return err
}
}
return ErrContentLocationNotFound
}
func (c *ContentSection) setID(id uint32) {
c.id = id
}
func (c *ContentSection) setMeta(meta *ContentMeta) {
c.meta = meta
for _, child := range c.Children {
child.setMeta(meta)
}
}
func (c *ContentSection) ID() uint32 {
return c.id
}
func (c *ContentSection) Meta() *ContentMeta {
return c.meta
}
func (c *ContentSection) AsJQData() Data {
return c.AsData()
}
// Compact removes empty sections from the content tree.
func (c *ContentSection) Compact() {
c.Children = slices.DeleteFunc(c.Children, func(c Content) bool {
_, ok := c.(*ContentEmpty)
return ok
})
for _, child := range c.Children {
if section, ok := child.(*ContentSection); ok {
section.Compact()
}
}
}
// AsData returns the content tree as a map.
func (c *ContentSection) AsData() Data {
if c == nil {
return nil
}
children := make(ListData, len(c.Children))
for i, child := range c.Children {
children[i] = child.AsData()
}
return MapData{
"type": StringData("section"),
"id": NumberData(c.id),
"children": children,
"meta": c.meta.AsData(),
}
}
type ContentElement struct {
id uint32
Markdown string
meta *ContentMeta
}
func (c *ContentElement) ID() uint32 {
return c.id
}
func (c *ContentElement) Print() string {
return c.Markdown
}
func (c *ContentElement) setID(id uint32) {
c.id = id
}
func (c *ContentElement) Meta() *ContentMeta {
return c.meta
}
func (c *ContentElement) setMeta(meta *ContentMeta) {
c.meta = meta
}
func (c *ContentElement) AsJQData() Data {
return c.AsData()
}
func (c *ContentElement) AsData() Data {
if c == nil {
return nil
}
return MapData{
"type": StringData("element"),
"id": NumberData(c.id),
"markdown": StringData(c.Markdown),
"meta": c.meta.AsData(),
}
}
type ContentMeta struct {
Provider string
Plugin string
Version string
}
func (meta *ContentMeta) AsData() Data {
if meta == nil {
return nil
}
return MapData{
"provider": StringData(meta.Provider),
"plugin": StringData(meta.Plugin),
"version": StringData(meta.Version),
}
}
func ParseContentData(data MapData) (Content, error) {
if data == nil {
return nil, nil
}
typ, ok := data["type"].(StringData)
if !ok {
return nil, fmt.Errorf("missing type")
}
switch string(typ) {
case "section":
return parseContentSection(data)
case "element":
return parseContentElement(data)
case "empty":
return parseContentEmpty(data)
default:
return nil, fmt.Errorf("unknown type: %s", typ)
}
}
func parseContentSection(data MapData) (*ContentSection, error) {
if data == nil {
return nil, nil
}
section := &ContentSection{}
children, ok := data["children"].(ListData)
if !ok {
return nil, fmt.Errorf("missing children")
}
section.Children = make([]Content, len(children))
var err error
for i, child := range children {
section.Children[i], err = ParseContentData(child.(MapData))
if err != nil {
return nil, err
}
}
id, ok := data["id"].(NumberData)
if ok {
section.id = uint32(id)
}
meta, ok := data["meta"].(MapData)
if ok {
section.meta = ParseContentMeta(meta)
}
return section, nil
}
func parseContentElement(data MapData) (*ContentElement, error) {
if data == nil {
return nil, nil
}
elem := &ContentElement{}
markdown, ok := data["markdown"].(StringData)
if !ok {
return nil, fmt.Errorf("missing markdown")
}
elem.Markdown = string(markdown)
id, ok := data["id"].(NumberData)
if ok {
elem.id = uint32(id)
}
meta, ok := data["meta"].(MapData)
if ok {
elem.meta = ParseContentMeta(meta)
}
return elem, nil
}
func parseContentEmpty(data MapData) (*ContentEmpty, error) {
if data == nil {
return nil, nil
}
empty := &ContentEmpty{}
id, ok := data["id"].(NumberData)
if !ok {
return nil, fmt.Errorf("missing id")
}
empty.id = uint32(id)
meta, ok := data["meta"].(MapData)
if ok {
empty.meta = ParseContentMeta(meta)
}
return empty, nil
}
func ParseContentMeta(data Data) *ContentMeta {
if data == nil {
return nil
}
meta := data.(MapData)
provider, _ := meta["provider"].(StringData)
plugin, _ := meta["plugin"].(StringData)
version, _ := meta["version"].(StringData)
return &ContentMeta{
Provider: string(provider),
Plugin: string(plugin),
Version: string(version),
}
}