-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.go
executable file
·348 lines (317 loc) · 8.19 KB
/
site.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"encoding/json"
"github.com/flosch/pongo"
"github.com/hoisie/web"
"github.com/russross/blackfriday"
"io/ioutil"
"math"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
//"fmt"
)
// Struct representing the configuration
type Config struct {
ContentFolder string
TemplateFolder string
ReadMoreText string
ArticlesPerPage int
ServerIp string
}
// Struct representing a menu item
type MenuItem struct {
Title, Link, Section string
}
// Class representing a menu. Implements the sortable interface
type Menu []*MenuItem
// Return the length of the menu
func (m Menu) Len() int {
return len(m)
}
// Comparison function used in sorting. Orders alphabetically by section slug
func (m Menu) Less(i, j int) bool {
return m[i].Section < m[j].Section
}
// Function used for swapping menu items, used in sorting
func (m Menu) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
// Returns a copy of the menu item that matches the given section
func (m Menu) GetCurrent(s string) MenuItem {
sort.Sort(m)
index := 0
for i, item := range m {
if s != item.Section {
continue
}
index = i
break
}
return *m[index]
}
// Error type representing a pagination error
type PaginationError struct {
message string
}
// Error function for pagination, returns the error message
func (e PaginationError) Error() string {
return e.message
}
// Sortable list of files in a folder
type SortableFileList struct {
FileList []os.FileInfo
}
// Copies an external FileInfo list into the internal one
func (l SortableFileList) setList(list []os.FileInfo) {
l.FileList = make([]os.FileInfo, len(list))
copy(l.FileList, list)
}
// Returns the sorted list of files
func (l SortableFileList) getList() []os.FileInfo {
sort.Sort(l)
return l.FileList
}
// Comparison function used in sorting. Orders descending by modifictaion date
func (l SortableFileList) Less(i, j int) bool {
return l.FileList[i].ModTime().After(l.FileList[j].ModTime())
}
// Function used for swapping menu items, used in sorting
func (l SortableFileList) Swap(i, j int) {
l.FileList[i], l.FileList[j] = l.FileList[j], l.FileList[i]
}
// Returns the length of the list
func (l SortableFileList) Len() int {
return len(l.FileList)
}
/**
* Returns a Config struct filled in with values from the config file
*/
func getConfig() (Config, error) {
configEntry := new(Config)
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return *configEntry, err
}
bs, err := ioutil.ReadFile(dir + "/config.json")
if err != nil {
return *configEntry, err
}
err = json.Unmarshal(bs, configEntry)
if err != nil {
return *configEntry, err
}
return *configEntry, nil
}
/**
* Returns a slice with menu items
*/
func getMenu(conf *Config) (Menu, error) {
var menu Menu
dir, err := os.Open(conf.ContentFolder)
if err != nil {
return menu, err
}
defer dir.Close()
fileInfos, err := dir.Readdir(-1)
if err != nil {
return menu, err
}
var link string
re := regexp.MustCompile("^[0-9]+-")
for _, fi := range fileInfos {
if !fi.IsDir() || strings.HasPrefix(fi.Name(), ".") {
continue
}
link = "/" + fi.Name()
menu = append(menu,
&MenuItem{Title: strings.Title(
strings.Replace(
strings.TrimPrefix(
fi.Name(),
re.FindString(
fi.Name())),
"-", " ", -1)),
Section: fi.Name(),
Link: link})
}
sort.Sort(menu)
menu[0].Link = "/"
return menu, nil
}
/**
* Returns a string containing the abstracts of the articles on the page
*/
func getAbstracts(section string, pageNum int, conf *Config) (string, error) {
dir, err := os.Open(conf.ContentFolder + "/" + section)
if err != nil {
return "", err
}
defer dir.Close()
fileInfos, err := dir.Readdir(-1)
if err != nil {
return "", err
}
sortedFiles := SortableFileList{FileList: fileInfos}
paginatedFiles := sortedFiles.getList()
articleCount := len(paginatedFiles)
var fileName, page, pageContent string
content := make([]string, 1)
start := conf.ArticlesPerPage * (pageNum - 1)
end := start + conf.ArticlesPerPage
if start < 0 {
start = 0
}
if end > articleCount {
end = articleCount
}
if start >= articleCount {
e := PaginationError{message: "No such page"}
return "", e
}
paginatedFiles = paginatedFiles[start:end]
for _, fi := range paginatedFiles {
fileName = fi.Name()
if !strings.HasSuffix(fileName, ".md") {
continue
}
page = strings.Split(fileName, ".")[0]
if pageContent, err = getPage(section, page, conf); err != nil {
continue
}
if articleCount > 1 {
pageContent = strings.Join(strings.SplitN(pageContent, "\n", 4)[0:3], "\n")
}
content = append(content, pageContent)
if articleCount > 1 {
content = append(content, "["+conf.ReadMoreText+"](/"+section+"/"+page+")")
}
}
if articleCount > len(paginatedFiles) {
pagination := make([]string, 1)
pagination = append(pagination, "<ul class=\"pagination\">")
var l string
for i := 1; i <= int(math.Ceil(float64(articleCount)/float64(conf.ArticlesPerPage))); i++ {
if i == 1 {
l = "/" + section
} else {
l = "/" + section + "/" + strconv.Itoa(i)
}
if i != pageNum {
pagination = append(
pagination,
"<li><a href=\""+l+"\">"+strconv.Itoa(i)+"</a></li>")
} else {
pagination = append(
pagination,
"<li class=\"active\"><a href=\""+l+"\">"+strconv.Itoa(i)+"</a></li>")
}
}
pagination = append(pagination, "</ul>")
content = append(content, strings.Join(pagination, " "))
}
return strings.Join(content, "\n\n"), nil
}
/**
* Returns the content of a page
*/
func getPage(section string, page string, conf *Config) (string, error) {
pageContent, err := ioutil.ReadFile(conf.ContentFolder + "/" + section + "/" + page + ".md")
if err != nil {
return "", err
}
return string(pageContent), nil
}
/*
* Page handler, displays the requested page from a template and from Md files
*/
func handlePage(ctx *web.Context, section string, page string) string {
config, err := getConfig()
if err != nil {
ctx.Abort(500, "Configuration error.")
return ""
}
tpl := pongo.Must(pongo.FromFile(config.TemplateFolder+"/template.html", nil))
menu, err := getMenu(&config)
if err != nil {
ctx.Abort(501, "Could not load menu")
return ""
}
var content, output string
output, err = getPage(section, page, &config)
if err != nil {
ctx.Abort(404, "Page not found.")
return ""
}
content = string(blackfriday.MarkdownCommon([]byte(output)))
var response *string
response, err = tpl.Execute(&pongo.Context{"content": content,
"menu": menu, "currentMenu": menu.GetCurrent(section)})
if err != nil {
ctx.Abort(501, "")
return err.Error()
}
return *response
}
/**
* Handles request for section
*/
func handlePaginatedSection(ctx *web.Context, section string, page string) string {
config, err := getConfig()
if err != nil {
ctx.Abort(500, "Configuration error.")
return ""
}
tpl := pongo.Must(pongo.FromFile(config.TemplateFolder+"/template.html", nil))
var content, output string
p, _ := strconv.Atoi(page)
output, err = getAbstracts(section, p, &config)
if err != nil {
ctx.Abort(404, "Page not found. Could not load abstracts")
return ""
}
content = string(blackfriday.MarkdownCommon([]byte(output)))
menu, err := getMenu(&config)
if err != nil {
ctx.Abort(501, "Could not load menu")
return ""
}
var response *string
response, err = tpl.Execute(&pongo.Context{"content": content, "menu": menu,
"currentMenu": menu.GetCurrent(section)})
if err != nil {
ctx.Abort(501, "")
return err.Error()
}
return *response
}
// Wrapper for handling paginated section when no section is given
func handleSection(ctx *web.Context, section string) string {
if len(section) == 0 {
config, err := getConfig()
if err != nil {
ctx.Abort(500, "Configuration error.")
return ""
}
menu, err := getMenu(&config)
if err != nil {
ctx.Abort(501, "Could not load menu")
return ""
}
return handlePaginatedSection(ctx, menu[0].Section, "1")
}
return handlePaginatedSection(ctx, section, "1")
}
func main() {
config, err := getConfig()
if err != nil {
panic(err.Error())
}
web.Get("/([a-zA-Z0-9-]*)", handleSection)
web.Get("/([a-zA-Z0-9-]+)/([0-9]+)", handlePaginatedSection)
web.Get("/([a-zA-Z0-9-]+)/([a-zA-Z]{1}[a-zA-Z0-9-]*)", handlePage)
web.Run(config.ServerIp)
}