-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.go
178 lines (163 loc) · 3.56 KB
/
parse.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
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
//-----------------------------------------------------------------------------
// Copyright (c) 2020-present Detlef Stern
//
// This file is part of Zettelstore.
//
// Zettelstore is licensed under the latest version of the EUPL (European Union
// Public License). Please see file LICENSE.txt for your rights and obligations
// under this license.
//
// SPDX-License-Identifier: EUPL-1.2
// SPDX-FileCopyrightText: 2020-present Detlef Stern
//-----------------------------------------------------------------------------
package meta
import (
"strings"
"zettelstore.de/client.fossil/api"
"zettelstore.de/client.fossil/input"
"zettelstore.de/client.fossil/maps"
"zettelstore.de/z/strfun"
"zettelstore.de/z/zettel/id"
)
// NewFromInput parses the meta data of a zettel.
func NewFromInput(zid id.Zid, inp *input.Input) *Meta {
if inp.Ch == '-' && inp.PeekN(0) == '-' && inp.PeekN(1) == '-' {
skipToEOL(inp)
inp.EatEOL()
}
meta := New(zid)
for {
skipSpace(inp)
switch inp.Ch {
case '\r':
if inp.Peek() == '\n' {
inp.Next()
}
fallthrough
case '\n':
inp.Next()
return meta
case input.EOS:
return meta
case '%':
skipToEOL(inp)
inp.EatEOL()
continue
}
parseHeader(meta, inp)
if inp.Ch == '-' && inp.PeekN(0) == '-' && inp.PeekN(1) == '-' {
skipToEOL(inp)
inp.EatEOL()
meta.YamlSep = true
return meta
}
}
}
func parseHeader(m *Meta, inp *input.Input) {
pos := inp.Pos
for isHeader(inp.Ch) {
inp.Next()
}
key := inp.Src[pos:inp.Pos]
skipSpace(inp)
if inp.Ch == ':' {
inp.Next()
}
var val []byte
for {
skipSpace(inp)
pos = inp.Pos
skipToEOL(inp)
val = append(val, inp.Src[pos:inp.Pos]...)
inp.EatEOL()
if !input.IsSpace(inp.Ch) {
break
}
val = append(val, ' ')
}
addToMeta(m, string(key), string(val))
}
func skipSpace(inp *input.Input) {
for input.IsSpace(inp.Ch) {
inp.Next()
}
}
func skipToEOL(inp *input.Input) {
for {
switch inp.Ch {
case '\n', '\r', input.EOS:
return
}
inp.Next()
}
}
// Return true iff rune is valid for header key.
func isHeader(ch rune) bool {
return ('a' <= ch && ch <= 'z') ||
('0' <= ch && ch <= '9') ||
ch == '-' ||
('A' <= ch && ch <= 'Z')
}
type predValidElem func(string) bool
func addToSet(set strfun.Set, elems []string, useElem predValidElem) {
for _, s := range elems {
if len(s) > 0 && useElem(s) {
set.Set(s)
}
}
}
func addSet(m *Meta, key, val string, useElem predValidElem) {
newElems := strings.Fields(val)
oldElems, ok := m.GetList(key)
if !ok {
oldElems = nil
}
set := make(strfun.Set, len(newElems)+len(oldElems))
addToSet(set, newElems, useElem)
if len(set) == 0 {
// Nothing to add. Maybe because of rejected elements.
return
}
addToSet(set, oldElems, useElem)
m.SetList(key, maps.Keys(set))
}
func addData(m *Meta, k, v string) {
if o, ok := m.Get(k); !ok || o == "" {
m.Set(k, v)
} else if v != "" {
m.Set(k, o+" "+v)
}
}
func addToMeta(m *Meta, key, val string) {
v := trimValue(val)
key = strings.ToLower(key)
if !KeyIsValid(key) {
return
}
switch key {
case "", api.KeyID:
// Empty key and 'id' key will be ignored
return
}
switch Type(key) {
case TypeTagSet:
addSet(m, key, strings.ToLower(v), func(s string) bool { return s[0] == '#' && len(s) > 1 })
case TypeWord:
m.Set(key, strings.ToLower(v))
case TypeID:
if _, err := id.Parse(v); err == nil {
m.Set(key, v)
}
case TypeIDSet:
addSet(m, key, v, func(s string) bool {
_, err := id.Parse(s)
return err == nil
})
case TypeTimestamp:
if _, ok := TimeValue(v); ok {
m.Set(key, v)
}
default:
addData(m, key, v)
}
}