Refactoring, small fix, added creating posts using JSON
parent
ea0400ab9d
commit
9aab54619a
64
entry.go
64
entry.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -49,7 +50,16 @@ func CreateEntry(contentType ContentType, r *http.Request) (*Entry, error) {
|
||||||
}
|
}
|
||||||
return createEntryFromValueMap(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")
|
bodyString, err := parseRequestBody(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
parsedMfItem := &MicroformatItem{}
|
||||||
|
err = yaml.Unmarshal([]byte(bodyString), &parsedMfItem)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("failed to parse Json")
|
||||||
|
}
|
||||||
|
return createEntryFromMicroformat(parsedMfItem)
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("unsupported content-type")
|
return nil, errors.New("unsupported content-type")
|
||||||
}
|
}
|
||||||
|
@ -65,13 +75,14 @@ func parseRequestBody(r *http.Request) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createEntryFromValueMap(values map[string][]string) (*Entry, error) {
|
func createEntryFromValueMap(values map[string][]string) (*Entry, error) {
|
||||||
if h, ok := values["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 := values["content"]; ok {
|
if _, ok := values["content"]; ok {
|
||||||
entry := new(Entry)
|
entry := &Entry{
|
||||||
entry.content = values["content"][0]
|
content: values["content"][0],
|
||||||
if name, ok := values["title"]; ok {
|
}
|
||||||
|
if name, ok := values["name"]; ok {
|
||||||
entry.title = name[0]
|
entry.title = name[0]
|
||||||
}
|
}
|
||||||
if category, ok := values["category"]; ok {
|
if category, ok := values["category"]; ok {
|
||||||
|
@ -102,6 +113,38 @@ func createEntryFromValueMap(values map[string][]string) (*Entry, error) {
|
||||||
return nil, errors.New("error parsing the entry")
|
return nil, errors.New("error parsing the entry")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
if mfEntry.Properties != nil && len(mfEntry.Properties.Content) == 1 && len(mfEntry.Properties.Content[0]) > 0 {
|
||||||
|
entry := &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]
|
||||||
|
}
|
||||||
|
err := computeExtraSettings(entry)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return entry, nil
|
||||||
|
}
|
||||||
|
return nil, errors.New("error parsing the entry")
|
||||||
|
}
|
||||||
|
|
||||||
func computeExtraSettings(entry *Entry) error {
|
func computeExtraSettings(entry *Entry) error {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
// Set date
|
// Set date
|
||||||
|
@ -153,15 +196,12 @@ func computeExtraSettings(entry *Entry) error {
|
||||||
entry.section = "micro"
|
entry.section = "micro"
|
||||||
}
|
}
|
||||||
entry.section = strings.ToLower(entry.section)
|
entry.section = strings.ToLower(entry.section)
|
||||||
if entry.section == "posts" {
|
if entry.section == "thoughts" || entry.section == "links" || entry.section == "micro" {
|
||||||
entry.filename = "content/" + entry.section + "/" + entry.slug + ".md"
|
|
||||||
entry.location = BlogUrl + entry.section + "/" + entry.slug
|
|
||||||
} else if entry.section == "thoughts" || entry.section == "links" || entry.section == "micro" {
|
|
||||||
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 = fmt.Sprintf("content/%v/%v.md", entry.section, entry.slug)
|
||||||
entry.location = BlogUrl + entry.section + "/" + entry.slug
|
entry.location = fmt.Sprintf("%v%v/%v/", BlogUrl, entry.section, entry.slug)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
1
go.sum
1
go.sum
|
@ -1,5 +1,6 @@
|
||||||
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
|
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
|
||||||
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
|
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
|
||||||
|
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
|
||||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
github.com/wsxiaoys/terminal v0.0.0-20160513160801-0940f3fc43a0/go.mod h1:IXCdmsXIht47RaVFLEdVnh1t+pgYtTAhQGj73kz+2DM=
|
github.com/wsxiaoys/terminal v0.0.0-20160513160801-0940f3fc43a0/go.mod h1:IXCdmsXIht47RaVFLEdVnh1t+pgYtTAhQGj73kz+2DM=
|
||||||
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type MicroformatItem struct {
|
||||||
|
Type []string `json:"type"`
|
||||||
|
Properties *MicroformatProperties `json:"properties"`
|
||||||
|
}
|
||||||
|
|
||||||
|
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"`
|
||||||
|
MpSlug []string `json:"mp-slug,omitempty"`
|
||||||
|
}
|
36
post.go
36
post.go
|
@ -7,44 +7,44 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Frontmatter struct {
|
type HugoFrontmatter struct {
|
||||||
Title string `yaml:"title,omitempty"`
|
Title string `yaml:"title,omitempty"`
|
||||||
Date string `yaml:"date,omitempty"`
|
Date string `yaml:"date,omitempty"`
|
||||||
Lastmod string `yaml:"lastmod,omitempty"`
|
Lastmod string `yaml:"lastmod,omitempty"`
|
||||||
Tags []string `yaml:"tags,omitempty"`
|
Tags []string `yaml:"tags,omitempty"`
|
||||||
ExternalURL string `yaml:"externalURL,omitempty"`
|
ExternalURL string `yaml:"externalURL,omitempty"`
|
||||||
Indieweb Indieweb `yaml:"indieweb,omitempty"`
|
Indieweb HugoFrontmatterIndieweb `yaml:"indieweb,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Indieweb struct {
|
type HugoFrontmatterIndieweb struct {
|
||||||
Reply Reply `yaml:"reply,omitempty"`
|
Reply HugoFrontmatterReply `yaml:"reply,omitempty"`
|
||||||
Like Like `yaml:"like,omitempty"`
|
Like HugoFrontmatterLike `yaml:"like,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Reply struct {
|
type HugoFrontmatterReply struct {
|
||||||
Link string `yaml:"link,omitempty"`
|
Link string `yaml:"link,omitempty"`
|
||||||
Title string `yaml:"title,omitempty"`
|
Title string `yaml:"title,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Like struct {
|
type HugoFrontmatterLike struct {
|
||||||
Link string `yaml:"link,omitempty"`
|
Link string `yaml:"link,omitempty"`
|
||||||
Title string `yaml:"title,omitempty"`
|
Title string `yaml:"title,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeFrontMatter(entry *Entry) (frontmatter string, err error) {
|
func writeFrontMatter(entry *Entry) (frontmatter string, err error) {
|
||||||
var buff bytes.Buffer
|
var buff bytes.Buffer
|
||||||
writeFrontmatter := &Frontmatter{
|
writeFrontmatter := &HugoFrontmatter{
|
||||||
Title: entry.title,
|
Title: entry.title,
|
||||||
Date: entry.date,
|
Date: entry.date,
|
||||||
Lastmod: entry.lastmod,
|
Lastmod: entry.lastmod,
|
||||||
Tags: entry.tags,
|
Tags: entry.tags,
|
||||||
ExternalURL: entry.link,
|
ExternalURL: entry.link,
|
||||||
Indieweb: Indieweb{
|
Indieweb: HugoFrontmatterIndieweb{
|
||||||
Reply: Reply{
|
Reply: HugoFrontmatterReply{
|
||||||
Link: entry.replyLink,
|
Link: entry.replyLink,
|
||||||
Title: entry.replyTitle,
|
Title: entry.replyTitle,
|
||||||
},
|
},
|
||||||
Like: Like{
|
Like: HugoFrontmatterLike{
|
||||||
Link: entry.likeLink,
|
Link: entry.likeLink,
|
||||||
Title: entry.likeTitle,
|
Title: entry.likeTitle,
|
||||||
},
|
},
|
||||||
|
@ -76,7 +76,7 @@ func WriteHugoPost(entry *Entry) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func readFrontMatter(frontmatter string, entry *Entry) (err error) {
|
func readFrontMatter(frontmatter string, entry *Entry) (err error) {
|
||||||
parsedFrontmatter := &Frontmatter{}
|
parsedFrontmatter := &HugoFrontmatter{}
|
||||||
err = yaml.Unmarshal([]byte(frontmatter), &parsedFrontmatter)
|
err = yaml.Unmarshal([]byte(frontmatter), &parsedFrontmatter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.New("failed parsing frontmatter")
|
err = errors.New("failed parsing frontmatter")
|
||||||
|
@ -124,4 +124,4 @@ func ReadHugoPost(fileContent string) (entry *Entry, err error) {
|
||||||
}
|
}
|
||||||
entry.content = strings.TrimSuffix(parts[2], "\n")
|
entry.content = strings.TrimSuffix(parts[2], "\n")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
22
query.go
22
query.go
|
@ -8,21 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type ItemList struct {
|
type ItemList struct {
|
||||||
Items []*Item `json:"items"`
|
Items []*MicroformatItem `json:"items"`
|
||||||
}
|
|
||||||
|
|
||||||
type Item struct {
|
|
||||||
Type []string `json:"type"`
|
|
||||||
Properties *Properties `json:"properties"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Properties struct {
|
|
||||||
Name []string `json:"name"`
|
|
||||||
Published []string `json:"published"`
|
|
||||||
Updated []string `json:"updated"`
|
|
||||||
Category []string `json:"category"`
|
|
||||||
Content []string `json:"content"`
|
|
||||||
Url []string `json:"url"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func QueryURL(url string, limit int) ([]byte, error) {
|
func QueryURL(url string, limit int) ([]byte, error) {
|
||||||
|
@ -62,14 +48,14 @@ func QueryURL(url string, limit int) ([]byte, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getItem(url string) (item *Item, err error) {
|
func getItem(url string) (item *MicroformatItem, err error) {
|
||||||
entry, err := ReadEntry(url)
|
entry, err := ReadEntry(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
item = &Item{
|
item = &MicroformatItem{
|
||||||
Type: []string{"h-entry"},
|
Type: []string{"h-entry"},
|
||||||
Properties: &Properties{
|
Properties: &MicroformatProperties{
|
||||||
Name: []string{entry.title},
|
Name: []string{entry.title},
|
||||||
Published: []string{entry.date},
|
Published: []string{entry.date},
|
||||||
Updated: []string{entry.lastmod},
|
Updated: []string{entry.lastmod},
|
||||||
|
|
Loading…
Reference in New Issue