-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (107 loc) · 2.93 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
"log"
"os"
"github.com/oetherington/smetana"
"github.com/otiai10/copy"
)
var CONTEXT = smetana.NewSmetanaWithPalettes(smetana.Palettes{
"default": createDefaultPalette(),
})
const PUBLIC_DIR = "./public"
const OUTPUT_DIR = "./build"
func writeString(value string, targetName string) error {
targetPath := fmt.Sprintf("%s/%s", OUTPUT_DIR, targetName)
file, err := os.Create(targetPath)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(value)
return err
}
func generateHtml(node smetana.Node, targetName string) error {
html := smetana.RenderHtml(node)
return writeString(html, targetName)
}
func generateCss(stylesheet smetana.StyleSheet, targetName string) error {
css := smetana.RenderCss(stylesheet, CONTEXT.Palettes["default"])
return writeString(css, targetName)
}
func generateSitemap(sitemap smetana.Sitemap, targetName string) error {
css := smetana.RenderSitemap(sitemap)
return writeString(css, targetName)
}
const BASE_URL = "https://www.etherington.xyz/"
func main() {
fmt.Println("Removing old build")
if err := os.RemoveAll(OUTPUT_DIR); err != nil {
log.Fatalln(err)
}
fmt.Println("Copying public files")
if err := copy.Copy(PUBLIC_DIR, OUTPUT_DIR); err != nil {
log.Fatalln(err)
}
fmt.Println("Copying fonts")
fontSrcDir := "./font/"
fontOutDir := fmt.Sprintf("%s/css", OUTPUT_DIR)
if err := os.MkdirAll(fontOutDir, 0777); err != nil {
log.Fatalln(err)
}
fontFormats := []string{"ttf", "woff", "woff2"}
for _, fontFormat := range fontFormats {
src := fontSrcDir + "UnifontLatin." + fontFormat
dest := fontOutDir + "/UnifontLatin." + fontFormat
if err := copy.Copy(src, dest); err != nil {
log.Fatalln(err)
}
}
fmt.Println("Loading articles")
articleInfo, err := loadArticleInfo()
if err != nil {
log.Fatalln(err)
}
fmt.Println("Compiling styles")
CONTEXT.Styles = createStyles()
styles := CONTEXT.RenderStyles()
for name, css := range styles {
path := fmt.Sprintf("css/%s.css", name)
err = writeString(css, path)
if err != nil {
log.Fatalln(err)
}
}
if err := generateCss(createPrintStyles(), "css/print.css"); err != nil {
log.Fatalln(err)
}
fmt.Println("Compiling index")
index := Layout("", Index(articleInfo))
if err := generateHtml(index, "index.html"); err != nil {
log.Fatalln(err)
}
fmt.Println("Compiling articles...")
for _, article := range articleInfo {
if !article.Published {
continue
}
fmt.Println("...", article.Name)
articleHtml := MdArticle(article)
fileName := fmt.Sprintf("%s.html", article.Path)
if err = generateHtml(articleHtml, fileName); err != nil {
log.Fatalln(err)
}
}
fmt.Println("Compiling sitemap")
staticRoutes := []StaticRoute{
{"", "./index.go"},
}
sitemap, err := getSitemap(BASE_URL, staticRoutes, articleInfo)
if err != nil {
log.Fatalln(err)
}
if err := generateSitemap(sitemap, "sitemap.xml"); err != nil {
log.Fatalln(err)
}
fmt.Println("Done! 🎉")
}