2020-12-04 12:50:20 +01:00
|
|
|
package site
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-12-31 13:28:09 +01:00
|
|
|
func renderEWNLStaticPages(targetPath string, tpl *template.Template, _ Posts, statics []*StaticPage) error {
|
2020-12-04 12:50:20 +01:00
|
|
|
for _, static := range statics {
|
|
|
|
destPath := filepath.Join(targetPath, static.Name)
|
|
|
|
if err := os.MkdirAll(destPath, dirMode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pageFile, err := os.Create(filepath.Join(destPath, "index.html"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer pageFile.Close()
|
|
|
|
|
2020-12-04 16:52:25 +01:00
|
|
|
mainHTML, err := ioutil.ReadFile(filepath.Join(static.Path, "main.html"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-04 12:50:20 +01:00
|
|
|
data := struct {
|
|
|
|
Title string
|
2020-12-04 16:52:25 +01:00
|
|
|
Main string
|
2020-12-04 12:50:20 +01:00
|
|
|
}{
|
|
|
|
Title: strings.Title(static.Name),
|
2020-12-04 16:52:25 +01:00
|
|
|
Main: string(mainHTML),
|
2020-12-04 12:50:20 +01:00
|
|
|
}
|
2020-12-04 16:52:25 +01:00
|
|
|
if err := tpl.Execute(pageFile, data); err != nil {
|
2020-12-04 12:50:20 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-04 16:52:25 +01:00
|
|
|
if err := copyFiles(filepath.Join(static.Path, "resources", "*"), destPath); err != nil {
|
2020-12-04 12:50:20 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-31 13:28:09 +01:00
|
|
|
func renderEWNLHome(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error {
|
2020-12-21 07:36:45 +01:00
|
|
|
var summaries []*HTMLSummary
|
2020-12-29 15:34:55 +01:00
|
|
|
for _, p := range posts.RemoveKind(KIND_NOTE).Limit(10) {
|
2020-12-21 07:36:45 +01:00
|
|
|
summaries = append(summaries, p.HTMLSummary())
|
|
|
|
}
|
2020-12-04 12:50:20 +01:00
|
|
|
data := struct {
|
|
|
|
Title string
|
|
|
|
Summaries []*HTMLSummary
|
|
|
|
}{
|
|
|
|
Title: "Recent",
|
2020-12-21 07:36:45 +01:00
|
|
|
Summaries: summaries,
|
2020-12-04 12:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
hPath := filepath.Join(targetPath, "index.html")
|
|
|
|
homeFile, err := os.Create(hPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer homeFile.Close()
|
|
|
|
|
|
|
|
return tpl.Execute(homeFile, data)
|
|
|
|
}
|
|
|
|
|
2020-12-31 13:28:09 +01:00
|
|
|
func renderEWNLArchive(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error {
|
2020-12-04 12:50:20 +01:00
|
|
|
archPath := filepath.Join(targetPath, "archive")
|
|
|
|
if err := os.MkdirAll(archPath, dirMode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
archFile, err := os.Create(filepath.Join(archPath, "index.html"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type link struct {
|
|
|
|
Name string
|
|
|
|
Link string
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := []link{}
|
|
|
|
for _, tag := range posts.TagList() {
|
|
|
|
tags = append(tags, link{
|
|
|
|
Name: tag,
|
|
|
|
Link: fmt.Sprintf("%s/", path.Join("/tags", tag)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
yearLinks := map[Kind][]link{
|
|
|
|
KIND_ARTICLE: {},
|
|
|
|
KIND_NOTE: {},
|
|
|
|
KIND_STORY: {},
|
|
|
|
}
|
|
|
|
for kind := range yearLinks {
|
2020-12-29 15:34:55 +01:00
|
|
|
for _, year := range posts.SelectKind(kind).YearList() {
|
2020-12-04 12:50:20 +01:00
|
|
|
yearLinks[kind] = append(yearLinks[kind], link{
|
|
|
|
Name: year,
|
|
|
|
Link: fmt.Sprintf("%s/", path.Join("/", pluralKind[kind], year)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data := struct {
|
|
|
|
Title string
|
|
|
|
Tags []link
|
|
|
|
ArticleYears []link
|
|
|
|
NoteYears []link
|
|
|
|
StoryYears []link
|
|
|
|
}{
|
2020-12-29 15:34:55 +01:00
|
|
|
Title: "Archive",
|
2020-12-04 12:50:20 +01:00
|
|
|
Tags: tags,
|
|
|
|
ArticleYears: yearLinks[KIND_ARTICLE],
|
|
|
|
NoteYears: yearLinks[KIND_NOTE],
|
|
|
|
StoryYears: yearLinks[KIND_STORY],
|
|
|
|
}
|
|
|
|
|
|
|
|
return tpl.Execute(archFile, data)
|
|
|
|
}
|
|
|
|
|
2020-12-31 13:28:09 +01:00
|
|
|
func renderEWNLListings(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error {
|
2020-12-04 12:50:20 +01:00
|
|
|
for _, kind := range []Kind{KIND_NOTE, KIND_STORY, KIND_ARTICLE} {
|
2020-12-29 15:34:55 +01:00
|
|
|
for _, year := range posts.SelectKind(kind).YearList() {
|
2020-12-04 12:50:20 +01:00
|
|
|
title := fmt.Sprintf("%s in %s", strings.Title(pluralKind[kind]), year)
|
2020-12-29 15:34:55 +01:00
|
|
|
kyposts := posts.SelectKind(kind).SelectYear(year)
|
2020-12-21 07:36:45 +01:00
|
|
|
var summaries []*HTMLSummary
|
|
|
|
for _, p := range kyposts {
|
|
|
|
summaries = append(summaries, p.HTMLSummary())
|
|
|
|
}
|
2020-12-04 12:50:20 +01:00
|
|
|
path := filepath.Join(targetPath, pluralKind[kind], year)
|
2020-12-31 13:28:09 +01:00
|
|
|
if err := renderEWNLListing(path, tpl, title, summaries); err != nil {
|
2020-12-04 12:50:20 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tag := range posts.TagList() {
|
|
|
|
title := fmt.Sprintf("Posts Tagged with \"%s\"", tag)
|
2020-12-29 15:34:55 +01:00
|
|
|
tposts := posts.SelectTag(Tag(tag))
|
2020-12-21 07:36:45 +01:00
|
|
|
var summaries []*HTMLSummary
|
|
|
|
for _, p := range tposts {
|
|
|
|
summaries = append(summaries, p.HTMLSummary())
|
|
|
|
}
|
2020-12-04 12:50:20 +01:00
|
|
|
path := filepath.Join(targetPath, "tags", tag)
|
2020-12-31 13:28:09 +01:00
|
|
|
if err := renderEWNLListing(path, tpl, title, summaries); err != nil {
|
2020-12-04 12:50:20 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-31 13:28:09 +01:00
|
|
|
func renderEWNLListing(path string, tpl *template.Template, title string, summaries []*HTMLSummary) error {
|
2020-12-04 12:50:20 +01:00
|
|
|
data := struct {
|
|
|
|
Title string
|
|
|
|
Summaries []*HTMLSummary
|
|
|
|
}{
|
|
|
|
Title: title,
|
|
|
|
Summaries: summaries,
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(path, dirMode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lPath := filepath.Join(path, "index.html")
|
|
|
|
f, err := os.Create(lPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return tpl.Execute(f, data)
|
|
|
|
}
|
|
|
|
|
2020-12-31 13:28:09 +01:00
|
|
|
func renderEWNLPosts(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error {
|
2020-12-04 12:50:20 +01:00
|
|
|
for _, post := range posts {
|
|
|
|
data := post.HTMLPost()
|
|
|
|
if data.Slug == "" {
|
|
|
|
return ErrInvalidPost
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(targetPath, pluralKind[post.Kind], post.Year(), data.Slug)
|
|
|
|
if err := os.MkdirAll(path, dirMode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nPath := filepath.Join(path, "index.html")
|
|
|
|
f, err := os.Create(nPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if err := tpl.Execute(f, data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-31 13:28:09 +01:00
|
|
|
func renderEWNLRSS(targetPath string, tpl *template.Template, posts Posts, _ []*StaticPage) error {
|
2020-12-04 12:50:20 +01:00
|
|
|
rssPath := filepath.Join(targetPath, "index.xml")
|
|
|
|
rssFile, err := os.Create(rssPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rssFile.Close()
|
|
|
|
|
|
|
|
var xmlPosts []*XMLPost
|
2020-12-31 08:46:23 +01:00
|
|
|
for _, p := range posts.RemoveKind(KIND_NOTE).Limit(10) {
|
2020-12-04 12:50:20 +01:00
|
|
|
xmlPosts = append(xmlPosts, p.XMLPost())
|
|
|
|
}
|
|
|
|
|
|
|
|
data := struct {
|
|
|
|
DateFormal string
|
|
|
|
Posts []*XMLPost
|
|
|
|
}{
|
|
|
|
DateFormal: time.Now().Format(time.RFC1123Z),
|
|
|
|
Posts: xmlPosts,
|
|
|
|
}
|
|
|
|
return tpl.Execute(rssFile, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyFiles(srcPattern, destPath string) error {
|
|
|
|
filePaths, err := filepath.Glob(srcPattern)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(destPath, dirMode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fPath := range filePaths {
|
|
|
|
destFPath := filepath.Join(destPath, filepath.Base(fPath))
|
|
|
|
content, err := ioutil.ReadFile(fPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(destFPath, content, 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|