Hide settings in content and not categories anymore, refactoring
parent
db58ecb3aa
commit
93433de827
115
entry.go
115
entry.go
|
@ -1,6 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
@ -12,17 +14,17 @@ import (
|
|||
)
|
||||
|
||||
type Entry struct {
|
||||
Content string
|
||||
Name string
|
||||
Categories []string
|
||||
Slug string
|
||||
Summary string
|
||||
InReplyTo string
|
||||
LikeOf string
|
||||
RepostOf string
|
||||
content string
|
||||
title string
|
||||
section string
|
||||
location string
|
||||
tags []string
|
||||
link string
|
||||
slug string
|
||||
replyLink string
|
||||
replyTitle string
|
||||
filename string
|
||||
location string
|
||||
mentions []string
|
||||
token string
|
||||
}
|
||||
|
||||
|
@ -36,13 +38,13 @@ func CreateEntry(contentType ContentType, r *http.Request) (*Entry, error) {
|
|||
if err != nil {
|
||||
return nil, errors.New("failed to parse query")
|
||||
}
|
||||
return createEntryFromURLValues(bodyValues)
|
||||
return createEntryFromValueMap(bodyValues)
|
||||
} else if contentType == Multipart {
|
||||
err := r.ParseMultipartForm(1024 * 1024 * 16)
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to parse Multipart")
|
||||
}
|
||||
return createEntryFromURLValues(r.MultipartForm.Value)
|
||||
return createEntryFromValueMap(r.MultipartForm.Value)
|
||||
} else if contentType == Json {
|
||||
return nil, errors.New("json content-type is not implemented yet")
|
||||
} else {
|
||||
|
@ -59,39 +61,30 @@ func parseRequestBody(r *http.Request) (string, error) {
|
|||
return string(bodyBytes), nil
|
||||
}
|
||||
|
||||
func createEntryFromURLValues(bodyValues map[string][]string) (*Entry, error) {
|
||||
if h, ok := bodyValues["h"]; ok && len(h) == 1 && h[0] != "entry" {
|
||||
func createEntryFromValueMap(values map[string][]string) (*Entry, error) {
|
||||
if h, ok := values["h"]; ok && len(h) == 1 && h[0] != "entry" {
|
||||
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.Content = bodyValues["content"][0]
|
||||
if name, ok := bodyValues["name"]; ok {
|
||||
entry.Name = name[0]
|
||||
entry.content = values["content"][0]
|
||||
if name, ok := values["title"]; ok {
|
||||
entry.title = name[0]
|
||||
}
|
||||
if category, ok := bodyValues["category"]; ok {
|
||||
entry.Categories = category
|
||||
} else if categories, ok := bodyValues["category[]"]; ok {
|
||||
entry.Categories = categories
|
||||
if category, ok := values["category"]; ok {
|
||||
entry.tags = category
|
||||
} else if categories, ok := values["category[]"]; ok {
|
||||
entry.tags = categories
|
||||
} else {
|
||||
entry.Categories = nil
|
||||
entry.tags = nil
|
||||
}
|
||||
if slug, ok := bodyValues["mp-slug"]; ok && len(slug) > 0 && slug[0] != "" {
|
||||
entry.Slug = slug[0]
|
||||
if slug, ok := values["mp-slug"]; ok && len(slug) > 0 && slug[0] != "" {
|
||||
entry.slug = slug[0]
|
||||
}
|
||||
if summary, ok := bodyValues["summary"]; ok {
|
||||
entry.Summary = summary[0]
|
||||
if inReplyTo, ok := values["in-reply-to"]; ok {
|
||||
entry.replyLink = inReplyTo[0]
|
||||
}
|
||||
if inReplyTo, ok := bodyValues["in-reply-to"]; 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 {
|
||||
if token, ok := values["access_token"]; ok {
|
||||
entry.token = "Bearer " + token[0]
|
||||
}
|
||||
err := computeExtraSettings(entry)
|
||||
|
@ -106,22 +99,36 @@ func createEntryFromURLValues(bodyValues map[string][]string) (*Entry, error) {
|
|||
func computeExtraSettings(entry *Entry) error {
|
||||
now := time.Now()
|
||||
entry.section = "micro"
|
||||
// Find settings hidden in category strings
|
||||
filteredCategories := make([]string, 0)
|
||||
for _, category := range entry.Categories {
|
||||
if strings.HasPrefix(category, "section-") {
|
||||
entry.section = strings.TrimPrefix(category, "section-")
|
||||
} else if strings.HasPrefix(category, "slug-") {
|
||||
entry.Slug = strings.TrimPrefix(category, "slug-")
|
||||
// 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: ")
|
||||
} 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 {
|
||||
filteredCategories = append(filteredCategories, category)
|
||||
_, _ = fmt.Fprintln(&filteredContent, text)
|
||||
}
|
||||
}
|
||||
entry.Categories = filteredCategories
|
||||
entry.content = filteredContent.String()
|
||||
|
||||
// Compute slug if empty
|
||||
if len(entry.Slug) == 0 || entry.Slug == "" {
|
||||
if len(entry.slug) == 0 || entry.slug == "" {
|
||||
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
|
||||
blogURL, err := GetBlogURL()
|
||||
|
@ -129,14 +136,14 @@ func computeExtraSettings(entry *Entry) error {
|
|||
return err
|
||||
}
|
||||
if entry.section == "posts" {
|
||||
entry.filename = "content/" + entry.section + "/" + entry.Slug + ".md"
|
||||
entry.location = blogURL + entry.section + "/" + entry.Slug
|
||||
entry.filename = "content/" + entry.section + "/" + entry.slug + ".md"
|
||||
entry.location = blogURL + entry.section + "/" + entry.slug
|
||||
} 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.location = fmt.Sprintf("%v%v/%02d/%02d/%v", blogURL, 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)
|
||||
} else {
|
||||
entry.filename = "content/" + entry.section + "/" + entry.Slug + ".md"
|
||||
entry.location = blogURL + entry.section + "/" + entry.Slug
|
||||
entry.filename = "content/" + entry.section + "/" + entry.slug + ".md"
|
||||
entry.location = blogURL + entry.section + "/" + entry.slug
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -153,7 +160,7 @@ func generateRandomString(now time.Time, n int) string {
|
|||
|
||||
func WriteEntry(entry *Entry) (string, error) {
|
||||
file := WriteHugoPost(entry)
|
||||
err := CommitEntry(entry.filename, file, entry.Name)
|
||||
err := CommitEntry(entry.filename, file, entry.title)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
20
post.go
20
post.go
|
@ -9,17 +9,23 @@ func writeFrontMatter(entry *Entry) string {
|
|||
var buff bytes.Buffer
|
||||
t := time.Now().Format(time.RFC3339)
|
||||
buff.WriteString("---\n")
|
||||
if len(entry.Name) > 0 {
|
||||
buff.WriteString("title: \"" + entry.Name + "\"\n")
|
||||
if len(entry.title) > 0 {
|
||||
buff.WriteString("title: \"" + entry.title + "\"\n")
|
||||
}
|
||||
buff.WriteString("date: " + t + "\n")
|
||||
buff.WriteString("tags:\n")
|
||||
for _, tag := range entry.Categories {
|
||||
for _, tag := range entry.tags {
|
||||
buff.WriteString("- " + tag + "\n")
|
||||
}
|
||||
if len(entry.link) > 0 {
|
||||
buff.WriteString("externalURL: " + entry.link + "\n")
|
||||
}
|
||||
buff.WriteString("indieweb:\n")
|
||||
if len(entry.InReplyTo) > 0 {
|
||||
buff.WriteString(" reply:\n link: " + entry.InReplyTo + "\n")
|
||||
if len(entry.replyLink) > 0 {
|
||||
buff.WriteString(" reply:\n link: " + entry.replyLink + "\n")
|
||||
if len(entry.replyTitle) > 0 {
|
||||
buff.WriteString(" title: " + entry.replyTitle + "\n")
|
||||
}
|
||||
}
|
||||
buff.WriteString("---\n")
|
||||
return buff.String()
|
||||
|
@ -28,8 +34,8 @@ func writeFrontMatter(entry *Entry) string {
|
|||
func WriteHugoPost(entry *Entry) string {
|
||||
var buff bytes.Buffer
|
||||
buff.WriteString(writeFrontMatter(entry))
|
||||
if len(entry.Content) > 0 {
|
||||
buff.WriteString(entry.Content + "\n")
|
||||
if len(entry.content) > 0 {
|
||||
buff.WriteString(entry.content)
|
||||
}
|
||||
return buff.String()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue