Version 0.5.0
- add replacements via options UI - restructure debug functions
This commit is contained in:
@@ -2,22 +2,22 @@
|
||||
local _G = _G
|
||||
local Grichelde = _G.Grichelde
|
||||
|
||||
local ipairs, tContains, tFilter, tInsert, tConcat, find, sub, gsub, match, toLower, trim, length
|
||||
= Grichelde.functions.ipairs, Grichelde.functions.tContains, Grichelde.functions.tFilter, Grichelde.functions.tInsert, Grichelde.functions.tConcat, Grichelde.functions.find, Grichelde.functions.sub, Grichelde.functions.gsub, Grichelde.functions.match, Grichelde.functions.toLower, Grichelde.functions.trim, Grichelde.functions.length
|
||||
local nilOrEmpty, ipairs, tContains, tFilter, tInsert, tConcat, find, sub, gsub, toLower, trim, length
|
||||
= Grichelde.functions.nilOrEmpty, Grichelde.functions.ipairs, Grichelde.functions.tContains, Grichelde.functions.tFilter, Grichelde.functions.tInsert, Grichelde.functions.tConcat, Grichelde.functions.find, Grichelde.functions.sub, Grichelde.functions.gsub, Grichelde.functions.toLower, Grichelde.functions.trim, Grichelde.functions.length
|
||||
|
||||
--- Before a chat message is sent, check if replacement is required and replace the text accordingly.
|
||||
--- @param message string
|
||||
--- @param type string
|
||||
--- @param language string
|
||||
--- @param channel string
|
||||
-- @param message string
|
||||
-- @param type string
|
||||
-- @param language string
|
||||
-- @param channel string
|
||||
function Grichelde:SendChatMessage(message, type, language, channel, ...)
|
||||
local replacedText = self:CheckAndReplace(message, type)
|
||||
|
||||
self:DebugPrint("SendChatMessage : replacedText: " .. replacedText)
|
||||
self:DebugPrint("SendChatMessage : replacedText:", replacedText)
|
||||
|
||||
-- Send text in chunks if length exceeds 255 bytes after replacement
|
||||
local chunks = self:SplitText(replacedText)
|
||||
self:DebugPrint("SendChatMessage : #chunks: " .. #chunks)
|
||||
self:DebugPrint("SendChatMessage : #chunks:", #chunks)
|
||||
|
||||
for _, chunk in ipairs(chunks) do
|
||||
self.hooks["SendChatMessage"](chunk, type, language, channel, ...);
|
||||
@@ -37,20 +37,26 @@ function Grichelde:CheckAndReplace(message, type)
|
||||
end
|
||||
|
||||
function Grichelde:CheckReplacement(text, channel)
|
||||
-- skip if not enabled
|
||||
if (not self.db.profile.enabled) then
|
||||
self:DebugPrint("CheckReplacement : disabled")
|
||||
return false
|
||||
end
|
||||
|
||||
-- check channel type
|
||||
-- skip if no text
|
||||
if nilOrEmpty(text) then
|
||||
return false
|
||||
end
|
||||
|
||||
-- skip if wrong channel
|
||||
local allowedChannels = tFilter(self.db.profile.channels,
|
||||
function(_,v) return v == true end,
|
||||
function(k,_) return k end
|
||||
)
|
||||
self:DebugPrint("CheckReplacement : allowed channels: %s", tConcat(allowedChannels, ", "))
|
||||
self:DebugPrint("CheckReplacement : allowed channels:", tConcat(allowedChannels, ", "))
|
||||
local type = self:ConvertBlizChannelToType(channel)
|
||||
if (type ~= nil and not tContains(allowedChannels, type)) then
|
||||
self:DebugPrint("CheckReplacement : skip channel type %s", type)
|
||||
self:DebugPrint("CheckReplacement : skip channel type:", type)
|
||||
return false
|
||||
end
|
||||
|
||||
@@ -74,56 +80,19 @@ function Grichelde:ConvertBlizChannelToType(channel)
|
||||
return type
|
||||
end
|
||||
|
||||
--- Checks if the text starts with a preversable ignore pattern, such as itemLinks, textures or raid target icons
|
||||
--- and returns the end location of the match, or 0 if no pattern was found
|
||||
--- @param text string
|
||||
--- @return number
|
||||
function Grichelde:CheckForPreversableText(text)
|
||||
self:DebugPrint("CheckForPreversableText : text is " .. text)
|
||||
|
||||
-- do not replace these patterns
|
||||
local ignorePatterns = {
|
||||
"|[Cc]%x%x%x%x%x%x%x%x.-|r", -- colored items (or links)
|
||||
"|H.-|h", -- item links (http://www.wowwiki.com/ItemLink)
|
||||
"|T.-|t", -- textures
|
||||
"|n", -- newline
|
||||
"{rt[1-8]}", -- rumbered raid target icons
|
||||
"{Star}", -- named raid target icon 1
|
||||
"{Circle}", -- named raid target icon 2
|
||||
"{Coin}", -- named raid target icon 2
|
||||
"{Diamond}", -- named raid target icon 3
|
||||
"{Triangle}", -- named raid target icon 4
|
||||
"{Moon}", -- named raid target icon 5
|
||||
"{Square}", -- named raid target icon 6
|
||||
"{Cross}", -- named raid target icon 7
|
||||
"{X}", -- named raid target icon 7
|
||||
"{Skull}" -- named raid target icon 8
|
||||
}
|
||||
|
||||
-- Calling find on ever pattern might be inefficient but its way less code.
|
||||
for _, pattern in ipairs(ignorePatterns) do
|
||||
local pos1, pos2 = find(text, pattern)
|
||||
if pos1 == 1 and pos2 ~= nil then
|
||||
self:DebugPrint("CheckForPreversableText : Found ignore pattern \"%s\" at (%d, %d)", pattern, pos1, pos2)
|
||||
return pos2
|
||||
end
|
||||
end
|
||||
self:DebugPrint("CheckForPreversableText : no ignore pattern found")
|
||||
return 0
|
||||
end
|
||||
|
||||
--- Replaces all character occurrences for which replacements have been defined in the options,
|
||||
--- while preserving any itemLinks or textures. (http://www.wowwiki.com/ItemLink)
|
||||
--- @param text string
|
||||
--- @return string
|
||||
-- @param text string
|
||||
-- @return string
|
||||
function Grichelde:ReplaceText(text)
|
||||
local lookAheads = {'|', '{', '%'}
|
||||
local finalText = ""
|
||||
local newText = text
|
||||
|
||||
-- don't replace non-chat related slash commands
|
||||
local firstWord, line = self:SplitOnFirstMatch(text)
|
||||
if (firstWord ~= nil and tContains(self.slashCommands, firstWord)) then
|
||||
self:DebugPrint("ReplaceText : Found slash command %s", firstWord )
|
||||
self:DebugPrint("ReplaceText : Found slash command:", firstWord )
|
||||
-- skip chat slash command
|
||||
finalText = finalText .. firstWord .. ' '
|
||||
newText = line
|
||||
@@ -136,7 +105,7 @@ function Grichelde:ReplaceText(text)
|
||||
local currentChar = sub(newText, current, current)
|
||||
self:DebugPrint("current/char : %s,%s", current, currentChar)
|
||||
|
||||
if currentChar ~= '|' and currentChar ~= '{' then
|
||||
if ( not tContains(lookAheads, currentChar)) then
|
||||
current = current + 1
|
||||
else
|
||||
|
||||
@@ -153,7 +122,7 @@ function Grichelde:ReplaceText(text)
|
||||
finalText = finalText .. replacement .. preservedText
|
||||
current = current + posEnd
|
||||
lastStart = current
|
||||
self:DebugPrint("ReplaceText : restarting at " .. lastStart)
|
||||
self:DebugPrint("ReplaceText : restarting at", lastStart)
|
||||
else
|
||||
-- no corresponding end was found to start pattern, continue loop with next char
|
||||
current = current + 1
|
||||
@@ -166,14 +135,64 @@ function Grichelde:ReplaceText(text)
|
||||
local replacement = self:ReplaceCharacters(remainingText)
|
||||
finalText = finalText .. replacement
|
||||
|
||||
self:DebugPrint("ReplaceText : replaced \"" .. text .. "\"")
|
||||
self:DebugPrint("ReplaceText : with \"" .. finalText .. "\"")
|
||||
self:DebugPrint("ReplaceText : replaced \"%s\"", text)
|
||||
self:DebugPrint("ReplaceText : with \"%s\"", finalText)
|
||||
return finalText
|
||||
end
|
||||
|
||||
|
||||
--- Checks if the text starts with a preversable ignore pattern, such as itemLinks, textures, raid target icons or
|
||||
--- %-substitutons and returns the end location of the match, or 0 if no pattern was found
|
||||
-- @param text string
|
||||
-- @return number
|
||||
function Grichelde:CheckForPreversableText(text)
|
||||
self:DebugPrint("CheckForPreversableText : text:", text)
|
||||
|
||||
-- do not replace these patterns
|
||||
local ignorePatterns = {
|
||||
"|[Cc]%x%x%x%x%x%x%x%x.-|r", -- colored items (or links)
|
||||
"|H.-|h", -- item links (http://www.wowwiki.com/ItemLink)
|
||||
"|T.-|t", -- textures
|
||||
"|n", -- newline
|
||||
|
||||
"{rt[1-8]}", -- rumbered raid target icons
|
||||
"{Star}", -- named raid target icon 1
|
||||
"{Circle}", -- named raid target icon 2
|
||||
"{Coin}", -- named raid target icon 2
|
||||
"{Diamond}", -- named raid target icon 3
|
||||
"{Triangle}", -- named raid target icon 4
|
||||
"{Moon}", -- named raid target icon 5
|
||||
"{Square}", -- named raid target icon 6
|
||||
"{Cross}", -- named raid target icon 7
|
||||
"{X}", -- named raid target icon 7
|
||||
"{Skull}", -- named raid target icon 8
|
||||
|
||||
"%%n", -- player's name
|
||||
"%%z", -- player's currnt zone
|
||||
"%%sz", -- player's current sub-zone
|
||||
"%%loc", -- player's map coordinates
|
||||
"%%t", -- name of target
|
||||
"%%f", -- name of focus target
|
||||
"%%m", -- name of mouseover unit
|
||||
"%%p", -- name of player pet
|
||||
"%%tt" -- name of player's target's target
|
||||
}
|
||||
|
||||
-- Calling find on ever pattern might be inefficient but its way less code.
|
||||
for _, pattern in ipairs(ignorePatterns) do
|
||||
local pos1, pos2 = find(text, pattern)
|
||||
if pos1 == 1 and pos2 ~= nil then
|
||||
self:DebugPrint("CheckForPreversableText : Found ignore pattern \"%s\" at (%d, %d)", pattern, pos1, pos2)
|
||||
return pos2
|
||||
end
|
||||
end
|
||||
self:DebugPrint("CheckForPreversableText : no ignore pattern found")
|
||||
return 0
|
||||
end
|
||||
|
||||
--- Replaces all character occurrences for which replacements have been defined in the options
|
||||
--- @param text string
|
||||
--- @return string
|
||||
-- @param text string
|
||||
-- @return string
|
||||
function Grichelde:ReplaceCharacters(text)
|
||||
-- todo: read from options
|
||||
-- todo: case (in)sensitivity
|
||||
@@ -189,12 +208,12 @@ function Grichelde:ReplaceCharacters(text)
|
||||
end
|
||||
|
||||
--- Splits a long text in longest possible chunks of <= 255 length, split at last available space
|
||||
--- @param text string
|
||||
--- @return table
|
||||
-- @param text string
|
||||
-- @return table
|
||||
function Grichelde:SplitText(text)
|
||||
local chunks = {}
|
||||
local splitText = text
|
||||
local textSize = length(splitText)
|
||||
local textSize = length(splitText or "")
|
||||
|
||||
while textSize > 255 do
|
||||
local chunk = sub(splitText, 1, 255)
|
||||
@@ -212,7 +231,7 @@ function Grichelde:SplitText(text)
|
||||
end
|
||||
end
|
||||
|
||||
self:DebugPrint("SplitText : chunk: " .. chunk )
|
||||
self:DebugPrint("SplitText : chunk:", chunk)
|
||||
|
||||
tInsert(chunks, chunk)
|
||||
splitText = remaining .. sub(splitText, 256)
|
||||
@@ -220,26 +239,8 @@ function Grichelde:SplitText(text)
|
||||
end
|
||||
|
||||
-- pickup remaining text < 255
|
||||
self:DebugPrint("SplitText : last chunk: " .. splitText)
|
||||
self:DebugPrint("SplitText : last chunk:", splitText)
|
||||
tInsert(chunks, splitText)
|
||||
|
||||
return chunks
|
||||
end
|
||||
|
||||
-- split at first word of a text line
|
||||
function Grichelde:SplitOnFirstMatch(text, start)
|
||||
self:DebugPrint("SplitOnFirstMatch : text: %s, start: %d", text, start)
|
||||
local pos = start or 1
|
||||
local left, right = match(text, "^.- .+", pos)
|
||||
self:DebugPrint("SplitOnFirstMatch : left: %s, right: %s", left, right)
|
||||
return left or text, right
|
||||
end
|
||||
|
||||
-- split at last word of a text line
|
||||
function Grichelde:SplitOnLastMatch(text, start)
|
||||
self:DebugPrint("SplitOnLastMatch : text: %s, start: %d", text, start)
|
||||
local pos = start or 1
|
||||
local left, right = match(text, ".+ .-$", pos)
|
||||
self:DebugPrint("SplitOnLastMatch : left: %s, right: %s", left, right)
|
||||
return left, right or text
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user