-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.go
More file actions
154 lines (136 loc) · 2.8 KB
/
models.go
File metadata and controls
154 lines (136 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"net/url"
"time"
"github.com/andyleap/microformats"
)
type PostData struct {
Type string
Content *json.RawMessage
}
type Post interface {
Render(t *template.Template) template.HTML
Slug() string
}
type HEntry struct {
Published time.Time
Mentions []*Mention
}
type Note struct {
HEntry
Message string
Draft bool
}
type Article struct {
HEntry
Title string
Content string
Draft bool
}
type Mention struct {
Source *url.URL
Data *microformats.MicroFormat
Published time.Time
}
const (
TypeNote = "note"
TypeArticle = "article"
)
func UnmarshalPost(data []byte) Post {
var post PostData
json.Unmarshal(data, &post)
switch post.Type {
case TypeNote:
var note Note
json.Unmarshal(*post.Content, ¬e)
return note
case TypeArticle:
var article Article
json.Unmarshal(*post.Content, &article)
return article
}
return nil
}
func MarshalPost(post Post) []byte {
contentdata, _ := json.Marshal(post)
var data PostData
contentjson := json.RawMessage(contentdata)
data.Content = &contentjson
switch post.(type) {
case Note:
data.Type = TypeNote
case Article:
data.Type = TypeArticle
}
datadata, _ := json.Marshal(data)
return datadata
}
func (n Note) Render(t *template.Template) template.HTML {
buf := &bytes.Buffer{}
if err := t.ExecuteTemplate(buf, "note.tpl", n); err != nil {
fmt.Println(err)
}
return template.HTML(buf.String())
}
func (a Article) Render(t *template.Template) template.HTML {
buf := &bytes.Buffer{}
if err := t.ExecuteTemplate(buf, "article.tpl", a); err != nil {
fmt.Println(err)
}
return template.HTML(buf.String())
}
func (e HEntry) MentionItems() []struct {
Content string
URL string
Mention *Mention
} {
mentions := make([]struct {
Content string
URL string
Mention *Mention
}, 0)
for _, m := range e.Mentions {
renContent := ""
renURL := ""
if content, ok := m.Data.Properties["content"]; ok && renContent == "" {
if content, ok := content[0].(*microformats.MicroFormat); ok {
renContent = content.Value
}
}
if summary, ok := m.Data.Properties["summary"]; ok && renContent == "" {
if summary, ok := summary[0].(string); ok {
renContent = summary
}
}
if name, ok := m.Data.Properties["name"]; ok && renContent == "" {
if name, ok := name[0].(string); ok {
renContent = name
}
}
if url, ok := m.Data.Properties["url"]; ok {
if url, ok := url[0].(string); ok {
renURL = url
}
}
mentions = append(mentions, struct {
Content string
URL string
Mention *Mention
}{
renContent,
renURL,
m,
})
}
return mentions
}
func (h HEntry) Render(t *template.Template) template.HTML {
return template.HTML("")
}
func (e HEntry) Slug() string {
return TimeToSlug(e.Published)
}