Skip to content

Commit

Permalink
Merge pull request #28 from kabukky/development
Browse files Browse the repository at this point in the history
Changed: Another theme will now be loaded if the one set in the db is no...
  • Loading branch information
kabukky committed Apr 29, 2015
2 parents d48e7f7 + ce21e29 commit f9115f6
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 16 deletions.
14 changes: 14 additions & 0 deletions database/update.go
Expand Up @@ -82,6 +82,20 @@ func UpdateSettings(title []byte, description []byte, logo []byte, cover []byte,
return writeDB.Commit()
}

func UpdateActiveTheme(activeTheme string, updated_at time.Time, updated_by int64) error {
writeDB, err := readDB.Begin()
if err != nil {
writeDB.Rollback()
return err
}
_, err = writeDB.Exec(stmtUpdateSettings, activeTheme, updated_at, updated_by, "activeTheme")
if err != nil {
writeDB.Rollback()
return err
}
return writeDB.Commit()
}

func UpdateUser(id int64, email []byte, image []byte, cover []byte, bio []byte, website []byte, location []byte, updated_at time.Time, updated_by int64) error {
writeDB, err := readDB.Begin()
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions structure/methods/blog.go
Expand Up @@ -17,6 +17,14 @@ func UpdateBlog(b *structure.Blog, userId int64) error {
return nil
}

func UpdateActiveTheme(activeTheme string, userId int64) error {
err := database.UpdateActiveTheme(activeTheme, time.Now(), userId)
if err != nil {
return err
}
return nil
}

func GenerateBlog() (*structure.Blog, error) {
// Generate blog from db
blog, err := database.RetrieveBlog()
Expand Down
77 changes: 61 additions & 16 deletions templates/generation.go
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/kabukky/journey/helpers"
"github.com/kabukky/journey/plugins"
"github.com/kabukky/journey/structure"
"github.com/kabukky/journey/structure/methods"
"github.com/kabukky/journey/watcher"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -204,23 +204,12 @@ func inspectTemplateFile(filePath string, info os.FileInfo, err error) error {
return nil
}

func Generate() error {
compiledTemplates.Lock()
defer compiledTemplates.Unlock()
activeTheme, err := database.RetrieveActiveTheme()
if err != nil {
return err
}
// Compile all template files
// First clear compiledTemplates map (theme could have been changed)
compiledTemplates.m = make(map[string]*structure.Helper)
currentThemePath := filepath.Join(filenames.ThemesFilepath, *activeTheme)
func compileTheme(themePath string) error {
// Check if the theme folder exists
if _, err := os.Stat(currentThemePath); os.IsNotExist(err) {
log.Fatal("Error: Couldn't find theme files in " + currentThemePath + ": " + err.Error())
return err
if _, err := os.Stat(themePath); os.IsNotExist(err) {
return errors.New("Couldn't find theme files in " + themePath + ": " + err.Error())
}
err = filepath.Walk(currentThemePath, inspectTemplateFile)
err := filepath.Walk(themePath, inspectTemplateFile)
if err != nil {
return err
}
Expand All @@ -231,9 +220,65 @@ func Generate() error {
if _, ok := compiledTemplates.m["post"]; !ok {
return errors.New("Couldn't compile template 'post'. Is post.hbs missing?")
}
return nil
}

func checkThemes() error {
// Get currently set theme from database
activeTheme, err := database.RetrieveActiveTheme()
if err != nil {
return err
}
currentThemePath := filepath.Join(filenames.ThemesFilepath, *activeTheme)
err = compileTheme(currentThemePath)
if err == nil {
return nil
}
// If the currently set theme couldnt be compiled, try the default theme (promenade)
err = compileTheme(filepath.Join(filenames.ThemesFilepath, "promenade"))
if err == nil {
// Update the theme name in the database
err = methods.UpdateActiveTheme("promenade", 1)
if err != nil {
return err
}
return nil
}
// If all of that didn't work, try the available themes in order
allThemes := GetAllThemes()
for _, theme := range allThemes {
err = compileTheme(filepath.Join(filenames.ThemesFilepath, theme))
if err == nil {
// Update the theme name in the database
err = methods.UpdateActiveTheme(theme, 1)
if err != nil {
return err
}
return nil
}
}
return errors.New("Couldn't find a theme to use in " + filenames.ThemesFilepath)
}

func Generate() error {
compiledTemplates.Lock()
defer compiledTemplates.Unlock()
// First clear compiledTemplates map (theme could have been changed)
compiledTemplates.m = make(map[string]*structure.Helper)
// Compile all template files
err := checkThemes()
if err != nil {
return err
}
// If the dev flag is set, watch the theme directory and the plugin directoy for changes
// TODO: It seems unclean to do the watching of the plugins in the templates package. Move this somewhere else.
if flags.IsInDevMode {
// Get the currently used theme path
activeTheme, err := database.RetrieveActiveTheme()
if err != nil {
return err
}
currentThemePath := filepath.Join(filenames.ThemesFilepath, *activeTheme)
// Create watcher
err = watcher.Watch([]string{currentThemePath, filenames.PluginsFilepath}, map[string]func() error{".hbs": Generate, ".lua": plugins.Load})
if err != nil {
Expand Down

0 comments on commit f9115f6

Please sign in to comment.