metla.go 985 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package metla
  2. import (
  3. "fmt"
  4. "io"
  5. "path/filepath"
  6. "time"
  7. "git.ali33.ru/fcg-xvii/mjs"
  8. )
  9. func New(modifiedCallback func(string) int64, contentCallback func(string) ([]byte, error)) *Metla {
  10. m := &Metla{
  11. contentCallback: contentCallback,
  12. }
  13. m.js = mjs.New(modifiedCallback, m.content)
  14. return m
  15. }
  16. type Metla struct {
  17. js *mjs.Mjs
  18. contentCallback func(string) ([]byte, error)
  19. }
  20. func (s *Metla) content(name string) (content []byte, err error) {
  21. if content, err = s.contentCallback(name); err == nil {
  22. if filepath.Ext(name) != ".script" && filepath.Ext(name) != ".sjs" {
  23. content, err = parseBytes(content, name)
  24. }
  25. }
  26. return
  27. }
  28. func (s *Metla) Exec(name string, data map[string]interface{}, w io.Writer) (modified int64, err error) {
  29. if data == nil {
  30. data = make(map[string]interface{})
  31. }
  32. data["flush"] = func(vals ...interface{}) {
  33. for _, v := range vals {
  34. w.Write([]byte(fmt.Sprint(v)))
  35. }
  36. }
  37. return s.js.Exec(name, data, time.Second*60)
  38. }