Implement "replacement" variable that simply replaces the match

This commit is contained in:
2025-03-26 12:58:51 +01:00
parent 7e19cf4e2c
commit 7f4392b10e
2 changed files with 29 additions and 23 deletions

View File

@@ -213,8 +213,14 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
continue continue
} }
replacement := ""
replacementVar := L.GetGlobal("replacement")
if replacementVar.Type() != lua.LTNil {
replacement = replacementVar.String()
}
if replacement == "" {
// Apply the modifications to the original match // Apply the modifications to the original match
replacement := match replacement = match
for i := len(modsMap) - 1; i >= 0; i-- { for i := len(modsMap) - 1; i >= 0; i-- {
newVal := modsMap[i] newVal := modsMap[i]
log.Printf("Applying modification: %s", newVal) log.Printf("Applying modification: %s", newVal)
@@ -235,7 +241,7 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
luaValue := L.GetGlobal(capture.Name).String() luaValue := L.GetGlobal(capture.Name).String()
replacement = replacement[:groupStart] + luaValue + replacement[groupEnd:] replacement = replacement[:groupStart] + luaValue + replacement[groupEnd:]
} }
}
modificationCount++ modificationCount++
result = result[:matchIndices[0]] + replacement + result[matchIndices[1]:] result = result[:matchIndices[0]] + replacement + result[matchIndices[1]:]
log.Printf("Modification count updated: %d", modificationCount) log.Printf("Modification count updated: %d", modificationCount)

View File

@@ -871,7 +871,7 @@ func TestLuaFunctionsOnNamedCaptures(t *testing.T) {
`<user name="(?<name>[^"]+)" role="(?<role>[^"]+)"`, `<user name="(?<name>[^"]+)" role="(?<role>[^"]+)"`,
`-- Capitalize first letters for regular users `-- Capitalize first letters for regular users
if role == "user" then if role == "user" then
name = name:gsub("(%w)(%w*)", function(first, rest) return first:upper()..rest end):gsub(" (%w)(%w*)", " %1%2":gsub("%%1", function(x) return x:upper() end)) name = name:gsub("(%w)(%w*)", function(first, rest) return first:upper()..rest end):gsub(" (%w)(%w*)", " %1%2")
else else
-- Uppercase for admins -- Uppercase for admins
name = string.upper(name) name = string.upper(name)
@@ -910,7 +910,7 @@ func TestNamedCaptureWithMath(t *testing.T) {
p := &RegexProcessor{} p := &RegexProcessor{}
result, mods, matches, err := p.ProcessContent( result, mods, matches, err := p.ProcessContent(
content, content,
`<item price="(?<price>\d+\.\d+)" quantity="(?<qty>\d+)"`, `<item price="(?<price>\d+\.\d+)" quantity="(?<qty>\d+)"!any$`,
`-- Calculate and add total `-- Calculate and add total
replacement = string.format('<item price="%s" quantity="%s" total="%.2f" />', replacement = string.format('<item price="%s" quantity="%s" total="%.2f" />',
price, qty, price * qty)`) price, qty, price * qty)`)