Split ParseMeta out of SplitMeta
This commit is contained in:
parent
88c5ef2922
commit
847eff17b9
33
meta.go
33
meta.go
@ -12,8 +12,11 @@ type Meta = map[string] string
|
|||||||
// along with the rest of the input as a string. If there is no metadata, an
|
// along with the rest of the input as a string. If there is no metadata, an
|
||||||
// empty map will be returned.
|
// empty map will be returned.
|
||||||
func SplitMeta (input string) (Meta, string, error) {
|
func SplitMeta (input string) (Meta, string, error) {
|
||||||
// i hate crlf!!!!! uwehhh!!!
|
// i hate crlf!!!!! uwehhh!!! TODO remove
|
||||||
input = strings.ReplaceAll(input, "\r\n", "\n")
|
input = strings.ReplaceAll(input, "\r\n", "\n")
|
||||||
|
// TODO call internal function that takes in an io.Reader and scans it
|
||||||
|
// by line instead of operating directly on the string. have that call
|
||||||
|
// yet another function which will solve #11
|
||||||
|
|
||||||
// stop if there is no front matter
|
// stop if there is no front matter
|
||||||
if !strings.HasPrefix(input, frontMatterRule + "\n") {
|
if !strings.HasPrefix(input, frontMatterRule + "\n") {
|
||||||
@ -28,26 +31,34 @@ func SplitMeta (input string) (Meta, string, error) {
|
|||||||
}
|
}
|
||||||
frontMatterRaw := input[:index]
|
frontMatterRaw := input[:index]
|
||||||
body := input[index + len(frontMatterRule) + 2:]
|
body := input[index + len(frontMatterRule) + 2:]
|
||||||
frontMatter := make(Meta)
|
|
||||||
|
|
||||||
// iterate over the lines
|
// parse meta
|
||||||
for _, line := range strings.Split(frontMatterRaw, "\n") {
|
meta, err := ParseMeta(frontMatterRaw)
|
||||||
|
if err != nil { return Meta { }, "", err }
|
||||||
|
|
||||||
|
return meta, body, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseMeta parses isolated metadata (without the horizontal starting and
|
||||||
|
// ending rules).
|
||||||
|
func ParseMeta (input string) (Meta, error) {
|
||||||
|
meta := make(Meta)
|
||||||
|
for _, line := range strings.Split(input, "\n") {
|
||||||
line = strings.TrimSpace(line)
|
line = strings.TrimSpace(line)
|
||||||
if line == "" { continue }
|
if line == "" { continue }
|
||||||
key, value, ok := strings.Cut(line, ":")
|
key, value, ok := strings.Cut(line, ":")
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, "", ErrFrontMatterMalformed
|
return nil, ErrFrontMatterMalformed
|
||||||
}
|
}
|
||||||
key = strings.ToLower(strings.TrimSpace(key))
|
key = strings.ToLower(strings.TrimSpace(key))
|
||||||
value = strings.TrimSpace(value)
|
value = strings.TrimSpace(value)
|
||||||
if key == "" {
|
if key == "" {
|
||||||
return nil, "", ErrFrontMatterMalformed
|
return nil, ErrFrontMatterMalformed
|
||||||
}
|
}
|
||||||
if _, exists := frontMatter[key]; exists {
|
if _, exists := meta[key]; exists {
|
||||||
return nil, "", ErrFrontMatterDuplicateKey
|
return nil, ErrFrontMatterDuplicateKey
|
||||||
}
|
}
|
||||||
frontMatter[key] = value
|
meta[key] = value
|
||||||
}
|
}
|
||||||
|
return meta, nil
|
||||||
return frontMatter, body, nil
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user