Fix reading lua variables
This commit is contained in:
@@ -81,33 +81,13 @@ func (p *RegexProcessor) FromLua(L *lua.LState) (interface{}, error) {
|
||||
sLuaVal := L.GetGlobal(sVarName)
|
||||
|
||||
// First check string variables (s1, s2, etc.) as they should have priority
|
||||
if sLuaVal != lua.LNil {
|
||||
if sStr, ok := sLuaVal.(lua.LString); ok {
|
||||
newStrVal := string(sStr)
|
||||
modifications[i] = newStrVal
|
||||
continue
|
||||
}
|
||||
if sLuaVal.Type() != lua.LTNil {
|
||||
modifications[i] = sLuaVal.String()
|
||||
}
|
||||
|
||||
// Then check numeric variables (v1, v2, etc.)
|
||||
if vLuaVal != lua.LNil {
|
||||
switch v := vLuaVal.(type) {
|
||||
case lua.LNumber:
|
||||
// Convert numeric value to string
|
||||
newNumVal := strconv.FormatFloat(float64(v), 'f', -1, 64)
|
||||
modifications[i] = newNumVal
|
||||
continue
|
||||
case lua.LString:
|
||||
// Use string value directly
|
||||
newStrVal := string(v)
|
||||
modifications[i] = newStrVal
|
||||
continue
|
||||
default:
|
||||
// Convert other types to string
|
||||
newDefaultVal := fmt.Sprintf("%v", v)
|
||||
modifications[i] = newDefaultVal
|
||||
continue
|
||||
}
|
||||
if vLuaVal.Type() != lua.LTNil {
|
||||
modifications[i] = vLuaVal.String()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,6 +106,10 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
|
||||
return "", 0, 0, fmt.Errorf("error compiling pattern: %v", err)
|
||||
}
|
||||
|
||||
previous := luaExpr
|
||||
luaExpr = BuildLuaScript(luaExpr)
|
||||
fmt.Printf("Changing Lua expression from: %s to: %s\n", previous, luaExpr)
|
||||
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
|
||||
@@ -151,28 +135,32 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
|
||||
captures := compiledPattern.FindStringSubmatch(match)
|
||||
if len(captures) <= 1 {
|
||||
// No capture groups, return unchanged
|
||||
fmt.Println("No capture groups for lua to chew on")
|
||||
return match
|
||||
}
|
||||
|
||||
// Pass the captures to Lua environment
|
||||
if err := p.ToLua(L, captures); err != nil {
|
||||
fmt.Println("Error setting Lua variables:", err)
|
||||
return match
|
||||
}
|
||||
|
||||
// Execute the user's Lua code
|
||||
if err := L.DoString(luaExpr); err != nil {
|
||||
fmt.Println("Error executing Lua code:", err)
|
||||
return match // Return unchanged on error
|
||||
}
|
||||
|
||||
// Get modifications from Lua
|
||||
modResult, err := p.FromLua(L)
|
||||
if err != nil {
|
||||
fmt.Println("Error getting modifications:", err)
|
||||
return match
|
||||
}
|
||||
|
||||
// Apply modifications to the matched text
|
||||
modsMap, ok := modResult.(map[int]string)
|
||||
if !ok || len(modsMap) == 0 {
|
||||
fmt.Println("No modifications to apply")
|
||||
return match // No changes
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user