Browse Source

💡 GoDoc comments on functions

master
n0m1s 6 years ago
parent
commit
c172372ebd
Signed by: nomis GPG Key ID: BC0454CAD76FE803
1 changed files with 23 additions and 0 deletions
  1. +23
    -0
      jdr/sheet.go

+ 23
- 0
jdr/sheet.go View File

@ -9,10 +9,13 @@ import (
"os" "os"
) )
// Page represents a page of a character sheet.
type Page struct { type Page struct {
Items []SheetItem 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 { func (page *Page) ToHTML() template.HTML {
ret := "" ret := ""
@ -23,20 +26,25 @@ func (page *Page) ToHTML() template.HTML {
return template.HTML(ret) return template.HTML(ret)
} }
// SheetItem represents an item in the character sheet (either a structural element or a value)
type SheetItem interface { type SheetItem interface {
Name() string Name() string
ToHTML() template.HTML 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 { type Block struct {
name string name string
Items []SheetItem Items []SheetItem
} }
// Name of the block (possibly empty if the block is unnamed)
func (block *Block) Name() string { func (block *Block) Name() string {
return block.name return block.name
} }
// ToHTML transforms a Block into a HTML code.
func (block *Block) ToHTML() template.HTML { func (block *Block) ToHTML() template.HTML {
begin := `<fieldset>` begin := `<fieldset>`
legend := `<legend>` + template.HTMLEscapeString(block.Name()) + `</legend>` legend := `<legend>` + template.HTMLEscapeString(block.Name()) + `</legend>`
@ -55,15 +63,18 @@ func (block *Block) ToHTML() template.HTML {
return ret 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 Variable struct {
Type VariableType Type VariableType
name string name string
} }
// Name of the variable
func (v *Variable) Name() string { func (v *Variable) Name() string {
return v.name return v.name
} }
// ToHTML transforms a Variable into a HTML code
func (v *Variable) ToHTML() template.HTML { func (v *Variable) ToHTML() template.HTML {
label := "" label := ""
if v.name != "" { if v.name != "" {
@ -78,15 +89,19 @@ func (v *Variable) ToHTML() template.HTML {
return template.HTML(label) + html + template.HTML(`<br/>`) return template.HTML(label) + html + template.HTML(`<br/>`)
} }
// CharacterSheet is a role-play character sheet.
// It is structured in Pages, Blocks and Variables
type CharacterSheet struct { type CharacterSheet struct {
types map[string]VariableType types map[string]VariableType
Pages []*Page Pages []*Page
} }
// Script renders the Javascript code necessary to render the character sheet web page interactive
func (cs *CharacterSheet) Script() template.JS { func (cs *CharacterSheet) Script() template.JS {
return template.JS("") return template.JS("")
} }
// Render the CharacterSheet into an embeddable HTML code
func (cs *CharacterSheet) Render() template.HTML { func (cs *CharacterSheet) Render() template.HTML {
ret := template.HTML("") ret := template.HTML("")
@ -97,7 +112,9 @@ func (cs *CharacterSheet) Render() template.HTML {
return ret return ret
} }
// AddType adds a custom variable type to the character sheet
func (cs *CharacterSheet) AddType(t VariableType) { func (cs *CharacterSheet) AddType(t VariableType) {
//create the type array if it doesn't exist yet
if cs.types == nil { if cs.types == nil {
cs.types = make(map[string]VariableType) cs.types = make(map[string]VariableType)
} }
@ -106,6 +123,7 @@ func (cs *CharacterSheet) AddType(t VariableType) {
cs.types[name] = t cs.types[name] = t
} }
// AddDefaultTypes adds the default variable types to the character sheet
func (cs *CharacterSheet) AddDefaultTypes() { func (cs *CharacterSheet) AddDefaultTypes() {
cs.AddType(&VariableType_bool{}) cs.AddType(&VariableType_bool{})
cs.AddType(&VariableType_int{}) cs.AddType(&VariableType_int{})
@ -114,6 +132,7 @@ func (cs *CharacterSheet) AddDefaultTypes() {
cs.AddType(&VariableType_d{}) 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) { func (cs *CharacterSheet) getType(typename string) (VariableType, error) {
t, ok := cs.types[typename] t, ok := cs.types[typename]
if !ok { if !ok {
@ -123,6 +142,8 @@ func (cs *CharacterSheet) getType(typename string) (VariableType, error) {
return t, nil return t, nil
} }
// xmlNode is a temporary structure used for parsing the character sheet XML file
// without knowing its structure à priori
type xmlNode struct { type xmlNode struct {
XMLName xml.Name XMLName xml.Name
Attrs []xml.Attr `xml:"-"` Attrs []xml.Attr `xml:"-"`
@ -347,6 +368,8 @@ func parseSheet(root xmlNode) (*CharacterSheet, error) {
return sheet, nil 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) { func ReadCharacterSheet(filename string) (*CharacterSheet, error) {
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {


Loading…
Cancel
Save