/
utils.go
105 lines (90 loc) · 2.41 KB
/
utils.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
97
98
99
100
101
102
103
104
105
// utils.go
package main
import (
"fmt"
"github.com/chownplusx/mustache"
"github.com/chownplusx/web"
md "github.com/russross/blackfriday"
"io/ioutil"
"os"
"path"
"sort"
"strings"
)
type Files []os.FileInfo
type ByModTime struct{ Files }
func (s Files) Len() int {
return len(s)
}
func (s Files) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByModTime) Less(i, j int) bool {
return s.Files[i].ModTime().Unix() > s.Files[j].ModTime().Unix()
}
func SortByModified(files []os.FileInfo) []os.FileInfo {
sort.Sort(ByModTime{files})
return files
}
func ReverseOrder(a []os.FileInfo) []os.FileInfo {
//This reverses the order of the posts
//But you can't tell just by looking ;)
for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
a[i], a[j] = a[j], a[i]
}
return a
}
func PostsInRange(start int, end int) string {
filelist, _err := ioutil.ReadDir("posts")
if _err != nil {
fmt.Println(_err)
}
SortByModified(filelist)
content := ""
for i := start; i < end; i++ {
name := filelist[i].Name()
// This formats the date nice and pretty
date := filelist[i].ModTime().Format("2006-01-02 15:04")
post, _post := ioutil.ReadFile(path.Join("posts", filelist[i].Name()))
if _post != nil {
fmt.Println(_post)
}
permalink := "<a href=\"/post/" + name + "\">Permalink</a>"
content += Loadmustache("perpost.mustache", &map[string]string{"title": name,
"content": string(md.MarkdownCommon([]byte(post))),
"details": "Posted at " + date + " " + permalink})
}
return content
}
func GetPostByName(name string) string {
post, _post := ioutil.ReadFile(path.Join("posts", name))
FileInfo, _file := os.Stat(path.Join("posts", name))
if _post != nil {
fmt.Println(_post)
}
if _file != nil {
fmt.Println(_file)
}
date := FileInfo.ModTime().Format("2006-01-02 15:04")
return Loadmustache("perpost.mustache", &map[string]string{"title": name,
"content": string(md.MarkdownCommon([]byte(post))),
"details": "Posted at " + date})
}
func Loadmustache(filename string, args *map[string]string) string {
file, _err := ioutil.ReadFile("Mst/" + filename)
if _err != nil {
fmt.Println(_err)
return "File not found"
}
data := mustache.Render(string(file), args)
return data
}
func Sendstatic(ctx *web.Context, val string) {
file, _err := ioutil.ReadFile("static/" + val)
if _err != nil {
return
}
filetype := strings.Split(val, ".")
ctx.ContentType(filetype[len(filetype)-1])
ctx.WriteString(string(file))
}