-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
body.go
58 lines (45 loc) · 902 Bytes
/
body.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
package hype
import (
"encoding/json"
)
// Body is a container for all the elements in a document.
type Body struct {
*Element
}
func (b *Body) MarshalJSON() ([]byte, error) {
if b == nil {
return nil, ErrIsNil("body")
}
b.RLock()
defer b.RUnlock()
m, err := b.JSONMap()
if err != nil {
return nil, err
}
m["type"] = toType(b)
return json.MarshalIndent(m, "", " ")
}
// AsPage returns the body as a Page.
func (b *Body) AsPage() *Page {
return &Page{
Element: b.Element,
}
}
// NewBody creates a new Body.
func NewBody(el *Element) (*Body, error) {
if el == nil {
return nil, el.WrapErr(ErrIsNil("element"))
}
body := &Body{
Element: el,
}
return body, nil
}
// NewBodyNodes implements the ParseElementFn type
func NewBodyNodes(p *Parser, el *Element) (Nodes, error) {
body, err := NewBody(el)
if err != nil {
return nil, err
}
return Nodes{body}, nil
}