gowiki/templates.sh
2018-09-04 23:15:47 +02:00

89 lines
2.2 KiB
Bash
Executable file

#!/bin/bash
# Copyright (C) 2017 Marius Schellenberger
# this file generates Go source code for the gowiki
# it contains html and css and uses the contents
# of the templates directory
templates_go="pkg/server/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}
// Copyright (C) 2017 Marius Schellenberger
// This file is auto-generated by build/templates.sh
// using the contents of the templates directory
package server
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}
)
func (s *HTTPServer) loadTemplates() {
var errf bool
s.templ = make(map[string]*template.Template)
s.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
s.res["bootstrap.css"] = []byte(bootstrapCSS)
s.res["custom.css"] = []byte(customCSS)
// pages.go
s.templ["notFoundHandler"] = parse(index, menu, notFound)
s.templ["loginHandler"] = parse(index, menu, login)
s.templ["searchHandler"] = parse(index, menu, search)
s.templ["allHandler"] = parse(index, menu, all)
s.templ["wikiNewHandler"] = parse(index, menu, wikiNew)
s.templ["wikiEditHandler"] = parse(index, menu, wikiEdit)
s.templ["wikiHandler"] = parse(index, menu, wiki)
s.templ["wikiMDHandler"] = parse(index, menu, wikiMD)
// users.go
s.templ["usersHandler"] = parse(index, menu, users)
s.templ["userNewHandler"] = parse(index, menu, userNew)
s.templ["userEditHandler"] = parse(index, menu, userEdit)
s.templ["userDelHandler"] = parse(index, menu, userDel)
if errf {
log.Fatal("parse: ", errors.New("parsing templates failed"))
}
}
EOF