Implement printing from lua
This commit is contained in:
@@ -2,6 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -239,6 +240,8 @@ modified = false
|
||||
if err := L.DoString(helperScript); err != nil {
|
||||
return fmt.Errorf("error loading helper functions: %v", err)
|
||||
}
|
||||
|
||||
L.SetGlobal("print", L.NewFunction(printToGo))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -295,6 +298,20 @@ func BuildLuaScript(luaExpr string) string {
|
||||
return fullScript
|
||||
}
|
||||
|
||||
func printToGo(L *lua.LState) int {
|
||||
// Get the number of arguments passed to the Lua print function
|
||||
n := L.GetTop()
|
||||
// Create a slice to hold the arguments
|
||||
args := make([]interface{}, n)
|
||||
for i := 1; i <= n; i++ {
|
||||
args[i-1] = L.Get(i) // Get the argument from Lua stack
|
||||
}
|
||||
// Print the arguments to Go's stdout
|
||||
log.Print("Lua: ")
|
||||
log.Println(args...)
|
||||
return 0 // No return values
|
||||
}
|
||||
|
||||
// Max returns the maximum of two integers
|
||||
func Max(a, b int) int {
|
||||
if a > b {
|
||||
|
@@ -107,14 +107,6 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
|
||||
luaExpr = BuildLuaScript(luaExpr)
|
||||
log.Printf("Changing Lua expression from: %s to: %s", previous, luaExpr)
|
||||
|
||||
L, err := NewLuaState()
|
||||
if err != nil {
|
||||
log.Printf("Error creating Lua state: %v", err)
|
||||
return "", 0, 0, fmt.Errorf("error creating Lua state: %v", err)
|
||||
}
|
||||
defer L.Close()
|
||||
log.Printf("Lua state created successfully")
|
||||
|
||||
// Initialize Lua environment
|
||||
modificationCount := 0
|
||||
|
||||
@@ -128,6 +120,17 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
|
||||
// By going backwards we fuck up all the indices to the end of the file that we don't care about
|
||||
// Because there either aren't any (last match) or they're already modified (subsequent matches)
|
||||
for i := len(indices) - 1; i >= 0; i-- {
|
||||
L, err := NewLuaState()
|
||||
if err != nil {
|
||||
log.Printf("Error creating Lua state: %v", err)
|
||||
return "", 0, 0, fmt.Errorf("error creating Lua state: %v", err)
|
||||
}
|
||||
// Hmm... Maybe we don't want to defer this..
|
||||
// Maybe we want to close them every iteration
|
||||
// We'll leave it as is for now
|
||||
defer L.Close()
|
||||
log.Printf("Lua state created successfully")
|
||||
|
||||
matchIndices := indices[i]
|
||||
log.Printf("Processing match indices: %v", matchIndices)
|
||||
|
||||
@@ -150,9 +153,19 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
|
||||
log.Println("Odd number of indices of groups, what the fuck?")
|
||||
continue
|
||||
}
|
||||
for _, index := range groups {
|
||||
if index == -1 {
|
||||
// return "", 0, 0, fmt.Errorf("negative indices encountered: %v. This indicates that there was an issue with the match indices, possibly due to an empty match or an unexpected pattern. Please check the regex pattern and input content.", matchIndices)
|
||||
log.Printf("Negative indices encountered: %v. This indicates that there was an issue with the match indices, possibly due to an empty match or an unexpected pattern. This is not an error but it's possibly not what you want.", matchIndices)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
captures := make([]string, 0, len(groups)/2)
|
||||
for j := 0; j < len(groups); j += 2 {
|
||||
if groups[j] == -1 || groups[j+1] == -1 {
|
||||
continue
|
||||
}
|
||||
captures = append(captures, content[groups[j]:groups[j+1]])
|
||||
}
|
||||
log.Printf("Captured groups: %v", captures)
|
||||
@@ -167,6 +180,9 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
if groups[i*2] == -1 || groups[i*2+1] == -1 {
|
||||
continue
|
||||
}
|
||||
namedCaptures = append(namedCaptures, NamedCapture{
|
||||
Name: name,
|
||||
Value: captures[i],
|
||||
|
@@ -1233,9 +1233,12 @@ func TestVariousNamedCaptureFormats(t *testing.T) {
|
||||
p := &RegexProcessor{}
|
||||
result, mods, matches, err := p.ProcessContent(
|
||||
content,
|
||||
`<entry id="(?<id_num>\d+)" value="(?<val>\d+)"(?: status="(?<status>[^"]*)")?`,
|
||||
`<entry id="(?<id_num>\d+)" value="(?<val>\d+)"(?: status="(?<status>[^"]*)")? />`,
|
||||
`-- Prefix the ID with "ID-"
|
||||
id_num = "ID-" .. id_num
|
||||
print(id_num)
|
||||
print(val)
|
||||
print(status)
|
||||
|
||||
-- Double the value except for inactive status
|
||||
if not status or status ~= "inactive" then
|
||||
|
Reference in New Issue
Block a user