forked from WarwickCyberSoc/society-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
209 lines (181 loc) · 4.97 KB
/
main.go
File metadata and controls
209 lines (181 loc) · 4.97 KB
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
package main
import (
"fmt"
"html/template"
"os"
"gopkg.in/yaml.v2"
)
var templates map[string]*template.Template
var config Config
func init() {
// Remove build directory
err := os.RemoveAll("build")
if err != nil {
fmt.Println("Error removing build directory:", err)
os.Exit(1)
}
// Create build directory
err = os.Mkdir("build", 0755)
if err != nil {
fmt.Println("Error creating build directory:", err)
os.Exit(1)
}
templates = make(map[string]*template.Template)
config = Config{}
file, err := os.Open("config.yaml")
if err != nil {
fmt.Println("Error opening config file:", err)
os.Exit(1)
}
defer file.Close()
decoder := yaml.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
fmt.Println("Error decoding config file:", err)
os.Exit(1)
}
// If NavBar is empty, add default values
if len(config.Navbar) == 0 {
config.Navbar = append(config.Navbar, struct {
Name string `yaml:"name"`
Link string `yaml:"link"`
}{Name: "Home", Link: "/"})
}
// Save the config to a file
file, err = os.Create("parsedConfig.yaml")
if err != nil {
fmt.Println("Error creating config file:", err)
os.Exit(1)
}
defer file.Close()
encoder := yaml.NewEncoder(file)
err = encoder.Encode(config)
if err != nil {
fmt.Println("Error encoding config file:", err)
os.Exit(1)
}
}
func main() {
// Load every template
// layout
templates["layout"] = template.Must(template.ParseFiles("templates/layout.tmpl"))
templates["layout_misc0nfig"] = template.Must(template.ParseFiles("templates/layout_misc0nfig.tmpl"))
// Load every file in templates folder
files, err := os.ReadDir("templates")
if err != nil {
fmt.Println("Error reading templates directory:", err)
os.Exit(1)
}
Schedule := conferenceSchedule{
Rooms: config.Rooms,
Timeslots: config.Timeslots,
Events: config.Events,
}
skipMap := prepareSchedule(&Schedule)
Schedule.SkipMap = skipMap
Timetable := timetable{
Days: config.TimetableDays,
Weeks: config.TimetableWeeks,
CurrentEvents: config.TimetableCurrentEvents,
}
templateData := TemplateData{
Config: config,
Schedule: Schedule,
Timetable: Timetable,
}
for _, file := range files {
if file.IsDir() {
continue
}
if file.Name() == "layout.tmpl" || file.Name() == "layout_misc0nfig.tmpl" {
continue
}
if file.Name() == "misc0nfig.tmpl" || file.Name() == "calendar.tmpl" {
templates[file.Name()] = template.Must(templates["layout_misc0nfig"].Clone())
} else {
templates[file.Name()] = template.Must(templates["layout"].Clone())
}
// Generate the template
templates[file.Name()] = template.Must(templates[file.Name()].ParseFiles("templates/" + file.Name()))
// Execute the template (swap tmpl with html)
outFile, err := os.Create("build/" + file.Name()[:len(file.Name())-5] + ".html")
if err != nil {
fmt.Println("Error creating file:", err)
os.Exit(1)
}
if file.Name() == "misc0nfig.tmpl" || file.Name() == "calendar.tmpl" {
err = templates[file.Name()].ExecuteTemplate(outFile, "layout_misc0nfig", templateData)
} else {
err = templates[file.Name()].ExecuteTemplate(outFile, "layout", config)
}
if err != nil {
fmt.Println("Error executing template:", err)
os.Exit(1)
}
outFile.Close()
}
// Copy static files from public folder
// List all files in public folder
copyDir("public")
}
func copyDir(dir string) {
// List all files in directory
files, err := os.ReadDir(dir)
if err != nil {
fmt.Println("Error reading directory:", err)
os.Exit(1)
}
// Create directory in build directory
if dir != "public" {
err = os.Mkdir("build/" + dir[7:], 0755)
if err != nil {
fmt.Println("Error creating directory:", err)
os.Exit(1)
}
}
// Copy each file to build directory
for _, file := range files {
if file.IsDir() {
copyDir(dir + "/" + file.Name())
continue
}
data, err := os.ReadFile(dir + "/" + file.Name())
if err != nil {
fmt.Println("Error reading file:", err)
os.Exit(1)
}
// Remove public from path
path := dir[6:]
// add slash if not present and length is not 0
if len(path) != 0 && path[len(path)-1] != '/' {
path += "/"
}
err = os.WriteFile("build/" + path + file.Name(), data, 0644)
if err != nil {
fmt.Println("Error copying file:", err)
os.Exit(1)
}
}
}
func prepareSchedule(schedule *conferenceSchedule) map[string]bool {
timeToIndex := make(map[string]int)
for idx, t := range schedule.Timeslots {
timeToIndex[t] = idx
}
skipMap := make(map[string]bool)
for i, event := range schedule.Events {
startIdx := timeToIndex[event.Start]
endIdx := timeToIndex[event.End]
if endIdx > startIdx {
schedule.Events[i].RowSpan = endIdx - startIdx
// Mark skip slots
for t := startIdx + 1; t < endIdx; t++ {
key := event.Room + "|" + schedule.Timeslots[t]
skipMap[key] = true
}
} else {
schedule.Events[i].RowSpan = 1
}
}
return skipMap
}