-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
format.go
70 lines (59 loc) · 2.04 KB
/
format.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
package frontmatter
import (
"encoding/json"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
)
// UnmarshalFunc decodes the passed in `data` and stores it into
// the value pointed to by `v`.
type UnmarshalFunc func(data []byte, v interface{}) error
// Format describes a front matter. It holds all the information
// necessary in order to detect and decode a front matter format.
type Format struct {
// Start defines the starting delimiter of the front matter.
// E.g.: `---` or `---yaml`.
Start string
// End defines the ending delimiter of the front matter.
// E.g.: `---`.
End string
// Unmarshal defines the unmarshal function used to decode
// the front matter data, after it has been detected.
// E.g.: json.Unmarshal (from the `encoding/json` package).
Unmarshal UnmarshalFunc
// UnmarshalDelims specifies whether the front matter
// delimiters are included in the data to be unmarshaled.
// Should be `false` in most cases.
UnmarshalDelims bool
// RequiresNewLine specifies whether a new (empty) line is
// required after the front matter.
// Should be `false` in most cases.
RequiresNewLine bool
}
// NewFormat returns a new front matter format.
func NewFormat(start, end string, unmarshal UnmarshalFunc) *Format {
return newFormat(start, end, unmarshal, false, false)
}
func newFormat(start, end string, unmarshal UnmarshalFunc,
unmarshalDelims, requiresNewLine bool) *Format {
return &Format{
Start: start,
End: end,
Unmarshal: unmarshal,
UnmarshalDelims: unmarshalDelims,
RequiresNewLine: requiresNewLine,
}
}
func defaultFormats() []*Format {
return []*Format{
// YAML.
newFormat("---", "---", yaml.Unmarshal, false, false),
newFormat("---yaml", "---", yaml.Unmarshal, false, false),
// TOML.
newFormat("+++", "+++", toml.Unmarshal, false, false),
newFormat("---toml", "---", toml.Unmarshal, false, false),
// JSON.
newFormat(";;;", ";;;", json.Unmarshal, false, false),
newFormat("---json", "---", json.Unmarshal, false, false),
newFormat("{", "}", json.Unmarshal, true, true),
}
}