Something went wrong. Try again.
A framework using Svelte to generate a website for one's TTRPG notes with buttons that activate Foundry commands
Something went wrong. Try again.
2.3 kB · 117 lines
Go
at go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118package 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, `<button data-campaign="%s" data-dc="%d" data-skill="%s" class="checkButton">`, button.Campaign, button.Dc, button.Skill, )
fmt.Fprintf( writer, "DC %d %s", button.Dc, formatSkill(button.Skill), )
writer.WriteString("</button>") return ast.WalkContinue, nil}
func (r *CheckButtonRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { reg.Register(KindCheckButton, r.renderCheckButton)}