src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/md/trace.go (about)

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