51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"html/template"
|
|
"io/ioutil"
|
|
"os"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// CurrentTheme is the set of template being used.
|
|
var CurrentTheme *template.Template
|
|
|
|
// themeCSS is the CSS of the current theme.
|
|
var themeCSS []byte
|
|
|
|
// SetTheme parses the provided theme name and sets it as the current theme.
|
|
func SetTheme(themeName string) error {
|
|
var err error
|
|
|
|
folder := "templates/" + themeName + "/"
|
|
CurrentTheme, err = template.ParseFiles(
|
|
folder+"index.tmpl", // Homepage (listing).
|
|
folder+"404.tmpl", // Post not found.
|
|
folder+"500.tmpl", // Internal error.
|
|
folder+"post.tmpl", // Page for posts.
|
|
)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Check if the style CSS exists and is not a directory.
|
|
info, err := os.Stat(folder + "style.css")
|
|
if os.IsNotExist(err) || (info != nil && info.IsDir()) {
|
|
return nil
|
|
}
|
|
f, err := os.Open(folder + "style.css")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
err := f.Close()
|
|
if err != nil {
|
|
logrus.Fatalf("Error while closing theme CSS file: %v", err)
|
|
}
|
|
}()
|
|
themeCSS, err = ioutil.ReadAll(f)
|
|
return err
|
|
}
|