2024-09-24 19:10:16 +00:00
|
|
|
package blogposts
|
|
|
|
|
|
|
|
import (
|
2024-09-24 19:26:42 +00:00
|
|
|
"bufio"
|
2024-09-24 19:10:16 +00:00
|
|
|
"io"
|
|
|
|
"io/fs"
|
2024-09-24 19:26:42 +00:00
|
|
|
"strings"
|
2024-09-24 19:10:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Post struct {
|
|
|
|
Title, Description, Body string
|
|
|
|
Tags []string
|
|
|
|
}
|
|
|
|
|
2024-09-24 19:26:42 +00:00
|
|
|
const (
|
|
|
|
titleSeparator = "Title: "
|
|
|
|
descriptionSeparator = "Description: "
|
|
|
|
)
|
|
|
|
|
2024-09-24 19:10:16 +00:00
|
|
|
func getPost(fileSystem fs.FS, fileName string) (Post, error) {
|
|
|
|
postFile, err := fileSystem.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return Post{}, err
|
|
|
|
}
|
|
|
|
defer postFile.Close()
|
|
|
|
|
|
|
|
return newPost(postFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Does newPost have to be coupled to an fs.File ?
|
|
|
|
// Do we use all the methods and data from this type? What do we really need?
|
|
|
|
func newPost(postFile io.Reader) (Post, error) {
|
2024-09-24 19:26:42 +00:00
|
|
|
scanner := bufio.NewScanner(postFile)
|
|
|
|
|
|
|
|
readMetaLine := func(tagName string) string {
|
|
|
|
scanner.Scan()
|
|
|
|
return strings.TrimPrefix(scanner.Text(), tagName)
|
2024-09-24 19:10:16 +00:00
|
|
|
}
|
|
|
|
|
2024-09-24 19:26:42 +00:00
|
|
|
post := Post{
|
|
|
|
Title: readMetaLine(titleSeparator),
|
|
|
|
Description: readMetaLine(descriptionSeparator),
|
|
|
|
}
|
2024-09-24 19:10:16 +00:00
|
|
|
return post, nil
|
|
|
|
}
|