Debian Codegraph @

texmf-dist/tex/luatex/luaxml/luaxml-pretty.lua

raw
1 --module(...,package.seeall)
2
3 --- Lua pretty printer from <a href="http://mini.net/cgi-bin/lua/44.html">http://mini.net/cgi-bin/lua/44.html</a><br/>
4 -- This was extracted from utility code in "util.lua".<br/>
5 -- 23/02/2001 [email protected]<br/>
6 -- Pretty displays a value, properly dealing with tables and cycles
7
8 local displayvalue=
9 function (s)
10 if not s or type(s)=='function' or type(s)=='userdata' then
11 s=tostring(s)
12 elseif type(s)~='number' then
13 s=string.gsub(string.format('%q',s),'^"([^"\']*)"$',"'%1'")
14 end
15 return s
16 end
17
18 local askeystr=
19 function (u,s)
20 if type(u)=='string' and string.find(u,'^[%w_]+$') then return s..u end
21 return '['..displayvalue(u)..']'
22 end
23
24 local horizvec=
25 function (x,n)
26 local o,e='',''
27 for i=1,#x do
28 if type(x[i])=='table' then return end
29 o=o..e..displayvalue(x[i])
30 if string.len(o)>n then return end
31 e=','
32 end
33 return '('..o..')'
34 end
35
36 local horizmap=
37 function (x,n)
38 local o,e='',''
39 for k,v in pairs(x) do
40 if type(v)=='table' then return end
41 o=o..e..askeystr(k,'')..'='..displayvalue(v)
42 if string.len(o)>n then return end
43 e=','
44 end
45 return '{'..o..'}'
46 end
47 local M = {}
48 local function pretty(p,x,h,q)
49 if not p then p,x='globals',globals() end
50 if type(x)=='table' then
51 if not h then h={} end
52 if h[x] then
53 x=h[x]
54 else
55 if not q then q=p end
56 h[x]=q
57 local s={}
58 for k,v in pairs(x) do table.insert(s,k) end
59 if #s>0 then
60 local n=75-string.len(p)
61 local f=#s==#x and horizvec(x,n)
62 if not f then f=horizmap(x,n) end
63 if not f then
64 table.sort(s,function (a,b)
65 --if tag(a)~=tag(b) then a,b=tag(b),tag(a) end
66 if type(a)~=type(b) then a,b=type(b),type(a) end
67 return a<b
68 end)
69 for i=1,#s do
70 if s[i] then
71 local u=askeystr(s[i],'.')
72 pretty(p..u,x[s[i]],h,q..u)
73 p=string.rep(' ',string.len(p))
74 end
75 end
76 return
77 end
78 x=f
79 else
80 x='{}'
81 end
82 end
83 else
84 x=displayvalue(x)
85 end
86 print(p..' = '..x)
87 end
88 M.pretty = pretty
89 return M