Browse Source

Support alternative text for photos

master
Jan-Lukas Else 10 months ago
parent
commit
7e819f4017
3 changed files with 66 additions and 24 deletions
  1. +46
    -8
      entry.go
  2. +14
    -14
      microformat.go
  3. +6
    -2
      post.go

+ 46
- 8
entry.go View File

@ -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


+ 14
- 14
microformat.go View File

@ -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"`
}

+ 6
- 2
post.go View File

@ -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


Loading…
Cancel
Save