From 7e819f4017becd49c15de34b800dc472fcd470a1 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Fri, 20 Mar 2020 18:28:57 +0100 Subject: [PATCH] Support alternative text for photos --- entry.go | 54 ++++++++++++++++++++++++++++++++++++++++++-------- microformat.go | 28 +++++++++++++------------- post.go | 8 ++++++-- 3 files changed, 66 insertions(+), 24 deletions(-) diff --git a/entry.go b/entry.go index 123f063..ea53e5d 100644 --- a/entry.go +++ b/entry.go @@ -30,13 +30,18 @@ type Entry struct { syndicate []string language string translationKey string - images []string + images []Image audio string filename string location string token string } +type Image struct { + url string + alt string +} + func CreateEntry(contentType ContentType, r *http.Request) (*Entry, error) { if contentType == WwwForm { bodyString, err := parseRequestBody(r) @@ -113,9 +118,22 @@ func createEntryFromValueMap(values map[string][]string) (*Entry, error) { entry.syndicate = syndicates } if photo, ok := values["photo"]; ok { - entry.images = photo - } else if photo, ok := values["photo[]"]; ok { - entry.images = photo + entry.images = append(entry.images, Image{url: photo[0]}) + } else if photos, ok := values["photo[]"]; ok { + for _, photo := range photos { + entry.images = append(entry.images, Image{url: photo}) + } + } + if photoAlt, ok := values["mp-photo-alt"]; ok { + if len(entry.images) > 0 { + entry.images[0].alt = photoAlt[0] + } + } else if photoAlts, ok := values["mp-photo-alt[]"]; ok { + for i, photoAlt := range photoAlts { + if len(entry.images) > i { + entry.images[i].alt = photoAlt + } + } } if audio, ok := values["audio"]; ok { entry.audio = audio[0] @@ -162,7 +180,21 @@ func createEntryFromMicroformat(mfEntry *MicroformatItem) (*Entry, error) { entry.syndicate = mfEntry.Properties.MpSyndicateTo } if len(mfEntry.Properties.Photo) > 0 { - entry.images = mfEntry.Properties.Photo + for _, photo := range mfEntry.Properties.Photo { + if theString, justString := photo.(string); justString { + entry.images = append(entry.images, Image{url: theString}) + } else if thePhoto, isPhoto := photo.(map[string]interface{}); isPhoto { + image := Image{} + // Micropub spec says "value" is correct, but not sure about that + if photoUrl, ok := thePhoto["value"].(string); ok { + image.url = photoUrl + } + if alt, ok := thePhoto["alt"].(string); ok { + image.alt = alt + } + entry.images = append(entry.images, image) + } + } } if len(mfEntry.Properties.Audio) > 0 { entry.audio = mfEntry.Properties.Audio[0] @@ -218,7 +250,9 @@ func computeExtraSettings(entry *Entry) error { entry.translationKey = strings.TrimPrefix(text, "translationkey: ") } else if strings.HasPrefix(text, "images: ") { // Images - entry.images = strings.Split(strings.TrimPrefix(text, "images: "), ",") + for _, image := range strings.Split(strings.TrimPrefix(text, "images: "), ",") { + entry.images = append(entry.images, Image{url: image}) + } } else if strings.HasPrefix(text, "audio: ") { // Audio entry.audio = strings.TrimPrefix(text, "audio: ") @@ -229,8 +263,12 @@ func computeExtraSettings(entry *Entry) error { 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" + if !strings.Contains(entry.content, image.url) { + if len(image.alt) > 0 { + entry.content += "\n![" + image.alt + "](" + image.url + " \"" + image.alt + "\")\n" + } else { + entry.content += "\n![](" + image.url + ")\n" + } } } // Compute slug if empty diff --git a/microformat.go b/microformat.go index 17d517a..a795a6d 100644 --- a/microformat.go +++ b/microformat.go @@ -6,17 +6,17 @@ type MicroformatItem struct { } type MicroformatProperties struct { - Name []string `json:"name,omitempty"` - Published []string `json:"published,omitempty"` - Updated []string `json:"updated,omitempty"` - Category []string `json:"category,omitempty"` - Content []string `json:"content,omitempty"` - Url []string `json:"url,omitempty"` - InReplyTo []string `json:"in-reply-to,omitempty"` - LikeOf []string `json:"like-of,omitempty"` - 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"` -} + Name []string `json:"name,omitempty"` + Published []string `json:"published,omitempty"` + Updated []string `json:"updated,omitempty"` + Category []string `json:"category,omitempty"` + Content []string `json:"content,omitempty"` + Url []string `json:"url,omitempty"` + InReplyTo []string `json:"in-reply-to,omitempty"` + LikeOf []string `json:"like-of,omitempty"` + BookmarkOf []string `json:"bookmark-of,omitempty"` + MpSlug []string `json:"mp-slug,omitempty"` + MpSyndicateTo []string `json:"mp-syndicate-to,omitempty"` + Photo []interface{} `json:"photo,omitempty"` + Audio []string `json:"audio,omitempty"` +} \ No newline at end of file diff --git a/post.go b/post.go index 5f48241..ac0aeb6 100644 --- a/post.go +++ b/post.go @@ -55,9 +55,11 @@ func writeFrontMatter(entry *Entry) (frontmatter string, err error) { }, Syndicate: entry.syndicate, TranslationKey: entry.translationKey, - Images: entry.images, Audio: entry.audio, } + for _, image := range entry.images { + writeFrontmatter.Images = append(writeFrontmatter.Images, image.url) + } yamlBytes, err := yaml.Marshal(&writeFrontmatter) if err != nil { err = errors.New("failed marshaling frontmatter") @@ -123,7 +125,9 @@ func readFrontMatter(frontmatter string, entry *Entry) (err error) { entry.translationKey = parsedFrontmatter.TranslationKey } if len(parsedFrontmatter.Images) > 0 { - entry.images = parsedFrontmatter.Images + for _, image := range parsedFrontmatter.Images { + entry.images = append(entry.images, Image{url: image}) + } } if len(parsedFrontmatter.Audio) > 0 { entry.audio = parsedFrontmatter.Audio