forked from SoCloz/goprismic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structured_text.go
128 lines (117 loc) · 2.66 KB
/
structured_text.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
package fragment
import (
"fmt"
"github.com/anilamilineni/goprismic/fragment/block"
"github.com/anilamilineni/goprismic/fragment/link"
)
// A structured text fragment is a list of blocks
type StructuredText []block.Block
func NewStructuredText(enc interface{}) (*StructuredText, error) {
dec, ok := enc.([]interface{})
if !ok {
return nil, fmt.Errorf("%#v is not a slice", enc)
}
st := make(StructuredText, 0, len(dec))
return &st, nil
}
func (st *StructuredText) Decode(_ string, enc interface{}) error {
dec := enc.([]interface{})
for _, v := range dec {
dec, ok := v.(map[string]interface{})
if !ok {
return fmt.Errorf("%+v is not a map", v)
}
var b block.Block
switch dec["type"] {
case "heading1", "heading2", "heading3", "heading4", "heading5", "heading6":
b = new(block.Heading)
case "paragraph":
b = new(block.Paragraph)
case "preformatted":
b = new(block.Preformatted)
case "list-item":
b = new(block.ListItem)
case "o-list-item":
b = new(block.OrderedListItem)
case "image":
b = new(block.Image)
case "embed":
b = new(block.Embed)
default:
panic(fmt.Sprintf("unknown block type %s", dec["type"]))
}
err := b.Decode(v)
if err != nil {
return err
}
*st = append(*st, b)
}
return nil
}
// Formats the fragment content as html
func (st StructuredText) AsHtml() string {
parentTag := ""
html := ""
for _, v := range st {
if parentTag != v.ParentHtmlTag() {
if parentTag != "" {
html += fmt.Sprintf("</%s>", parentTag)
}
parentTag = v.ParentHtmlTag()
if parentTag != "" {
html += fmt.Sprintf("<%s>", parentTag)
}
}
html += v.AsHtml()
}
if parentTag != "" {
html += fmt.Sprintf("</%s>", parentTag)
}
return html
}
// Formats the fragment content as text
func (st StructuredText) AsText() string {
text := ""
for _, v := range st {
if text != "" {
text += "\n"
}
text += v.AsText()
}
return text
}
// Returns the first paragraph fragment
func (st StructuredText) GetFirstParagraph() (*block.Paragraph, bool) {
for k := range st {
b, ok := st[k].(*block.Paragraph)
if ok {
return b, true
}
}
return nil, false
}
// Returns the first preformatted fragment
func (st StructuredText) GetFirstPreformatted() (*block.Preformatted, bool) {
for k := range st {
b, ok := st[k].(*block.Preformatted)
if ok {
return b, true
}
}
return nil, false
}
// Returns the first image fragment
func (st StructuredText) GetFirstImage() (*block.Image, bool) {
for k := range st {
b, ok := st[k].(*block.Image)
if ok {
return b, true
}
}
return nil, false
}
func (st StructuredText) ResolveLinks(r link.Resolver) {
for _, v := range st {
v.ResolveLinks(r)
}
}