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"
|
|
|
|
"strings"
|
2019-11-07 10:00:24 +00:00
|
|
|
)
|
|
|
|
|
2019-12-22 07:26:38 +00:00
|
|
|
type HugoFrontmatter struct {
|
2020-01-12 17:17:40 +00:00
|
|
|
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"`
|
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
|
|
|
}
|
|
|
|
|
2019-12-22 07:26:38 +00:00
|
|
|
type HugoFrontmatterIndieweb struct {
|
|
|
|
Reply HugoFrontmatterReply `yaml:"reply,omitempty"`
|
|
|
|
Like HugoFrontmatterLike `yaml:"like,omitempty"`
|
2019-12-05 09:35:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 07:26:38 +00:00
|
|
|
type HugoFrontmatterReply struct {
|
2019-12-05 09:35:53 +00:00
|
|
|
Link string `yaml:"link,omitempty"`
|
|
|
|
Title string `yaml:"title,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-12-22 07:26:38 +00:00
|
|
|
type HugoFrontmatterLike struct {
|
2019-12-05 09:35:53 +00:00
|
|
|
Link string `yaml:"link,omitempty"`
|
|
|
|
Title string `yaml:"title,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeFrontMatter(entry *Entry) (frontmatter string, err error) {
|
2019-11-07 10:00:24 +00:00
|
|
|
var buff bytes.Buffer
|
2019-12-22 07:26:38 +00:00
|
|
|
writeFrontmatter := &HugoFrontmatter{
|
2019-12-05 09:35:53 +00:00
|
|
|
Title: entry.title,
|
2019-12-06 09:32:30 +00:00
|
|
|
Date: entry.date,
|
2019-12-05 09:35:53 +00:00
|
|
|
Lastmod: entry.lastmod,
|
|
|
|
Tags: entry.tags,
|
|
|
|
ExternalURL: entry.link,
|
2019-12-22 07:26:38 +00:00
|
|
|
Indieweb: HugoFrontmatterIndieweb{
|
|
|
|
Reply: HugoFrontmatterReply{
|
2019-12-05 09:35:53 +00:00
|
|
|
Link: entry.replyLink,
|
|
|
|
Title: entry.replyTitle,
|
|
|
|
},
|
2019-12-22 07:26:38 +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
|
|
|
Images: entry.images,
|
|
|
|
Audio: entry.audio,
|
2019-12-05 09:35:53 +00:00
|
|
|
}
|
|
|
|
yamlBytes, err := yaml.Marshal(&writeFrontmatter)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New("failed marshaling frontmatter")
|
|
|
|
return
|
2019-11-19 09:02:20 +00:00
|
|
|
}
|
2019-11-07 10:00:24 +00:00
|
|
|
buff.WriteString("---\n")
|
2019-12-05 09:35:53 +00:00
|
|
|
buff.Write(yamlBytes)
|
|
|
|
buff.WriteString("---\n")
|
|
|
|
frontmatter = buff.String()
|
|
|
|
return
|
2019-11-07 10:00:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 09:35:53 +00:00
|
|
|
func WriteHugoPost(entry *Entry) (string, error) {
|
2019-11-07 10:00:24 +00:00
|
|
|
var buff bytes.Buffer
|
2019-12-05 09:35:53 +00:00
|
|
|
frontmatter, err := writeFrontMatter(entry)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
buff.WriteString(frontmatter)
|
2019-11-08 18:46:07 +00:00
|
|
|
if len(entry.content) > 0 {
|
|
|
|
buff.WriteString(entry.content)
|
2019-11-07 10:00:24 +00:00
|
|
|
}
|
2019-12-05 09:35:53 +00:00
|
|
|
return buff.String(), nil
|
2019-11-07 10:00:24 +00:00
|
|
|
}
|
2019-11-14 17:46:49 +00:00
|
|
|
|
|
|
|
func readFrontMatter(frontmatter string, entry *Entry) (err error) {
|
2019-12-22 07:26:38 +00:00
|
|
|
parsedFrontmatter := &HugoFrontmatter{}
|
2019-11-14 17:46:49 +00:00
|
|
|
err = yaml.Unmarshal([]byte(frontmatter), &parsedFrontmatter)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New("failed parsing frontmatter")
|
|
|
|
}
|
|
|
|
if len(parsedFrontmatter.Title) > 0 {
|
|
|
|
entry.title = parsedFrontmatter.Title
|
|
|
|
}
|
|
|
|
if len(parsedFrontmatter.Date) > 0 {
|
|
|
|
entry.date = parsedFrontmatter.Date
|
|
|
|
}
|
|
|
|
if len(parsedFrontmatter.Lastmod) > 0 {
|
|
|
|
entry.lastmod = parsedFrontmatter.Lastmod
|
|
|
|
}
|
|
|
|
if len(parsedFrontmatter.Tags) > 0 {
|
|
|
|
entry.tags = parsedFrontmatter.Tags
|
|
|
|
}
|
|
|
|
if len(parsedFrontmatter.ExternalURL) > 0 {
|
|
|
|
entry.link = parsedFrontmatter.ExternalURL
|
|
|
|
}
|
|
|
|
if len(parsedFrontmatter.Indieweb.Reply.Link) > 0 {
|
|
|
|
entry.replyLink = parsedFrontmatter.Indieweb.Reply.Link
|
|
|
|
}
|
|
|
|
if len(parsedFrontmatter.Indieweb.Reply.Title) > 0 {
|
|
|
|
entry.replyTitle = parsedFrontmatter.Indieweb.Reply.Title
|
|
|
|
}
|
2019-12-05 09:35:53 +00:00
|
|
|
if len(parsedFrontmatter.Indieweb.Like.Link) > 0 {
|
|
|
|
entry.replyLink = parsedFrontmatter.Indieweb.Like.Link
|
|
|
|
}
|
|
|
|
if len(parsedFrontmatter.Indieweb.Like.Title) > 0 {
|
|
|
|
entry.replyTitle = parsedFrontmatter.Indieweb.Like.Title
|
|
|
|
}
|
2019-12-22 12:58:50 +00:00
|
|
|
if len(parsedFrontmatter.Syndicate) > 0 {
|
|
|
|
entry.syndicate = parsedFrontmatter.Syndicate
|
|
|
|
}
|
2020-01-12 17:17:40 +00:00
|
|
|
if len(parsedFrontmatter.TranslationKey) > 0 {
|
|
|
|
entry.translationKey = parsedFrontmatter.TranslationKey
|
|
|
|
}
|
2020-01-19 07:36:10 +00:00
|
|
|
if len(parsedFrontmatter.Images) > 0 {
|
|
|
|
entry.images = parsedFrontmatter.Images
|
|
|
|
}
|
|
|
|
if len(parsedFrontmatter.Audio) > 0 {
|
|
|
|
entry.audio = parsedFrontmatter.Audio
|
|
|
|
}
|
2019-11-14 17:46:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadHugoPost(fileContent string) (entry *Entry, err error) {
|
|
|
|
parts := strings.Split(fileContent, "---\n")
|
|
|
|
if len(parts) != 3 {
|
|
|
|
err = errors.New("empty frontmatter or content")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
entry = new(Entry)
|
|
|
|
err = readFrontMatter(parts[1], entry)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
entry.content = strings.TrimSuffix(parts[2], "\n")
|
|
|
|
return
|
2019-12-22 07:26:38 +00:00
|
|
|
}
|