changes
This commit is contained in:
parent
dfa7a280f0
commit
8385620f09
11
new.html
Normal file
11
new.html
Normal file
|
@ -0,0 +1,11 @@
|
|||
<html>
|
||||
<body>
|
||||
<article>
|
||||
<p>
|
||||
hi</br>
|
||||
<time datetime="2015-05-15" />
|
||||
by lisa
|
||||
</p>
|
||||
</article>
|
||||
</body>
|
||||
</html>
|
|
@ -2,8 +2,11 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/net/html"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
@ -16,26 +19,86 @@ type Post struct {
|
|||
Content template.HTML
|
||||
}
|
||||
|
||||
var lewd = "Lewd Interlude"
|
||||
var reveries = "Takunomi Coffee Reveries, Vol. I"
|
||||
var romantic = "Romantic Japanese Christmas Takunomi Saturday Recap"
|
||||
const layout = "2006-01-02"
|
||||
|
||||
var modTime time.Time
|
||||
var postsCollection []Post
|
||||
|
||||
func postHandlerRoutine(duration time.Duration) {
|
||||
for {
|
||||
if isFolderModified() == true {
|
||||
postsCollection = nil
|
||||
filepath.Walk("./posts/", newPost)
|
||||
for _, post := range postsCollection {
|
||||
sortPostAccordingToDate(post, postsCollection)
|
||||
}
|
||||
}
|
||||
time.Sleep(duration * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func isFolderModified() bool {
|
||||
f, _ := os.Open("./dir")
|
||||
info, _ := f.Stat()
|
||||
_modTime := info.ModTime()
|
||||
if modTime != _modTime {
|
||||
modTime = _modTime
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func newPost(path string, f os.FileInfo, err error) error {
|
||||
postsCollection = append(postsCollection, getPost(path))
|
||||
return nil
|
||||
}
|
||||
|
||||
func getPost(title string) Post {
|
||||
content, err := ioutil.ReadFile(title)
|
||||
if err != nil {
|
||||
fmt.Println(content)
|
||||
return Post{}
|
||||
}
|
||||
|
||||
s := string(content)
|
||||
doc, _ := html.Parse(strings.NewReader(s))
|
||||
|
||||
var p = Post{
|
||||
getDate(doc),
|
||||
title,
|
||||
spaceToHyphen(title),
|
||||
template.HTML(string(content[:len(content)])),
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func getDate(n *html.Node) time.Time {
|
||||
var t time.Time
|
||||
if n.Type == html.ElementNode && n.Data == "time" {
|
||||
for _, a := range n.Attr {
|
||||
if a.Key == "datetime" {
|
||||
t, _ = time.Parse(layout, a.Val)
|
||||
}
|
||||
}
|
||||
}
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
t = getDate(c)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func spaceToHyphen(s string) string {
|
||||
return strings.Replace(s, " ", "-", -1)
|
||||
}
|
||||
|
||||
func setDate(year int, month time.Month, day int) time.Time {
|
||||
return time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
|
||||
}
|
||||
|
||||
func newPost(date time.Time, title string) Post {
|
||||
return Post{date, title, spaceToHyphen(title), getPostContent(title)}
|
||||
}
|
||||
|
||||
var postsCollection = []Post{
|
||||
newPost(setDate(2017, time.September, 12), lewd),
|
||||
newPost(setDate(2017, time.March, 4), reveries),
|
||||
newPost(setDate(2017, time.April, 23), romantic),
|
||||
func getNewestPosts(numberOfPosts int) []Post {
|
||||
var posts []Post
|
||||
for i := numberOfPosts; i > 0; i-- {
|
||||
post := postsCollection[i]
|
||||
posts = append(posts, getPostByURLTitle(post.URLTitle))
|
||||
}
|
||||
return posts
|
||||
}
|
||||
|
||||
func getPostByURLTitle(title string) Post {
|
||||
|
@ -47,20 +110,14 @@ func getPostByURLTitle(title string) Post {
|
|||
return Post{}
|
||||
}
|
||||
|
||||
func getPostContent(title string) template.HTML {
|
||||
content, err := ioutil.ReadFile("posts/" + title)
|
||||
if err != nil {
|
||||
fmt.Println(content)
|
||||
return template.HTML("")
|
||||
func sortPostAccordingToDate(post Post, arr []Post) {
|
||||
for i, p := range arr {
|
||||
if p.Date.After(post.Date) {
|
||||
s := make([]Post, len(arr)+1, cap(arr)+1)
|
||||
copy(s[:], arr[:]) //make a copy of the slice
|
||||
copy(s[i+1:], arr[i:]) //move the upper part of the slice ahead, creating a hole
|
||||
s[i] = post //insert new element into hole
|
||||
}
|
||||
}
|
||||
return template.HTML(string(content[:len(content)]))
|
||||
}
|
||||
|
||||
func getNewestPosts(numberOfPosts int) []Post {
|
||||
var posts []Post
|
||||
for i := 0; i < numberOfPosts; i++ {
|
||||
post := postsCollection[i]
|
||||
posts = append(posts, getPostByURLTitle(post.URLTitle))
|
||||
}
|
||||
return posts
|
||||
postsCollection = append(postsCollection, post)
|
||||
}
|
||||
|
|
BIN
websiteTemplate
BIN
websiteTemplate
Binary file not shown.
Loading…
Reference in New Issue
Block a user