-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
318 lines (294 loc) · 7.77 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"bytes"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
)
type App struct {
SiteTemplate string
SrcDir string
DistDir string
Layouts map[string]string
Pages []Page
IgnoreFolders map[string]bool
IgnoreFiles map[string]bool
ThemeConfig ThemeConfig
}
type Page struct {
Title string
Body string
Layout string
Filepath string
}
type InvalidPageError struct {
s string
}
func (e InvalidPageError) Error() string {
return e.s
}
func check(e error) {
if e != nil {
panic(e)
}
}
func (app App) getPage(fp string) (Page, error) {
page := Page{Filepath: fp}
// read the markdown file
md, err := os.ReadFile(fp)
if err != nil {
fmt.Println("Could not read file: ", fp)
return page, err
}
// render the markdown file
opts := html.RendererOptions{
Flags: html.FlagsNone,
RenderNodeHook: app.renderHook,
}
renderer := html.NewRenderer(opts)
page.Body = string(markdown.ToHTML(md, nil, renderer))
// Get metadata
lines := strings.Split(string(md), "\n")
for lineNumber, line := range lines {
if strings.HasPrefix(line, "[_metadata_:title]:- \"") {
title := strings.TrimPrefix(line, "[_metadata_:title]:- \"")
page.Title = strings.TrimSuffix(title, "\"")
}
if strings.HasPrefix(line, "[_metadata_:layout]:- \"") {
layout := strings.TrimPrefix(line, "[_metadata_:layout]:- \"")
page.Layout = strings.TrimSuffix(layout, "\"")
}
if lineNumber > 2 {
break
}
}
// If the page metadata cannot be found, return an error to skip the page
// This is useful for markdown that are not pages
if page.Title == "" {
return page, InvalidPageError{s: fmt.Sprintf("no title found in %v", fp)}
}
if page.Layout == "" {
return page, InvalidPageError{s: fmt.Sprintf("no layout found in %v", fp)}
}
return page, nil
}
func (app App) renderPage(page Page) (err error) {
innerLayout, ok := app.Layouts[page.Layout]
if !ok {
// Skip the page if the layout is not found
fmt.Printf("Could not find layout %v for page %v", page.Layout, page.Filepath)
return nil
}
// Parse inner template
t := template.New("page")
t, err = t.Parse(innerLayout)
if err != nil {
fmt.Printf("error parsing template file at %v: %v\n", app.Layouts[page.Layout], err)
return err
}
t = template.Must(t, err)
var inner bytes.Buffer
err = t.Execute(&inner, page)
if err != nil {
fmt.Println("error executing template: ", err)
return err
}
page.Body = inner.String()
// Parse outer template
t = template.New("Render")
t, err = t.Parse(app.SiteTemplate)
if err != nil {
fmt.Println("Could not parse template: ", err)
return err
}
t = template.Must(t, err)
var processed bytes.Buffer
err = t.Execute(&processed, page)
if err != nil {
fmt.Println("Could not execute template: ", err)
return err
}
// write the page to a file
relpath, err := filepath.Rel(app.SrcDir, page.Filepath)
if err != nil {
fmt.Println("Could not get relative path: ", err)
return err
}
newFilePath := relpath[:len(relpath)-3] + ".html"
newFilePath = filepath.Join(app.DistDir, newFilePath)
if err := os.MkdirAll(filepath.Dir(newFilePath), 0755); err != nil {
fmt.Println("Could not create directory: ", err)
return err
}
err = os.WriteFile(newFilePath, processed.Bytes(), 0644)
if err != nil {
fmt.Println("Could not write file: ", err)
return err
}
return err
}
func (app *App) parseSrcDirectory() error {
app.Layouts = make(map[string]string)
app.Pages = make([]Page, 0)
err := filepath.Walk(app.SrcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Ignore directories and files
if info.IsDir() {
if _, ok := app.IgnoreFolders[info.Name()]; ok {
return filepath.SkipDir
}
if strings.HasPrefix(info.Name(), ".") {
return filepath.SkipDir
}
return nil
}
if _, ok := app.IgnoreFiles[info.Name()]; ok {
return nil
}
if strings.HasPrefix(info.Name(), ".") {
return nil
}
// parse the layouts
ext := filepath.Ext(path)
base := filepath.Base(path)
if base == "layout.html" {
layoutByte, err := os.ReadFile(path)
if err != nil {
fmt.Println("Could not read file: ", path)
return err
}
app.SiteTemplate = string(layoutByte)
} else if ext == ".html" && strings.HasPrefix(base, "layout_") {
name := filepath.Base(path)
name = strings.TrimSuffix(name, ".html")
name = strings.TrimPrefix(name, "layout_")
layoutByte, err := os.ReadFile(path)
if err != nil {
fmt.Printf("error reading template file at %v: %v\n", path, err)
return err
}
app.Layouts[name] = string(layoutByte)
} else if ext == ".md" {
page, err := app.getPage(path)
// Skip pages we can't read because they could be README, LICENSE, drafts, etc.
if _, ok := err.(InvalidPageError); ok {
return nil
} else if err != nil {
fmt.Println("Could not read file: ", path)
return err
}
app.Pages = append(app.Pages, page)
} else {
// Copy any other file to the dist directory
relpath, err := filepath.Rel(app.SrcDir, path)
if err != nil {
fmt.Println("Could not get relative path: ", err)
return err
}
newFilePath := filepath.Join(app.DistDir, relpath)
if err := os.MkdirAll(filepath.Dir(newFilePath), 0755); err != nil {
fmt.Println("Could not create directory: ", err)
return err
}
source, err := os.Open(path)
if err != nil {
fmt.Println("Could not open source file: ", err)
return err
}
defer source.Close()
destination, err := os.Create(newFilePath)
if err != nil {
fmt.Println("Could not create destination file: ", err)
return err
}
defer destination.Close()
_, err = io.Copy(destination, source)
if err != nil {
fmt.Println("Could not copy file: ", err)
return err
}
}
return nil
})
return err
}
func InitApp(srcDir string) (App, error) {
app := App{SrcDir: srcDir}
// Parse the theme config
squatchConfig, err := getSquatchConfig(filepath.Join(app.SrcDir, ".squatch"))
if err != nil {
return app, err
}
app.DistDir = squatchConfig.DistDir
// parse the comma separated list of folders to ignore
app.IgnoreFolders = map[string]bool{app.DistDir: true}
ignoreFoldersList := strings.Split(squatchConfig.IgnoreFolders, ",")
for _, folder := range ignoreFoldersList {
app.IgnoreFolders[folder] = true
}
app.IgnoreFiles = map[string]bool{}
// parse the comma separated list of files to ignore
ignoreFilesList := strings.Split(squatchConfig.IgnoreFiles, ",")
for _, file := range ignoreFilesList {
app.IgnoreFiles[file] = true
}
// Remove existing dist directory
os.RemoveAll(app.DistDir)
// Create new dist directory
os.Mkdir(app.DistDir, 0755)
err = app.parseSrcDirectory()
if err != nil {
return app, err
}
return app, nil
}
func (app App) printDistFolder() {
filepath.Walk(app.DistDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
fmt.Println(path)
return nil
})
}
func Build(srcDir string) {
fmt.Println("Starting build...")
// Get input variables from Github Actions
srcDirEnv := os.Getenv("INPUT_SRCDIR")
if len(srcDirEnv) != 0 {
srcDir = srcDirEnv
} else if len(srcDir) == 0 {
srcDir = "src"
}
// Initialize the app
app, err := InitApp(srcDir)
check(err)
// Convert all pages
for _, page := range app.Pages {
err = app.renderPage(page)
check(err)
}
fmt.Println("Build complete! Dist folder:")
app.printDistFolder()
}
func main() {
var srcDir string
flag.StringVar(&srcDir, "src-dir", "src", "Source directory")
var port string
flag.StringVar(&port, "port", "8080", "Port to run the live server on")
liveServerPtr := flag.Bool("live-server", false, "Run a live server")
flag.Parse()
if *liveServerPtr {
LiveServer(srcDir, port)
} else {
Build(srcDir)
}
}