Use yaml library to generate frontmatter

This should fix edge cases for the title etc.
kcoram/uplift
Jan-Lukas Else 2019-12-05 10:35:53 +01:00
parent 1a07bbe047
commit 965bd45f84
3 changed files with 77 additions and 52 deletions

View File

@ -178,13 +178,17 @@ func generateRandomString(now time.Time, n int) string {
return string(b)
}
func WriteEntry(entry *Entry) (string, error) {
file := WriteHugoPost(entry)
err := CreateFile(entry.filename, file, entry.title)
func WriteEntry(entry *Entry) (location string, err error) {
file, err := WriteHugoPost(entry)
if err != nil {
return "", err
return
}
return entry.location, nil
err = CreateFile(entry.filename, file, entry.title)
if err != nil {
return
}
location = entry.location
return
}
func analyzeURL(url string) (filePath string, section string, slug string, err error) {

2
go.mod
View File

@ -4,5 +4,5 @@ go 1.13
require (
gopkg.in/yaml.v2 v2.2.7
willnorris.com/go/webmention v0.0.0-20191104072158-c7fb13569b62 // indirect
willnorris.com/go/webmention v0.0.0-20191104072158-c7fb13569b62
)

113
post.go
View File

@ -8,61 +8,76 @@ import (
"time"
)
func writeFrontMatter(entry *Entry) string {
var buff bytes.Buffer
t := time.Now().Format(time.RFC3339)
buff.WriteString("---\n")
if len(entry.title) > 0 {
buff.WriteString("title: \"" + entry.title + "\"\n")
}
buff.WriteString("date: " + t + "\n")
buff.WriteString("tags:\n")
for _, tag := range entry.tags {
buff.WriteString("- " + tag + "\n")
}
if len(entry.link) > 0 {
buff.WriteString("externalURL: " + entry.link + "\n")
}
buff.WriteString("indieweb:\n")
if len(entry.replyLink) > 0 {
buff.WriteString(" reply:\n link: " + entry.replyLink + "\n")
if len(entry.replyTitle) > 0 {
buff.WriteString(" title: " + entry.replyTitle + "\n")
}
}
if len(entry.likeLink) > 0 {
buff.WriteString(" like:\n link: " + entry.likeLink + "\n")
if len(entry.likeTitle) > 0 {
buff.WriteString(" title: " + entry.likeTitle + "\n")
}
}
buff.WriteString("---\n")
return buff.String()
type Frontmatter 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 Indieweb `yaml:"indieweb,omitempty"`
}
func WriteHugoPost(entry *Entry) string {
type Indieweb struct {
Reply Reply `yaml:"reply,omitempty"`
Like Like `yaml:"like,omitempty"`
}
type Reply struct {
Link string `yaml:"link,omitempty"`
Title string `yaml:"title,omitempty"`
}
type Like struct {
Link string `yaml:"link,omitempty"`
Title string `yaml:"title,omitempty"`
}
func writeFrontMatter(entry *Entry) (frontmatter string, err error) {
var buff bytes.Buffer
buff.WriteString(writeFrontMatter(entry))
writeFrontmatter := &Frontmatter{
Title: entry.title,
Date: time.Now().Format(time.RFC3339),
Lastmod: entry.lastmod,
Tags: entry.tags,
ExternalURL: entry.link,
Indieweb: Indieweb{
Reply: Reply{
Link: entry.replyLink,
Title: entry.replyTitle,
},
Like: Like{
Link: entry.likeLink,
Title: entry.likeTitle,
},
},
}
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()
return buff.String(), nil
}
func readFrontMatter(frontmatter string, entry *Entry) (err error) {
parsedFrontmatter := &struct {
Title string
Date string
Lastmod string
Tags []string
ExternalURL string `yaml:"externalURL"`
Indieweb struct {
Reply struct {
Link string
Title string
}
}
}{}
parsedFrontmatter := &Frontmatter{}
err = yaml.Unmarshal([]byte(frontmatter), &parsedFrontmatter)
if err != nil {
err = errors.New("failed parsing frontmatter")
@ -88,6 +103,12 @@ func readFrontMatter(frontmatter string, entry *Entry) (err error) {
if len(parsedFrontmatter.Indieweb.Reply.Title) > 0 {
entry.replyTitle = parsedFrontmatter.Indieweb.Reply.Title
}
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
}
return
}