diff --git a/README.md b/README.md index a9a3b1aa..34ff7fd1 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A blog engine written in Go, compatible with Ghost themes. ![Editor](https://raw.githubusercontent.com/kabukky/journey/gh-pages/images/journey.png) ## About -Please note that Journey is still in alpha and has not been tested in production. +Please note that Journey is still in alpha and has not been tested in production. Not all Ghost theme helpers have been implemented yet so there is bound to be trouble with some Ghost themes. Please open a [New Issue](https://github.com/kabukky/journey/issues) if you encounter a theme that doesn't work. #### Easy to work with Create or update your posts from any place and any device. Simply point your browser to yourblog.url/admin/, log in, and start typing away! diff --git a/plugins/conversion.go b/plugins/conversion.go index 73ae2c57..5f5d7d93 100644 --- a/plugins/conversion.go +++ b/plugins/conversion.go @@ -2,9 +2,19 @@ package plugins import ( "github.com/kabukky/journey/structure" + "github.com/kabukky/journey/structure/methods" "github.com/yuin/gopher-lua" ) +func convertArguments(vm *lua.LState, structureArguments []structure.Helper) *lua.LTable { + table := vm.NewTable() + arguments := methods.ProcessHelperArguments(structureArguments) + for key, value := range arguments { + table.RawSet(lua.LString(key), lua.LString(value)) + } + return table +} + func convertPost(vm *lua.LState, structurePost *structure.Post) *lua.LTable { post := vm.NewTable() post.RawSet(lua.LString("id"), lua.LNumber(structurePost.Id)) diff --git a/plugins/execution.go b/plugins/execution.go index cdee812e..1dbe154d 100644 --- a/plugins/execution.go +++ b/plugins/execution.go @@ -6,13 +6,13 @@ import ( "log" ) -func Execute(name string, values *structure.RequestData) ([]byte, error) { +func Execute(helper *structure.Helper, values *structure.RequestData) ([]byte, error) { // Retrieve the lua state - vm := values.PluginVMs[name] + vm := values.PluginVMs[helper.Name] // Execute plugin - err := vm.CallByParam(lua.P{Fn: vm.GetGlobal(name), NRet: 1, Protect: true}) + err := vm.CallByParam(lua.P{Fn: vm.GetGlobal(helper.Name), NRet: 1, Protect: true}) if err != nil { - log.Println("Error while executing plugin for helper "+name+":", err) + log.Println("Error while executing plugin for helper "+helper.Name+":", err) // Since the vm threw an error, close all vms and don't put the map back into the pool for _, luavm := range values.PluginVMs { luavm.Close() diff --git a/plugins/loading.go b/plugins/loading.go index 0bda87e8..6c802773 100644 --- a/plugins/loading.go +++ b/plugins/loading.go @@ -56,12 +56,13 @@ func getHelperNames(fileName string) ([]string, error) { defer vm.Close() // Set up vm functions values := &structure.RequestData{} + helper := &structure.Helper{} absDir, err := filepath.Abs(fileName) if err != nil { log.Println("Error while determining absolute path to lua file:", err) return helperList, err } - setUpVm(vm, values, absDir) + setUpVm(vm, helper, values, absDir) // Execute plugin // TODO: Is there a better way to just load the file? We only need to execute the register function (see below) err = vm.DoFile(absDir) @@ -91,7 +92,7 @@ func getHelperNames(fileName string) ([]string, error) { } // Creates all methods that can be used from Lua. -func setUpVm(vm *lua.LState, values *structure.RequestData, absPathToLuaFile string) { +func setUpVm(vm *lua.LState, helper *structure.Helper, values *structure.RequestData, absPathToLuaFile string) { luaPath := filepath.Dir(absPathToLuaFile) // Function to get the directory of the current file (to add to LUA_PATH in Lua) vm.SetGlobal("getCurrentDir", vm.NewFunction(func(vm *lua.LState) int { @@ -103,6 +104,11 @@ func setUpVm(vm *lua.LState, values *structure.RequestData, absPathToLuaFile str log.Println(vm.Get(-1).String()) return 0 // Number of results })) + // Function to get helper arguments + vm.SetGlobal("getArguments", vm.NewFunction(func(vm *lua.LState) int { + vm.Push(convertArguments(vm, helper.Arguments)) + return 1 // Number of results + })) // Function to get number of posts in values vm.SetGlobal("getNumberOfPosts", vm.NewFunction(func(vm *lua.LState) int { vm.Push(lua.LNumber(len(values.Posts))) diff --git a/plugins/luapool.go b/plugins/luapool.go index 27d22848..1f4fe497 100644 --- a/plugins/luapool.go +++ b/plugins/luapool.go @@ -15,7 +15,7 @@ type lStatePool struct { saved []map[string]*lua.LState } -func (pl *lStatePool) Get(values *structure.RequestData) map[string]*lua.LState { +func (pl *lStatePool) Get(helper *structure.Helper, values *structure.RequestData) map[string]*lua.LState { pl.m.Lock() defer pl.m.Unlock() n := len(pl.saved) @@ -23,7 +23,7 @@ func (pl *lStatePool) Get(values *structure.RequestData) map[string]*lua.LState x := pl.New() // Since these are new lua states, do the lua file. for key, value := range x { - setUpVm(value, values, LuaPool.files[key]) + setUpVm(value, helper, values, LuaPool.files[key]) value.DoFile(LuaPool.files[key]) } return x @@ -31,7 +31,7 @@ func (pl *lStatePool) Get(values *structure.RequestData) map[string]*lua.LState x := pl.saved[n-1] // Set the new values for this request in every lua state for key, value := range x { - setUpVm(value, values, LuaPool.files[key]) + setUpVm(value, helper, values, LuaPool.files[key]) } pl.saved = pl.saved[0 : n-1] return x diff --git a/structure/methods/helper.go b/structure/methods/helper.go new file mode 100644 index 00000000..cc38f62f --- /dev/null +++ b/structure/methods/helper.go @@ -0,0 +1,20 @@ +package methods + +import ( + "github.com/kabukky/journey/structure" + "strings" +) + +func ProcessHelperArguments(arguments []structure.Helper) map[string]string { + argumentsMap := make(map[string]string) + for index, _ := range arguments { + // Separate = arguments and put them in map + argumentParts := strings.SplitN(arguments[index].Name, "=", 2) + if len(argumentParts) > 1 { + argumentsMap[argumentParts[0]] = argumentParts[1] + } else { + argumentsMap[arguments[index].Name] = "" + } + } + return argumentsMap +} diff --git a/templates/handlebars.go b/templates/handlebars.go index 0a3f09eb..b5d6f16a 100644 --- a/templates/handlebars.go +++ b/templates/handlebars.go @@ -7,6 +7,7 @@ import ( "github.com/kabukky/journey/filenames" "github.com/kabukky/journey/plugins" "github.com/kabukky/journey/structure" + "github.com/kabukky/journey/structure/methods" "html" "log" "net/url" @@ -22,16 +23,20 @@ var jqueryCodeForFooter = []byte("