plain_text.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package ace
  2. import (
  3. "bytes"
  4. "io"
  5. "strings"
  6. )
  7. // plainText represents a plain text.
  8. type plainText struct {
  9. elementBase
  10. insertBr bool
  11. }
  12. // WriteTo writes data to w.
  13. func (e *plainText) WriteTo(w io.Writer) (int64, error) {
  14. var bf bytes.Buffer
  15. // Write the plain text.
  16. bf.WriteString(strings.Join(e.ln.tokens[1:], space))
  17. if len(e.ln.tokens) > 1 && e.insertBr {
  18. bf.WriteString(htmlBr)
  19. }
  20. // Write the children's HTML.
  21. if len(e.children) > 0 {
  22. bf.WriteString(lf)
  23. if i, err := e.writeChildren(&bf); err != nil {
  24. return i, err
  25. }
  26. }
  27. // Write the buffer.
  28. i, err := w.Write(bf.Bytes())
  29. return int64(i), err
  30. }
  31. // ContainPlainText returns true.
  32. func (e *plainText) ContainPlainText() bool {
  33. return true
  34. }
  35. // InsertBr returns true if the br tag is inserted to the line.
  36. func (e *plainText) InsertBr() bool {
  37. return e.insertBr
  38. }
  39. // newPlainText creates and returns a plain text.
  40. func newPlainText(ln *line, rslt *result, src *source, parent element, opts *Options) *plainText {
  41. return &plainText{
  42. elementBase: newElementBase(ln, rslt, src, parent, opts),
  43. insertBr: ln.tokens[0] == doublePipe,
  44. }
  45. }