|
1 #!/usr/local/bin/lua |
|
2 |
|
3 local lom = require "lxp.lom" |
|
4 |
|
5 local tests = { |
|
6 [[<abc a1="A1" a2="A2">inside tag `abc'</abc>]], |
|
7 [[<qwerty q1="q1" q2="q2"> |
|
8 <asdf>some text</asdf> |
|
9 </qwerty>]], |
|
10 } |
|
11 |
|
12 function table._tostring (tab, indent, spacing) |
|
13 local s = {} |
|
14 spacing = spacing or "" |
|
15 indent = indent or "\t" |
|
16 table.insert (s, "{\n") |
|
17 for nome, val in pairs (tab) do |
|
18 table.insert (s, spacing..indent) |
|
19 local t = type(nome) |
|
20 if t == "string" then |
|
21 table.insert (s, string.format ("[%q] = ", tostring (nome))) |
|
22 elseif t == "number" or t == "boolean" then |
|
23 table.insert (s, string.format ("[%s] = ", tostring (nome))) |
|
24 else |
|
25 table.insert (s, t) |
|
26 end |
|
27 t = type(val) |
|
28 if t == "string" or t == "number" then |
|
29 table.insert (s, string.format ("%q", val)) |
|
30 elseif t == "table" then |
|
31 table.insert (s, table._tostring (val, indent, spacing..indent)) |
|
32 else |
|
33 table.insert (s, t) |
|
34 end |
|
35 table.insert (s, ",\n") |
|
36 end |
|
37 table.insert (s, spacing.."}") |
|
38 return table.concat (s) |
|
39 end |
|
40 |
|
41 function table.print (tab, indent, spacing) |
|
42 io.write (table._tostring (tab, indent, spacing)) |
|
43 end |
|
44 |
|
45 |
|
46 for i, s in ipairs(tests) do |
|
47 --s = string.gsub (s, "[\n\r\t]", "") |
|
48 local ds = assert (lom.parse ([[<?xml version="1.0" encoding="ISO-8859-1"?>]]..s)) |
|
49 print(table._tostring(ds)) |
|
50 end |