forked from zedhead037/mdconf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
353 lines (328 loc) · 8.96 KB
/
main.go
File metadata and controls
353 lines (328 loc) · 8.96 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package mdconf
import (
"bufio"
"errors"
"fmt"
"io"
"regexp"
"strings"
)
// this is here because UnreadByte somehow can't unread a whole line.
type trueLineReader struct {
reader *bufio.Reader
buffer []string
}
func newTrueLineReader(r *bufio.Reader) *trueLineReader {
return &trueLineReader{
reader: r,
buffer: make([]string, 0),
}
}
func (r *trueLineReader) readLine() (string, error) {
if len(r.buffer) > 0 {
rb, e := r.buffer[:len(r.buffer)-1], r.buffer[len(r.buffer)-1]
r.buffer = rb
return e, nil
}
rr, err := r.reader.ReadString('\n')
if err != nil { return "", err }
return strings.TrimSuffix(rr, "\n"), nil
}
func (r *trueLineReader) unreadLine(l string) {
r.buffer = append(r.buffer, l)
}
type MDConfSection struct {
Level int8
SectionName string
ValueMap map[string]string
Subsection []*MDConfSection
}
var whitespaceStr = " \t\r\n\v\b\f"
func isWhite(c byte) bool {
return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\v' || c == '\b' || c == '\f'
}
var reEscape = regexp.MustCompile(`\\(.)`)
func unescape(s string) string {
return reEscape.ReplaceAllString(s, "$1")
}
var reSectionHeader = regexp.MustCompile(`^\s*(#+)\s*((?:\\.|.)*)\s*$`)
var reKV = regexp.MustCompile(`^\s*\+\s*((?:\\.|[^:\s])*)\s*:\s*((?:\\.|.)*?)$`)
func parseInner(currentSection *MDConfSection, br *trueLineReader) *MDConfSection {
currentMultiLineValueLineStack := make([]string, 0)
currentMultiLineKey := ""
currentlyReadingMultiLine := false
for {
r, err := br.readLine()
if errors.Is(err, io.EOF) { break }
rLen := len(r)
if currentlyReadingMultiLine {
if strings.HasSuffix(r, "\\") {
currentMultiLineValueLineStack = append(currentMultiLineValueLineStack, unescape(r[:rLen-1]))
continue
}
currentSection.ValueMap[currentMultiLineKey] = strings.Join(currentMultiLineValueLineStack, "\n")
currentlyReadingMultiLine = false
continue
}
trimmed := strings.TrimSpace(r)
trimmedLen := len(trimmed)
if trimmedLen <= 0 { continue }
if strings.HasPrefix(trimmed, "//") { continue }
matchres := reKV.FindStringSubmatch(r)
if len(matchres) > 0 {
if currentSection.ValueMap == nil {
currentSection.ValueMap = make(map[string]string, 0)
}
key := matchres[1]
val := matchres[2]
if len(val) <= 0 {
currentSection.ValueMap[key] = ""
} else if val[len(val)-1] == '\\' {
currentMultiLineKey = key
currentMultiLineValueLineStack = make([]string, 0)
currentMultiLineValueLineStack = append(currentMultiLineValueLineStack, unescape(val[:len(val)-1]))
currentlyReadingMultiLine = true
} else {
i := len(val)-1
for i >= 0 && isWhite(val[i]) { i -= 1 }
if i > 0 {
if val[i] == '\\' {
i += 2
val = val[:i]
} else {
i += 1
val = val[:i]
}
} else {
val = ""
}
currentSection.ValueMap[key] = unescape(val)
}
continue
}
matchres = reSectionHeader.FindStringSubmatch(r)
if len(matchres) > 0 {
level := int8(len(matchres[1]))
sectionHeader := matchres[2]
if level <= currentSection.Level {
br.unreadLine(r)
return currentSection
}
if currentSection.Subsection == nil {
currentSection.Subsection = make([]*MDConfSection, 0)
}
newSubSection := &MDConfSection{
Level: level,
SectionName: sectionHeader,
ValueMap: nil,
Subsection: nil,
}
currentSection.Subsection = append(currentSection.Subsection, parseInner(newSubSection, br))
}
}
if currentlyReadingMultiLine {
currentSection.ValueMap[currentMultiLineKey] = strings.Join(currentMultiLineValueLineStack, "\n")
}
return currentSection
}
func Parse(r io.Reader) *MDConfSection {
br := bufio.NewReader(r)
tlr := newTrueLineReader(br)
result := &MDConfSection{
Level: 0,
SectionName: "",
ValueMap: nil,
Subsection: nil,
}
return parseInner(result, tlr)
}
func escapeValue(s string) string {
if len(s) >= 2 {
if isWhite(s[0]) {
if isWhite(s[len(s)-1]) {
return "\\" + s[:len(s)-1] + "\\" + s[len(s)-1:]
} else {
return "\\" + s
}
} else if isWhite(s[len(s)-1]) {
return s[:len(s)-1] + "\\" + s[len(s)-1:]
} else {
return s
}
} else if len(s) == 1 {
if isWhite(s[0]) {
return "\\" + s
} else {
return s
}
} else { return "" }
}
func toString(br *strings.Builder, mdc *MDConfSection) {
br.WriteString(fmt.Sprintf("%s %s\n", strings.Repeat("#", int(mdc.Level)), mdc.SectionName))
if mdc.ValueMap != nil {
for k, v := range mdc.ValueMap {
ss := strings.Split(v, "\n")
if len(ss) > 1 {
br.WriteString(fmt.Sprintf("+ %s: %s\\", k, escapeValue(ss[0])))
i := 1
for i < len(ss)-1 {
br.WriteString(ss[i])
br.WriteString("\\\n")
}
br.WriteString(ss[len(ss)-1])
br.WriteString("\n")
} else {
br.WriteString(fmt.Sprintf("+ %s: %s\n", k, escapeValue(v)))
}
}
br.WriteString("\n")
}
if mdc.Subsection != nil {
for _, k := range mdc.Subsection {
toString(br, k)
br.WriteString("\n")
}
}
}
func (mdc *MDConfSection) ToString() string {
res := new(strings.Builder)
toString(res, mdc)
return res.String()
}
func ParseString(s string) *MDConfSection {
nr := strings.NewReader(s)
return Parse(nr)
}
var ErrNotFound = errors.New("key not found")
var ErrEmptyKey = errors.New("key is empty")
var ErrEmptySubject = errors.New("subject is empty")
func (mdc *MDConfSection) LocalQueryKey(k string) (string, error) {
if mdc == nil { return "", ErrNotFound }
if mdc.ValueMap == nil { return "", ErrNotFound }
v, ok := mdc.ValueMap[k]
if !ok { return "", ErrNotFound }
return v, nil
}
func (mdc *MDConfSection) QueryKey(k []string) (string, error) {
if mdc == nil { return "", ErrNotFound }
i := 0
subj := mdc
if len(k) <= 0 { return "", ErrEmptyKey }
for i < len(k) - 1 {
if subj.Subsection == nil { return "", ErrNotFound }
found := false
for _, subsec := range subj.Subsection {
if subsec.SectionName != k[i] { continue }
found = true
subj = subsec
break
}
if !found { return "", ErrNotFound }
i += 1
}
if subj.ValueMap == nil { return "", ErrNotFound }
v, ok := subj.ValueMap[k[i]]
if !ok { return "", ErrNotFound }
return v, nil
}
func (mdc *MDConfSection) LocalQuerySection(k string) (*MDConfSection, error) {
if mdc == nil { return nil, ErrNotFound }
if mdc.Subsection == nil { return nil, ErrNotFound }
for _, subsec := range mdc.Subsection {
if subsec.SectionName == k { return subsec, nil }
}
return nil, ErrNotFound
}
func (mdc *MDConfSection) QuerySection(k []string) (*MDConfSection, error) {
if mdc == nil { return nil, ErrNotFound }
i := 0
subj := mdc
if len(k) <= 0 { return mdc, nil }
for i < len(k) {
if subj.Subsection == nil { return nil, ErrNotFound }
found := false
for _, subsec := range subj.Subsection {
if subsec.SectionName != k[i] { continue }
found = true
subj = subsec
break
}
if !found { return nil, ErrNotFound }
i += 1
}
return subj, nil
}
func (mdc *MDConfSection) SetKey(k []string, val string) error {
if mdc == nil { return ErrEmptySubject }
i := 0
subj := mdc
if len(k) <= 0 { return ErrEmptyKey }
for i < len(k) - 1 {
if subj.Subsection == nil { return ErrNotFound }
found := false
for _, subsec := range subj.Subsection {
if subsec.SectionName != k[i] { continue }
found = true
subj = subsec
break
}
if !found { return ErrNotFound }
i += 1
}
if subj.ValueMap == nil {
subj.ValueMap = make(map[string]string, 0)
}
subj.ValueMap[k[len(k)-1]] = val
return nil
}
func (mdc *MDConfSection) LocalSetKey(k string, val string) error {
if mdc == nil { return ErrEmptySubject }
if mdc.ValueMap == nil {
mdc.ValueMap = make(map[string]string, 0)
}
mdc.ValueMap[k] = val
return nil
}
func (mdc *MDConfSection) AddSection(k []string, name string) (*MDConfSection, error) {
// Add a new subsection at location specified by the key `k`. The
// section name should be not used by other subsections. If
// subsections with the same name exists, this function would
// simply return that subsection and not do anything.
if mdc == nil { return nil, ErrEmptySubject }
i := 0
subj := mdc
for i < len(k) {
if subj.Subsection == nil { return nil, ErrNotFound }
found := false
for _, subsec := range subj.Subsection {
if subsec.SectionName != k[i] { continue }
found = true
subj = subsec
break
}
if !found { return nil, ErrNotFound }
i += 1
}
return subj.LocalAddSection(name)
}
func (mdc *MDConfSection) LocalAddSection(name string) (*MDConfSection, error) {
// Add a new subsection directly at the current section. The
// section name should be not used by other subsections. If
// subsections with the same name exists, this function would
// simply return that subsection and not do anything.
if mdc == nil { return nil, ErrEmptySubject }
if mdc.Subsection == nil {
mdc.Subsection = make([]*MDConfSection, 0)
}
for _, v := range mdc.Subsection {
if v.SectionName == name { return v, nil }
}
res := &MDConfSection{
Level: mdc.Level + 1,
SectionName: name,
ValueMap: nil,
Subsection: nil,
}
mdc.Subsection = append(mdc.Subsection, res)
return res, nil
}