-
Notifications
You must be signed in to change notification settings - Fork 0
/
articles.go
53 lines (45 loc) · 1.28 KB
/
articles.go
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
package main
import (
"time"
"encoding/json"
"fmt"
"github.com/russross/blackfriday"
"html/template"
)
type Article struct {
Title string `json:"title"`
PublishedDate time.Time `json:"publishedDate"`
SimpleName string `json:"simpleName"`
Published bool `json:"published"`
Body string `json:"body"`
AuthorName string `json:"authorName"`
}
func (article *Article) ParsedBody() template.HTML {
output := blackfriday.MarkdownCommon([]byte(article.Body))
return template.HTML(output)
}
func (article *Article) FormattedPublishedDate() string {
return fmt.Sprintf("%d-%02d-%02d", article.PublishedDate.Year(),
article.PublishedDate.Month(),
article.PublishedDate.Day())
}
func (article *Article) PublishedDateXml() string {
return fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02dZ",
article.PublishedDate.Year(),
article.PublishedDate.Month(),
article.PublishedDate.Day(),
article.PublishedDate.Hour(),
article.PublishedDate.Minute(),
article.PublishedDate.Second())
}
func (article *Article) ToJson() []byte {
encoded, err := json.Marshal(article)
if err != nil {
panic(err)
}
return encoded
}
func (article *Article) FromJson(raw []byte) {
json.Unmarshal(raw, article)
}
type Articles []Article