jlelse
/
hugo-micropub
Archived
1
Fork 0

Hide settings in content and not categories anymore, refactoring

This commit is contained in:
Jan-Lukas Else 2019-11-08 19:46:07 +01:00
parent db58ecb3aa
commit 93433de827
2 changed files with 74 additions and 61 deletions

115
entry.go
View File

@ -1,6 +1,8 @@
package main package main
import ( import (
"bufio"
"bytes"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -12,17 +14,17 @@ import (
) )
type Entry struct { type Entry struct {
Content string content string
Name string title string
Categories []string
Slug string
Summary string
InReplyTo string
LikeOf string
RepostOf string
section string section string
location string tags []string
link string
slug string
replyLink string
replyTitle string
filename string filename string
location string
mentions []string
token string token string
} }
@ -36,13 +38,13 @@ func CreateEntry(contentType ContentType, r *http.Request) (*Entry, error) {
if err != nil { if err != nil {
return nil, errors.New("failed to parse query") return nil, errors.New("failed to parse query")
} }
return createEntryFromURLValues(bodyValues) return createEntryFromValueMap(bodyValues)
} else if contentType == Multipart { } else if contentType == Multipart {
err := r.ParseMultipartForm(1024 * 1024 * 16) err := r.ParseMultipartForm(1024 * 1024 * 16)
if err != nil { if err != nil {
return nil, errors.New("failed to parse Multipart") return nil, errors.New("failed to parse Multipart")
} }
return createEntryFromURLValues(r.MultipartForm.Value) return createEntryFromValueMap(r.MultipartForm.Value)
} else if contentType == Json { } else if contentType == Json {
return nil, errors.New("json content-type is not implemented yet") return nil, errors.New("json content-type is not implemented yet")
} else { } else {
@ -59,39 +61,30 @@ func parseRequestBody(r *http.Request) (string, error) {
return string(bodyBytes), nil return string(bodyBytes), nil
} }
func createEntryFromURLValues(bodyValues map[string][]string) (*Entry, error) { func createEntryFromValueMap(values map[string][]string) (*Entry, error) {
if h, ok := bodyValues["h"]; ok && len(h) == 1 && h[0] != "entry" { if h, ok := values["h"]; ok && len(h) == 1 && h[0] != "entry" {
return nil, errors.New("only entry type is supported so far") return nil, errors.New("only entry type is supported so far")
} }
if _, ok := bodyValues["content"]; ok { if _, ok := values["content"]; ok {
entry := new(Entry) entry := new(Entry)
entry.Content = bodyValues["content"][0] entry.content = values["content"][0]
if name, ok := bodyValues["name"]; ok { if name, ok := values["title"]; ok {
entry.Name = name[0] entry.title = name[0]
} }
if category, ok := bodyValues["category"]; ok { if category, ok := values["category"]; ok {
entry.Categories = category entry.tags = category
} else if categories, ok := bodyValues["category[]"]; ok { } else if categories, ok := values["category[]"]; ok {
entry.Categories = categories entry.tags = categories
} else { } else {
entry.Categories = nil entry.tags = nil
} }
if slug, ok := bodyValues["mp-slug"]; ok && len(slug) > 0 && slug[0] != "" { if slug, ok := values["mp-slug"]; ok && len(slug) > 0 && slug[0] != "" {
entry.Slug = slug[0] entry.slug = slug[0]
} }
if summary, ok := bodyValues["summary"]; ok { if inReplyTo, ok := values["in-reply-to"]; ok {
entry.Summary = summary[0] entry.replyLink = inReplyTo[0]
} }
if inReplyTo, ok := bodyValues["in-reply-to"]; ok { if token, ok := values["access_token"]; ok {
entry.InReplyTo = inReplyTo[0]
}
if likeOf, ok := bodyValues["like-of"]; ok {
entry.LikeOf = likeOf[0]
}
if repostOf, ok := bodyValues["repost-of"]; ok {
entry.RepostOf = repostOf[0]
}
if token, ok := bodyValues["access_token"]; ok {
entry.token = "Bearer " + token[0] entry.token = "Bearer " + token[0]
} }
err := computeExtraSettings(entry) err := computeExtraSettings(entry)
@ -106,22 +99,36 @@ func createEntryFromURLValues(bodyValues map[string][]string) (*Entry, error) {
func computeExtraSettings(entry *Entry) error { func computeExtraSettings(entry *Entry) error {
now := time.Now() now := time.Now()
entry.section = "micro" entry.section = "micro"
// Find settings hidden in category strings // Find settings hidden in content
filteredCategories := make([]string, 0) var filteredContent bytes.Buffer
for _, category := range entry.Categories { contentScanner := bufio.NewScanner(strings.NewReader(entry.content))
if strings.HasPrefix(category, "section-") { for contentScanner.Scan() {
entry.section = strings.TrimPrefix(category, "section-") text := contentScanner.Text()
} else if strings.HasPrefix(category, "slug-") { if strings.HasPrefix(text, "section: ") {
entry.Slug = strings.TrimPrefix(category, "slug-") // Section
entry.section = strings.TrimPrefix(text, "section: ")
} else if strings.HasPrefix(text, "slug: ") {
// Slug
entry.slug = strings.TrimPrefix(text, "slug: ")
} 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 { } else {
filteredCategories = append(filteredCategories, category) _, _ = fmt.Fprintln(&filteredContent, text)
} }
} }
entry.Categories = filteredCategories entry.content = filteredContent.String()
// Compute slug if empty // Compute slug if empty
if len(entry.Slug) == 0 || entry.Slug == "" { if len(entry.slug) == 0 || entry.slug == "" {
random := generateRandomString(now, 5) random := generateRandomString(now, 5)
entry.Slug = fmt.Sprintf("%v-%02d-%02d-%v", now.Year(), int(now.Month()), now.Day(), random) entry.slug = fmt.Sprintf("%v-%02d-%02d-%v", now.Year(), int(now.Month()), now.Day(), random)
} }
// Compute filename and location // Compute filename and location
blogURL, err := GetBlogURL() blogURL, err := GetBlogURL()
@ -129,14 +136,14 @@ func computeExtraSettings(entry *Entry) error {
return err return err
} }
if entry.section == "posts" { if entry.section == "posts" {
entry.filename = "content/" + entry.section + "/" + entry.Slug + ".md" entry.filename = "content/" + entry.section + "/" + entry.slug + ".md"
entry.location = blogURL + entry.section + "/" + entry.Slug entry.location = blogURL + entry.section + "/" + entry.slug
} else if entry.section == "thoughts" || entry.section == "links" { } else if entry.section == "thoughts" || entry.section == "links" {
entry.filename = fmt.Sprintf("content/%v/%02d/%02d/%v.md", entry.section, now.Year(), int(now.Month()), entry.Slug) entry.filename = fmt.Sprintf("content/%v/%02d/%02d/%v.md", entry.section, now.Year(), int(now.Month()), entry.slug)
entry.location = fmt.Sprintf("%v%v/%02d/%02d/%v", blogURL, entry.section, now.Year(), int(now.Month()), entry.Slug) entry.location = fmt.Sprintf("%v%v/%02d/%02d/%v", blogURL, entry.section, now.Year(), int(now.Month()), entry.slug)
} else { } else {
entry.filename = "content/" + entry.section + "/" + entry.Slug + ".md" entry.filename = "content/" + entry.section + "/" + entry.slug + ".md"
entry.location = blogURL + entry.section + "/" + entry.Slug entry.location = blogURL + entry.section + "/" + entry.slug
} }
return nil return nil
} }
@ -153,7 +160,7 @@ func generateRandomString(now time.Time, n int) string {
func WriteEntry(entry *Entry) (string, error) { func WriteEntry(entry *Entry) (string, error) {
file := WriteHugoPost(entry) file := WriteHugoPost(entry)
err := CommitEntry(entry.filename, file, entry.Name) err := CommitEntry(entry.filename, file, entry.title)
if err != nil { if err != nil {
return "", err return "", err
} }

20
post.go
View File

@ -9,17 +9,23 @@ func writeFrontMatter(entry *Entry) string {
var buff bytes.Buffer var buff bytes.Buffer
t := time.Now().Format(time.RFC3339) t := time.Now().Format(time.RFC3339)
buff.WriteString("---\n") buff.WriteString("---\n")
if len(entry.Name) > 0 { if len(entry.title) > 0 {
buff.WriteString("title: \"" + entry.Name + "\"\n") buff.WriteString("title: \"" + entry.title + "\"\n")
} }
buff.WriteString("date: " + t + "\n") buff.WriteString("date: " + t + "\n")
buff.WriteString("tags:\n") buff.WriteString("tags:\n")
for _, tag := range entry.Categories { for _, tag := range entry.tags {
buff.WriteString("- " + tag + "\n") buff.WriteString("- " + tag + "\n")
} }
if len(entry.link) > 0 {
buff.WriteString("externalURL: " + entry.link + "\n")
}
buff.WriteString("indieweb:\n") buff.WriteString("indieweb:\n")
if len(entry.InReplyTo) > 0 { if len(entry.replyLink) > 0 {
buff.WriteString(" reply:\n link: " + entry.InReplyTo + "\n") buff.WriteString(" reply:\n link: " + entry.replyLink + "\n")
if len(entry.replyTitle) > 0 {
buff.WriteString(" title: " + entry.replyTitle + "\n")
}
} }
buff.WriteString("---\n") buff.WriteString("---\n")
return buff.String() return buff.String()
@ -28,8 +34,8 @@ func writeFrontMatter(entry *Entry) string {
func WriteHugoPost(entry *Entry) string { func WriteHugoPost(entry *Entry) string {
var buff bytes.Buffer var buff bytes.Buffer
buff.WriteString(writeFrontMatter(entry)) buff.WriteString(writeFrontMatter(entry))
if len(entry.Content) > 0 { if len(entry.content) > 0 {
buff.WriteString(entry.Content + "\n") buff.WriteString(entry.content)
} }
return buff.String() return buff.String()
} }