blogposts: finish up

This commit is contained in:
vinchent 2024-09-24 21:58:06 +02:00
parent 48df8ace6e
commit e8b8166ae9

View File

@ -16,7 +16,7 @@ const (
titleLine = "Title: " titleLine = "Title: "
descriptionLine = "Description: " descriptionLine = "Description: "
tagsLine = "Tags: " tagsLine = "Tags: "
tagSeparator = "," tagSeparator = ", "
bodySeparator = "---" bodySeparator = "---"
) )
@ -40,15 +40,16 @@ func newPost(postFile io.Reader) (Post, error) {
return strings.TrimPrefix(scanner.Text(), tagName) return strings.TrimPrefix(scanner.Text(), tagName)
} }
title := readMetaLine(titleLine) post := Post{
description := readMetaLine(descriptionLine) Title: readMetaLine(titleLine),
tagsOneLine := readMetaLine(tagsLine) Description: readMetaLine(descriptionLine),
Tags: strings.Split(readMetaLine(tagsLine), tagSeparator),
tags := strings.Split(tagsOneLine, ",") Body: readBody(scanner),
for i, tag := range tags {
tags[i] = strings.TrimSpace(tag)
} }
return post, nil
}
func readBody(scanner *bufio.Scanner) string {
for scanner.Scan() { for scanner.Scan() {
if strings.TrimSpace(scanner.Text()) == bodySeparator { if strings.TrimSpace(scanner.Text()) == bodySeparator {
break break
@ -61,13 +62,5 @@ func newPost(postFile io.Reader) (Post, error) {
buf.Write(scanner.Bytes()) buf.Write(scanner.Bytes())
buf.Write([]byte{'\n'}) buf.Write([]byte{'\n'})
} }
body := strings.TrimSuffix(buf.String(), "\n") return strings.TrimSuffix(buf.String(), "\n")
post := Post{
Title: title,
Description: description,
Tags: tags,
Body: body,
}
return post, nil
} }