docstore/templates.sh
2019-08-20 23:57:43 +02:00

108 lines
2.8 KiB
Bash
Executable file

#!/bin/bash
# Copyright (C) 2019 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) 2019 Marius Schellenberger
// This file is auto-generated by templates.sh
// using the contents of the templates directory
package server
import (
"encoding/base64"
"errors"
"git.giftfish.de/ston1th/docstore/pkg/log"
"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
echo " favicon = \"$(base64 -w 0 templates/favicon.ico)\"" >> ${templates_go}
cat << EOF >> ${templates_go}
)
func (s *HTTPServer) loadTemplates() {
var fatal 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)
fatal = true
return
}
}
return
}
// resources
s.res["bootstrap.css"] = []byte(bootstrapCSS)
s.res["custom.css"] = []byte(customCSS)
fav, err := base64.StdEncoding.DecodeString(favicon)
if err != nil {
log.Println("favicon:", err)
fatal = true
}
s.res["favicon.ico"] = fav
// register
s.templ["registerHandler"] = parse(index, menu, register)
s.templ["dirHandler"] = parse(index, menu, dir)
s.templ["fileHandler"] = parse(index, menu, file)
s.templ["uploadHandler"] = parse(index, menu, upload)
s.templ["newDirHandler"] = parse(index, menu, newDir)
s.templ["moveHandler"] = parse(index, menu, move)
s.templ["enoentHandler"] = parse(index, menu, enoent)
s.templ["deleteHandler"] = parse(index, menu, delete)
s.templ["notFoundHandler"] = parse(index, menu, notFound)
s.templ["forbiddenHandler"] = parse(index, menu, forbidden)
s.templ["iseHandler"] = parse(index, menu, ise)
s.templ["loginHandler"] = parse(index, menu, login)
s.templ["loginTotpHandler"] = parse(index, menu, loginTotp)
s.templ["searchHandler"] = parse(index, menu, search)
s.templ["userEditHandler"] = parse(index, menu, userEdit)
// totp
s.templ["userTotpHandler"] = parse(index, menu, userTotp)
// tags
s.templ["tagsHandler"] = parse(index, menu, tagsList)
s.templ["tagsEditHandler"] = parse(index, menu, tagsEdit)
s.templ["tagsDelHandler"] = parse(index, menu, tagsDel)
if fatal {
log.Fatal("parse: ", errors.New("parsing templates failed"))
}
}
EOF