Fix oopsie

This commit is contained in:
2025-03-26 02:52:28 +01:00
parent 66a522aa12
commit bb14087598
2 changed files with 14 additions and 8 deletions

View File

@@ -43,11 +43,10 @@ func (p *XMLProcessor) ProcessContent(content string, path string, luaExpr strin
} }
defer L.Close() defer L.Close()
table, err := p.ToLua(L, node) err = p.ToLua(L, node)
if err != nil { if err != nil {
return content, modCount, matchCount, fmt.Errorf("error converting to Lua: %v", err) return content, modCount, matchCount, fmt.Errorf("error converting to Lua: %v", err)
} }
L.SetGlobal("v", table)
err = L.DoString(BuildLuaScript(luaExpr)) err = L.DoString(BuildLuaScript(luaExpr))
if err != nil { if err != nil {
@@ -92,8 +91,17 @@ func (p *XMLProcessor) ProcessContent(content string, path string, luaExpr strin
return ConvertToNamedEntities(doc.OutputXML(true)), modCount, matchCount, nil return ConvertToNamedEntities(doc.OutputXML(true)), modCount, matchCount, nil
} }
func (p *XMLProcessor) ToLua(L *lua.LState, data interface{}) error {
table, err := p.ToLuaTable(L, data)
if err != nil {
return err
}
L.SetGlobal("v", table)
return nil
}
// ToLua converts XML node values to Lua variables // ToLua converts XML node values to Lua variables
func (p *XMLProcessor) ToLua(L *lua.LState, data interface{}) (lua.LValue, error) { func (p *XMLProcessor) ToLuaTable(L *lua.LState, data interface{}) (lua.LValue, error) {
// Check if data is an xmlquery.Node // Check if data is an xmlquery.Node
node, ok := data.(*xmlquery.Node) node, ok := data.(*xmlquery.Node)
if !ok { if !ok {
@@ -111,7 +119,7 @@ func (p *XMLProcessor) ToLua(L *lua.LState, data interface{}) (lua.LValue, error
// Add children if any // Add children if any
children := L.NewTable() children := L.NewTable()
for child := node.FirstChild; child != nil; child = child.NextSibling { for child := node.FirstChild; child != nil; child = child.NextSibling {
childTable, err := p.ToLua(L, child) childTable, err := p.ToLuaTable(L, child)
if err == nil { if err == nil {
children.Append(childTable) children.Append(childTable)
} }

View File

@@ -1555,11 +1555,10 @@ func TestXMLToLua(t *testing.T) {
} }
// Convert to Lua // Convert to Lua
table, err := processor.ToLua(L, root) err := processor.ToLua(L, root)
if err != nil { if err != nil {
t.Fatalf("Failed to convert to Lua: %v", err) t.Fatalf("Failed to convert to Lua: %v", err)
} }
L.SetGlobal("v", table)
// Verify the result // Verify the result
luaTable := L.GetGlobal("v") luaTable := L.GetGlobal("v")
@@ -1606,11 +1605,10 @@ func TestXMLToLua(t *testing.T) {
} }
// Convert to Lua // Convert to Lua
table, err := processor.ToLua(L, street) err := processor.ToLua(L, street)
if err != nil { if err != nil {
t.Fatalf("Failed to convert to Lua: %v", err) t.Fatalf("Failed to convert to Lua: %v", err)
} }
L.SetGlobal("v", table)
// Verify the result // Verify the result
luaTable := L.GetGlobal("v") luaTable := L.GetGlobal("v")