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

62
post.go
View File

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