helper_method_content.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package ace
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. )
  7. // helperMethodContent represents a helper method content.
  8. type helperMethodContent struct {
  9. elementBase
  10. name string
  11. }
  12. // WriteTo writes data to w.
  13. func (e *helperMethodContent) WriteTo(w io.Writer) (int64, error) {
  14. var bf bytes.Buffer
  15. inner := e.src.inner
  16. if inner == nil {
  17. return 0, fmt.Errorf("inner is not specified [file: %s][line: %d]", e.ln.fileName(), e.ln.no)
  18. }
  19. // Write a define action.
  20. bf.WriteString(fmt.Sprintf(actionDefine, e.opts.DelimLeft, inner.path+doubleColon+e.name, e.opts.DelimRight))
  21. // Write the children's HTML.
  22. if i, err := e.writeChildren(&bf); err != nil {
  23. return i, err
  24. }
  25. // Write an end action.
  26. bf.WriteString(fmt.Sprintf(actionEnd, e.opts.DelimLeft, e.opts.DelimRight))
  27. // Write the buffer.
  28. i, err := w.Write(bf.Bytes())
  29. return int64(i), err
  30. }
  31. func (e *helperMethodContent) IsBlockElement() bool {
  32. return true
  33. }
  34. // newHelperMethodContent creates and returns a helper method content.
  35. func newHelperMethodContent(ln *line, rslt *result, src *source, parent element, opts *Options) (*helperMethodContent, error) {
  36. if len(ln.tokens) < 3 || ln.tokens[2] == "" {
  37. return nil, fmt.Errorf("no name is specified [file: %s][line: %d]", ln.fileName(), ln.no)
  38. }
  39. e := &helperMethodContent{
  40. elementBase: newElementBase(ln, rslt, src, parent, opts),
  41. name: ln.tokens[2],
  42. }
  43. return e, nil
  44. }