hugo-micropub/entry.go

342 lines
9.9 KiB
Go
Raw Normal View History

2019-11-07 10:00:24 +00:00
package main
import (
"bufio"
"bytes"
2019-12-22 09:23:57 +00:00
"encoding/json"
2019-11-07 10:00:24 +00:00
"errors"
"fmt"
"math/rand"
2019-11-07 11:10:45 +00:00
"net/http"
2019-11-07 10:00:24 +00:00
"strings"
"text/template"
2019-11-07 10:00:24 +00:00
"time"
)
type Entry struct {
2020-01-12 17:17:40 +00:00
content string
title string
date string
lastmod string
section string
tags []string
2020-03-31 15:02:05 +00:00
series []string
2020-01-12 17:17:40 +00:00
link string
slug string
replyLink string
replyTitle string
likeLink string
likeTitle string
syndicate []string
language string
translationKey string
2020-03-20 17:28:57 +00:00
images []Image
2020-01-19 07:36:10 +00:00
audio string
2020-01-12 17:17:40 +00:00
filename string
location string
token string
2019-11-07 10:00:24 +00:00
}
2020-03-20 17:28:57 +00:00
type Image struct {
url string
alt string
}
2019-11-07 11:10:45 +00:00
func CreateEntry(contentType ContentType, r *http.Request) (*Entry, error) {
2019-11-07 10:00:24 +00:00
if contentType == WwwForm {
2020-03-26 09:49:13 +00:00
err := r.ParseForm()
2019-11-07 11:10:45 +00:00
if err != nil {
2020-03-26 09:49:13 +00:00
return nil, errors.New("failed to parse Form")
2019-11-07 11:10:45 +00:00
}
2020-03-26 09:49:13 +00:00
return createEntryFromValueMap(r.Form)
2019-11-07 11:10:45 +00:00
} else if contentType == Multipart {
err := r.ParseMultipartForm(1024 * 1024 * 16)
if err != nil {
return nil, errors.New("failed to parse Multipart")
}
return createEntryFromValueMap(r.MultipartForm.Value)
2019-11-07 11:10:45 +00:00
} else if contentType == Json {
2020-03-26 09:49:13 +00:00
decoder := json.NewDecoder(r.Body)
parsedMfItem := &MicroformatItem{}
2020-03-26 09:49:13 +00:00
err := decoder.Decode(&parsedMfItem)
if err != nil {
return nil, errors.New("failed to parse Json")
}
return createEntryFromMicroformat(parsedMfItem)
2019-11-07 10:00:24 +00:00
} else {
return nil, errors.New("unsupported content-type")
}
}
func createEntryFromValueMap(values map[string][]string) (*Entry, error) {
if h, ok := values["h"]; ok && (len(h) != 1 || h[0] != "entry") {
2019-11-07 10:00:24 +00:00
return nil, errors.New("only entry type is supported so far")
}
2020-01-12 16:30:32 +00:00
entry := &Entry{}
if content, ok := values["content"]; ok {
entry.content = content[0]
}
if name, ok := values["name"]; ok {
entry.title = name[0]
}
if category, ok := values["category"]; ok {
entry.tags = category
} else if categories, ok := values["category[]"]; ok {
entry.tags = categories
}
if slug, ok := values["mp-slug"]; ok && len(slug) > 0 && slug[0] != "" {
entry.slug = slug[0]
}
if inReplyTo, ok := values["in-reply-to"]; ok {
entry.replyLink = inReplyTo[0]
}
if likeOf, ok := values["like-of"]; ok {
entry.likeLink = likeOf[0]
}
if bookmarkOf, ok := values["bookmark-of"]; ok {
entry.link = bookmarkOf[0]
}
if syndicate, ok := values["mp-syndicate-to"]; ok {
entry.syndicate = syndicate
} else if syndicates, ok := values["mp-syndicate-to[]"]; ok {
entry.syndicate = syndicates
2020-01-19 07:36:10 +00:00
}
if photo, ok := values["photo"]; ok {
2020-03-20 17:28:57 +00:00
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
}
}
2020-01-19 07:36:10 +00:00
}
if audio, ok := values["audio"]; ok {
entry.audio = audio[0]
} else if audio, ok := values["audio[]"]; ok {
entry.audio = audio[0]
2020-01-12 16:30:32 +00:00
}
if token, ok := values["access_token"]; ok {
entry.token = "Bearer " + token[0]
2019-11-07 10:00:24 +00:00
}
2020-01-12 16:30:32 +00:00
err := computeExtraSettings(entry)
if err != nil {
return nil, err
}
return entry, nil
2019-11-07 10:00:24 +00:00
}
func createEntryFromMicroformat(mfEntry *MicroformatItem) (*Entry, error) {
if len(mfEntry.Type) != 1 || mfEntry.Type[0] != "h-entry" {
return nil, errors.New("only entry type is supported so far")
}
2020-01-12 16:30:32 +00:00
entry := &Entry{}
if mfEntry.Properties != nil && len(mfEntry.Properties.Content) == 1 && len(mfEntry.Properties.Content[0]) > 0 {
2020-01-12 16:30:32 +00:00
entry.content = mfEntry.Properties.Content[0]
}
if len(mfEntry.Properties.Name) == 1 {
entry.title = mfEntry.Properties.Name[0]
}
if len(mfEntry.Properties.Category) > 0 {
entry.tags = mfEntry.Properties.Category
}
if len(mfEntry.Properties.MpSlug) == 1 && len(mfEntry.Properties.MpSlug[0]) > 0 {
entry.slug = mfEntry.Properties.MpSlug[0]
}
if len(mfEntry.Properties.InReplyTo) == 1 {
entry.replyLink = mfEntry.Properties.InReplyTo[0]
}
if len(mfEntry.Properties.LikeOf) == 1 {
entry.likeLink = mfEntry.Properties.LikeOf[0]
}
if len(mfEntry.Properties.BookmarkOf) == 1 {
entry.link = mfEntry.Properties.BookmarkOf[0]
}
if len(mfEntry.Properties.MpSyndicateTo) > 0 {
entry.syndicate = mfEntry.Properties.MpSyndicateTo
}
2020-01-19 07:36:10 +00:00
if len(mfEntry.Properties.Photo) > 0 {
2020-03-20 17:28:57 +00:00
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)
}
}
2020-01-19 07:36:10 +00:00
}
if len(mfEntry.Properties.Audio) > 0 {
entry.audio = mfEntry.Properties.Audio[0]
}
2020-01-12 16:30:32 +00:00
err := computeExtraSettings(entry)
if err != nil {
return nil, err
}
2020-01-12 16:30:32 +00:00
return entry, nil
}
2019-11-07 10:00:24 +00:00
func computeExtraSettings(entry *Entry) error {
2019-12-06 09:32:30 +00:00
now := time.Now()
// Set date
entry.date = now.Format(time.RFC3339)
// Find settings hidden in content
var filteredContent bytes.Buffer
contentScanner := bufio.NewScanner(strings.NewReader(entry.content))
for contentScanner.Scan() {
text := contentScanner.Text()
if strings.HasPrefix(text, "section: ") {
// Section
entry.section = strings.TrimPrefix(text, "section: ")
2019-11-08 23:40:55 +00:00
} else if strings.HasPrefix(text, "title: ") {
// Title
entry.title = strings.TrimPrefix(text, "title: ")
} else if strings.HasPrefix(text, "slug: ") {
// Slug
entry.slug = strings.TrimPrefix(text, "slug: ")
} else if strings.HasPrefix(text, "tags: ") {
// Tags
entry.tags = strings.Split(strings.TrimPrefix(text, "tags: "), ",")
2020-03-31 15:02:05 +00:00
} else if strings.HasPrefix(text, "series: ") {
// Series
entry.series = strings.Split(strings.TrimPrefix(text, "series: "), ",")
} else if strings.HasPrefix(text, "link: ") {
// Link
entry.link = strings.TrimPrefix(text, "link: ")
} else if strings.HasPrefix(text, "reply-link: ") {
// Reply link
entry.replyLink = strings.TrimPrefix(text, "reply-link: ")
} else if strings.HasPrefix(text, "reply-title: ") {
// Reply title
entry.replyTitle = strings.TrimPrefix(text, "reply-title: ")
} else if strings.HasPrefix(text, "like-link: ") {
// Like link
entry.likeLink = strings.TrimPrefix(text, "like-link: ")
} else if strings.HasPrefix(text, "like-title: ") {
// Like title
entry.likeTitle = strings.TrimPrefix(text, "like-title: ")
2020-01-12 17:17:40 +00:00
} 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: ")
2020-01-19 07:36:10 +00:00
} else if strings.HasPrefix(text, "images: ") {
// Images
2020-03-20 17:28:57 +00:00
for _, image := range strings.Split(strings.TrimPrefix(text, "images: "), ",") {
entry.images = append(entry.images, Image{url: image})
}
2020-01-19 07:36:10 +00:00
} else if strings.HasPrefix(text, "audio: ") {
// Audio
entry.audio = strings.TrimPrefix(text, "audio: ")
2019-11-07 10:00:24 +00:00
} else {
_, _ = fmt.Fprintln(&filteredContent, text)
2019-11-07 10:00:24 +00:00
}
}
entry.content = filteredContent.String()
2020-01-19 07:36:10 +00:00
// Check if content contains images or add them
for _, image := range entry.images {
2020-03-20 17:28:57 +00:00
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"
}
2020-01-19 07:36:10 +00:00
}
}
2019-11-07 10:00:24 +00:00
// Compute slug if empty
if len(entry.slug) == 0 || entry.slug == "" {
2019-11-07 10:00:24 +00:00
random := generateRandomString(now, 5)
entry.slug = fmt.Sprintf("%v-%02d-%02d-%v", now.Year(), int(now.Month()), now.Day(), random)
2019-11-07 10:00:24 +00:00
}
// Set language
if len(entry.language) == 0 {
entry.language = DefaultLanguage
}
2019-11-07 10:00:24 +00:00
// Compute filename and location
lang := Languages[entry.language]
contentFolder := lang.ContentDir
2020-01-12 17:17:40 +00:00
localizedBlogUrl := BlogUrl
if len(lang.BlogUrl) != 0 {
localizedBlogUrl = lang.BlogUrl
2020-01-12 17:17:40 +00:00
}
if len(entry.section) == 0 {
entry.section = lang.DefaultSection
2019-12-03 16:46:16 +00:00
}
entry.section = strings.ToLower(entry.section)
section := lang.Sections[entry.section]
pathVars := struct {
LocalContentFolder string
LocalBlogUrl string
Year int
Month int
Slug string
Section string
}{
LocalContentFolder: contentFolder,
LocalBlogUrl: localizedBlogUrl,
Year: now.Year(),
Month: int(now.Month()),
Slug: entry.slug,
Section: entry.section,
}
filenameTmpl, err := template.New("filename").Parse(section.FilenameTemplate)
if err != nil {
return errors.New("failed to parse filename template")
}
filename := new(bytes.Buffer)
err = filenameTmpl.Execute(filename, pathVars)
if err != nil {
return errors.New("failed to execute filename template")
}
entry.filename = filename.String()
locationTmpl, err := template.New("location").Parse(section.LocationTemplate)
if err != nil {
return errors.New("failed to parse location template")
}
location := new(bytes.Buffer)
err = locationTmpl.Execute(location, pathVars)
if err != nil {
return errors.New("failed to execute location template")
2019-11-07 10:00:24 +00:00
}
entry.location = location.String()
2019-11-07 10:00:24 +00:00
return nil
}
func generateRandomString(now time.Time, n int) string {
rand.Seed(now.UnixNano())
letters := []rune("abcdefghijklmnopqrstuvwxyz")
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func WriteEntry(entry *Entry) (location string, err error) {
file, err := WriteHugoPost(entry)
2019-11-07 10:00:24 +00:00
if err != nil {
return
2019-11-07 10:00:24 +00:00
}
2019-12-22 20:17:11 +00:00
err = SelectedStorage.CreateFile(entry.filename, file, entry.title)
if err != nil {
return
}
location = entry.location
return
2020-03-31 15:02:05 +00:00
}