Add support for posting German posts

master
Jan-Lukas Else 2020-01-12 18:17:40 +01:00
parent fb76567cef
commit 2872afbc28
4 changed files with 75 additions and 34 deletions

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"github.com/caarlos0/env/v6" "github.com/caarlos0/env/v6"
"log" "log"
"strings"
) )
var ( var (
@ -46,9 +47,15 @@ func initConfig() (err error) {
return errors.New("failed to parse config, probably not all required env vars set") return errors.New("failed to parse config, probably not all required env vars set")
} }
// Blog URL (required) // Blog URL (required)
if !strings.HasSuffix(cfg.BlogUrl, "/") {
return errors.New("missing trailing slash in BLOG_URL")
}
BlogUrl = cfg.BlogUrl BlogUrl = cfg.BlogUrl
// Media endpoint // Media endpoint
MediaEndpointUrl = cfg.BaseUrl + "/media" if !strings.HasSuffix(cfg.BaseUrl, "/") {
return errors.New("missing trailing slash in BASE_URL")
}
MediaEndpointUrl = cfg.BaseUrl + "media"
// Ignored Webmention URLs (optional) // Ignored Webmention URLs (optional)
IgnoredWebmentionUrls = cfg.IgnoredWebmentionUrls IgnoredWebmentionUrls = cfg.IgnoredWebmentionUrls
// Syndication Targets (optional) // Syndication Targets (optional)
@ -77,7 +84,8 @@ func initConfig() (err error) {
// Find selected media storage (Optional) // Find selected media storage (Optional)
SelectedMediaStorage = func() MediaStorage { SelectedMediaStorage = func() MediaStorage {
// BunnyCDN // BunnyCDN
if len(cfg.BunnyCdnStorageKey) > 0 && len(cfg.BunnyCdnStorageName) > 0 && len(cfg.MediaUrl) > 0 { // MEDIA_URL needs trailing slash too
if len(cfg.BunnyCdnStorageKey) > 0 && len(cfg.BunnyCdnStorageName) > 0 && len(cfg.MediaUrl) > 0 && strings.HasSuffix(cfg.MediaUrl, "/") {
return &BunnyCdnStorage{ return &BunnyCdnStorage{
key: cfg.BunnyCdnStorageKey, key: cfg.BunnyCdnStorageKey,
storageZoneName: cfg.BunnyCdnStorageName, storageZoneName: cfg.BunnyCdnStorageName,

View File

@ -28,6 +28,8 @@ type Entry struct {
likeLink string likeLink string
likeTitle string likeTitle string
syndicate []string syndicate []string
language string
translationKey string
filename string filename string
location string location string
token string token string
@ -194,6 +196,12 @@ func computeExtraSettings(entry *Entry) error {
} else if strings.HasPrefix(text, "like-title: ") { } else if strings.HasPrefix(text, "like-title: ") {
// Like title // Like title
entry.likeTitle = strings.TrimPrefix(text, "like-title: ") entry.likeTitle = strings.TrimPrefix(text, "like-title: ")
} else if strings.HasPrefix(text, "language: ") {
// Language
entry.language = strings.TrimPrefix(text, "language: ")
} else if strings.HasPrefix(text, "translationkey: ") {
// Translation key
entry.translationKey = strings.TrimPrefix(text, "translationkey: ")
} else { } else {
_, _ = fmt.Fprintln(&filteredContent, text) _, _ = fmt.Fprintln(&filteredContent, text)
} }
@ -205,16 +213,24 @@ func computeExtraSettings(entry *Entry) error {
entry.slug = fmt.Sprintf("%v-%02d-%02d-%v", now.Year(), int(now.Month()), now.Day(), random) entry.slug = fmt.Sprintf("%v-%02d-%02d-%v", now.Year(), int(now.Month()), now.Day(), random)
} }
// Compute filename and location // Compute filename and location
contentFolder := "content"
localizedBlogUrl := BlogUrl
if len(entry.language) > 0 && entry.language != "en" {
// Append language to content folder: "content-de" for language "de"
contentFolder += "-" + entry.language
// Append language to BlogUrl
localizedBlogUrl += entry.language + "/"
}
if len(entry.section) < 1 { if len(entry.section) < 1 {
entry.section = "micro" entry.section = "micro"
} }
entry.section = strings.ToLower(entry.section) entry.section = strings.ToLower(entry.section)
if entry.section == "thoughts" || entry.section == "links" || entry.section == "micro" { if entry.section == "thoughts" || entry.section == "links" || entry.section == "micro" {
entry.filename = fmt.Sprintf("content/%v/%02d/%02d/%v.md", entry.section, now.Year(), int(now.Month()), entry.slug) entry.filename = fmt.Sprintf("%v/%v/%02d/%02d/%v.md", contentFolder, entry.section, now.Year(), int(now.Month()), entry.slug)
entry.location = fmt.Sprintf("%v%v/%02d/%02d/%v/", BlogUrl, entry.section, now.Year(), int(now.Month()), entry.slug) entry.location = fmt.Sprintf("%v%v/%02d/%02d/%v/", localizedBlogUrl, entry.section, now.Year(), int(now.Month()), entry.slug)
} else { } else {
entry.filename = fmt.Sprintf("content/%v/%v.md", entry.section, entry.slug) entry.filename = fmt.Sprintf("%v/%v/%v.md", contentFolder, entry.section, entry.slug)
entry.location = fmt.Sprintf("%v%v/%v/", BlogUrl, entry.section, entry.slug) entry.location = fmt.Sprintf("%v%v/%v/", localizedBlogUrl, entry.section, entry.slug)
} }
return nil return nil
} }
@ -242,20 +258,29 @@ func WriteEntry(entry *Entry) (location string, err error) {
return return
} }
func analyzeURL(url string) (filePath string, section string, slug string, err error) { func analyzeURL(url string) (filePath string, section string, slug string, lang string, err error) {
if !strings.HasPrefix(url, BlogUrl) { if !strings.HasPrefix(url, BlogUrl) {
return return
} }
path := strings.TrimSuffix(strings.TrimPrefix(url, BlogUrl), "/") contentFolder := "content"
path := ""
if strings.HasPrefix(url, BlogUrl+"de/") {
lang = "de"
// German content folder
contentFolder += "-" + lang
path = strings.TrimSuffix(strings.TrimPrefix(url, BlogUrl+lang+"/"), "/")
} else {
path = strings.TrimSuffix(strings.TrimPrefix(url, BlogUrl), "/")
}
pathParts := strings.Split(path, "/") pathParts := strings.Split(path, "/")
filePath = "content/" + path + ".md" filePath = contentFolder + "/" + path + ".md"
section = pathParts[0] section = pathParts[0]
slug = pathParts[len(pathParts)-1] slug = pathParts[len(pathParts)-1]
return return
} }
func ReadEntry(url string) (entry *Entry, err error) { func ReadEntry(url string) (entry *Entry, err error) {
filePath, section, slug, err := analyzeURL(url) filePath, section, slug, lang, err := analyzeURL(url)
if err != nil { if err != nil {
return return
} }
@ -269,6 +294,7 @@ func ReadEntry(url string) (entry *Entry, err error) {
entry.filename = filePath entry.filename = filePath
entry.section = section entry.section = section
entry.slug = slug entry.slug = slug
entry.language = lang
} }
return return
} }

2
go.sum
View File

@ -2,11 +2,13 @@ github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRy
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/caarlos0/env/v6 v6.1.0 h1:4FbM+HmZA/Q5wdSrH2kj0KQXm7xnhuO8y3TuOTnOvqc= github.com/caarlos0/env/v6 v6.1.0 h1:4FbM+HmZA/Q5wdSrH2kj0KQXm7xnhuO8y3TuOTnOvqc=
github.com/caarlos0/env/v6 v6.1.0/go.mod h1:iUA6X3VCAOwDhoqvgKlTGjjwJzQseIJaFYApUqQkt+8= github.com/caarlos0/env/v6 v6.1.0/go.mod h1:iUA6X3VCAOwDhoqvgKlTGjjwJzQseIJaFYApUqQkt+8=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.5-0.20190904012038-b33efeebc785+incompatible h1:OT02onvXX618RBcjxeUA4H7d1PSm5Apg4IET72VgVlE= github.com/go-telegram-bot-api/telegram-bot-api v4.6.5-0.20190904012038-b33efeebc785+incompatible h1:OT02onvXX618RBcjxeUA4H7d1PSm5Apg4IET72VgVlE=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.5-0.20190904012038-b33efeebc785+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM= github.com/go-telegram-bot-api/telegram-bot-api v4.6.5-0.20190904012038-b33efeebc785+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=

View File

@ -15,6 +15,7 @@ type HugoFrontmatter struct {
ExternalURL string `yaml:"externalURL,omitempty"` ExternalURL string `yaml:"externalURL,omitempty"`
Indieweb HugoFrontmatterIndieweb `yaml:"indieweb,omitempty"` Indieweb HugoFrontmatterIndieweb `yaml:"indieweb,omitempty"`
Syndicate []string `yaml:"syndicate,omitempty"` Syndicate []string `yaml:"syndicate,omitempty"`
TranslationKey string `yaml:"translationKey,omitempty"`
} }
type HugoFrontmatterIndieweb struct { type HugoFrontmatterIndieweb struct {
@ -51,6 +52,7 @@ func writeFrontMatter(entry *Entry) (frontmatter string, err error) {
}, },
}, },
Syndicate: entry.syndicate, Syndicate: entry.syndicate,
TranslationKey: entry.translationKey,
} }
yamlBytes, err := yaml.Marshal(&writeFrontmatter) yamlBytes, err := yaml.Marshal(&writeFrontmatter)
if err != nil { if err != nil {
@ -113,6 +115,9 @@ func readFrontMatter(frontmatter string, entry *Entry) (err error) {
if len(parsedFrontmatter.Syndicate) > 0 { if len(parsedFrontmatter.Syndicate) > 0 {
entry.syndicate = parsedFrontmatter.Syndicate entry.syndicate = parsedFrontmatter.Syndicate
} }
if len(parsedFrontmatter.TranslationKey) > 0 {
entry.translationKey = parsedFrontmatter.TranslationKey
}
return return
} }