router_static.go: do multiple rounds of templating

This commit is contained in:
Adhityaa Chandrasekar 2018-09-26 03:18:36 -04:00
parent c21329ac4e
commit 93595f3877

View File

@ -27,6 +27,8 @@ type staticHtmlPlugs struct {
} }
func staticRouterInit(router *mux.Router) error { func staticRouterInit(router *mux.Router) error {
subdir := pathStrip(os.Getenv("ORIGIN"))
asset := make(map[string][]byte) asset := make(map[string][]byte)
gzippedAsset := make(map[string][]byte) gzippedAsset := make(map[string][]byte)
@ -57,8 +59,6 @@ func staticRouterInit(router *mux.Router) error {
gzip := (os.Getenv("GZIP_STATIC") == "true") gzip := (os.Getenv("GZIP_STATIC") == "true")
subdir := pathStrip(os.Getenv("ORIGIN"))
asset[subdir+p] = []byte(prefix + string(contents)) asset[subdir+p] = []byte(prefix + string(contents))
if gzip { if gzip {
gzippedAsset[subdir+p], err = gzipStatic(asset[subdir+p]) gzippedAsset[subdir+p], err = gzipStatic(asset[subdir+p])
@ -101,6 +101,10 @@ func staticRouterInit(router *mux.Router) error {
} }
html := make(map[string]string) html := make(map[string]string)
for _, page := range pages {
html[subdir+page] = ""
}
for _, page := range pages { for _, page := range pages {
sl := string(os.PathSeparator) sl := string(os.PathSeparator)
page = sl + page page = sl + page
@ -112,22 +116,30 @@ func staticRouterInit(router *mux.Router) error {
return err return err
} }
t, err := template.New(page).Delims("[[[", "]]]").Parse(string(contents)) result := string(contents)
if err != nil {
logger.Errorf("cannot parse %s%s template: %v", os.Getenv("STATIC"), file, err) for {
return err t, err := template.New(page).Delims("[[[", "]]]").Parse(result)
if err != nil {
logger.Errorf("cannot parse %s%s template: %v", os.Getenv("STATIC"), file, err)
return err
}
var buf bytes.Buffer
t.Execute(&buf, &staticHtmlPlugs{
Origin: os.Getenv("ORIGIN"),
CdnPrefix: os.Getenv("CDN_PREFIX"),
Footer: template.HTML(string(footer)),
})
result = buf.String()
if result == html[subdir+page] {
break
} else {
html[subdir+page] = result
continue
}
} }
var buf bytes.Buffer
t.Execute(&buf, &staticHtmlPlugs{
Origin: os.Getenv("ORIGIN"),
CdnPrefix: os.Getenv("CDN_PREFIX"),
Footer: template.HTML(string(footer)),
})
subdir := pathStrip(os.Getenv("ORIGIN"))
html[subdir+page] = buf.String()
} }
for _, page := range pages { for _, page := range pages {