summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/node-ser.lmt
blob: e45d7f2497a957e3a0b81910b010984966700417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
if not modules then modules = { } end modules ['node-ser'] = {
    version   = 1.001,
    comment   = "companion to node-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- needs to be updated (mayeb fetch field types)

local type, tostring = type, tostring
local concat, tohash, sortedkeys, sortedhash, printtable, serialize = table.concat, table.tohash, table.sortedkeys, table.sortedhash, table.print, table.serialize
local formatters, format, rep = string.formatters, string.format, string.rep

local allocate = utilities.storage.allocate

local context     = context
local nodes       = nodes

local getfields   = nodes.fields
local isnode      = nodes.isnode
local nodecodes   = nodes.nodecodes
local subtypes    = nodes.subtypes
local tonode      = nodes.tonode
local tonut       = nodes.tonut

local hlist_code  = nodecodes.hlist
local vlist_code  = nodecodes.vlist

local f_char      = formatters["%U"]
local f_attr      = formatters["<attribute : %i>"]
local f_recurse   = formatters["<recursive : %i>"]

-- flat    : don't use next, but indexes
-- verbose : also add type

local canbezero = {
    integer   = true,
    dimension = true,
    number    = true,
}

local canbelist = {
    attribute  = "<attribute>",
    node       = "<node>",
    token      = "<token>",
    node_list  = "<nodes>",
    token_list = "<tokens>",
}

local canbeignored = {
    prev = "<node>"
}

local canbechar = {
    char      = true,
    smallchar = true,
    largechar = true,
}

local fieldtypes = table.setmetatableindex(function(t,k)
    local v = getfields(k,true) or false
    t[k] = v
    return v
end)

nodes.fieldtypes = fieldtypes

-- todo: subtype zero too

local function astable(n)
    n = tonode(n)
    if n then
        local id     = n.id
        local fields = fieldtypes[id]
        if fields then
            local subtype = n.subtype
            local result  = { }
            for field, fieldtype in sortedhash(fields) do -- no need to sort
                local value = n[field]
                if value then
                    if field == "subtype" then
                        -- we always show them now
                    elseif canbeignored[field] then
                        value = nil
                    elseif canbezero[fieldtype] and value == 0 then
                        value = nil
                    elseif canbelist[fieldtype] then
                        value = canbelist[fieldtype]
                    end
                    if value then
                        result[field] = value
                    end
                end
            end
            id = nodecodes[id]
            result.id = id
            if subtype then
                local subtypes = subtypes[id]
                if subtypes then
                    result.subtype = subtypes[subtype]
                end
            end
            return result
        end
    end
end

nodes.astable = astable

setinspector("node",function(v) if isnode(v) then printtable(astable(v),tostring(v)) return true end end)

local function to_table(n,flat,verbose,noattributes,done)
    local d = tonut(n)
    if done[d] then
        return f_recurse(d)
    else
        done[d] = true
        local fields = fieldtypes[n.id]
        if fields then
            local result = { }
            for field, fieldtype in sortedhash(fields) do
                local value = n[field]
                if value then
                    if fieldtype == "attribute" then
                        if noattributes then
                            result[value] = canbeignored[value]
                        else
                            result[value] = to_table(value,flat,verbose,noattributes,done)
                        end
                    elseif canbeignored[field] then
                        result[value] = canbeignored[value]
                    elseif not verbose and canbezero[fieldtype] and value == 0 then
                        value = nil
                    elseif canbelist[fieldtype] then
                        if flat then
                            result[value] = canbelist[value]
                        else
                            result[value] = to_table(value,flat,verbose,noattributes,done)
                        end
                    end
                    if value then
                        result[field] = value
                    end
                end
            end
            if verbose then
                local id = result.id
                if id then
                    id = nodecodes[id]
                    result.id = id
                    local subtype = result.subtype
                    if subtype then
                        local subtypes = subtypes[id]
                        if subtypes then
                            result.subtype = subtypes[subtype]
                        end
                    end
                end
                for k, v in next, canbechar do
                    local v = result[k]
                    if v then
                        result[k] = f_char(v)
                    end
                end
            end
            return result
        end
    end
end

local function totable(n,flat,verbose,noattributes) -- nicest: n,true,true,true
    if n then
        local d = { }
        if flat then
            local t, tn = { }, 0
            while n do
                tn = tn + 1
                local nt = to_table(n,flat,verbose,noattributes,d)
                t[tn] = nt
                nt.next = nil
                nt.prev = nil
                n = n.next
            end
            done = nil
            return t
        else
            local t = to_table(n,flat,verbose,noattributes,d)
            local n = n.next
            if n then
                t.next = totable(n,flat,verbose,noattributes,d)
            end
            return t
        end
    else
        return { }
    end
end

nodes.totable = function(n,...) return totable(tonode(n),...) end
nodes.totree  = function(n)     return totable(tonode(n),true,true,true) end -- no attributes, todo: attributes in k,v list

local function key(k)
    return ((type(k) == "number") and "["..k.."]") or k
end

function nodes.serialize(root,flat,verbose,noattributes,name)
    return serialize(totable(tonode(root),flat,verbose,noattributes),name)
end

function nodes.serializebox(n,flat,verbose,noattributes,name)
    return serialize(totable(tex.box[n],flat,verbose,noattributes),name)
end

function nodes.visualizebox(n,flat,verbose,noattributes,name)
    context.tocontext(totable(tex.box[n],flat,verbose,noattributes),name)
end

function nodes.list(head,n) -- name might change to nodes.type -- to be checked .. will move to module anyway
    head = tonode(head)
    if not n then
        context.starttyping(true)
    end
    while head do
        local id = head.id
        context(rep(" ",n or 0) .. tostring(head) .. "\n")
        if id == hlist_code or id == vlist_code then
            nodes.list(head.list,(n or 0)+1)
        end
        head = head.next
    end
    if not n then
        context.stoptyping(true)
    end
end

function nodes.print(head,n)
    head = tonode(head)
    while head do
        local id = head.id
        logs.writer(string.formatters["%w%S"],n or 0,head)
        if id == hlist_code or id == vlist_code then
            nodes.print(head.list,(n or 0)+1)
        end
        head = head.next
    end
end

-- quick hack, nicer is to have a proper expand per node type already prepared

local function apply(n,action)
    while n do
        action(n)
        local id = n.id
        if id == hlist_code or id == vlist_code then
            apply(n.list,action)
        end
        n = n.next
    end
end

nodes.apply = apply

local nuts    = nodes.nuts
local getid   = nuts.getid
local getlist = nuts.getlist
local getnext = nuts.getnext

local function apply(n,action)
    while n do
        action(n)
        local id = getid(n)
        if id == hlist_code or id == vlist_code then
            local list = getlist(n,action)
            if list then
                apply(list,action)
            end
        end
        n = getnext(n)
    end
end

nuts.apply = apply