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
|
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)
type templateData struct {
SiteTitle string
StylesheetUrl string
Nav []navItem
Content []string
}
func applyTemplate(dir string, data templateData, tmpl *template.Template) error {
// ensure directory exists
os.MkdirAll(strings.ToLower(dir), 0755)
file := filepath.Join(strings.ToLower(dir), "index.html")
// create file, fail if it already exists
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()
// apply template, write to file
err = tmpl.Execute(f, data)
if err != nil {
return err
}
return nil
}
func loadTemplate(cfg config) (templ *template.Template) {
tmpl_raw, err := ioutil.ReadFile(cfg.TemplateFile)
if err != nil {
panic(err)
}
templ, err = template.New("template").Parse(string(tmpl_raw))
if err != nil {
panic(err)
}
return
}
func generatePages(pages map[string]page, nav []navItem, cfg config) {
template := loadTemplate(cfg)
data := templateData{SiteTitle: "TestTitle",
StylesheetUrl: cfg.CssFile,
Nav: nav}
for k, v := range pages {
data.Content = v.content
err := applyTemplate("/tmp/tempgodocs/"+k, data, template)
if err != nil {
panic(err)
}
}
}
|