diff --git a/processor/xml.go b/processor/xml.go index 2bb1d32..8cd1b2d 100644 --- a/processor/xml.go +++ b/processor/xml.go @@ -43,11 +43,10 @@ func (p *XMLProcessor) ProcessContent(content string, path string, luaExpr strin } defer L.Close() - table, err := p.ToLua(L, node) + err = p.ToLua(L, node) if err != nil { return content, modCount, matchCount, fmt.Errorf("error converting to Lua: %v", err) } - L.SetGlobal("v", table) err = L.DoString(BuildLuaScript(luaExpr)) 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 } +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 -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 node, ok := data.(*xmlquery.Node) if !ok { @@ -111,7 +119,7 @@ func (p *XMLProcessor) ToLua(L *lua.LState, data interface{}) (lua.LValue, error // Add children if any children := L.NewTable() for child := node.FirstChild; child != nil; child = child.NextSibling { - childTable, err := p.ToLua(L, child) + childTable, err := p.ToLuaTable(L, child) if err == nil { children.Append(childTable) } diff --git a/processor/xml_test.go b/processor/xml_test.go index 2d3a983..4cd07ee 100644 --- a/processor/xml_test.go +++ b/processor/xml_test.go @@ -1555,11 +1555,10 @@ func TestXMLToLua(t *testing.T) { } // Convert to Lua - table, err := processor.ToLua(L, root) + err := processor.ToLua(L, root) if err != nil { t.Fatalf("Failed to convert to Lua: %v", err) } - L.SetGlobal("v", table) // Verify the result luaTable := L.GetGlobal("v") @@ -1606,11 +1605,10 @@ func TestXMLToLua(t *testing.T) { } // Convert to Lua - table, err := processor.ToLua(L, street) + err := processor.ToLua(L, street) if err != nil { t.Fatalf("Failed to convert to Lua: %v", err) } - L.SetGlobal("v", table) // Verify the result luaTable := L.GetGlobal("v")