funcmap.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package engine
  2. import (
  3. "html/template"
  4. "net/http"
  5. "strings"
  6. log "github.com/sirupsen/logrus"
  7. "github.com/xyproto/datablock"
  8. )
  9. // Functions for concurrent use by rendering.go and handlers.go
  10. // Lua2funcMap runs in a Lua file and returns the functions as a template.FuncMap (or an error)
  11. func (ac *Config) Lua2funcMap(w http.ResponseWriter, req *http.Request, filename, luafilename, ext string, errChan chan error, funcMapChan chan template.FuncMap) {
  12. // Make functions from the given Lua data available
  13. funcs := make(template.FuncMap)
  14. // Try reading data.lua, if possible
  15. luablock, err := ac.cache.Read(luafilename, ac.shouldCache(ext))
  16. if err != nil {
  17. // Could not find and/or read data.lua
  18. luablock = datablock.EmptyDataBlock
  19. // This only means the file wasn't cached, so just ignore this error
  20. }
  21. // luablock can be empty if there was an error or if the file was empty
  22. if luablock.HasData() {
  23. // There was Lua code available. Now make the functions and
  24. // variables available for the template.
  25. funcs, err = ac.LuaFunctionMap(w, req, luablock.Bytes(), luafilename)
  26. if err != nil {
  27. funcMapChan <- funcs
  28. errChan <- err
  29. return
  30. }
  31. if ac.debugMode && ac.verboseMode {
  32. s := "These functions from " + luafilename
  33. s += " are useable for " + filename + ": "
  34. // Create a comma separated list of the available functions
  35. for key := range funcs {
  36. s += key + ", "
  37. }
  38. // Remove the final comma
  39. s = strings.TrimSuffix(s, ", ")
  40. // Output the message
  41. log.Info(s)
  42. }
  43. }
  44. funcMapChan <- funcs
  45. errChan <- err
  46. }