From 962818c9313c0c195f5faac359fb4ee1de22f2d0 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sun, 19 Jan 2020 08:36:10 +0100 Subject: [PATCH] Add support for images and audio --- entry.go | 34 ++++++++++++++++++++++++++++++---- microformat.go | 2 ++ post.go | 10 ++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/entry.go b/entry.go index 6006619..3a60e66 100644 --- a/entry.go +++ b/entry.go @@ -30,6 +30,8 @@ type Entry struct { syndicate []string language string translationKey string + images []string + audio string filename string location string token string @@ -92,8 +94,6 @@ func createEntryFromValueMap(values map[string][]string) (*Entry, error) { entry.tags = category } else if categories, ok := values["category[]"]; ok { entry.tags = categories - } else { - entry.tags = nil } if slug, ok := values["mp-slug"]; ok && len(slug) > 0 && slug[0] != "" { entry.slug = slug[0] @@ -111,8 +111,16 @@ func createEntryFromValueMap(values map[string][]string) (*Entry, error) { entry.syndicate = syndicate } else if syndicates, ok := values["mp-syndicate-to[]"]; ok { entry.syndicate = syndicates - } else { - entry.syndicate = nil + } + if photo, ok := values["photo"]; ok { + entry.images = photo + } else if photo, ok := values["photo[]"]; ok { + entry.images = photo + } + if audio, ok := values["audio"]; ok { + entry.audio = audio[0] + } else if audio, ok := values["audio[]"]; ok { + entry.audio = audio[0] } if token, ok := values["access_token"]; ok { entry.token = "Bearer " + token[0] @@ -153,6 +161,12 @@ func createEntryFromMicroformat(mfEntry *MicroformatItem) (*Entry, error) { if len(mfEntry.Properties.MpSyndicateTo) > 0 { entry.syndicate = mfEntry.Properties.MpSyndicateTo } + if len(mfEntry.Properties.Photo) > 0 { + entry.images = mfEntry.Properties.Photo + } + if len(mfEntry.Properties.Audio) > 0 { + entry.audio = mfEntry.Properties.Audio[0] + } err := computeExtraSettings(entry) if err != nil { return nil, err @@ -202,11 +216,23 @@ func computeExtraSettings(entry *Entry) error { } else if strings.HasPrefix(text, "translationkey: ") { // Translation key entry.translationKey = strings.TrimPrefix(text, "translationkey: ") + } else if strings.HasPrefix(text, "images: ") { + // Images + entry.images = strings.Split(strings.TrimPrefix(text, "images: "), ",") + } else if strings.HasPrefix(text, "audio: ") { + // Audio + entry.audio = strings.TrimPrefix(text, "audio: ") } else { _, _ = fmt.Fprintln(&filteredContent, text) } } entry.content = filteredContent.String() + // Check if content contains images or add them + for _, image := range entry.images { + if !strings.Contains(entry.content, image) { + entry.content += "\n![](" + image + ")\n" + } + } // Compute slug if empty if len(entry.slug) == 0 || entry.slug == "" { random := generateRandomString(now, 5) diff --git a/microformat.go b/microformat.go index 4565f50..17d517a 100644 --- a/microformat.go +++ b/microformat.go @@ -17,4 +17,6 @@ type MicroformatProperties struct { BookmarkOf []string `json:"bookmark-of,omitempty"` MpSlug []string `json:"mp-slug,omitempty"` MpSyndicateTo []string `json:"mp-syndicate-to,omitempty"` + Photo []string `json:"photo,omitempty"` + Audio []string `json:"audio,omitempty"` } diff --git a/post.go b/post.go index fc3eeb1..5f48241 100644 --- a/post.go +++ b/post.go @@ -16,6 +16,8 @@ type HugoFrontmatter struct { 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 { @@ -53,6 +55,8 @@ func writeFrontMatter(entry *Entry) (frontmatter string, err error) { }, Syndicate: entry.syndicate, TranslationKey: entry.translationKey, + Images: entry.images, + Audio: entry.audio, } yamlBytes, err := yaml.Marshal(&writeFrontmatter) if err != nil { @@ -118,6 +122,12 @@ func readFrontMatter(frontmatter string, entry *Entry) (err error) { if len(parsedFrontmatter.TranslationKey) > 0 { entry.translationKey = parsedFrontmatter.TranslationKey } + if len(parsedFrontmatter.Images) > 0 { + entry.images = parsedFrontmatter.Images + } + if len(parsedFrontmatter.Audio) > 0 { + entry.audio = parsedFrontmatter.Audio + } return }