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.7 kB · 140 lines
Go
at go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141package noteButtons
import ( "bytes" "fmt" "log" "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 RollButton struct { ast.BaseBlock
DieFormula string Flavor *string Campaign string Label *string}
type RollButtonData struct { DieFormula string `json:"dieFormula"` Flavor *string `json:"flavor,omitempty"` Campaign string `json:"campaign"` Label *string `json:"label,omitempty"`}
var KindRollButton = ast.NewNodeKind("RollButton")
func (b *RollButton) Kind() ast.NodeKind { return KindRollButton}
func (b *RollButton) Dump(source []byte, level int) { ast.DumpHelper(b, source, level, nil, nil)}
type RollButtonParser struct{}
func (p *RollButtonParser) Trigger() []byte { return []byte{'{'}}
func (p *RollButtonParser) 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, "rollButton") { return nil }
contents = strings.TrimSpace(strings.TrimPrefix(contents, "rollButton")) args, err := parseArgs(contents)
if err != nil { log.Default().Println(err) return nil }
var flavor *string = nil var label *string = nil
flavorStr, ok := args["flavor"]
if ok { flavor = &flavorStr }
labelStr, ok := args["label"]
if ok { label = &labelStr }
campaign, _ := pc.Get(CampaignContextKey).(string) reader.Advance(end + 2)
return &RollButton{ Campaign: campaign, DieFormula: args["dieFormula"], Flavor: flavor, Label: label, }}
type RollButtonRenderer struct{}
func (r *RollButtonRenderer) renderRollButton(writer util.BufWriter, _ []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { if !entering { return ast.WalkContinue, nil }
button := node.(*RollButton)
fmt.Fprintf( writer, `<button data-campaign="%s" data-dieformula="%s"`, button.Campaign, button.DieFormula, )
if button.Flavor != nil { fmt.Fprintf( writer, ` data-flavor="%s"`, *button.Flavor, ) }
writer.WriteString(` class="rollButton">`)
if button.Label == nil { writer.WriteString(button.DieFormula) } else { writer.WriteString(*button.Label) }
writer.WriteString("</button>") return ast.WalkContinue, nil}
func (r *RollButtonRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { reg.Register(KindRollButton, r.renderRollButton)}