Improve and refactor code that generates hugo post file

master
Jan-Lukas Else 2020-03-26 11:08:36 +01:00
parent 4997f1092e
commit 686a53707d
1 changed files with 28 additions and 34 deletions

58
post.go
View File

@ -6,48 +6,47 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
type HugoFrontmatter struct { type HugoFrontMatter struct {
Title string `yaml:"title,omitempty"` Title string `yaml:"title,omitempty"`
Date string `yaml:"date,omitempty"` Published string `yaml:"date,omitempty"`
Lastmod string `yaml:"lastmod,omitempty"` Updated string `yaml:"lastmod,omitempty"`
Tags []string `yaml:"tags,omitempty"` Tags []string `yaml:"tags,omitempty"`
ExternalURL string `yaml:"externalURL,omitempty"` ExternalURL string `yaml:"externalURL,omitempty"`
Indieweb HugoFrontmatterIndieweb `yaml:"indieweb,omitempty"` IndieWeb HugoFrontMatterIndieWeb `yaml:"indieweb,omitempty"`
Syndicate []string `yaml:"syndicate,omitempty"` Syndicate []string `yaml:"syndicate,omitempty"`
TranslationKey string `yaml:"translationKey,omitempty"` TranslationKey string `yaml:"translationKey,omitempty"`
Images []string `yaml:"images,omitempty"` Images []string `yaml:"images,omitempty"`
Audio string `yaml:"audio,omitempty"` Audio string `yaml:"audio,omitempty"`
} }
type HugoFrontmatterIndieweb struct { type HugoFrontMatterIndieWeb struct {
Reply HugoFrontmatterReply `yaml:"reply,omitempty"` Reply HugoFrontMatterReply `yaml:"reply,omitempty"`
Like HugoFrontmatterLike `yaml:"like,omitempty"` Like HugoFrontMatterLike `yaml:"like,omitempty"`
} }
type HugoFrontmatterReply struct { type HugoFrontMatterReply struct {
Link string `yaml:"link,omitempty"` Link string `yaml:"link,omitempty"`
Title string `yaml:"title,omitempty"` Title string `yaml:"title,omitempty"`
} }
type HugoFrontmatterLike struct { type HugoFrontMatterLike struct {
Link string `yaml:"link,omitempty"` Link string `yaml:"link,omitempty"`
Title string `yaml:"title,omitempty"` Title string `yaml:"title,omitempty"`
} }
func writeFrontMatter(entry *Entry) (frontmatter string, err error) { func writeFrontMatter(entry *Entry) (string, error) {
var buff bytes.Buffer frontMatter := &HugoFrontMatter{
writeFrontmatter := &HugoFrontmatter{
Title: entry.title, Title: entry.title,
Date: entry.date, Published: entry.date,
Lastmod: entry.lastmod, Updated: entry.lastmod,
Tags: entry.tags, Tags: entry.tags,
ExternalURL: entry.link, ExternalURL: entry.link,
Indieweb: HugoFrontmatterIndieweb{ IndieWeb: HugoFrontMatterIndieWeb{
Reply: HugoFrontmatterReply{ Reply: HugoFrontMatterReply{
Link: entry.replyLink, Link: entry.replyLink,
Title: entry.replyTitle, Title: entry.replyTitle,
}, },
Like: HugoFrontmatterLike{ Like: HugoFrontMatterLike{
Link: entry.likeLink, Link: entry.likeLink,
Title: entry.likeTitle, Title: entry.likeTitle,
}, },
@ -57,30 +56,25 @@ func writeFrontMatter(entry *Entry) (frontmatter string, err error) {
Audio: entry.audio, Audio: entry.audio,
} }
for _, image := range entry.images { for _, image := range entry.images {
writeFrontmatter.Images = append(writeFrontmatter.Images, image.url) frontMatter.Images = append(frontMatter.Images, image.url)
} }
yamlBytes, err := yaml.Marshal(&writeFrontmatter) writer := new(bytes.Buffer)
writer.WriteString("---\n")
err := yaml.NewEncoder(writer).Encode(&frontMatter)
if err != nil { if err != nil {
err = errors.New("failed marshaling frontmatter") return "", errors.New("failed encoding frontmatter")
return
} }
buff.WriteString("---\n") writer.WriteString("---\n")
buff.Write(yamlBytes) return writer.String(), nil
buff.WriteString("---\n")
frontmatter = buff.String()
return
} }
func WriteHugoPost(entry *Entry) (string, error) { func WriteHugoPost(entry *Entry) (string, error) {
var buff bytes.Buffer buff := new(bytes.Buffer)
frontmatter, err := writeFrontMatter(entry) f, err := writeFrontMatter(entry)
if err != nil { if err != nil {
return "", err return "", err
} }
buff.WriteString(frontmatter) buff.WriteString(f)
if len(entry.content) > 0 {
buff.WriteString(entry.content) buff.WriteString(entry.content)
}
return buff.String(), nil return buff.String(), nil
} }