2019-11-07 10:00:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-11-14 17:46:49 +00:00
|
|
|
"errors"
|
|
|
|
"gopkg.in/yaml.v2"
|
2019-11-07 10:00:24 +00:00
|
|
|
)
|
|
|
|
|
2020-03-26 10:08:36 +00:00
|
|
|
type HugoFrontMatter struct {
|
2020-01-12 17:17:40 +00:00
|
|
|
Title string `yaml:"title,omitempty"`
|
2020-03-26 10:08:36 +00:00
|
|
|
Published string `yaml:"date,omitempty"`
|
|
|
|
Updated string `yaml:"lastmod,omitempty"`
|
2020-01-12 17:17:40 +00:00
|
|
|
Tags []string `yaml:"tags,omitempty"`
|
|
|
|
ExternalURL string `yaml:"externalURL,omitempty"`
|
2020-03-26 10:08:36 +00:00
|
|
|
IndieWeb HugoFrontMatterIndieWeb `yaml:"indieweb,omitempty"`
|
2020-01-12 17:17:40 +00:00
|
|
|
Syndicate []string `yaml:"syndicate,omitempty"`
|
|
|
|
TranslationKey string `yaml:"translationKey,omitempty"`
|
2020-01-19 07:36:10 +00:00
|
|
|
Images []string `yaml:"images,omitempty"`
|
|
|
|
Audio string `yaml:"audio,omitempty"`
|
2019-12-05 09:35:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 10:08:36 +00:00
|
|
|
type HugoFrontMatterIndieWeb struct {
|
|
|
|
Reply HugoFrontMatterReply `yaml:"reply,omitempty"`
|
|
|
|
Like HugoFrontMatterLike `yaml:"like,omitempty"`
|
2019-12-05 09:35:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 10:08:36 +00:00
|
|
|
type HugoFrontMatterReply struct {
|
2019-12-05 09:35:53 +00:00
|
|
|
Link string `yaml:"link,omitempty"`
|
|
|
|
Title string `yaml:"title,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-03-26 10:08:36 +00:00
|
|
|
type HugoFrontMatterLike struct {
|
2019-12-05 09:35:53 +00:00
|
|
|
Link string `yaml:"link,omitempty"`
|
|
|
|
Title string `yaml:"title,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-03-26 10:08:36 +00:00
|
|
|
func writeFrontMatter(entry *Entry) (string, error) {
|
|
|
|
frontMatter := &HugoFrontMatter{
|
2019-12-05 09:35:53 +00:00
|
|
|
Title: entry.title,
|
2020-03-26 10:08:36 +00:00
|
|
|
Published: entry.date,
|
|
|
|
Updated: entry.lastmod,
|
2019-12-05 09:35:53 +00:00
|
|
|
Tags: entry.tags,
|
|
|
|
ExternalURL: entry.link,
|
2020-03-26 10:08:36 +00:00
|
|
|
IndieWeb: HugoFrontMatterIndieWeb{
|
|
|
|
Reply: HugoFrontMatterReply{
|
2019-12-05 09:35:53 +00:00
|
|
|
Link: entry.replyLink,
|
|
|
|
Title: entry.replyTitle,
|
|
|
|
},
|
2020-03-26 10:08:36 +00:00
|
|
|
Like: HugoFrontMatterLike{
|
2019-12-05 09:35:53 +00:00
|
|
|
Link: entry.likeLink,
|
|
|
|
Title: entry.likeTitle,
|
|
|
|
},
|
|
|
|
},
|
2020-01-12 17:17:40 +00:00
|
|
|
Syndicate: entry.syndicate,
|
|
|
|
TranslationKey: entry.translationKey,
|
2020-01-19 07:36:10 +00:00
|
|
|
Audio: entry.audio,
|
2019-12-05 09:35:53 +00:00
|
|
|
}
|
2020-03-20 17:28:57 +00:00
|
|
|
for _, image := range entry.images {
|
2020-03-26 10:08:36 +00:00
|
|
|
frontMatter.Images = append(frontMatter.Images, image.url)
|
2020-03-20 17:28:57 +00:00
|
|
|
}
|
2020-03-26 10:08:36 +00:00
|
|
|
writer := new(bytes.Buffer)
|
|
|
|
writer.WriteString("---\n")
|
|
|
|
err := yaml.NewEncoder(writer).Encode(&frontMatter)
|
2019-12-05 09:35:53 +00:00
|
|
|
if err != nil {
|
2020-03-26 10:08:36 +00:00
|
|
|
return "", errors.New("failed encoding frontmatter")
|
2019-11-19 09:02:20 +00:00
|
|
|
}
|
2020-03-26 10:08:36 +00:00
|
|
|
writer.WriteString("---\n")
|
|
|
|
return writer.String(), nil
|
2019-11-07 10:00:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 09:35:53 +00:00
|
|
|
func WriteHugoPost(entry *Entry) (string, error) {
|
2020-03-26 10:08:36 +00:00
|
|
|
buff := new(bytes.Buffer)
|
|
|
|
f, err := writeFrontMatter(entry)
|
2019-12-05 09:35:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-03-26 10:08:36 +00:00
|
|
|
buff.WriteString(f)
|
|
|
|
buff.WriteString(entry.content)
|
2019-12-05 09:35:53 +00:00
|
|
|
return buff.String(), nil
|
2020-03-26 10:08:36 +00:00
|
|
|
}
|