summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/lxml-xml.lua
blob: d1520800f10ab54c28c7da22db2ee0b8fed00d40 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
if not modules then modules = { } end modules ['lxml-xml'] = {
    version   = 1.001,
    comment   = "this module is the basis for the lxml-* ones",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local tonumber, next = tonumber, next
local concat = table.concat
local find, lower, upper = string.find, string.lower, string.upper

local xml = xml

local finalizers     = xml.finalizers.xml
local xmlfilter      = xml.filter -- we could inline this one for speed
local xmltostring    = xml.tostring
local xmlserialize   = xml.serialize
local xmlcollected   = xml.collected
local xmlnewhandlers = xml.newhandlers

local function first(collected) -- wrong ?
    return collected and collected[1]
end

local function last(collected)
    return collected and collected[#collected]
end

local function all(collected)
    return collected
end

-- local function reverse(collected)
--     if collected then
--         local nc = #collected
--         if nc > 0 then
--             local reversed, r = { }, 0
--             for c=nc,1,-1 do
--                 r = r + 1
--                 reversed[r] = collected[c]
--             end
--             return reversed
--         else
--             return collected
--         end
--     end
-- end

local reverse = table.reversed

local function attribute(collected,name)
    if collected and #collected > 0 then
        local at = collected[1].at
        return at and at[name]
    end
end

local function att(id,name)
    local at = id.at
    return at and at[name]
end

local function count(collected)
    return collected and #collected or 0
end

local function position(collected,n)
    if not collected then
        return 0
    end
    local nc = #collected
    if nc == 0 then
        return 0
    end
    n = tonumber(n) or 0
    if n < 0 then
        return collected[nc + n + 1]
    elseif n > 0 then
        return collected[n]
    else
        return collected[1].mi or 0
    end
end

local function match(collected)
    return collected and #collected > 0 and collected[1].mi or 0 -- match
end

local function index(collected)
    return collected and #collected > 0 and collected[1].ni or 0 -- 0 is new
end

local function attributes(collected,arguments)
    if collected and #collected > 0 then
        local at = collected[1].at
        if arguments then
            return at[arguments]
        elseif next(at) then
            return at -- all of them
        end
    end
end

local function chainattribute(collected,arguments) -- todo: optional levels
    if collected and #collected > 0 then
        local e = collected[1]
        while e do
            local at = e.at
            if at then
                local a = at[arguments]
                if a then
                    return a
                end
            else
                break -- error
            end
            e = e.__p__
        end
    end
    return ""
end

local function raw(collected) -- hybrid (not much different from text so it might go)
    if collected and #collected > 0 then
        local e = collected[1] or collected
        return e and xmltostring(e) or "" -- only first as we cannot concat function
    else
        return ""
    end
end

--

local xmltexthandler = xmlnewhandlers {
    name       = "string",
    initialize = function()
        result = { }
        return result
    end,
    finalize   = function()
        return concat(result)
    end,
    handle     = function(...)
        result[#result+1] = concat { ... }
    end,
    escape     = false,
}

local function xmltotext(root)
    local dt = root.dt
    if not dt then
        return ""
    end
    local nt = #dt -- string or table
    if nt == 0 then
        return ""
    elseif nt == 1 and type(dt[1]) == "string" then
        return dt[1] -- no escaping of " ' < > &
    else
        return xmlserialize(root,xmltexthandler) or ""
    end
end

function xml.serializetotext(root)
    return root and xmlserialize(root,xmltexthandler) or ""
end

--

local function text(collected) -- hybrid
    if collected then -- no # test here !
        local e = collected[1] or collected -- why fallback to element, how about cdata
        return e and xmltotext(e) or ""
    else
        return ""
    end
end

local function texts(collected)
    if not collected then
        return { } -- why no nil
    end
    local nc = #collected
    if nc == 0 then
        return { } -- why no nil
    end
    local t, n = { }, 0
    for c=1,nc do
        local e = collected[c]
        if e and e.dt then
            n = n + 1
            t[n] = e.dt
        end
    end
    return t
end

local function tag(collected,n)
    if not collected then
        return
    end
    local nc = #collected
    if nc == 0 then
        return
    end
    local c
    if n == 0 or not n then
        c = collected[1]
    elseif n > 1 then
        c = collected[n]
    else
        c = collected[nc-n+1]
    end
    return c and c.tg
end

local function name(collected,n)
    if not collected then
        return
    end
    local nc = #collected
    if nc == 0 then
        return
    end
    local c
    if n == 0 or not n then
        c = collected[1]
    elseif n > 1 then
        c = collected[n]
    else
        c = collected[nc-n+1]
    end
    if not c then
        -- sorry
    elseif c.ns == "" then
        return c.tg
    else
        return c.ns .. ":" .. c.tg
    end
end

local function tags(collected,nonamespace)
    if not collected then
        return
    end
    local nc = #collected
    if nc == 0 then
        return
    end
    local t, n = { }, 0
    for c=1,nc do
        local e = collected[c]
        local ns, tg = e.ns, e.tg
        n = n + 1
        if nonamespace or ns == "" then
            t[n] = tg
        else
            t[n] = ns .. ":" .. tg
        end
    end
    return t
end

local function empty(collected,spacesonly)
    if not collected then
        return true
    end
    local nc = #collected
    if nc == 0 then
        return true
    end
    for c=1,nc do
        local e = collected[c]
        if e then
            local edt = e.dt
            if edt then
                local n = #edt
                if n == 1 then
                    local edk = edt[1]
                    local typ = type(edk)
                    if typ == "table" then
                        return false
                    elseif edk ~= "" then
                        return false
                    elseif spacesonly and not find(edk,"%S") then
                        return false
                    end
                elseif n > 1 then
                    return false
                end
            end
        end
    end
    return true
end

finalizers.first          = first
finalizers.last           = last
finalizers.all            = all
finalizers.reverse        = reverse
finalizers.elements       = all
finalizers.default        = all
finalizers.attribute      = attribute
finalizers.att            = att
finalizers.count          = count
finalizers.position       = position
finalizers.match          = match
finalizers.index          = index
finalizers.attributes     = attributes
finalizers.chainattribute = chainattribute
finalizers.text           = text
finalizers.texts          = texts
finalizers.tag            = tag
finalizers.name           = name
finalizers.tags           = tags
finalizers.empty          = empty

-- shortcuts -- we could support xmlfilter(id,pattern,first)

function xml.first(id,pattern)
    return first(xmlfilter(id,pattern))
end

function xml.last(id,pattern)
    return last(xmlfilter(id,pattern))
end

function xml.count(id,pattern)
    return count(xmlfilter(id,pattern))
end

function xml.attribute(id,pattern,a,default)
    return attribute(xmlfilter(id,pattern),a,default)
end

function xml.raw(id,pattern)
    if pattern then
        return raw(xmlfilter(id,pattern))
    else
        return raw(id)
    end
end

function xml.text(id,pattern) -- brrr either content or element (when cdata)
    if pattern then
     -- return text(xmlfilter(id,pattern))
        local collected = xmlfilter(id,pattern)
        return collected and #collected > 0 and xmltotext(collected[1]) or ""
    elseif id then
     -- return text(id)
        return xmltotext(id) or ""
    else
        return ""
    end
end

xml.content = text

--

function xml.position(id,pattern,n) -- element
    return position(xmlfilter(id,pattern),n)
end

function xml.match(id,pattern) -- number
    return match(xmlfilter(id,pattern))
end

function xml.empty(id,pattern,spacesonly)
    return empty(xmlfilter(id,pattern),spacesonly)
end

xml.all    = xml.filter
xml.index  = xml.position
xml.found  = xml.filter

-- a nice one:

local function totable(x)
    local t = { }
    for e in xmlcollected(x[1] or x,"/*") do
        t[e.tg] = xmltostring(e.dt) or ""
    end
    return next(t) and t or nil
end

xml.table        = totable
finalizers.table = totable

local function textonly(e,t)
    if e then
        local edt = e.dt
        if edt then
            for i=1,#edt do
                local e = edt[i]
                if type(e) == "table" then
                    textonly(e,t)
                else
                    t[#t+1] = e
                end
            end
        end
    end
    return t
end

function xml.textonly(e) -- no pattern
    return concat(textonly(e,{}))
end

--

-- local x = xml.convert("<x><a x='+'>1<B>2</B>3</a></x>")
-- xml.filter(x,"**/lowerall()") print(x)
-- xml.filter(x,"**/upperall()") print(x)

function finalizers.lowerall(collected)
    for c=1,#collected do
        local e = collected[c]
        if not e.special then
            e.tg = lower(e.tg)
            local eat = e.at
            if eat then
                local t = { }
                for k,v in next, eat do
                    t[lower(k)] = v
                end
                e.at = t
            end
        end
    end
end

function finalizers.upperall(collected)
    for c=1,#collected do
        local e = collected[c]
        if not e.special then
            e.tg = upper(e.tg)
            local eat = e.at
            if eat then
                local t = { }
                for k,v in next, eat do
                    t[upper(k)] = v
                end
                e.at = t
            end
        end
    end
end