gowiki/templates.sh
2016-09-05 22:49:40 +02:00

90 lines
2.1 KiB
Bash
Executable file

#!/bin/bash
# this file generates Go source code for the gowiki
# it contains html and css and uses the contents
# of the templates directory
templates_go="templates.go"
echo "Generating templates.go"
if [ ! -d templates ]; then
echo "error: templates directory not found" 1>&2
exit 2
fi
cat << EOF > ${templates_go}
// This file is auto-generated by build/templates.sh
// using the contents of the templates directory
package main
import (
"errors"
"html/template"
)
EOF
echo "const (" >> ${templates_go}
for f in $(ls templates/*.html); do
fname=${f/templates\//}
fname=${fname/.html/}
echo " ${fname} = \`$(cat ${f})\`" >> ${templates_go}
done
for f in $(ls templates/*.css); do
fname=${f/templates\//}
fname=${fname/.css/CSS}
echo " ${fname} = \`$(cat ${f})\`" >> ${templates_go}
done
cat << EOF >> ${templates_go}
)
var (
templ map[string]*template.Template
res map[string][]byte
)
func loadTemplates() {
var errf bool
templ = make(map[string]*template.Template)
res = make(map[string][]byte)
parse := func(html ...string) (temp *template.Template) {
var err error
temp = template.New("")
for _, s := range html {
temp, err = temp.Parse(s)
if err != nil {
log.Println("parse:", err)
errf = true
return
}
}
return
}
// resources
res["bootstrap.css"] = []byte(bootstrapCSS)
res["custom.css"] = []byte(customCSS)
// pages.go
templ["notFoundHandler"] = parse(index, menu, notFound)
templ["loginHandler"] = parse(index, menu, login)
templ["searchHandler"] = parse(index, menu, search)
templ["allHandler"] = parse(index, menu, all)
templ["wikiNewHandler"] = parse(index, menu, wikiNew)
templ["wikiEditHandler"] = parse(index, menu, wikiEdit)
templ["wikiHandler"] = parse(index, menu, wiki)
templ["wikiMDHandler"] = parse(index, menu, wikiMD)
// users.go
templ["usersHandler"] = parse(index, menu, users)
templ["userNewHandler"] = parse(index, menu, userNew)
templ["userEditHandler"] = parse(index, menu, userEdit)
templ["userDelHandler"] = parse(index, menu, userDel)
if errf {
log.Fatal("parse: ", errors.New("parsing templates failed"))
}
}
EOF