Version 0.8.1-beta

- stop replacements on match
- better ooc recognition patterns

- keep cases of over-long replacements
This commit is contained in:
2020-06-16 01:10:36 +02:00
parent 44dd7ac8eb
commit cc4df96bac
11 changed files with 57 additions and 48 deletions

View File

@@ -95,31 +95,6 @@ local function IsOneBigEmote(this, text)
return isOneBigEmote
end
--[[
--- Detect OOC in text, patterns are (( ooc )) or ooc:
local function IsOoc(this, text)
local firstWord, _ = this:SplitOnFirstMatch(text)
assert(firstWord ~= nil, "firstWord is never nil")
-- scheme: (( ooc ))
if sub(firstWord, 1, 2) == "((" then
-- search for emote end
local _, oocEnd = find(text, "%)%)", 3)
if (oocEnd == length(text)) then
this:TracePrintPrint("IsOoc : skip ((ooc))", text)
return true
end
end
-- scheme: ooc:
if sub(firstWord, 1, 4) == "ooc:" then
this:TracePrint("IsOoc : skip ooc:", text)
return true
end
return false
end
]]
--- Checks if a message can be replaced according to configuration.
-- @return boolean
function Grichelde:CheckReplacementAllowed(text, channel)
@@ -281,11 +256,13 @@ function Grichelde:ReplaceText(text)
local newText = text
local finalText = ""
local currentChar, previousChar
local current = 1
local lastStart = 1
while current <= length(newText) do
local currentChar = sub(newText, current, current)
previousChar = currentChar
currentChar = sub(newText, current, current)
self:TracePrint("current/char : %s,%s", current, currentChar)
if ( not tContains(lookAheads, currentChar)) then
@@ -293,7 +270,7 @@ function Grichelde:ReplaceText(text)
else
-- lookahead-check for all preservable patterns (itemLinks, textures, emotes, ooc, etc.)
local textAhead = sub(newText, current)
local posEnd = self:CheckForPreversableText(textAhead)
local posEnd = self:CheckForPreversableText(textAhead, previousChar)
if posEnd > 0 then
self:DebugPrint("ReplaceText : Found an ignore pattern")
@@ -318,7 +295,7 @@ function Grichelde:ReplaceText(text)
finalText = finalText .. replacement
self:DebugPrint("ReplaceText : replaced \"%s\"", text)
self:DebugPrint("ReplaceText : with \"%s\"", finalText)
self:DebugPrint("ReplaceText : with \"%s\"", finalText)
return finalText
end
@@ -327,7 +304,7 @@ end
--- emotes, ooc 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)
function Grichelde:CheckForPreversableText(text, previousChar)
self:TracePrint("CheckForPreversableText : text:", text)
-- Calling find on ever pattern might be inefficient but its way less code than marching over every character
@@ -375,8 +352,8 @@ function Grichelde:CheckForPreversableText(text)
end
end
-- ooc: detection remaing text is treated as ooc completely!
if sub(lowerText, 1, 4) == "ooc:" then
-- ooc detection remaing text is treated as ooc completely!
if (previousChar == nil or previousChar == ' ') and find(lowerText, "^ooc[%:%s]") then
self:DebugPrint("CheckForPreversableText : ooc for remaing text")
return length(text)
end
@@ -395,6 +372,7 @@ function Grichelde:ReplaceCharacters(text)
local result = text
local consolidate = {}
local stopOnMatch = nil
-- replacements are done first
for replName, replTable in spairs(replacements) do
@@ -414,6 +392,10 @@ function Grichelde:ReplaceCharacters(text)
local pos1, pos2 = find(oldResult, search, pos)
self:TracePrint("ReplaceCharacters : pos1: %d, pos2: %d", pos1, pos2)
while (pos1 and pos2 and pos1 <= pos2) do
if replTable.stopOnMatch and stopOnMatch == nil then
stopOnMatch = replName
end
local pre = sub(result, 1, pos1 - 1 + offset)
local post = sub(result, pos2 + 1 + offset)
self:TracePrint("ReplaceCharacters : pre: %s, post: %s", pre, post)
@@ -455,6 +437,10 @@ function Grichelde:ReplaceCharacters(text)
local pos1, pos2 = find(lowerResult, lowerSearch, pos)
self:TracePrint("ReplaceCharacters : pos1: %d, pos2: %d", pos1, pos2)
while (pos1 and pos2 and pos1 <= pos2) do
if replTable.stopOnMatch and stopOnMatch == nil then
stopOnMatch = replName
end
local pre = sub(result, 1, pos1 - 1 + offset)
local match = sub(result, pos1 + offset, pos2 + offset)
local post = sub(result, pos2 + 1 + offset)
@@ -508,13 +494,11 @@ function Grichelde:ReplaceCharacters(text)
if lastCase == nil then
if (isUpper(nextLetter)) then
repl = repl .. toUpper(remainingReplace)
elseif (isLower(nextLetter)) then
repl = repl .. toLower(remainingReplace)
else
repl = repl .. remainingReplace
end
elseif lastCase == false then
repl = repl .. toLower(remainingReplace)
repl = repl .. remainingReplace
else
if (isLower(nextLetter)) then
repl = repl .. toLower(remainingReplace)
@@ -566,6 +550,10 @@ function Grichelde:ReplaceCharacters(text)
else
self:DebugPrint("ReplaceCharacters : Skip replacement for %s", replName)
end
if stopOnMatch ~= nil then
break
end
end
-- consolidation is done last
@@ -573,7 +561,7 @@ function Grichelde:ReplaceCharacters(text)
local before = result
local search = replTable.searchText
if not nilOrEmpty(search) and replTable.active then
if not nilOrEmpty(search) and replTable.active then
local replace = replTable.replaceText
local lowerResult = toLower(result)
local offset = 0
@@ -608,6 +596,10 @@ function Grichelde:ReplaceCharacters(text)
else
self:DebugPrint("ReplaceCharacters : Skip consolidation for %s", replName)
end
if stopOnMatch == replName then
break
end
end
self:DebugPrint("ReplaceCharacters : final text:", result)