2020-11-30 15:29:44 +07:00
|
|
|
package handler
|
|
|
|
|
2020-12-01 14:16:56 +07:00
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
2020-11-30 15:29:44 +07:00
|
|
|
|
|
|
|
// CurrentTheme is the set of template being used.
|
|
|
|
var CurrentTheme *template.Template
|
|
|
|
|
2020-12-01 14:16:56 +07:00
|
|
|
// themeCSS is the CSS of the current theme.
|
|
|
|
var themeCSS []byte
|
|
|
|
|
2020-11-30 15:29:44 +07:00
|
|
|
// 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.
|
|
|
|
)
|
|
|
|
|
2020-12-01 14:16:56 +07:00
|
|
|
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)
|
2020-11-30 15:29:44 +07:00
|
|
|
return err
|
|
|
|
}
|