Support alternative text for photos

master
Jan-Lukas Else 2020-03-20 18:28:57 +01:00
parent f4bac41d11
commit 7e819f4017
3 changed files with 66 additions and 24 deletions

View File

@ -30,13 +30,18 @@ type Entry struct {
syndicate []string syndicate []string
language string language string
translationKey string translationKey string
images []string images []Image
audio string audio string
filename string filename string
location string location string
token string token string
} }
type Image struct {
url string
alt string
}
func CreateEntry(contentType ContentType, r *http.Request) (*Entry, error) { func CreateEntry(contentType ContentType, r *http.Request) (*Entry, error) {
if contentType == WwwForm { if contentType == WwwForm {
bodyString, err := parseRequestBody(r) bodyString, err := parseRequestBody(r)
@ -113,9 +118,22 @@ func createEntryFromValueMap(values map[string][]string) (*Entry, error) {
entry.syndicate = syndicates entry.syndicate = syndicates
} }
if photo, ok := values["photo"]; ok { if photo, ok := values["photo"]; ok {
entry.images = photo entry.images = append(entry.images, Image{url: photo[0]})
} else if photo, ok := values["photo[]"]; ok { } else if photos, ok := values["photo[]"]; ok {
entry.images = photo 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 { if audio, ok := values["audio"]; ok {
entry.audio = audio[0] entry.audio = audio[0]
@ -162,7 +180,21 @@ func createEntryFromMicroformat(mfEntry *MicroformatItem) (*Entry, error) {
entry.syndicate = mfEntry.Properties.MpSyndicateTo entry.syndicate = mfEntry.Properties.MpSyndicateTo
} }
if len(mfEntry.Properties.Photo) > 0 { 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 { if len(mfEntry.Properties.Audio) > 0 {
entry.audio = 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: ") entry.translationKey = strings.TrimPrefix(text, "translationkey: ")
} else if strings.HasPrefix(text, "images: ") { } else if strings.HasPrefix(text, "images: ") {
// 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: ") { } else if strings.HasPrefix(text, "audio: ") {
// Audio // Audio
entry.audio = strings.TrimPrefix(text, "audio: ") entry.audio = strings.TrimPrefix(text, "audio: ")
@ -229,8 +263,12 @@ func computeExtraSettings(entry *Entry) error {
entry.content = filteredContent.String() entry.content = filteredContent.String()
// Check if content contains images or add them // Check if content contains images or add them
for _, image := range entry.images { for _, image := range entry.images {
if !strings.Contains(entry.content, image) { if !strings.Contains(entry.content, image.url) {
entry.content += "\n![](" + image + ")\n" 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 // Compute slug if empty

View File

@ -6,17 +6,17 @@ type MicroformatItem struct {
} }
type MicroformatProperties struct { type MicroformatProperties struct {
Name []string `json:"name,omitempty"` Name []string `json:"name,omitempty"`
Published []string `json:"published,omitempty"` Published []string `json:"published,omitempty"`
Updated []string `json:"updated,omitempty"` Updated []string `json:"updated,omitempty"`
Category []string `json:"category,omitempty"` Category []string `json:"category,omitempty"`
Content []string `json:"content,omitempty"` Content []string `json:"content,omitempty"`
Url []string `json:"url,omitempty"` Url []string `json:"url,omitempty"`
InReplyTo []string `json:"in-reply-to,omitempty"` InReplyTo []string `json:"in-reply-to,omitempty"`
LikeOf []string `json:"like-of,omitempty"` LikeOf []string `json:"like-of,omitempty"`
BookmarkOf []string `json:"bookmark-of,omitempty"` BookmarkOf []string `json:"bookmark-of,omitempty"`
MpSlug []string `json:"mp-slug,omitempty"` MpSlug []string `json:"mp-slug,omitempty"`
MpSyndicateTo []string `json:"mp-syndicate-to,omitempty"` MpSyndicateTo []string `json:"mp-syndicate-to,omitempty"`
Photo []string `json:"photo,omitempty"` Photo []interface{} `json:"photo,omitempty"`
Audio []string `json:"audio,omitempty"` Audio []string `json:"audio,omitempty"`
} }

View File

@ -55,9 +55,11 @@ func writeFrontMatter(entry *Entry) (frontmatter string, err error) {
}, },
Syndicate: entry.syndicate, Syndicate: entry.syndicate,
TranslationKey: entry.translationKey, TranslationKey: entry.translationKey,
Images: entry.images,
Audio: entry.audio, Audio: entry.audio,
} }
for _, image := range entry.images {
writeFrontmatter.Images = append(writeFrontmatter.Images, image.url)
}
yamlBytes, err := yaml.Marshal(&writeFrontmatter) yamlBytes, err := yaml.Marshal(&writeFrontmatter)
if err != nil { if err != nil {
err = errors.New("failed marshaling frontmatter") err = errors.New("failed marshaling frontmatter")
@ -123,7 +125,9 @@ func readFrontMatter(frontmatter string, entry *Entry) (err error) {
entry.translationKey = parsedFrontmatter.TranslationKey entry.translationKey = parsedFrontmatter.TranslationKey
} }
if len(parsedFrontmatter.Images) > 0 { 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 { if len(parsedFrontmatter.Audio) > 0 {
entry.audio = parsedFrontmatter.Audio entry.audio = parsedFrontmatter.Audio