-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
include.go
176 lines (134 loc) · 2.57 KB
/
include.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
package hype
import (
"encoding/json"
"fmt"
"path/filepath"
"strings"
"sync"
)
type Include struct {
*Element
dir string
pp sync.Once
}
func (inc *Include) MarshalJSON() ([]byte, error) {
if inc == nil {
return nil, ErrIsNil("include")
}
inc.RLock()
defer inc.RUnlock()
m, err := inc.JSONMap()
if err != nil {
return nil, err
}
m["type"] = toType(inc)
if inc.dir != "" {
m["dir"] = inc.dir
}
return json.MarshalIndent(m, "", " ")
}
func (inc *Include) PostParse(p *Parser, d *Document, err error) error {
if err != nil {
return nil
}
if inc == nil {
return ErrIsNil("include")
}
if err := inc.setSources(); err != nil {
return err
}
return nil
}
func (inc *Include) String() string {
if inc == nil {
return ""
}
return inc.Children().String()
}
func (inc *Include) MD() string {
if inc == nil {
return ""
}
return inc.Children().MD()
}
func NewInclude(p *Parser, el *Element) (*Include, error) {
if p == nil {
return nil, ErrIsNil("parser")
}
if el == nil {
return nil, ErrIsNil("element")
}
if p.FS == nil {
return nil, ErrIsNil("p.FS")
}
src, ok := el.Get("src")
if !ok {
return nil, fmt.Errorf("missing src attribute")
}
sdir := filepath.Dir(src)
base := filepath.Base(src)
p2, err := p.Sub(sdir)
if err != nil {
return nil, err
}
doc, err := p2.ParseFile(base)
if err != nil {
return nil, err
}
body, err := doc.Body()
if err != nil {
return nil, err
}
inc := &Include{
Element: el,
dir: sdir,
}
inc.Nodes = body.Nodes
fn := func(i int, fig *Figure) (string, error) {
id, err := fig.ValidAttr("id")
if err != nil {
return "", err
}
return fmt.Sprintf("%s.%d#%s", src, i, id), nil
}
if err := RestripeFigureIDs(inc.Nodes, fn); err != nil {
return nil, ParseError{
Err: err,
Filename: p.Filename,
Root: p.Root,
}
}
return inc, nil
}
// NewIncludeNodes implements the ParseElementFn type
func NewIncludeNodes(p *Parser, el *Element) (Nodes, error) {
inc, err := NewInclude(p, el)
if err != nil {
return nil, err
}
return Nodes{inc}, nil
}
func (inc *Include) setSources() error {
if inc == nil {
return nil
}
var err error
inc.pp.Do(func() {
inc.Nodes.updateFileName(inc.dir)
kids := ByAttrs(inc.Children(), map[string]string{
"src": "*",
})
for _, n := range kids {
ats := n.Attrs()
src, _ := ats.Get("src")
if strings.HasPrefix(src, "http") || strings.HasPrefix(src, inc.dir) {
continue
}
src = filepath.Join(inc.dir, src)
if err = ats.Set("src", src); err != nil {
return
}
}
})
return err
}