-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
164 lines (143 loc) · 3.7 KB
/
builder.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"cmp"
"fmt"
"html/template"
"io"
"io/fs"
"os"
"path/filepath"
"slices"
"strings"
"time"
)
var (
inputDir = "src"
outputDir = "docs"
postsDir = "posts"
projectsDir = "projects"
projectsFileName = "projects.yaml"
inputPostsDir = filepath.Join(inputDir, postsDir)
inputProjectsDir = filepath.Join(inputDir, projectsDir)
outputPostsDir = filepath.Join(outputDir, postsDir)
outputProjectsDir = filepath.Join(outputDir, projectsDir)
templatesDir = "templates"
)
func init() {
// remove already generated files
if err := os.RemoveAll(outputDir); err != nil {
fmt.Printf("ERROR REMOVING EXISTING DIRECTORY: %s\n", err.Error())
}
}
func main() {
t0 := time.Now()
err := buildWebsite()
if err != nil {
panic(err)
}
fmt.Printf("completed in %v\n", time.Since(t0))
}
func buildWebsite() error {
var (
posts []postMeta
projects projectsData
)
tmpl, err := parseTemplates(templatesDir)
if err != nil {
return err
}
err = filepath.WalkDir(inputDir, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
err = os.MkdirAll(strings.Replace(path, inputDir, outputDir, 1), os.ModePerm)
} else {
if filepath.Ext(d.Name()) == ".md" {
// ignore /posts and /projects path, will be treated later
if path == filepath.Join(inputPostsDir, "index.md") || path == filepath.Join(inputProjectsDir, "index.md") {
return nil
}
isPost := strings.HasPrefix(path, inputPostsDir)
pageMeta, content, err := toPageData(path, isPost)
if err != nil {
return err
}
if isPost {
filePath := filepath.Base(filepath.Dir(path))
posts = append(posts, postMeta{
Title: pageMeta.Title,
Path: filePath,
Date: pageMeta.Date,
})
}
outputPath := strings.TrimSuffix(strings.Replace(path, inputDir, outputDir, 1), ".md")
outputPath = fmt.Sprintf("%s.html", outputPath)
err = toHTML(tmpl, pageMeta.layout, outputPath, page[template.HTML]{
Meta: pageMeta,
Content: template.HTML(content),
})
if err != nil {
return err
}
} else if path == filepath.Join(inputProjectsDir, projectsFileName) {
projects, err = parseYAML[projectsData](path)
if err != nil {
return err
}
} else {
src, err := os.Open(path)
if err != nil {
return err
}
defer func() {
if err := src.Close(); err != nil {
fmt.Printf("ERROR CLOSING SOURCE FILE: %s\n", err.Error())
}
}()
dst, err := os.Create(strings.Replace(path, inputDir, outputDir, 1))
if err != nil {
return err
}
defer func() {
if err := dst.Close(); err != nil {
fmt.Printf("ERROR CLOSING DESTINATION FILE: %s\n", err.Error())
}
}()
_, err = io.Copy(dst, src)
if err != nil {
return err
}
}
}
return err
})
if err != nil {
return err
}
// create /posts page
err = toListPage(tmpl, filepath.Join(inputPostsDir, "index.md"), filepath.Join(outputPostsDir, "index.html"), posts, func(a, b postMeta) int {
return cmp.Compare(b.Date.Unix(), a.Date.Unix())
})
if err != nil {
return err
}
// create /projects page
err = toListPage(tmpl, filepath.Join(inputProjectsDir, "index.md"), filepath.Join(outputProjectsDir, "index.html"), projects.Projects, func(t1, t2 project) int { return 0 })
if err != nil {
return err
}
return nil
}
func toListPage[T any](tmpl *template.Template, in, out string, data []T, cmpFunc func(T, T) int) error {
slices.SortFunc(data, cmpFunc)
pageMeta, _, err := toPageData(in, false)
if err != nil {
return err
}
err = toHTML(tmpl, pageMeta.layout, out, page[[]T]{
Meta: pageMeta,
Content: data,
})
if err != nil {
return err
}
return nil
}