package noteButtons import ( "bytes" "fmt" "log" "strconv" "strings" "github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/parser" "github.com/yuin/goldmark/renderer" "github.com/yuin/goldmark/text" "github.com/yuin/goldmark/util" ) type CheckButton struct { ast.BaseBlock Dc int Skill string Campaign string } type CheckButtonData struct { Dc string `json:"dc"` Skill string `json:"skill"` Campaign string `json:"campaign"` } var KindCheckButton = ast.NewNodeKind("CheckButton") func (b *CheckButton) Kind() ast.NodeKind { return KindCheckButton } func (b *CheckButton) Dump(source []byte, level int) { ast.DumpHelper(b, source, level, nil, nil) } type CheckButtonParser struct{} func (p *CheckButtonParser) Trigger() []byte { return []byte{'{'} } func (p *CheckButtonParser) Parse(parent ast.Node, reader text.Reader, pc parser.Context) ast.Node { line, _ := reader.PeekLine() if !bytes.HasPrefix(line, []byte("{{")) { return nil } // Find the closing }} end := bytes.Index(line[2:], []byte("}}")) if end < 0 { return nil } end += 2 contents := strings.TrimSpace(string(line[2:end])) if !strings.HasPrefix(contents, "checkButton") { return nil } contents = strings.TrimSpace(strings.TrimPrefix(contents, "checkButton")) args, err := parseArgs(contents) if err != nil { log.Default().Print(err) return nil } dc, _ := strconv.Atoi(args["dc"]) campaign, _ := pc.Get(CampaignContextKey).(string) reader.Advance(end + 2) return &CheckButton{ Dc: dc, Skill: args["skill"], Campaign: campaign, } } type CheckButtonRenderer struct{} func (r *CheckButtonRenderer) renderCheckButton(writer util.BufWriter, _ []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { if !entering { return ast.WalkContinue, nil } button := node.(*CheckButton) fmt.Fprintf( writer, `") return ast.WalkContinue, nil } func (r *CheckButtonRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { reg.Register(KindCheckButton, r.renderCheckButton) }