metla.go 961 B

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