-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpage.go
96 lines (72 loc) · 1.34 KB
/
page.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
package hype
import (
"encoding/json"
"fmt"
)
type Page struct {
Title string
*Element
}
func (page *Page) MarshalJSON() ([]byte, error) {
if page == nil {
return nil, ErrIsNil("page")
}
m, err := page.JSONMap()
if err != nil {
return nil, err
}
m["type"] = toType(page)
if len(page.Title) > 0 {
m["title"] = page.Title
}
return json.MarshalIndent(m, "", " ")
}
func (page *Page) Body() (*Body, error) {
if page == nil {
return nil, ErrIsNil("document")
}
bodies := ByType[*Body](page.Nodes)
if len(bodies) == 0 {
return nil, ErrIsNil("body")
}
body := bodies[0]
return body, nil
}
func (page *Page) PostParse(p *Parser, d *Document, err error) error {
if err != nil {
return nil
}
if page == nil {
return ErrIsNil("page")
}
if len(page.Title) == 0 {
page.Title = FindTitle(page.Nodes)
}
mds := ByType[*Metadata](page.Children())
if len(mds) > 1 {
return fmt.Errorf("page has more than one metadata")
}
return nil
}
func (page *Page) MD() string {
if page == nil {
return ""
}
return page.Children().MD()
}
func NewPage(el *Element) (*Page, error) {
if el == nil {
return nil, ErrIsNil("el")
}
p := &Page{
Element: el,
}
return p, nil
}
func NewPageNodes(p *Parser, el *Element) (Nodes, error) {
page, err := NewPage(el)
if err != nil {
return nil, err
}
return Nodes{page}, nil
}