jlelse
/
hugo-micropub
Archived
1
Fork 0
This repository has been archived on 2020-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
hugo-micropub/post.go

87 lines
2.3 KiB
Go

package main
import (
"bytes"
"errors"
"gopkg.in/yaml.v2"
)
type HugoFrontmatter struct {
Title string `yaml:"title,omitempty"`
Date string `yaml:"date,omitempty"`
Lastmod string `yaml:"lastmod,omitempty"`
Tags []string `yaml:"tags,omitempty"`
ExternalURL string `yaml:"externalURL,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 HugoFrontmatterReply struct {
Link string `yaml:"link,omitempty"`
Title string `yaml:"title,omitempty"`
}
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{
Title: entry.title,
Date: entry.date,
Lastmod: entry.lastmod,
Tags: entry.tags,
ExternalURL: entry.link,
Indieweb: HugoFrontmatterIndieweb{
Reply: HugoFrontmatterReply{
Link: entry.replyLink,
Title: entry.replyTitle,
},
Like: HugoFrontmatterLike{
Link: entry.likeLink,
Title: entry.likeTitle,
},
},
Syndicate: entry.syndicate,
TranslationKey: entry.translationKey,
Audio: entry.audio,
}
for _, image := range entry.images {
writeFrontmatter.Images = append(writeFrontmatter.Images, image.url)
}
yamlBytes, err := yaml.Marshal(&writeFrontmatter)
if err != nil {
err = errors.New("failed marshaling frontmatter")
return
}
buff.WriteString("---\n")
buff.Write(yamlBytes)
buff.WriteString("---\n")
frontmatter = buff.String()
return
}
func WriteHugoPost(entry *Entry) (string, error) {
var buff bytes.Buffer
frontmatter, err := writeFrontMatter(entry)
if err != nil {
return "", err
}
buff.WriteString(frontmatter)
if len(entry.content) > 0 {
buff.WriteString(entry.content)
}
return buff.String(), nil
}