-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement_include.go
56 lines (44 loc) · 991 Bytes
/
element_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
// SPDX-FileCopyrightText: 2020 M. Shulhan <ms@kilabit.info>
// SPDX-License-Identifier: GPL-3.0-or-later
package asciidoctor
import (
"bytes"
"log"
"os"
"path/filepath"
)
type elementInclude struct {
fpath string
content []byte
attrs elementAttribute
}
func parseInclude(doc *Document, line []byte) (el *elementInclude) {
var (
path []byte
start int
end int
err error
)
if !bytes.HasPrefix(line, []byte(prefixInclude)) {
return nil
}
el = &elementInclude{}
line = line[len(prefixInclude):]
path, start = indexByteUnescape(line, '[')
if start == -1 {
return nil
}
_, end = indexByteUnescape(line[start:], ']')
if end == -1 {
return nil
}
el.attrs.parseElementAttribute(line[start : start+end+1])
path = applySubstitutions(doc, path)
el.fpath = filepath.Join(filepath.Dir(doc.file), string(path))
el.content, err = os.ReadFile(el.fpath)
if err != nil {
log.Printf(`parseInclude %q: %s`, el.fpath, err)
return nil
}
return el
}