Skip to content

Commit

Permalink
text/colour.go: Make sure HTML tag tokens that don't have a known col…
Browse files Browse the repository at this point in the history
…our are formatted appropriately. Resolves #129.
  • Loading branch information
Sandertv committed Jun 6, 2022
1 parent 84b5fe4 commit d95010a
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions minecraft/text/colour.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,14 @@ func (e *enc) process(tok html.Token) {
}
switch tok.Type {
case html.TextToken:
for _, s := range e.formatStack {
e.w.WriteString(s)
}
e.w.WriteString(tok.Data)
if len(e.formatStack) != 0 {
e.w.WriteString(reset)
}
e.writeText(tok.Data)
case html.StartTagToken:
if format, ok := strMap[tok.Data]; ok {
e.formatStack = append(e.formatStack, format)
return
}
// Not a known colour, so just write the token as a string.
e.w.WriteString("<" + tok.Data + ">")
e.writeText("<" + tok.Data + ">")
case html.EndTagToken:
for i, format := range e.formatStack {
if f, ok := strMap[tok.Data]; ok && f == format {
Expand All @@ -74,6 +68,17 @@ func (e *enc) process(tok html.Token) {
}
}
// Not a known colour, so just write the token as a string.
e.w.WriteString("</" + tok.Data + ">")
e.writeText("</" + tok.Data + ">")
}
}

// writeText writes text to the encoder by encasing it in the current format stack.
func (e *enc) writeText(s string) {
for _, format := range e.formatStack {
e.w.WriteString(format)
}
e.w.WriteString(s)
if len(e.formatStack) != 0 {
e.w.WriteString(reset)
}
}

0 comments on commit d95010a

Please sign in to comment.