From c172372ebd9fadcb05e3ea3b7bf9660797090f0f Mon Sep 17 00:00:00 2001 From: n0m1s Date: Tue, 29 Oct 2019 23:51:18 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A1=20GoDoc=20comments=20on=20function?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jdr/sheet.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/jdr/sheet.go b/jdr/sheet.go index d94f566..277fcfa 100644 --- a/jdr/sheet.go +++ b/jdr/sheet.go @@ -9,10 +9,13 @@ import ( "os" ) +// Page represents a page of a character sheet. type Page struct { Items []SheetItem } +// ToHTML transforms a Page into a HTML code. +// This HTML code is embeddable into the character sheet webpage func (page *Page) ToHTML() template.HTML { ret := "" @@ -23,20 +26,25 @@ func (page *Page) ToHTML() template.HTML { return template.HTML(ret) } +// SheetItem represents an item in the character sheet (either a structural element or a value) type SheetItem interface { Name() string ToHTML() template.HTML } +// Block represents a SheetItem that contains other SheetItems. +// It is used as a structural element to group values together. type Block struct { name string Items []SheetItem } +// Name of the block (possibly empty if the block is unnamed) func (block *Block) Name() string { return block.name } +// ToHTML transforms a Block into a HTML code. func (block *Block) ToHTML() template.HTML { begin := `
` legend := `` + template.HTMLEscapeString(block.Name()) + `` @@ -55,15 +63,18 @@ func (block *Block) ToHTML() template.HTML { return ret } +// Variable is a value in the character sheet, such as the name of the character, or its XP count type Variable struct { Type VariableType name string } +// Name of the variable func (v *Variable) Name() string { return v.name } +// ToHTML transforms a Variable into a HTML code func (v *Variable) ToHTML() template.HTML { label := "" if v.name != "" { @@ -78,15 +89,19 @@ func (v *Variable) ToHTML() template.HTML { return template.HTML(label) + html + template.HTML(`
`) } +// CharacterSheet is a role-play character sheet. +// It is structured in Pages, Blocks and Variables type CharacterSheet struct { types map[string]VariableType Pages []*Page } +// Script renders the Javascript code necessary to render the character sheet web page interactive func (cs *CharacterSheet) Script() template.JS { return template.JS("") } +// Render the CharacterSheet into an embeddable HTML code func (cs *CharacterSheet) Render() template.HTML { ret := template.HTML("") @@ -97,7 +112,9 @@ func (cs *CharacterSheet) Render() template.HTML { return ret } +// AddType adds a custom variable type to the character sheet func (cs *CharacterSheet) AddType(t VariableType) { + //create the type array if it doesn't exist yet if cs.types == nil { cs.types = make(map[string]VariableType) } @@ -106,6 +123,7 @@ func (cs *CharacterSheet) AddType(t VariableType) { cs.types[name] = t } +// AddDefaultTypes adds the default variable types to the character sheet func (cs *CharacterSheet) AddDefaultTypes() { cs.AddType(&VariableType_bool{}) cs.AddType(&VariableType_int{}) @@ -114,6 +132,7 @@ func (cs *CharacterSheet) AddDefaultTypes() { cs.AddType(&VariableType_d{}) } +// getType searches into the types (default & customs) of a character sheet for one matching "typename" func (cs *CharacterSheet) getType(typename string) (VariableType, error) { t, ok := cs.types[typename] if !ok { @@ -123,6 +142,8 @@ func (cs *CharacterSheet) getType(typename string) (VariableType, error) { return t, nil } +// xmlNode is a temporary structure used for parsing the character sheet XML file +// without knowing its structure à priori type xmlNode struct { XMLName xml.Name Attrs []xml.Attr `xml:"-"` @@ -347,6 +368,8 @@ func parseSheet(root xmlNode) (*CharacterSheet, error) { return sheet, nil } +// ReadCharacterSheet opens a character sheet XML file +// and parses the XML to get a CharacterSheet struct func ReadCharacterSheet(filename string) (*CharacterSheet, error) { file, err := os.Open(filename) if err != nil {