~shulhan/asciidoctor-go

ref: v0.1.0 asciidoctor-go/element_include.go -rw-r--r-- 1021 bytes
4c1ded16Shulhan Release asciidoctor-go v0.1.0 (2021-03-06) 4 months ago
                                                                                
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
// Copyright 2020, Shulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package asciidoctor

import (
	"bytes"
	"io/ioutil"
	"log"
	"path/filepath"
)

type elementInclude struct {
	fpath   string
	attrs   elementAttribute
	content []byte
}

func parseInclude(doc *Document, line []byte) (el *elementInclude) {
	var 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.fpath), string(path))

	el.content, err = ioutil.ReadFile(el.fpath)
	if err != nil {
		log.Printf("parseInclude %q: %s", doc.file, err)
		return nil
	}

	return el
}