trace.go 887 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package md
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // TraceCodec is a Codec that records all the Op's passed to its Do method.
  7. type TraceCodec struct{ strings.Builder }
  8. func (c *TraceCodec) Do(op Op) {
  9. c.WriteString(op.Type.String())
  10. if op.Number != 0 {
  11. fmt.Fprintf(c, " Number=%d", op.Number)
  12. }
  13. if op.Info != "" {
  14. fmt.Fprintf(c, " Info=%q", op.Info)
  15. }
  16. if op.MissingCloser {
  17. fmt.Fprintf(c, " MissingCloser")
  18. }
  19. c.WriteByte('\n')
  20. for _, line := range op.Lines {
  21. c.WriteString(" ")
  22. c.WriteString(line)
  23. c.WriteByte('\n')
  24. }
  25. for _, inlineOp := range op.Content {
  26. c.WriteString(" ")
  27. c.WriteString(inlineOp.Type.String())
  28. if inlineOp.Text != "" {
  29. fmt.Fprintf(c, " Text=%q", inlineOp.Text)
  30. }
  31. if inlineOp.Dest != "" {
  32. fmt.Fprintf(c, " Dest=%q", inlineOp.Dest)
  33. }
  34. if inlineOp.Alt != "" {
  35. fmt.Fprintf(c, " Alt=%q", inlineOp.Alt)
  36. }
  37. c.WriteString("\n")
  38. }
  39. }