-
Notifications
You must be signed in to change notification settings - Fork 2
/
content.go
96 lines (85 loc) · 1.75 KB
/
content.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
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
package main
import (
"fmt"
"github.com/russross/blackfriday"
"io/ioutil"
"os"
"sort"
"strings"
"time"
)
const DateFormat = "Jan _2 2006 15:04"
type Article struct {
Title string
Author string
Tags []string
Image string
HaveImage bool
Time time.Time
FullView bool
Path string
Body string
}
func GetArticle(fileName string) (*Article, error) {
haveImage := true
data, err := ioutil.ReadFile("./articles/" + fileName)
if err != nil {
return nil, err
}
md, cont := GetMetadata(data)
d, err := time.Parse(DateFormat, md.Date)
if err != nil {
fmt.Println("error getting pubdate:", err)
}
if md.Image == "" {
haveImage = false
}
return &Article{md.Title, md.Author, md.Tags, md.Image, haveImage, d, true, fileName, string(blackfriday.MarkdownCommon(cont))}, nil
}
func (a *Article) Date() string {
return a.Time.Format(DateFormat)
}
func (a *Article) HasTag(tag string) bool {
for _, t := range a.Tags {
if tag == t {
return true
}
}
return false
}
func GetArticles(include func(*Article) bool) ArticleSlice {
articles := make(ArticleSlice, 0)
file, err := os.Open("./articles")
if err != nil {
return nil
}
files, err := file.Readdirnames(0)
if err != nil {
return nil
}
for _, f := range files {
if strings.HasSuffix(f, ".md") {
a, err := GetArticle(f)
if err != nil {
continue
}
if include(a) {
articles = append(articles, a)
}
}
}
sort.Sort(sort.Reverse(articles))
return articles
}
type Page struct {
Title string
Body string
}
func GetPage(path string) (*Page, error) {
data, err := ioutil.ReadFile("./static/" + path)
if err != nil {
return nil, err
}
md, cont := GetMetadata(data)
return &Page{md.Title, string(blackfriday.MarkdownCommon(cont))}, nil
}