From 86dab4e6dcca603f79dd6a6b40960bfd38e5afa9 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Sat, 21 Aug 2021 06:11:31 -0700 Subject: [PATCH] Fix syntax coloring for 9.0-based clients. (#7) * Fix syntax coloring for 9.0-based clients. Caused by this change in WoW 9.0: > The `|r` escape sequence now pops nested `|c` color sequences in-order, instead of resetting the text to the default color. Unfortunately, it seems this color sequence stack is very small. Seemed to only be around 8 colors max before it gives up and gets stuck on a color permanently unless you manually `|r`eset the color. * Fixes so docs look good again. * Different approach that shouldn't break non-9.0 clients like classic. * Cleanup * Right, those bits aren't supposed to retain the brackets around them. --- Help.lua | 8 +++++++- Parser.lua | 17 +++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Help.lua b/Help.lua index a200c6c..85b9b2d 100644 --- a/Help.lua +++ b/Help.lua @@ -846,7 +846,13 @@ function DogTag:OpenHelp() local x = text:sub(3, -3) x = "[" .. x .. "]" x = DogTag:ColorizeCode(x) - local y = x:match("^|cff%x%x%x%x%x%x%[(|cff%x%x%x%x%x%x.*)|cff%x%x%x%x%x%x%]|r$") .. "|r" + -- Find the first opening bracket in the colored string, + -- which is the one we just prepended to `x`. + local first = string.find(x, "|cff%x%x%x%x%x%x%[") + 11 + -- Find the last closing bracket in the colored string, + -- which is the one we just appended to `x`. + local last = string.find(x, "|cff%x%x%x%x%x%x%]|r[^%]]*$") - 1 + local y = x:sub(first, last) return y end return DogTag:ColorizeCode(text:sub(2, -2)) diff --git a/Parser.lua b/Parser.lua index 6cf50fa..92dfe92 100644 --- a/Parser.lua +++ b/Parser.lua @@ -1689,18 +1689,24 @@ function DogTag:ColorizeCode(code) if v == inString and not lastStringBackslash then inString = false t[#t+1] = string_char(v) + t[#t+1] = "|r" else t[#t+1] = string_char(v) end elseif v == open_bracket_byte then + if inCode == 0 then + t[#t+1] = "|r" + end t[#t+1] = "|cff" t[#t+1] = colors.grouping t[#t+1] = "[" + t[#t+1] = "|r" inCode = inCode + 1 elseif v == close_bracket_byte then t[#t+1] = "|cff" t[#t+1] = colors.grouping t[#t+1] = "]" + t[#t+1] = "|r" inCode = inCode - 1 if inCode == 0 then t[#t+1] = "|cff" @@ -1712,14 +1718,17 @@ function DogTag:ColorizeCode(code) t[#t+1] = "|cff" t[#t+1] = colors.grouping t[#t+1] = "(" + t[#t+1] = "|r" elseif v == close_parenthesis_byte then t[#t+1] = "|cff" t[#t+1] = colors.grouping t[#t+1] = ")" + t[#t+1] = "|r" elseif v == comma_byte then t[#t+1] = "|cff" t[#t+1] = colors.grouping t[#t+1] = "," + t[#t+1] = "|r" elseif quotes[v] then t[#t+1] = "|cff" t[#t+1] = colors.literal @@ -1739,6 +1748,7 @@ function DogTag:ColorizeCode(code) t[#t+1] = "|cff" t[#t+1] = colors.operator t[#t+1] = isReserved + t[#t+1] = "|r" i = i + #isReserved - 1 else local j = i @@ -1760,6 +1770,7 @@ function DogTag:ColorizeCode(code) for q = i, j do t[#t+1] = string_char(tokens[q]) end + t[#t+1] = "|r" i = j else j = i @@ -1781,6 +1792,7 @@ function DogTag:ColorizeCode(code) for q = i, j do t[#t+1] = string_char(tokens[q]) end + t[#t+1] = "|r" i = j else t[#t+1] = string_char(v) @@ -1792,10 +1804,11 @@ function DogTag:ColorizeCode(code) lastChar = tokens[i] end end - t[#t+1] = "|r" + if inCode == 0 then + t[#t+1] = "|r" + end tokens = del(tokens) local s = table.concat(t) - s = s:gsub("|c%x%x%x%x%x%x%x%x(|[cr])", "%1") if s == "|r" then s = "" end