aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEddy Pedroni <eddy@0xf7.com>2020-02-01 11:27:48 +0100
committerEddy Pedroni <eddy@0xf7.com>2020-02-01 11:27:48 +0100
commitc1d6f344316789c560107777d9d332c9a8f15bd3 (patch)
tree7d6c304b13da5ba95b715f27836565bd481dbedf
parent4bedcc88d171a0886a114c604a79155d088bdbeb (diff)
Basic page generation works
-rw-r--r--main.go3
-rw-r--r--template.go53
2 files changed, 53 insertions, 3 deletions
diff --git a/main.go b/main.go
index ad5e819..7ed9a77 100644
--- a/main.go
+++ b/main.go
@@ -5,7 +5,6 @@ import (
"os"
)
-
func main() {
// show usage if config file is missing
if len(os.Args) < 2 {
@@ -35,6 +34,6 @@ func main() {
fmt.Println("Navigation:")
fmt.Println(navItems)
- generatePages(pages, navItems, cfg)
+ generatePages(pages, navItems, cfg)
}
diff --git a/template.go b/template.go
index ff69756..ed38d2c 100644
--- a/template.go
+++ b/template.go
@@ -1,5 +1,13 @@
package main
+import (
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "strings"
+ "text/template"
+)
+
type templateData struct {
SiteTitle string
StylesheetUrl string
@@ -7,6 +15,49 @@ type templateData struct {
Content []string
}
-func generatePages(pages map[string]page, nav []navItem, cfg config) () {
+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)
+ }
+ }
}