-
Notifications
You must be signed in to change notification settings - Fork 0
/
poststub.go
80 lines (68 loc) · 1.46 KB
/
poststub.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
package main
import (
"errors"
"net/url"
"strconv"
"strings"
"time"
)
type Stubs []*PostStub
type PostStub struct {
Path string
Title string
Date time.Time
LastModified time.Time
Post *Post
}
func (s Stubs) Len() int {
return len(s)
}
func (s Stubs) Less(i, j int) bool {
return s[i].Date.Before(s[j].Date)
}
func (s Stubs) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func GetUrlFragmentFromTitle(title string) string {
u, _ := url.Parse(title)
return u.EscapedPath()
}
func GetDateAndTitleFromFile(file string) (title string, date time.Time, err error) {
title = ""
date = time.Now()
err = nil
tokens := strings.SplitN(file, "-", 7)
if len(tokens) != 7 {
err = errors.New("Malformed string")
return
}
var location *time.Location
var year, monthNum, day, hour, min, sec int
var month time.Month
if year, err = strconv.Atoi(tokens[0]); err != nil {
return
}
if monthNum, err = strconv.Atoi(tokens[1]); err != nil {
return
}
month = time.Month(monthNum)
if day, err = strconv.Atoi(tokens[2]); err != nil {
return
}
if hour, err = strconv.Atoi(tokens[3]); err != nil {
return
}
if min, err = strconv.Atoi(tokens[4]); err != nil {
return
}
if sec, err = strconv.Atoi(tokens[5]); err != nil {
return
}
if location, err = time.LoadLocation("UTC"); err != nil {
return
}
date = time.Date(year, month, day, hour, min, sec, 0, location)
title = strings.TrimSuffix(tokens[6], ".md")
err = nil
return
}