2015-09-28 20:46:58 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/GeertJohan/go.rice"
|
|
|
|
"github.com/flosch/pongo2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Pongo2Loader struct {
|
|
|
|
box *rice.Box
|
|
|
|
}
|
|
|
|
|
2015-09-28 21:58:50 -04:00
|
|
|
func NewPongo2TemplatesLoader() (*Pongo2Loader, error) {
|
2015-09-28 20:46:58 -04:00
|
|
|
fs := &Pongo2Loader{}
|
|
|
|
|
2015-09-28 21:58:50 -04:00
|
|
|
p2l, err := rice.FindBox("templates")
|
2015-09-28 20:46:58 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.box = p2l
|
|
|
|
return fs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *Pongo2Loader) Get(path string) (io.Reader, error) {
|
|
|
|
myBytes, err := fs.box.Bytes(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes.NewReader(myBytes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *Pongo2Loader) Abs(base, name string) string {
|
|
|
|
me := path.Join(filepath.Dir(base), name)
|
|
|
|
return me
|
|
|
|
}
|
|
|
|
|
|
|
|
func populateTemplatesMap(tSet *pongo2.TemplateSet, tMap map[string]*pongo2.Template) error {
|
|
|
|
|
|
|
|
templates := [...]string{
|
|
|
|
"index.html",
|
|
|
|
"404.html",
|
2015-09-28 23:46:43 -04:00
|
|
|
"401.html",
|
2015-09-28 20:46:58 -04:00
|
|
|
"oops.html",
|
|
|
|
|
|
|
|
"display/audio.html",
|
|
|
|
"display/image.html",
|
|
|
|
"display/video.html",
|
|
|
|
"display/pdf.html",
|
|
|
|
"display/file.html",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tName := range templates {
|
|
|
|
tpl, err := tSet.FromFile(tName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tMap[tName] = tpl
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|