-
Notifications
You must be signed in to change notification settings - Fork 0
/
koekr.go
192 lines (156 loc) · 4.22 KB
/
koekr.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
package main
import (
"bytes"
template "text/template"
xmlTemplate "text/template"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/Masterminds/sprig"
"github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
)
type Koekr struct {
variables map[string]interface{}
t *template.Template
files []string
config struct {
watch bool
configFile string
template string
}
}
func (k *Koekr) findPages() []string {
searchDir := "./pages/"
_ = filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
fi, err := os.Stat(path)
if err != nil {
return nil
}
if fi.Mode().IsRegular() {
k.files = append(k.files, path)
}
return nil
})
return k.files
}
func (k *Koekr) parsePage(content string) map[string]interface{} {
// Split page config from page content
pageConfig := []string{}
pageContent := []string{}
parsingConfig := false
for _, line := range strings.Split(strings.TrimSuffix(content, "\n"), "\n") {
if strings.Contains(line, "---") {
// Parse config
parsingConfig = !parsingConfig
continue
}
if parsingConfig {
pageConfig = append(pageConfig, line)
} else {
pageContent = append(pageContent, line)
}
}
// Parse page config
pageConfigDecoded := map[string]interface{}{}
_, err := toml.Decode(strings.Join(pageConfig, "\n"), &pageConfigDecoded)
if err != nil {
log.Warnln("Couldn't parse the page config: ", err)
}
pageConfigDecoded["content"] = strings.Join(pageContent, "\n")
return pageConfigDecoded
}
func (k *Koekr) generatePage(file string) {
// t *template.Template, variables map[string]interface{}
// Read content from page
content, err := ioutil.ReadFile(file)
if err != nil {
log.Warnln("Couldn't read page:", file)
return
}
pageVariables := k.parsePage(string(content))
local_variables := k.variables
local_variables["page"] = pageVariables
// Process sub template. This allows page templates to be actual Go templates
executedContent := bytes.Buffer{}
localTemplate := template.New(file)
templateString, _ := pageVariables["content"].(string)
localTemplate.Parse(string(templateString))
err = localTemplate.Execute(&executedContent, local_variables)
if err != nil {
log.Warnln("There was an error while building the html output for a page", err)
}
pageVariables["content"] = executedContent.String()
local_variables["page"] = pageVariables
// Create file for output
outputFile, err := os.Create("./generated/" + filepath.Base(file))
if err != nil {
log.Warnln("Couldn't create output file: ", err)
return
}
// Execut template
if err := k.t.Execute(outputFile, local_variables); err != nil {
log.Warnln("There was an error while building the html output", err)
}
outputFile.Close()
}
func (k *Koekr) GenerateAllPages() {
k.findPages()
for _, file := range k.files {
k.generatePage(file)
}
k.GenerateSitemap()
}
func (k *Koekr) GenerateSitemap() {
sitemapTemplate :=
`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{{ $url := .Variables.site.url }}
{{ range $slug := .Slugs }}
<url>
<loc>{{ $url }}/{{ $slug }}</loc>
</url>
{{ end }}
</urlset>
`
outputFile, err := os.Create("./generated/sitemap.xml")
if err != nil {
log.Warnln("Couldn't create sitemap.xml file: ", err)
return
}
t, err := xmlTemplate.New("sitemap").Parse(sitemapTemplate)
if err != nil {
log.Warnln("Couldn't generate sitemap:", err)
return
}
slugs := []string{}
for _, file := range k.files {
slugs = append(slugs, filepath.Base(file))
}
data := struct{
Slugs []string
Variables map[string]interface{}
}{
Slugs: slugs,
Variables: k.variables,
}
if err := t.Execute(outputFile, data); err != nil {
log.Warnln("There was an error while building the sitemap output", err)
}
}
func (k *Koekr) ParseTemplates() error {
var err error
k.t, err = template.New("index.html").Funcs(template.FuncMap(sprig.FuncMap())).ParseFiles(k.config.template)
if err != nil {
log.Fatalln("Couldn't parse template files:", err)
}
return err
}
func (k *Koekr) ParseConfig() error {
if _, err := toml.DecodeFile(k.config.configFile, &k.variables); err != nil {
log.Fatalln("Couldn't process config file:", err)
return err
}
return nil
}