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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
// Copyright © 2013 Steve Francia <spf@spf13.com>.
//
// Licensed under the Simple Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://opensource.org/licenses/Simple-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package parser
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
)
type FrontmatterType struct {
markstart, markend []byte
Parse func([]byte) (interface{}, error)
includeMark bool
}
func InterfaceToConfig(in interface{}, mark rune) ([]byte, error) {
if in == nil {
return []byte{}, fmt.Errorf("input was nil")
}
b := new(bytes.Buffer)
switch mark {
case rune(YAML_LEAD[0]):
by, err := yaml.Marshal(in)
if err != nil {
return nil, err
}
b.Write(by)
_, err = b.Write([]byte("..."))
if err != nil {
return nil, err
}
return b.Bytes(), nil
case rune(TOML_LEAD[0]):
err := toml.NewEncoder(b).Encode(in)
if err != nil {
return nil, err
}
return b.Bytes(), nil
case rune(JSON_LEAD[0]):
by, err := json.MarshalIndent(in, "", " ")
if err != nil {
return nil, err
}
b.Write(by)
_, err = b.Write([]byte("\n"))
if err != nil {
return nil, err
}
return b.Bytes(), nil
default:
return nil, fmt.Errorf("Unsupported Format provided")
}
}
func InterfaceToFrontMatter(in interface{}, mark rune) ([]byte, error) {
if in == nil {
return []byte{}, fmt.Errorf("input was nil")
}
b := new(bytes.Buffer)
switch mark {
case rune(YAML_LEAD[0]):
_, err := b.Write([]byte(YAML_DELIM_UNIX))
if err != nil {
return nil, err
}
by, err := yaml.Marshal(in)
if err != nil {
return nil, err
}
b.Write(by)
_, err = b.Write([]byte(YAML_DELIM_UNIX))
if err != nil {
return nil, err
}
return b.Bytes(), nil
case rune(TOML_LEAD[0]):
_, err := b.Write([]byte(TOML_DELIM_UNIX))
if err != nil {
return nil, err
}
err = toml.NewEncoder(b).Encode(in)
if err != nil {
return nil, err
}
_, err = b.Write([]byte("\n" + TOML_DELIM_UNIX))
if err != nil {
return nil, err
}
return b.Bytes(), nil
case rune(JSON_LEAD[0]):
by, err := json.MarshalIndent(in, "", " ")
if err != nil {
return nil, err
}
b.Write(by)
_, err = b.Write([]byte("\n"))
if err != nil {
return nil, err
}
return b.Bytes(), nil
default:
return nil, fmt.Errorf("Unsupported Format provided")
}
}
func FormatToLeadRune(kind string) rune {
switch FormatSanitize(kind) {
case "yaml":
return rune([]byte(YAML_LEAD)[0])
case "json":
return rune([]byte(JSON_LEAD)[0])
default:
return rune([]byte(TOML_LEAD)[0])
}
}
func FormatSanitize(kind string) string {
switch strings.ToLower(kind) {
case "yaml", "yml":
return "yaml"
case "toml", "tml":
return "toml"
case "json", "js":
return "json"
default:
return "toml"
}
}
func DetectFrontMatter(mark rune) (f *FrontmatterType) {
switch mark {
case '-':
return &FrontmatterType{[]byte(YAML_DELIM), []byte(YAML_DELIM), HandleYAMLMetaData, false}
case '+':
return &FrontmatterType{[]byte(TOML_DELIM), []byte(TOML_DELIM), HandleTOMLMetaData, false}
case '{':
return &FrontmatterType{[]byte{'{'}, []byte{'}'}, HandleJSONMetaData, true}
default:
return nil
}
}
func HandleTOMLMetaData(datum []byte) (interface{}, error) {
m := map[string]interface{}{}
datum = removeTOMLIdentifier(datum)
if _, err := toml.Decode(string(datum), &m); err != nil {
return m, err
}
return m, nil
}
func removeTOMLIdentifier(datum []byte) []byte {
return bytes.Replace(datum, []byte(TOML_DELIM), []byte(""), -1)
}
func HandleYAMLMetaData(datum []byte) (interface{}, error) {
m := map[string]interface{}{}
if err := yaml.Unmarshal(datum, &m); err != nil {
return m, err
}
return m, nil
}
func HandleJSONMetaData(datum []byte) (interface{}, error) {
var f interface{}
if err := json.Unmarshal(datum, &f); err != nil {
return f, err
}
return f, nil
}
|